From ffb84276b288d62a438ecb350d83a87fac4abbf2 Mon Sep 17 00:00:00 2001 From: dnomd343 Date: Tue, 16 Aug 2022 19:12:35 +0800 Subject: [PATCH] feat: load default configure --- src/config.go | 43 +++++++++++++++++++++++++++++++++++++++++-- src/main.go | 6 +----- test.yml | 39 --------------------------------------- 3 files changed, 42 insertions(+), 46 deletions(-) delete mode 100644 test.yml diff --git a/src/config.go b/src/config.go index 2fb47e1..bb6ae1d 100644 --- a/src/config.go +++ b/src/config.go @@ -4,6 +4,7 @@ import ( log "github.com/sirupsen/logrus" "gopkg.in/yaml.v3" "net" + "os" "strings" ) @@ -41,6 +42,32 @@ type Config struct { } `yaml:"network"` } +var defaultConfig = `# default configure file for xproxy +proxy: + sniff: true + redirect: true + http: null + socks: null + addon: null + +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" +` + func isIP(ipAddr string, isCidr bool) bool { if !isCidr { return net.ParseIP(ipAddr) != nil @@ -57,10 +84,22 @@ func isIPv6(ipAddr string, isCidr bool) bool { return isIP(ipAddr, isCidr) && strings.Contains(ipAddr, ":") } -func loadConfig(rawConfig []byte) { +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 + err = yaml.Unmarshal(rawConfig, &config) // yaml (or json) decode if err != nil { log.Panicf("Decode config file error -> %v", err) } diff --git a/src/main.go b/src/main.go index d198f20..6954f32 100644 --- a/src/main.go +++ b/src/main.go @@ -36,11 +36,7 @@ func main() { log.SetLevel(log.DebugLevel) log.Warning("XProxy start") - content, err := os.ReadFile("test.yml") - if err != nil { - panic(err) - } - loadConfig(content) + loadConfig("/xproxy/config.yml") loadProxy("/etc/xproxy/config", "/xproxy") loadGeoSite("/xproxy/assets") diff --git a/test.yml b/test.yml deleted file mode 100644 index e210bbf..0000000 --- a/test.yml +++ /dev/null @@ -1,39 +0,0 @@ -proxy: - sniff: true - redirect: true - http: - ipv4: 1084 - ipv6: 1086 - socks: - nodeA: 1681 - nodeB: 1682 - nodeC: 1683 - addon: - - tag: metrics - port: 9090 - protocol: dokodemo-door - settings: - address: 127.0.0.1 - -network: - dns: - - 223.5.5.5 - - 119.29.29.29 - ipv4: - gateway: 192.168.2.1 - address: 192.168.2.2/24 - ipv6: - gateway: null - address: fc00::2/64 - bypass: - - 169.254.0.0/16 - - 224.0.0.0/3 - - fc00::/7 - - fe80::/10 - - ff00::/8 - -update: - cron: "0 */1 * * * *" - 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"