From d7e4dfb8c6276403df752493c8b8c6009da00825 Mon Sep 17 00:00:00 2001 From: dnomd343 Date: Wed, 17 Aug 2022 22:30:23 +0800 Subject: [PATCH] update: framework of config package --- cmd/config/decode.go | 109 ++++++++++++++++++++++++++++++++++++++++++ cmd/config/default.go | 24 ++++++++++ cmd/config/main.go | 39 +++++++++++++++ main.go | 2 +- src/config.go | 109 ------------------------------------------ 5 files changed, 173 insertions(+), 110 deletions(-) create mode 100644 cmd/config/decode.go create mode 100644 cmd/config/default.go create mode 100644 cmd/config/main.go diff --git a/cmd/config/decode.go b/cmd/config/decode.go new file mode 100644 index 0000000..805ec13 --- /dev/null +++ b/cmd/config/decode.go @@ -0,0 +1,109 @@ +package config + +import ( + "XProxy/cmd/common" + log "github.com/sirupsen/logrus" + "gopkg.in/yaml.v3" +) + +type yamlNetConfig struct { + Gateway string `yaml:"gateway"` // network gateway + Address string `yaml:"address"` // network address +} + +type yamlConfig struct { + Custom []string `yaml:"custom"` + Update struct { + Cron string `yaml:"cron"` + Url map[string]string `yaml:"url"` + } `yaml:"update"` + Proxy struct { + Sniff bool `yaml:"sniff"` + Redirect bool `yaml:"redirect"` + Http map[string]int `yaml:"http"` + Socks map[string]int `yaml:"socks"` + AddOn []interface{} `yaml:"addon"` + } `yaml:"proxy"` + Network struct { + DNS []string `yaml:"dns"` // system dns server + ByPass []string `yaml:"bypass"` // cidr bypass list + IPv4 yamlNetConfig `yaml:"ipv4"` // ipv4 network configure + IPv6 yamlNetConfig `yaml:"ipv6"` // ipv6 network configure + } `yaml:"network"` +} + +func yamlDecode(raw []byte) yamlConfig { + var config yamlConfig + log.Debugf("Decode yaml 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) + } + log.Debugf("Decoded config -> %v", config) + return config +} + +func decodeDns(rawConfig *yamlConfig) []string { + var dns []string + for _, address := range rawConfig.Network.DNS { // dns options + if common.IsIPv4(address, false) || common.IsIPv6(address, false) { + dns = append(dns, address) + } else { + log.Panicf("Invalid DNS server -> %s", address) + } + } + log.Debugf("DNS server -> %v", dns) + return dns +} + +func decodeBypass(rawConfig *yamlConfig) ([]string, []string) { + var v4Bypass, v6Bypass []string + for _, address := range rawConfig.Network.ByPass { // bypass options + if common.IsIPv4(address, true) { + v4Bypass = append(v4Bypass, address) + } else if common.IsIPv6(address, true) { + v6Bypass = append(v6Bypass, address) + } else { + log.Panicf("Invalid bypass CIDR -> %s", address) + } + } + log.Debugf("IPv4 bypass CIDR -> %s", v4Bypass) + log.Debugf("IPv6 bypass CIDR -> %s", v6Bypass) + return v4Bypass, v6Bypass +} + +func decodeIPv4(rawConfig *yamlConfig) (string, string) { + v4Address := rawConfig.Network.IPv4.Address + v4Gateway := rawConfig.Network.IPv4.Gateway + if v4Address != "" && !common.IsIPv4(v4Address, true) { + log.Panicf("Invalid IPv4 address -> %s", v4Address) + } + if v4Gateway != "" && !common.IsIPv4(v4Gateway, false) { + log.Panicf("Invalid IPv4 gateway -> %s", v4Gateway) + } + log.Infof("IPv4 -> address = %s | gateway = %s", v4Address, v4Gateway) + return v4Address, v4Gateway +} + +func decodeIPv6(rawConfig *yamlConfig) (string, string) { + v6Address := rawConfig.Network.IPv6.Address + v6Gateway := rawConfig.Network.IPv6.Gateway + if v6Address != "" && !common.IsIPv6(v6Address, true) { + log.Panicf("Invalid IPv6 address -> %s", v6Address) + } + if v6Gateway != "" && !common.IsIPv6(v6Gateway, false) { + log.Panicf("Invalid IPv6 gateway -> %s", v6Gateway) + } + log.Infof("IPv6 -> address = %s | gateway = %s", v6Address, v6Gateway) + return v6Address, v6Gateway +} + +func decode(rawConfig yamlConfig) Config { + var config Config + + config.DNS = decodeDns(&rawConfig) + config.V4Bypass, config.V6Bypass = decodeBypass(&rawConfig) + config.V4Address, config.V4Gateway = decodeIPv4(&rawConfig) + config.V6Address, config.V6Gateway = decodeIPv6(&rawConfig) + + return config +} diff --git a/cmd/config/default.go b/cmd/config/default.go new file mode 100644 index 0000000..8f7633b --- /dev/null +++ b/cmd/config/default.go @@ -0,0 +1,24 @@ +package config + +var defaultConfig = `# default configure file for xproxy +proxy: + sniff: true + redirect: true + +network: + dns: null + ipv4: null + ipv6: null + bypass: + - 169.254.0.0/16 + - 224.0.0.0/3 + - fc00::/7 + - fe80::/10 + - ff00::/8 + +update: + cron: "0 0 4 * * *" + url: + geoip.dat: "https://github.com/Loyalsoldier/v2ray-rules-dat/releases/latest/download/geoip.dat" + geosite.dat: "https://github.com/Loyalsoldier/v2ray-rules-dat/releases/latest/download/geosite.dat" +` diff --git a/cmd/config/main.go b/cmd/config/main.go new file mode 100644 index 0000000..2fe07b5 --- /dev/null +++ b/cmd/config/main.go @@ -0,0 +1,39 @@ +package config + +import ( + "XProxy/cmd/common" + log "github.com/sirupsen/logrus" + "os" +) + +type Config struct { + // asset update + + DNS []string + + V4Address string + V4Gateway string + V4Bypass []string + + V6Address string + V6Gateway string + V6Bypass []string + + // httpInbounds + // socksInbounds + // addOnInbounds + +} + +func Load(configFile string) Config { + if !common.IsFileExist(configFile) { // configure not exist -> load default + log.Infof("Load default configure -> %s", configFile) + common.WriteFile(configFile, defaultConfig, false) + } + raw, err := os.ReadFile(configFile) // read configure content + if err != nil { + log.Panicf("Failed to open %s -> %v", configFile, err) + } + rawConfig := yamlDecode(raw) // decode yaml content + return decode(rawConfig) +} diff --git a/main.go b/main.go index 5af74f6..60d0a95 100644 --- a/main.go +++ b/main.go @@ -10,7 +10,7 @@ import ( func main() { log.SetLevel(log.DebugLevel) - fmt.Println("xproxy start") + fmt.Println("XProxy start") network.Load(nil, network.Config{ RouteTable: 100, diff --git a/src/config.go b/src/config.go index bcaab03..1239fc0 100644 --- a/src/config.go +++ b/src/config.go @@ -2,8 +2,6 @@ package main import ( log "github.com/sirupsen/logrus" - "gopkg.in/yaml.v3" - "os" ) var v4Bypass []string @@ -15,116 +13,9 @@ var v4Address string var v6Gateway string var v6Address string -var defaultConfig = `# default configure file for xproxy -proxy: - sniff: true - redirect: true - -network: - dns: null - ipv4: null - ipv6: null - bypass: - - 169.254.0.0/16 - - 224.0.0.0/3 - - fc00::/7 - - fe80::/10 - - ff00::/8 - -update: - cron: "0 0 4 * * *" - url: - geoip.dat: "https://github.com/Loyalsoldier/v2ray-rules-dat/releases/latest/download/geoip.dat" - geosite.dat: "https://github.com/Loyalsoldier/v2ray-rules-dat/releases/latest/download/geosite.dat" -` - -type NetConfig struct { - Gateway string `yaml:"gateway"` // network gateway - Address string `yaml:"address"` // network address -} - -type Config struct { - Script []string `yaml:"script"` - Update struct { - Cron string `yaml:"cron"` - Url map[string]string `yaml:"url"` - } `yaml:"update"` - Proxy struct { - Sniff bool `yaml:"sniff"` - Redirect bool `yaml:"redirect"` - Http map[string]int `yaml:"http"` - Socks map[string]int `yaml:"socks"` - AddOn []interface{} `yaml:"addon"` - } `yaml:"proxy"` - Network struct { - DNS []string `yaml:"dns"` // system dns server - ByPass []string `yaml:"bypass"` // cidr bypass list - IPv4 NetConfig `yaml:"ipv4"` // ipv4 network configure - IPv6 NetConfig `yaml:"ipv6"` // ipv6 network configure - } `yaml:"network"` -} - func loadConfig(configFile string) { - if !isFileExist(configFile) { // load default configure - log.Infof("Load default configure -> %s", configFile) - err := os.WriteFile(configFile, []byte(defaultConfig), 0644) - if err != nil { - log.Panicf("File %s save error -> %v", configFile, err) - } - } config := Config{} - rawConfig, err := os.ReadFile(configFile) - if err != nil { - log.Panicf("Failed to open %s -> %v", configFile, err) - } - log.Debugf("Decode yaml content -> \n%s", string(rawConfig)) - err = yaml.Unmarshal(rawConfig, &config) // yaml (or json) decode - if err != nil { - log.Panicf("Decode config file error -> %v", err) - } - log.Debugf("Decoded config -> %v", config) - - for _, address := range config.Network.DNS { // dns options - if isIPv4(address, false) || isIPv6(address, false) { - dnsServer = append(dnsServer, address) - } else { - log.Panicf("Invalid DNS server -> %s", address) - } - } - log.Infof("DNS server -> %v", dnsServer) - - for _, address := range config.Network.ByPass { // bypass options - if isIPv4(address, true) { - v4Bypass = append(v4Bypass, address) - } else if isIPv6(address, true) { - v6Bypass = append(v6Bypass, address) - } else { - log.Panicf("Invalid bypass CIDR -> %s", address) - } - } - log.Infof("IPv4 bypass CIDR -> %s", v4Bypass) - log.Infof("IPv6 bypass CIDR -> %s", v6Bypass) - - v4Address = config.Network.IPv4.Address - v4Gateway = config.Network.IPv4.Gateway - if v4Address != "" && !isIPv4(v4Address, true) { - log.Panicf("Invalid IPv4 address -> %s", v4Address) - } - if v4Gateway != "" && !isIPv4(v4Gateway, false) { - log.Panicf("Invalid IPv4 gateway -> %s", v4Gateway) - } - log.Infof("IPv4 -> address = %s | gateway = %s", v4Address, v4Gateway) - - v6Address = config.Network.IPv6.Address - v6Gateway = config.Network.IPv6.Gateway - if v6Address != "" && !isIPv6(v6Address, true) { - log.Panicf("Invalid IPv6 address -> %s", v6Address) - } - if v6Gateway != "" && !isIPv6(v6Gateway, false) { - log.Panicf("Invalid IPv6 gateway -> %s", v6Gateway) - } - log.Infof("IPv6 -> address = %s | gateway = %s", v6Address, v6Gateway) enableSniff = config.Proxy.Sniff log.Infof("Connection sniff -> %v", enableSniff)