Unify environment var readers

This commit is contained in:
yuhan6665 2023-10-28 17:24:54 -04:00
parent e241e5bda6
commit 4f05e0ac2b
9 changed files with 32 additions and 23 deletions

View file

@ -147,7 +147,7 @@ var useReadv bool
func init() {
const defaultFlagValue = "NOT_DEFINED_AT_ALL"
value := platform.NewEnvFlag("xray.buf.readv").GetValue(func() string { return defaultFlagValue })
value := platform.NewEnvFlag(platform.UseReadV).GetValue(func() string { return defaultFlagValue })
switch value {
case defaultFlagValue, "auto", "enable":
useReadv = true

View file

@ -17,15 +17,13 @@ func LineSeparator() string {
}
func GetToolLocation(file string) string {
const name = "xray.location.tool"
toolPath := EnvFlag{Name: name, AltName: NormalizeEnvName(name)}.GetValue(getExecutableDir)
toolPath := NewEnvFlag(UnixToolLocation).GetValue(getExecutableDir)
return filepath.Join(toolPath, file)
}
// GetAssetLocation searches for `file` in certain locations
func GetAssetLocation(file string) string {
const name = "xray.location.asset"
assetPath := NewEnvFlag(name).GetValue(getExecutableDir)
assetPath := NewEnvFlag(UnixAssetLocation).GetValue(getExecutableDir)
defPath := filepath.Join(assetPath, file)
for _, p := range []string{
defPath,

View file

@ -84,3 +84,17 @@ func GetConfDirPath() string {
configPath := NewEnvFlag(name).GetValue(func() string { return "" })
return configPath
}
const (
UnixToolLocation = "xray.location.tool"
UnixAssetLocation = "xray.location.asset"
UseReadV = "xray.buf.readv"
UseFreedomSplice = "xray.buf.splice"
UseVmessPadding = "xray.vmess.padding"
UseCone = "xray.cone.disabled"
BrowserDialerAddress = "xray.browser.dialer"
XUDPLog = "xray.xudp.show"
XUDPBaseKey = "xray.xudp.basekey"
)

View file

@ -6,11 +6,11 @@ import (
"encoding/base64"
"fmt"
"io"
"os"
"strings"
"github.com/xtls/xray-core/common/buf"
"github.com/xtls/xray-core/common/net"
"github.com/xtls/xray-core/common/platform"
"github.com/xtls/xray-core/common/protocol"
"github.com/xtls/xray-core/common/session"
"lukechampine.com/blake3"
@ -28,20 +28,15 @@ var (
BaseKey []byte
)
const (
EnvShow = "XRAY_XUDP_SHOW"
EnvBaseKey = "XRAY_XUDP_BASEKEY"
)
func init() {
if strings.ToLower(os.Getenv(EnvShow)) == "true" {
if strings.ToLower(platform.NewEnvFlag(platform.XUDPLog).GetValue(func() string { return "" })) == "true" {
Show = true
}
if raw, found := os.LookupEnv(EnvBaseKey); found {
if raw := platform.NewEnvFlag(platform.XUDPBaseKey).GetValue(func() string { return "" }); raw != "" {
if BaseKey, _ = base64.RawURLEncoding.DecodeString(raw); len(BaseKey) == 32 {
return
}
panic(EnvBaseKey + ": invalid value: " + raw)
panic(platform.XUDPBaseKey + ": invalid value: " + raw)
}
rand.Read(BaseKey)
}