2017-05-25 19:11:38 -04:00
|
|
|
package platform
|
|
|
|
|
|
|
|
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
|
|
|
|
}
|
|
|
|
|
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.Replace(strings.ToUpper(strings.TrimSpace(name)), ".", "_", -1)
|
|
|
|
}
|
2017-11-04 16:33:09 -04:00
|
|
|
|
2017-11-11 16:29:00 -05: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)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-11-04 16:33:09 -04:00
|
|
|
func GetAssetLocation(file string) string {
|
2017-11-11 09:23:42 -05:00
|
|
|
const name = "v2ray.location.asset"
|
2017-11-11 16:29:00 -05:00
|
|
|
assetPath := EnvFlag{Name: name, AltName: NormalizeEnvName(name)}.GetValue(getExecutableDir)
|
2017-11-04 16:33:09 -04:00
|
|
|
return filepath.Join(assetPath, file)
|
|
|
|
}
|
2017-11-22 17:00:46 -05:00
|
|
|
|
|
|
|
func GetPluginDirectory() string {
|
|
|
|
const name = "v2ray.location.plugin"
|
2018-03-08 04:32:36 -05:00
|
|
|
pluginDir := EnvFlag{Name: name, AltName: NormalizeEnvName(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"
|
|
|
|
configPath := EnvFlag{Name: name, AltName: NormalizeEnvName(name)}.GetValue(getExecutableDir)
|
|
|
|
return filepath.Join(configPath, "config.json")
|
|
|
|
}
|