github.com/xtls/xray-core@v1.8.12-0.20240518155711-3168d27b0bdb/common/platform/platform.go (about)

     1  package platform // import "github.com/xtls/xray-core/common/platform"
     2  
     3  import (
     4  	"os"
     5  	"path/filepath"
     6  	"strconv"
     7  	"strings"
     8  )
     9  
    10  const (
    11  	PluginLocation  = "xray.location.plugin"
    12  	ConfigLocation  = "xray.location.config"
    13  	ConfdirLocation = "xray.location.confdir"
    14  	ToolLocation    = "xray.location.tool"
    15  	AssetLocation   = "xray.location.asset"
    16  
    17  	UseReadV         = "xray.buf.readv"
    18  	UseFreedomSplice = "xray.buf.splice"
    19  	UseVmessPadding  = "xray.vmess.padding"
    20  	UseCone          = "xray.cone.disabled"
    21  
    22  	BufferSize           = "xray.ray.buffer.size"
    23  	BrowserDialerAddress = "xray.browser.dialer"
    24  	XUDPLog              = "xray.xudp.show"
    25  	XUDPBaseKey          = "xray.xudp.basekey"
    26  )
    27  
    28  type EnvFlag struct {
    29  	Name    string
    30  	AltName string
    31  }
    32  
    33  func NewEnvFlag(name string) EnvFlag {
    34  	return EnvFlag{
    35  		Name:    name,
    36  		AltName: NormalizeEnvName(name),
    37  	}
    38  }
    39  
    40  func (f EnvFlag) GetValue(defaultValue func() string) string {
    41  	if v, found := os.LookupEnv(f.Name); found {
    42  		return v
    43  	}
    44  	if len(f.AltName) > 0 {
    45  		if v, found := os.LookupEnv(f.AltName); found {
    46  			return v
    47  		}
    48  	}
    49  
    50  	return defaultValue()
    51  }
    52  
    53  func (f EnvFlag) GetValueAsInt(defaultValue int) int {
    54  	useDefaultValue := false
    55  	s := f.GetValue(func() string {
    56  		useDefaultValue = true
    57  		return ""
    58  	})
    59  	if useDefaultValue {
    60  		return defaultValue
    61  	}
    62  	v, err := strconv.ParseInt(s, 10, 32)
    63  	if err != nil {
    64  		return defaultValue
    65  	}
    66  	return int(v)
    67  }
    68  
    69  func NormalizeEnvName(name string) string {
    70  	return strings.ReplaceAll(strings.ToUpper(strings.TrimSpace(name)), ".", "_")
    71  }
    72  
    73  func getExecutableDir() string {
    74  	exec, err := os.Executable()
    75  	if err != nil {
    76  		return ""
    77  	}
    78  	return filepath.Dir(exec)
    79  }
    80  
    81  func getExecutableSubDir(dir string) func() string {
    82  	return func() string {
    83  		return filepath.Join(getExecutableDir(), dir)
    84  	}
    85  }
    86  
    87  func GetPluginDirectory() string {
    88  	pluginDir := NewEnvFlag(PluginLocation).GetValue(getExecutableSubDir("plugins"))
    89  	return pluginDir
    90  }
    91  
    92  func GetConfigurationPath() string {
    93  	configPath := NewEnvFlag(ConfigLocation).GetValue(getExecutableDir)
    94  	return filepath.Join(configPath, "config.json")
    95  }
    96  
    97  // GetConfDirPath reads "xray.location.confdir"
    98  func GetConfDirPath() string {
    99  	configPath := NewEnvFlag(ConfdirLocation).GetValue(func() string { return "" })
   100  	return configPath
   101  }