mirror of https://github.com/v2ray/v2ray-core
You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
79 lines
1.7 KiB
79 lines
1.7 KiB
package platform
|
|
|
|
import (
|
|
"os"
|
|
"path/filepath"
|
|
"strconv"
|
|
"strings"
|
|
)
|
|
|
|
type EnvFlag struct {
|
|
Name string
|
|
AltName string
|
|
}
|
|
|
|
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.Replace(strings.ToUpper(strings.TrimSpace(name)), ".", "_", -1)
|
|
}
|
|
|
|
func getExecutableDir() string {
|
|
exec, err := os.Executable()
|
|
if err != nil {
|
|
return ""
|
|
}
|
|
return filepath.Dir(exec)
|
|
}
|
|
|
|
func getExecuableSubDir(dir string) func() string {
|
|
return func() string {
|
|
return filepath.Join(getExecutableDir(), dir)
|
|
}
|
|
}
|
|
|
|
func GetAssetLocation(file string) string {
|
|
const name = "v2ray.location.asset"
|
|
assetPath := EnvFlag{Name: name, AltName: NormalizeEnvName(name)}.GetValue(getExecutableDir)
|
|
return filepath.Join(assetPath, file)
|
|
}
|
|
|
|
func GetPluginDirectory() string {
|
|
const name = "v2ray.location.plugin"
|
|
pluginDir := EnvFlag{Name: name, AltName: NormalizeEnvName(name)}.GetValue(getExecuableSubDir("plugins"))
|
|
return pluginDir
|
|
}
|
|
|
|
func GetConfigurationPath() string {
|
|
const name = "v2ray.location.config"
|
|
configPath := EnvFlag{Name: name, AltName: NormalizeEnvName(name)}.GetValue(getExecutableDir)
|
|
return filepath.Join(configPath, "config.json")
|
|
}
|