From cf98acb5d974b8ddccb3b65909959ca4ec781bca Mon Sep 17 00:00:00 2001 From: dnomd343 Date: Sat, 20 Aug 2022 14:32:54 +0800 Subject: [PATCH] feat: support config with json format --- cmd/config/decode.go | 43 +++++++++++++++++++++++++------------------ cmd/config/main.go | 3 ++- cmd/xproxy.go | 19 +++++++++++++------ 3 files changed, 40 insertions(+), 25 deletions(-) diff --git a/cmd/config/decode.go b/cmd/config/decode.go index 98ddbec..9a3ed1e 100644 --- a/cmd/config/decode.go +++ b/cmd/config/decode.go @@ -5,39 +5,46 @@ import ( "XProxy/cmd/common" "XProxy/cmd/proxy" "XProxy/cmd/radvd" + "encoding/json" log "github.com/sirupsen/logrus" "gopkg.in/yaml.v3" ) -type yamlNetConfig struct { +type NetConfig struct { Gateway string `yaml:"gateway"` // network gateway Address string `yaml:"address"` // network address } -type yamlConfig struct { +type RawConfig struct { Custom []string `yaml:"custom" json:"custom"` Update asset.Config `yaml:"update" json:"update"` Radvd radvd.Config `yaml:"radvd" json:"radvd"` Proxy proxy.Config `yaml:"proxy" json:"proxy"` Network struct { - DNS []string `yaml:"dns" json:"dns"` - ByPass []string `yaml:"bypass" json:"bypass"` - IPv4 yamlNetConfig `yaml:"ipv4" json:"ipv4"` - IPv6 yamlNetConfig `yaml:"ipv6" json:"ipv6"` + DNS []string `yaml:"dns" json:"dns"` + ByPass []string `yaml:"bypass" json:"bypass"` + IPv4 NetConfig `yaml:"ipv4" json:"ipv4"` + IPv6 NetConfig `yaml:"ipv6" json:"ipv6"` } `yaml:"network" json:"network"` } -func yamlDecode(raw []byte) yamlConfig { - var config yamlConfig +func configDecode(raw []byte, fileSuffix string) RawConfig { + var config RawConfig log.Debugf("Config raw content -> \n%s", string(raw)) - if err := yaml.Unmarshal(raw, &config); err != nil { // yaml (or json) decode - log.Panicf("Decode config file error -> %v", err) + if fileSuffix == ".json" { + if err := json.Unmarshal(raw, &config); err != nil { // json format decode + log.Panicf("Decode config file error -> %v", err) + } + } else { + if err := yaml.Unmarshal(raw, &config); err != nil { // yaml format decode + log.Panicf("Decode config file error -> %v", err) + } } log.Debugf("Decoded configure -> %v", config) return config } -func decodeDns(rawConfig *yamlConfig, config *Config) { +func decodeDns(rawConfig *RawConfig, config *Config) { for _, address := range rawConfig.Network.DNS { // dns options if common.IsIPv4(address, false) || common.IsIPv6(address, false) { config.DNS = append(config.DNS, address) @@ -48,7 +55,7 @@ func decodeDns(rawConfig *yamlConfig, config *Config) { log.Debugf("DNS server -> %v", config.DNS) } -func decodeBypass(rawConfig *yamlConfig, config *Config) { +func decodeBypass(rawConfig *RawConfig, config *Config) { for _, address := range rawConfig.Network.ByPass { // bypass options if common.IsIPv4(address, true) { config.IPv4.Bypass = append(config.IPv4.Bypass, address) @@ -62,7 +69,7 @@ func decodeBypass(rawConfig *yamlConfig, config *Config) { log.Debugf("IPv6 bypass CIDR -> %s", config.IPv6.Bypass) } -func decodeIPv4(rawConfig *yamlConfig, config *Config) { +func decodeIPv4(rawConfig *RawConfig, config *Config) { config.IPv4.Address = rawConfig.Network.IPv4.Address config.IPv4.Gateway = rawConfig.Network.IPv4.Gateway if config.IPv4.Address != "" && !common.IsIPv4(config.IPv4.Address, true) { @@ -74,7 +81,7 @@ func decodeIPv4(rawConfig *yamlConfig, config *Config) { log.Debugf("IPv4 -> address = %s | gateway = %s", config.IPv4.Address, config.IPv4.Gateway) } -func decodeIPv6(rawConfig *yamlConfig, config *Config) { +func decodeIPv6(rawConfig *RawConfig, config *Config) { config.IPv6.Address = rawConfig.Network.IPv6.Address config.IPv6.Gateway = rawConfig.Network.IPv6.Gateway if config.IPv6.Address != "" && !common.IsIPv6(config.IPv6.Address, true) { @@ -86,7 +93,7 @@ func decodeIPv6(rawConfig *yamlConfig, config *Config) { log.Debugf("IPv6 -> address = %s | gateway = %s", config.IPv6.Address, config.IPv6.Gateway) } -func decodeProxy(rawConfig *yamlConfig, config *Config) { +func decodeProxy(rawConfig *RawConfig, config *Config) { config.Proxy = rawConfig.Proxy if config.Proxy.Core == "" { config.Proxy.Core = "xray" // use xray in default @@ -104,7 +111,7 @@ func decodeProxy(rawConfig *yamlConfig, config *Config) { log.Debugf("Connection sniff exlcude -> %v", config.Proxy.Sniff.Exclude) } -func decodeRadvd(rawConfig *yamlConfig, config *Config) { +func decodeRadvd(rawConfig *RawConfig, config *Config) { config.Radvd = rawConfig.Radvd log.Debugf("Radvd log level -> %d", config.Radvd.Log) log.Debugf("Radvd enable -> %t", config.Radvd.Enable) @@ -116,13 +123,13 @@ func decodeRadvd(rawConfig *yamlConfig, config *Config) { log.Debugf("Radvd DNSSL -> %v", config.Radvd.DNSSL) } -func decodeUpdate(rawConfig *yamlConfig, config *Config) { +func decodeUpdate(rawConfig *RawConfig, config *Config) { config.Update = rawConfig.Update log.Debugf("Update cron -> %s", config.Update.Cron) log.Debugf("Update urls -> %v", config.Update.Url) } -func decodeCustom(rawConfig *yamlConfig, config *Config) { +func decodeCustom(rawConfig *RawConfig, config *Config) { config.Script = rawConfig.Custom log.Debugf("Custom script -> %v", config.Script) } diff --git a/cmd/config/main.go b/cmd/config/main.go index 4039f6e..f57bb09 100644 --- a/cmd/config/main.go +++ b/cmd/config/main.go @@ -8,6 +8,7 @@ import ( "XProxy/cmd/radvd" log "github.com/sirupsen/logrus" "os" + "path" ) type Config struct { @@ -29,7 +30,7 @@ func Load(configFile string, config *Config) { if err != nil { log.Panicf("Failed to open %s -> %v", configFile, err) } - rawConfig := yamlDecode(raw) // decode yaml content + rawConfig := configDecode(raw, path.Ext(configFile)) // decode configure content decodeDns(&rawConfig, config) decodeBypass(&rawConfig, config) decodeIPv4(&rawConfig, config) diff --git a/cmd/xproxy.go b/cmd/xproxy.go index 103d04e..6ea024c 100644 --- a/cmd/xproxy.go +++ b/cmd/xproxy.go @@ -4,13 +4,14 @@ import ( "XProxy/cmd/common" "XProxy/cmd/config" "XProxy/cmd/process" + "flag" log "github.com/sirupsen/logrus" "os" "path" "strconv" ) -var version = "0.9.2" +var version = "0.9.3" var v4RouteTable = 100 var v6RouteTable = 106 var v4TProxyPort = 7288 @@ -22,16 +23,22 @@ var goVersion string var subProcess []*process.Process var assetDir, exposeDir, configFile string -func xproxyInit() { +func logInit(isDebug bool) { log.SetFormatter(&log.TextFormatter{ FullTimestamp: true, TimestampFormat: "2006-01-02 15:04:05", }) - if len(os.Args) > 1 && os.Args[1] == "--debug" { + log.SetLevel(log.InfoLevel) // default log level + if isDebug { log.SetLevel(log.DebugLevel) - } else { - log.SetLevel(log.InfoLevel) } +} + +func xproxyInit() { + var isDebug = flag.Bool("debug", false, "Enable debug mode") + var configName = flag.String("config", "xproxy.yml", "Config file name") + flag.Parse() + logInit(*isDebug) if os.Getenv("IPV4_TABLE") != "" { v4RouteTable, _ = strconv.Atoi(os.Getenv("IPV4_TABLE")) @@ -56,7 +63,7 @@ func xproxyInit() { } common.CreateFolder(exposeDir) assetDir = path.Join(exposeDir, "assets") - configFile = path.Join(exposeDir, "xproxy.yml") + configFile = path.Join(exposeDir, *configName) log.Debugf("Expose folder -> %s", exposeDir) log.Debugf("Assets folder -> %s", assetDir) log.Debugf("Config file -> %s", configFile)