1
0
mirror of https://github.com/v2fly/v2ray-core.git synced 2024-11-10 06:16:53 -05:00
v2fly/common/platform/platform.go

87 lines
1.7 KiB
Go
Raw Normal View History

2021-02-16 15:31:50 -05:00
package platform
2017-05-25 19:11:38 -04:00
import (
"os"
2017-11-04 16:33:09 -04:00
"path/filepath"
2017-05-25 19:11:38 -04:00
"strconv"
"strings"
)
type EnvFlag struct {
Name string
AltName string
}
2018-07-18 04:40:28 -04:00
func NewEnvFlag(name string) EnvFlag {
return EnvFlag{
Name: name,
AltName: NormalizeEnvName(name),
}
}
2017-11-06 06:23:21 -05:00
func (f EnvFlag) GetValue(defaultValue func() string) string {
2017-05-25 19:11:38 -04:00
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
}
}
2017-11-06 06:23:21 -05:00
return defaultValue()
2017-05-25 19:11:38 -04:00
}
func (f EnvFlag) GetValueAsInt(defaultValue int) int {
2017-11-06 06:23:21 -05:00
useDefaultValue := false
s := f.GetValue(func() string {
useDefaultValue = true
return ""
})
if useDefaultValue {
2017-05-25 19:11:38 -04:00
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)), ".", "_")
2017-05-25 19:11:38 -04:00
}
2017-11-04 16:33:09 -04:00
func getExecutableDir() string {
exec, err := os.Executable()
if err != nil {
return ""
}
return filepath.Dir(exec)
}
2018-03-08 04:32:36 -05:00
func getExecutableSubDir(dir string) func() string {
2017-11-22 17:00:46 -05:00
return func() string {
return filepath.Join(getExecutableDir(), dir)
}
}
func GetPluginDirectory() string {
const name = "v2ray.location.plugin"
2018-07-18 04:40:28 -04:00
pluginDir := NewEnvFlag(name).GetValue(getExecutableSubDir("plugins"))
2017-11-22 17:00:46 -05:00
return pluginDir
}
2017-12-05 05:58:12 -05:00
func GetConfigurationPath() string {
const name = "v2ray.location.config"
2018-07-18 04:40:28 -04:00
configPath := NewEnvFlag(name).GetValue(getExecutableDir)
2017-12-05 05:58:12 -05:00
return filepath.Join(configPath, "config.json")
}
2020-01-02 20:26:48 -05:00
// GetConfDirPath reads "v2ray.location.confdir"
func GetConfDirPath() string {
const name = "v2ray.location.confdir"
configPath := NewEnvFlag(name).GetValue(func() string { return "" })
return configPath
}