mirror of
https://github.com/v2rayA/v2rayA.git
synced 2026-11-20 23:02:25 +03:00
151 lines
7.5 KiB
Go
151 lines
7.5 KiB
Go
package conf
|
|
|
|
import (
|
|
"fmt"
|
|
log2 "log"
|
|
"os"
|
|
"path/filepath"
|
|
"strings"
|
|
"sync"
|
|
|
|
"github.com/stevenroose/gonfig"
|
|
"github.com/v2rayA/v2rayA/common"
|
|
"github.com/v2rayA/v2rayA/pkg/util/log"
|
|
"github.com/v2rayA/v2rayA/pkg/util/privilege"
|
|
)
|
|
|
|
type Params struct {
|
|
Address string `id:"address" short:"a" default:"0.0.0.0:2017" desc:"Listening address"`
|
|
Socket string `id:"socket" desc:"Unix socket path for listening"`
|
|
BaseUrl string `id:"baseurl" default:"/" desc:"Base URL path prefix"`
|
|
V2rayBin string `id:"v2ray-bin" short:"b" desc:"Executable v2ray binary path. Auto-detect if put it empty."`
|
|
Config string `id:"config" short:"c" desc:"v2rayA configuration directory"`
|
|
V2rayConfigDirectory string `id:"v2ray-confdir" desc:"Additional v2ray config directory, files in it will be combined with config generated by v2rayA"`
|
|
TinyTunBin string `id:"tinytun-bin" desc:"Deprecated and ignored: the TUN is built into the core. Kept so service units that still pass it start."`
|
|
V2rayAssetsDirectory string `id:"v2ray-assetsdir" desc:"v2ray-core assets directory for searching and downloading files like geoip.dat. This will override environment V2RAY_LOCATION_ASSET and XRAY_LOCATION_ASSET."`
|
|
CoreStartupTimeout int64 `id:"core-startup-timeout" default:"15" desc:"Timeout duration in seconds for starting v2ray or xray core. On devices with lower performance, consider increasing this value."`
|
|
TransparentHook string `id:"transparent-hook" desc:"the executable file to run in the transparent proxy life-cycle. v2rayA will pass in the --transparent-type (tproxy, redirect) and --stage (pre-start, post-start, pre-stop, post-stop) arguments."`
|
|
CoreHook string `id:"core-hook" desc:"the executable that parses and configures nodes of protocols v2rayA does not know. v2rayA runs it with --stage=parse or --stage=configuration, --link and --v2raya-confdir."`
|
|
PluginManager string `id:"plugin-manager" desc:"the executable that parses and configures nodes of protocols v2rayA does not know. v2rayA runs it with --stage=parse or --stage=configuration, --link and --v2raya-confdir."`
|
|
WebDir string `id:"webdir" desc:"v2rayA web files directory. use embedded files if not specify."`
|
|
IPV6Support string `id:"ipv6-support" default:"auto" desc:"Optional values: auto, on, off. Make sure your IPv6 network works fine before you turn it on."`
|
|
RedirectBoundDevice bool `id:"redirect-respect-bound-device" desc:"[Linux Only] In redirect mode, let TCP sockets bound to a device (SO_BINDTODEVICE, as NetworkManager's connectivity checks are) bypass the proxy. Needs kernel 5.14 or later and cgroup v2 at /sys/fs/cgroup."`
|
|
NftablesSupport string `id:"nftables-support" default:"auto" desc:"Optional values: auto, on, off. Experimental feature. Make sure you have installed nftables."`
|
|
PassCheckRoot bool `id:"passcheckroot" desc:"Skip privilege checking. Use it only when you cannot start v2raya but confirm you have root privilege"`
|
|
ResetPassword bool `id:"reset-password" ignore:"1"`
|
|
LogLevel string `id:"log-level" default:"info" desc:"Optional values: trace, debug, info, warn or error"`
|
|
LogFile string `id:"log-file" desc:"The path of log file"`
|
|
LogMaxDays int64 `id:"log-max-days" default:"3" desc:"Maximum number of days to keep log files"`
|
|
LogDisableColor bool `id:"log-disable-color" ignore:"1"`
|
|
LogDisableTimestamp bool `id:"log-disable-timestamp" desc:"Intended for use with systemd/journald to avoid duplicate timestamps in logs. This flag is ignored when using the --log-file flag or the V2RAYA_LOG_FILE environment variable." ignore:"1"`
|
|
Lite bool `id:"lite" desc:"Lite mode: no transparent proxy, configuration under the user's directory. Implied when not started as root or administrator." ignore:"1"`
|
|
WinEnvFile string `id:"win-envfile" desc:"[Windows Only] Path to environment variables file to load before service starts"`
|
|
ShowVersion bool `id:"version" ignore:"1"`
|
|
PrintReport string `id:"report" desc:"Print report" ignore:"1"`
|
|
}
|
|
|
|
var params Params
|
|
|
|
func initFunc() {
|
|
if err := loadPlatformEnv(); err != nil {
|
|
log.Warn("failed to load platform env: %v", err)
|
|
}
|
|
err := gonfig.Load(¶ms, gonfig.Conf{
|
|
FileDisable: true,
|
|
FlagIgnoreUnknown: false,
|
|
EnvPrefix: "V2RAYA_",
|
|
})
|
|
if err != nil {
|
|
// go test hands its own -test.* flags to the binary; only "-test.v"
|
|
// used to be tolerated, so every package whose tests reach this init
|
|
// died on the first other test flag.
|
|
if !strings.Contains(err.Error(), "parsing flags: '-test.") {
|
|
log2.Fatal(err)
|
|
}
|
|
}
|
|
if params.BaseUrl == "" {
|
|
params.BaseUrl = "/"
|
|
} else {
|
|
if !strings.HasPrefix(params.BaseUrl, "/") {
|
|
params.BaseUrl = "/" + params.BaseUrl
|
|
}
|
|
if params.BaseUrl != "/" {
|
|
params.BaseUrl = strings.TrimSuffix(params.BaseUrl, "/")
|
|
}
|
|
}
|
|
if params.ShowVersion {
|
|
fmt.Println(Version)
|
|
os.Exit(0)
|
|
}
|
|
// Started without root or administrator rights, v2rayA is in lite mode
|
|
// whether or not --lite was given: nothing it could do as root works, and
|
|
// the configuration directory has to be the user's. A service definition
|
|
// (brew services, a user unit) can then be one file for both, as root with
|
|
// the transparent proxy or as the user with the system proxy.
|
|
if !params.Lite && !params.PassCheckRoot && !privilege.IsRootOrAdmin() {
|
|
params.Lite = true
|
|
log.Warn("not running as root or administrator: lite mode, without transparent proxy. If you want tun, start as root or administrator.")
|
|
}
|
|
if params.Lite {
|
|
params.PassCheckRoot = true
|
|
}
|
|
if params.Config == "" {
|
|
params.Config = defaultConfigDir(params.Lite)
|
|
}
|
|
// Fix mis-detected Linux-style config path on Windows
|
|
params.Config = sanitizeConfigDirForPlatform(params.Config, params.Lite)
|
|
// replace all dots of the filename with underlines
|
|
params.Config = filepath.Join(
|
|
filepath.Dir(params.Config),
|
|
strings.ReplaceAll(filepath.Base(params.Config), ".", "_"),
|
|
)
|
|
// expand '~' with user home
|
|
params.Config, err = common.HomeExpand(params.Config)
|
|
if err != nil {
|
|
log2.Fatal(err)
|
|
}
|
|
if strings.Contains(params.Config, "$HOME") {
|
|
if h, err := os.UserHomeDir(); err == nil {
|
|
params.Config = strings.ReplaceAll(params.Config, "$HOME", h)
|
|
}
|
|
}
|
|
// 展开各路径参数中的平台相关环境变量(Windows 上处理 %VAR% 风格)
|
|
expandPlatformConfigPaths(¶ms)
|
|
if _, err := os.Stat(params.Config); os.IsNotExist(err) {
|
|
_ = os.MkdirAll(params.Config, os.ModeDir|0750)
|
|
} else if err != nil {
|
|
log.Warn("%v", err)
|
|
}
|
|
logWay := "console"
|
|
if params.LogFile != "" {
|
|
logWay = "file"
|
|
_ = os.MkdirAll(filepath.Dir(params.LogFile), os.ModeDir|0750)
|
|
}
|
|
log.InitLog(logWay, params.LogFile, params.LogLevel, params.LogMaxDays, params.LogDisableColor, params.LogDisableTimestamp)
|
|
|
|
// V2rayAssetsDirectory
|
|
if params.V2rayAssetsDirectory != "" {
|
|
if err = os.Setenv("V2RAY_LOCATION_ASSET", params.V2rayAssetsDirectory); err != nil {
|
|
log.Fatal("failed to set V2rayAssetsDirectory: %v", err)
|
|
}
|
|
if err = os.Setenv("XRAY_LOCATION_ASSET", params.V2rayAssetsDirectory); err != nil {
|
|
log.Fatal("failed to set V2rayAssetsDirectory: %v", err)
|
|
}
|
|
}
|
|
|
|
if params.CoreStartupTimeout <= 0 {
|
|
log.Fatal("invalid CoreStartupTimeout: %d seconds. It must be a positive integer greater than 0.", params.CoreStartupTimeout)
|
|
}
|
|
}
|
|
|
|
var once sync.Once
|
|
|
|
func GetEnvironmentConfig() *Params {
|
|
once.Do(initFunc)
|
|
return ¶ms
|
|
}
|
|
|
|
func SetConfig(config Params) {
|
|
params = config
|
|
}
|