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() { func init() {
const defaultFlagValue = "NOT_DEFINED_AT_ALL" 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 { switch value {
case defaultFlagValue, "auto", "enable": case defaultFlagValue, "auto", "enable":
useReadv = true useReadv = true

View File

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

View File

@ -84,3 +84,17 @@ func GetConfDirPath() string {
configPath := NewEnvFlag(name).GetValue(func() string { return "" }) configPath := NewEnvFlag(name).GetValue(func() string { return "" })
return configPath 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" "encoding/base64"
"fmt" "fmt"
"io" "io"
"os"
"strings" "strings"
"github.com/xtls/xray-core/common/buf" "github.com/xtls/xray-core/common/buf"
"github.com/xtls/xray-core/common/net" "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/protocol"
"github.com/xtls/xray-core/common/session" "github.com/xtls/xray-core/common/session"
"lukechampine.com/blake3" "lukechampine.com/blake3"
@ -28,20 +28,15 @@ var (
BaseKey []byte BaseKey []byte
) )
const (
EnvShow = "XRAY_XUDP_SHOW"
EnvBaseKey = "XRAY_XUDP_BASEKEY"
)
func init() { func init() {
if strings.ToLower(os.Getenv(EnvShow)) == "true" { if strings.ToLower(platform.NewEnvFlag(platform.XUDPLog).GetValue(func() string { return "" })) == "true" {
Show = 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 { if BaseKey, _ = base64.RawURLEncoding.DecodeString(raw); len(BaseKey) == 32 {
return return
} }
panic(EnvBaseKey + ": invalid value: " + raw) panic(platform.XUDPBaseKey + ": invalid value: " + raw)
} }
rand.Read(BaseKey) rand.Read(BaseKey)
} }

View File

@ -2,11 +2,11 @@ package core
import ( import (
"context" "context"
"os"
"reflect" "reflect"
"sync" "sync"
"github.com/xtls/xray-core/common" "github.com/xtls/xray-core/common"
"github.com/xtls/xray-core/common/platform"
"github.com/xtls/xray-core/common/serial" "github.com/xtls/xray-core/common/serial"
"github.com/xtls/xray-core/features" "github.com/xtls/xray-core/features"
"github.com/xtls/xray-core/features/dns" "github.com/xtls/xray-core/features/dns"
@ -181,7 +181,8 @@ func NewWithContext(ctx context.Context, config *Config) (*Instance, error) {
} }
func initInstanceWithConfig(config *Config, server *Instance) (bool, error) { func initInstanceWithConfig(config *Config, server *Instance) (bool, error) {
server.ctx = context.WithValue(server.ctx, "cone", os.Getenv("XRAY_CONE_DISABLED") != "true") server.ctx = context.WithValue(server.ctx, "cone",
platform.NewEnvFlag(platform.UseCone).GetValue(func() string { return "" }) != "true")
if config.Transport != nil { if config.Transport != nil {
features.PrintDeprecatedFeatureWarning("global transport settings") features.PrintDeprecatedFeatureWarning("global transport settings")

View File

@ -2,10 +2,10 @@ package conf
import ( import (
"encoding/json" "encoding/json"
"os"
"strings" "strings"
"github.com/xtls/xray-core/common/net" "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/protocol"
) )
@ -46,7 +46,7 @@ func (v *Address) UnmarshalJSON(data []byte) error {
return newError("invalid address: ", string(data)).Base(err) return newError("invalid address: ", string(data)).Base(err)
} }
if strings.HasPrefix(rawStr, "env:") { if strings.HasPrefix(rawStr, "env:") {
rawStr = os.Getenv(rawStr[4:]) rawStr = platform.NewEnvFlag(rawStr[4:]).GetValue(func() string { return "" })
} }
v.Address = net.ParseAddress(rawStr) v.Address = net.ParseAddress(rawStr)
@ -118,7 +118,7 @@ func parseIntPort(data []byte) (net.Port, error) {
func parseStringPort(s string) (net.Port, net.Port, error) { func parseStringPort(s string) (net.Port, net.Port, error) {
if strings.HasPrefix(s, "env:") { if strings.HasPrefix(s, "env:") {
s = os.Getenv(s[4:]) s = platform.NewEnvFlag(s[4:]).GetValue(func() string { return "" })
} }
pair := strings.SplitN(s, "-", 2) pair := strings.SplitN(s, "-", 2)

View File

@ -41,7 +41,7 @@ func init() {
return h, nil return h, nil
})) }))
const defaultFlagValue = "NOT_DEFINED_AT_ALL" const defaultFlagValue = "NOT_DEFINED_AT_ALL"
value := platform.NewEnvFlag("xray.buf.splice").GetValue(func() string { return defaultFlagValue }) value := platform.NewEnvFlag(platform.UseFreedomSplice).GetValue(func() string { return defaultFlagValue })
switch value { switch value {
case "auto", "enable": case "auto", "enable":
useSplice = true useSplice = true

View File

@ -245,7 +245,7 @@ func init() {
const defaultFlagValue = "NOT_DEFINED_AT_ALL" const defaultFlagValue = "NOT_DEFINED_AT_ALL"
paddingValue := platform.NewEnvFlag("xray.vmess.padding").GetValue(func() string { return defaultFlagValue }) paddingValue := platform.NewEnvFlag(platform.UseVmessPadding).GetValue(func() string { return defaultFlagValue })
if paddingValue != defaultFlagValue { if paddingValue != defaultFlagValue {
enablePadding = true enablePadding = true
} }

View File

@ -8,12 +8,12 @@ import (
"io" "io"
gonet "net" gonet "net"
"net/http" "net/http"
"os"
"time" "time"
"github.com/gorilla/websocket" "github.com/gorilla/websocket"
"github.com/xtls/xray-core/common" "github.com/xtls/xray-core/common"
"github.com/xtls/xray-core/common/net" "github.com/xtls/xray-core/common/net"
"github.com/xtls/xray-core/common/platform"
"github.com/xtls/xray-core/common/session" "github.com/xtls/xray-core/common/session"
"github.com/xtls/xray-core/transport/internet" "github.com/xtls/xray-core/transport/internet"
"github.com/xtls/xray-core/transport/internet/stat" "github.com/xtls/xray-core/transport/internet/stat"
@ -26,7 +26,8 @@ var webpage []byte
var conns chan *websocket.Conn var conns chan *websocket.Conn
func init() { func init() {
if addr := os.Getenv("XRAY_BROWSER_DIALER"); addr != "" { addr := platform.NewEnvFlag(platform.BrowserDialerAddress).GetValue(func() string { return "" })
if addr != "" {
conns = make(chan *websocket.Conn, 256) conns = make(chan *websocket.Conn, 256)
go http.ListenAndServe(addr, http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { go http.ListenAndServe(addr, http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
if r.URL.Path == "/websocket" { if r.URL.Path == "/websocket" {