From e75c8dada5918386196c13c071693dc644f38103 Mon Sep 17 00:00:00 2001 From: dnomd343 Date: Thu, 18 Aug 2022 00:18:51 +0800 Subject: [PATCH] update: complete config package --- cmd/config/decode.go | 36 +++++++++++++++++++++++--- cmd/config/main.go | 21 +++++++-------- main.go | 61 +++++++++++++++++++++++++------------------- src/config.go | 38 --------------------------- src/main.go | 5 ---- 5 files changed, 78 insertions(+), 83 deletions(-) delete mode 100644 src/config.go diff --git a/cmd/config/decode.go b/cmd/config/decode.go index 805ec13..f626717 100644 --- a/cmd/config/decode.go +++ b/cmd/config/decode.go @@ -80,7 +80,7 @@ func decodeIPv4(rawConfig *yamlConfig) (string, string) { if v4Gateway != "" && !common.IsIPv4(v4Gateway, false) { log.Panicf("Invalid IPv4 gateway -> %s", v4Gateway) } - log.Infof("IPv4 -> address = %s | gateway = %s", v4Address, v4Gateway) + log.Debugf("IPv4 -> address = %s | gateway = %s", v4Address, v4Gateway) return v4Address, v4Gateway } @@ -93,17 +93,45 @@ func decodeIPv6(rawConfig *yamlConfig) (string, string) { if v6Gateway != "" && !common.IsIPv6(v6Gateway, false) { log.Panicf("Invalid IPv6 gateway -> %s", v6Gateway) } - log.Infof("IPv6 -> address = %s | gateway = %s", v6Address, v6Gateway) + log.Debugf("IPv6 -> address = %s | gateway = %s", v6Address, v6Gateway) return v6Address, v6Gateway } +func decodeProxy(rawConfig *yamlConfig, config *Config) { + config.EnableSniff = rawConfig.Proxy.Sniff + log.Debugf("Connection sniff -> %v", config.EnableSniff) + config.EnableRedirect = rawConfig.Proxy.Redirect + log.Debugf("Connection redirect -> %v", config.EnableRedirect) + config.HttpInbounds = rawConfig.Proxy.Http + log.Debugf("Http inbounds -> %v", config.HttpInbounds) + config.SocksInbounds = rawConfig.Proxy.Socks + log.Debugf("Socks5 inbounds -> %v", config.SocksInbounds) + config.AddOnInbounds = rawConfig.Proxy.AddOn + log.Debugf("Add-on inbounds -> %v", config.AddOnInbounds) +} + +func decodeUpdate(rawConfig *yamlConfig) (string, map[string]string) { + updateCron := rawConfig.Update.Cron + log.Debugf("Update cron -> %s", updateCron) + updateUrls := rawConfig.Update.Url + log.Debugf("Update urls -> %v", updateUrls) + return updateCron, updateUrls +} + +func decodeCustom(rawConfig *yamlConfig) []string { + customScript := rawConfig.Custom + log.Debugf("Custom script -> %v", customScript) + return customScript +} + 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) - + decodeProxy(&rawConfig, &config) + config.UpdateCron, config.UpdateUrls = decodeUpdate(&rawConfig) + config.Script = decodeCustom(&rawConfig) return config } diff --git a/cmd/config/main.go b/cmd/config/main.go index 2fe07b5..4a51be4 100644 --- a/cmd/config/main.go +++ b/cmd/config/main.go @@ -7,22 +7,23 @@ import ( ) type Config struct { - // asset update - - DNS []string - + DNS []string + V4Bypass []string + V6Bypass []string V4Address string V4Gateway string - V4Bypass []string - V6Address string V6Gateway string - V6Bypass []string - // httpInbounds - // socksInbounds - // addOnInbounds + Script []string + UpdateCron string + UpdateUrls map[string]string + EnableSniff bool + EnableRedirect bool + HttpInbounds map[string]int + SocksInbounds map[string]int + AddOnInbounds []interface{} } func Load(configFile string) Config { diff --git a/main.go b/main.go index 60d0a95..a4f8e95 100644 --- a/main.go +++ b/main.go @@ -1,39 +1,48 @@ package main import ( + "XProxy/cmd/config" "XProxy/cmd/network" - "XProxy/cmd/proxy" "fmt" log "github.com/sirupsen/logrus" ) +var exposeDir = "/xproxy" + +var v4RouteTable = 100 +var v6RouteTable = 106 +var v4TProxyPort = 7288 +var v6TProxyPort = 7289 + +func loadNetwork(settings *config.Config) { + v4Settings := network.Config{ + RouteTable: v4RouteTable, + TProxyPort: v4TProxyPort, + Address: settings.V4Address, + Gateway: settings.V4Gateway, + Bypass: settings.V4Bypass, + } + v6Settings := network.Config{ + RouteTable: v6RouteTable, + TProxyPort: v6TProxyPort, + Address: settings.V6Address, + Gateway: settings.V6Gateway, + Bypass: settings.V6Bypass, + } + network.Load(settings.DNS, v4Settings, v6Settings) +} + func main() { log.SetLevel(log.DebugLevel) - fmt.Println("XProxy start") - network.Load(nil, network.Config{ - RouteTable: 100, - TProxyPort: 7288, - Address: "192.168.2.2", - Gateway: "192.168.2.1", - Bypass: make([]string, 0), - }, network.Config{ - RouteTable: 106, - TProxyPort: 7289, - Address: "fc00::2", - Gateway: "fc00::1", - Bypass: make([]string, 0), - }) - - proxy.Load("/etc/xproxy", "/xproxy", proxy.Config{ - Sniff: true, - Redirect: true, - V4TProxyPort: 7288, - V6TProxyPort: 7289, - LogLevel: "debug", - HttpInbounds: nil, - SocksInbounds: nil, - AddOnInbounds: nil, - }) + settings := config.Load(exposeDir + "/config.yml") + fmt.Println(settings) + + loadNetwork(&settings) + + // TODO: load proxy + // TODO: load asset + + // TODO: start xray service } diff --git a/src/config.go b/src/config.go deleted file mode 100644 index 1239fc0..0000000 --- a/src/config.go +++ /dev/null @@ -1,38 +0,0 @@ -package main - -import ( - log "github.com/sirupsen/logrus" -) - -var v4Bypass []string -var v6Bypass []string -var dnsServer []string - -var v4Gateway string -var v4Address string -var v6Gateway string -var v6Address string - -func loadConfig(configFile string) { - - config := Config{} - - enableSniff = config.Proxy.Sniff - log.Infof("Connection sniff -> %v", enableSniff) - enableRedirect = config.Proxy.Redirect - log.Infof("Connection redirect -> %v", enableRedirect) - httpInbounds = config.Proxy.Http - log.Infof("Http inbounds -> %v", httpInbounds) - socksInbounds = config.Proxy.Socks - log.Infof("Socks5 inbounds -> %v", socksInbounds) - addOnInbounds = config.Proxy.AddOn - log.Infof("Add-on inbounds -> %v", addOnInbounds) - - updateCron = config.Update.Cron - log.Infof("Update cron -> %s", updateCron) - updateUrls = config.Update.Url - log.Infof("Update url -> %v", updateUrls) - - preScript = config.Script - log.Infof("Pre-script -> %v", preScript) -} diff --git a/src/main.go b/src/main.go index 6a5ee0c..f043845 100644 --- a/src/main.go +++ b/src/main.go @@ -10,11 +10,6 @@ import ( var logLevel = "warning" -var v4RouteTable = 100 -var v6RouteTable = 106 -var v4TProxyPort = 7288 -var v6TProxyPort = 7289 - var preScript []string var updateCron string var updateUrls map[string]string