mirror of
https://github.com/XTLS/Xray-core.git
synced 2025-04-30 09:18:34 +00:00
v1.0.0
This commit is contained in:
parent
47d23e9972
commit
c7f7c08ead
711 changed files with 82154 additions and 2 deletions
86
common/platform/platform.go
Normal file
86
common/platform/platform.go
Normal file
|
@ -0,0 +1,86 @@
|
|||
package platform // import "github.com/xtls/xray-core/v1/common/platform"
|
||||
|
||||
import (
|
||||
"os"
|
||||
"path/filepath"
|
||||
"strconv"
|
||||
"strings"
|
||||
)
|
||||
|
||||
type EnvFlag struct {
|
||||
Name string
|
||||
AltName string
|
||||
}
|
||||
|
||||
func NewEnvFlag(name string) EnvFlag {
|
||||
return EnvFlag{
|
||||
Name: name,
|
||||
AltName: NormalizeEnvName(name),
|
||||
}
|
||||
}
|
||||
|
||||
func (f EnvFlag) GetValue(defaultValue func() string) string {
|
||||
if v, found := os.LookupEnv(f.Name); found {
|
||||
return v
|
||||
}
|
||||
if len(f.AltName) > 0 {
|
||||
if v, found := os.LookupEnv(f.AltName); found {
|
||||
return v
|
||||
}
|
||||
}
|
||||
|
||||
return defaultValue()
|
||||
}
|
||||
|
||||
func (f EnvFlag) GetValueAsInt(defaultValue int) int {
|
||||
useDefaultValue := false
|
||||
s := f.GetValue(func() string {
|
||||
useDefaultValue = true
|
||||
return ""
|
||||
})
|
||||
if useDefaultValue {
|
||||
return defaultValue
|
||||
}
|
||||
v, err := strconv.ParseInt(s, 10, 32)
|
||||
if err != nil {
|
||||
return defaultValue
|
||||
}
|
||||
return int(v)
|
||||
}
|
||||
|
||||
func NormalizeEnvName(name string) string {
|
||||
return strings.ReplaceAll(strings.ToUpper(strings.TrimSpace(name)), ".", "_")
|
||||
}
|
||||
|
||||
func getExecutableDir() string {
|
||||
exec, err := os.Executable()
|
||||
if err != nil {
|
||||
return ""
|
||||
}
|
||||
return filepath.Dir(exec)
|
||||
}
|
||||
|
||||
func getExecutableSubDir(dir string) func() string {
|
||||
return func() string {
|
||||
return filepath.Join(getExecutableDir(), dir)
|
||||
}
|
||||
}
|
||||
|
||||
func GetPluginDirectory() string {
|
||||
const name = "xray.location.plugin"
|
||||
pluginDir := NewEnvFlag(name).GetValue(getExecutableSubDir("plugins"))
|
||||
return pluginDir
|
||||
}
|
||||
|
||||
func GetConfigurationPath() string {
|
||||
const name = "xray.location.config"
|
||||
configPath := NewEnvFlag(name).GetValue(getExecutableDir)
|
||||
return filepath.Join(configPath, "config.json")
|
||||
}
|
||||
|
||||
// GetConfDirPath reads "xray.location.confdir"
|
||||
func GetConfDirPath() string {
|
||||
const name = "xray.location.confdir"
|
||||
configPath := NewEnvFlag(name).GetValue(func() string { return "" })
|
||||
return configPath
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue