Browse Source

feat: support config with json format

v1.x.x
dnomd343 2 years ago
parent
commit
cf98acb5d9
  1. 37
      cmd/config/decode.go
  2. 3
      cmd/config/main.go
  3. 19
      cmd/xproxy.go

37
cmd/config/decode.go

@ -5,16 +5,17 @@ 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"`
@ -22,22 +23,28 @@ type yamlConfig struct {
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"`
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
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)
}

3
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)

19
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)

Loading…
Cancel
Save