Hunter0x7c7
2022-08-11 b8230139fb40edea387617b6accd8371e37eda58
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
package 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 = "v2ray.location.plugin"
    pluginDir := NewEnvFlag(name).GetValue(getExecutableSubDir("plugins"))
    return pluginDir
}
 
func GetConfigurationPath() string {
    const name = "v2ray.location.config"
    configPath := NewEnvFlag(name).GetValue(getExecutableDir)
    return filepath.Join(configPath, "config.json")
}
 
// GetConfDirPath reads "v2ray.location.confdir"
func GetConfDirPath() string {
    const name = "v2ray.location.confdir"
    configPath := NewEnvFlag(name).GetValue(func() string { return "" })
    return configPath
}