diff --git a/cmd/config/decode.go b/cmd/config/decode.go index f626717..18d1ef5 100644 --- a/cmd/config/decode.go +++ b/cmd/config/decode.go @@ -12,6 +12,7 @@ type yamlNetConfig struct { } type yamlConfig struct { + Log string `yaml:"log"` Custom []string `yaml:"custom"` Update struct { Cron string `yaml:"cron"` @@ -126,6 +127,7 @@ func decodeCustom(rawConfig *yamlConfig) []string { func decode(rawConfig yamlConfig) Config { var config Config + config.LogLevel = rawConfig.Log config.DNS = decodeDns(&rawConfig) config.V4Bypass, config.V6Bypass = decodeBypass(&rawConfig) config.V4Address, config.V4Gateway = decodeIPv4(&rawConfig) diff --git a/cmd/config/default.go b/cmd/config/default.go index 8f7633b..8508d80 100644 --- a/cmd/config/default.go +++ b/cmd/config/default.go @@ -1,6 +1,8 @@ package config var defaultConfig = `# default configure file for xproxy +log: debug + proxy: sniff: true redirect: true diff --git a/cmd/config/main.go b/cmd/config/main.go index 4a51be4..6d7206f 100644 --- a/cmd/config/main.go +++ b/cmd/config/main.go @@ -16,6 +16,7 @@ type Config struct { V6Gateway string Script []string + LogLevel string UpdateCron string UpdateUrls map[string]string diff --git a/cmd/controller.go b/cmd/controller.go index 66864da..02f14c8 100644 --- a/cmd/controller.go +++ b/cmd/controller.go @@ -2,11 +2,32 @@ package main import ( "XProxy/cmd/asset" + "XProxy/cmd/common" "XProxy/cmd/config" "XProxy/cmd/network" "XProxy/cmd/proxy" + log "github.com/sirupsen/logrus" ) +func loadAsset(settings *config.Config) { + asset.LoadGeoSite(assetFile, assetDir) + asset.LoadGeoIp(assetFile, assetDir) + asset.AutoUpdate(settings.UpdateCron, settings.UpdateUrls, assetDir) +} + +func loadProxy(settings *config.Config) { + proxy.Load(configDir, exposeDir, proxy.Config{ + Sniff: settings.EnableSniff, + Redirect: settings.EnableRedirect, + V4TProxyPort: v4TProxyPort, + V6TProxyPort: v6TProxyPort, + LogLevel: settings.LogLevel, + HttpInbounds: settings.HttpInbounds, + SocksInbounds: settings.SocksInbounds, + AddOnInbounds: settings.AddOnInbounds, + }) +} + func loadNetwork(settings *config.Config) { v4Settings := network.Config{ RouteTable: v4RouteTable, @@ -25,21 +46,9 @@ func loadNetwork(settings *config.Config) { network.Load(settings.DNS, v4Settings, v6Settings) } -func loadProxy(settings *config.Config) { - proxy.Load(configDir, exposeDir, proxy.Config{ - Sniff: settings.EnableSniff, - Redirect: settings.EnableRedirect, - V4TProxyPort: v4TProxyPort, - V6TProxyPort: v6TProxyPort, - LogLevel: "warning", - HttpInbounds: settings.HttpInbounds, - SocksInbounds: settings.SocksInbounds, - AddOnInbounds: settings.AddOnInbounds, - }) -} - -func loadAsset(settings *config.Config) { - asset.LoadGeoSite(assetFile, assetDir) - asset.LoadGeoIp(assetFile, assetDir) - asset.AutoUpdate(settings.UpdateCron, settings.UpdateUrls, assetDir) +func runScript(settings *config.Config) { + for _, script := range settings.Script { + log.Infof("Run script command -> %s", script) + common.RunCommand("sh", "-c", script) + } } diff --git a/cmd/network/main.go b/cmd/network/main.go index 2f6c2c0..a57cd57 100644 --- a/cmd/network/main.go +++ b/cmd/network/main.go @@ -1,6 +1,9 @@ package network -import "time" +import ( + "XProxy/cmd/common" + "time" +) type Config struct { RouteTable int @@ -10,6 +13,8 @@ type Config struct { Bypass []string } +var run = common.RunCommand + func Load(dns []string, ipv4 Config, ipv6 Config) { loadDns(dns) // init dns server flushNetwork() // clear network settings diff --git a/cmd/network/network.go b/cmd/network/network.go index 0330f17..99d3954 100644 --- a/cmd/network/network.go +++ b/cmd/network/network.go @@ -1,14 +1,13 @@ package network import ( - "XProxy/cmd/common" log "github.com/sirupsen/logrus" "regexp" ) func getV4Cidr() []string { var v4Cidr []string - _, output := common.RunCommand("ip", "-4", "addr") + _, output := run("ip", "-4", "addr") for _, temp := range regexp.MustCompile(`inet (\S+)`).FindAllStringSubmatch(output, -1) { v4Cidr = append(v4Cidr, temp[1]) } @@ -17,7 +16,7 @@ func getV4Cidr() []string { func getV6Cidr() []string { var v6Cidr []string - _, output := common.RunCommand("ip", "-6", "addr") + _, output := run("ip", "-6", "addr") for _, temp := range regexp.MustCompile(`inet6 (\S+)`).FindAllStringSubmatch(output, -1) { v6Cidr = append(v6Cidr, temp[1]) } @@ -26,32 +25,32 @@ func getV6Cidr() []string { func flushNetwork() { log.Info("Flush system IP configure") - common.RunCommand("ip", "link", "set", "eth0", "down") - common.RunCommand("ip", "-4", "addr", "flush", "dev", "eth0") - common.RunCommand("ip", "-6", "addr", "flush", "dev", "eth0") - common.RunCommand("ip", "link", "set", "eth0", "down") + run("ip", "link", "set", "eth0", "down") + run("ip", "-4", "addr", "flush", "dev", "eth0") + run("ip", "-6", "addr", "flush", "dev", "eth0") + run("ip", "link", "set", "eth0", "down") } func loadV4Network(v4 Config) { log.Info("Enabled IPv4 forward") - common.RunCommand("sysctl", "-w", "net.ipv4.ip_forward=1") + run("sysctl", "-w", "net.ipv4.ip_forward=1") log.Info("Setting up system IPv4 configure") if v4.Address != "" { - common.RunCommand("ip", "-4", "addr", "add", v4.Address, "dev", "eth0") + run("ip", "-4", "addr", "add", v4.Address, "dev", "eth0") } if v4.Gateway != "" { - common.RunCommand("ip", "-4", "route", "add", "default", "via", v4.Gateway) + run("ip", "-4", "route", "add", "default", "via", v4.Gateway) } } func loadV6Network(v6 Config) { log.Info("Enabled IPv6 forward") - common.RunCommand("sysctl", "-w", "net.ipv6.conf.all.forwarding=1") + run("sysctl", "-w", "net.ipv6.conf.all.forwarding=1") log.Info("Setting up system IPv6 configure") if v6.Address != "" { - common.RunCommand("ip", "-6", "addr", "add", v6.Address, "dev", "eth0") + run("ip", "-6", "addr", "add", v6.Address, "dev", "eth0") } if v6.Gateway != "" { - common.RunCommand("ip", "-6", "route", "add", "default", "via", v6.Gateway) + run("ip", "-6", "route", "add", "default", "via", v6.Gateway) } } diff --git a/cmd/network/tproxy.go b/cmd/network/tproxy.go index 41046a4..af6d7de 100644 --- a/cmd/network/tproxy.go +++ b/cmd/network/tproxy.go @@ -1,7 +1,6 @@ package network import ( - "XProxy/cmd/common" log "github.com/sirupsen/logrus" "strconv" ) @@ -10,34 +9,34 @@ func loadV4TProxy(v4 Config, v4SysCidr []string) { log.Info("Setting up TProxy of IPv4") tableNum := strconv.Itoa(v4.RouteTable) v4Bypass := append(v4SysCidr, v4.Bypass...) - common.RunCommand("ip", "-4", "rule", "add", "fwmark", "1", "table", tableNum) - common.RunCommand("ip", "-4", "route", "add", "local", "0.0.0.0/0", "dev", "lo", "table", tableNum) - common.RunCommand("iptables", "-t", "mangle", "-N", "XPROXY") + run("ip", "-4", "rule", "add", "fwmark", "1", "table", tableNum) + run("ip", "-4", "route", "add", "local", "0.0.0.0/0", "dev", "lo", "table", tableNum) + run("iptables", "-t", "mangle", "-N", "XPROXY") log.Infof("Setting up IPv4 bypass CIDR -> %v", v4Bypass) for _, cidr := range v4Bypass { - common.RunCommand("iptables", "-t", "mangle", "-A", "XPROXY", "-d", cidr, "-j", "RETURN") + run("iptables", "-t", "mangle", "-A", "XPROXY", "-d", cidr, "-j", "RETURN") } - common.RunCommand("iptables", "-t", "mangle", "-A", "XPROXY", + run("iptables", "-t", "mangle", "-A", "XPROXY", "-p", "tcp", "-j", "TPROXY", "--on-port", strconv.Itoa(v4.TProxyPort), "--tproxy-mark", "1") - common.RunCommand("iptables", "-t", "mangle", "-A", "XPROXY", + run("iptables", "-t", "mangle", "-A", "XPROXY", "-p", "udp", "-j", "TPROXY", "--on-port", strconv.Itoa(v4.TProxyPort), "--tproxy-mark", "1") - common.RunCommand("iptables", "-t", "mangle", "-A", "PREROUTING", "-j", "XPROXY") + run("iptables", "-t", "mangle", "-A", "PREROUTING", "-j", "XPROXY") } func loadV6TProxy(v6 Config, v6SysCidr []string) { log.Info("Setting up TProxy of IPv6") tableNum := strconv.Itoa(v6.RouteTable) v6Bypass := append(v6SysCidr, v6.Bypass...) - common.RunCommand("ip", "-6", "rule", "add", "fwmark", "1", "table", tableNum) - common.RunCommand("ip", "-6", "route", "add", "local", "::/0", "dev", "lo", "table", tableNum) - common.RunCommand("ip6tables", "-t", "mangle", "-N", "XPROXY6") + run("ip", "-6", "rule", "add", "fwmark", "1", "table", tableNum) + run("ip", "-6", "route", "add", "local", "::/0", "dev", "lo", "table", tableNum) + run("ip6tables", "-t", "mangle", "-N", "XPROXY6") log.Infof("Setting up IPv6 bypass CIDR -> %v", v6Bypass) for _, cidr := range v6Bypass { - common.RunCommand("ip6tables", "-t", "mangle", "-A", "XPROXY6", "-d", cidr, "-j", "RETURN") + run("ip6tables", "-t", "mangle", "-A", "XPROXY6", "-d", cidr, "-j", "RETURN") } - common.RunCommand("ip6tables", "-t", "mangle", "-A", "XPROXY6", + run("ip6tables", "-t", "mangle", "-A", "XPROXY6", "-p", "tcp", "-j", "TPROXY", "--on-port", strconv.Itoa(v6.TProxyPort), "--tproxy-mark", "1") - common.RunCommand("ip6tables", "-t", "mangle", "-A", "XPROXY6", + run("ip6tables", "-t", "mangle", "-A", "XPROXY6", "-p", "udp", "-j", "TPROXY", "--on-port", strconv.Itoa(v6.TProxyPort), "--tproxy-mark", "1") - common.RunCommand("ip6tables", "-t", "mangle", "-A", "PREROUTING", "-j", "XPROXY6") + run("ip6tables", "-t", "mangle", "-A", "PREROUTING", "-j", "XPROXY6") } diff --git a/cmd/xproxy.go b/cmd/xproxy.go index 2520dc0..d12e95a 100644 --- a/cmd/xproxy.go +++ b/cmd/xproxy.go @@ -27,6 +27,7 @@ func main() { loadNetwork(&settings) loadProxy(&settings) loadAsset(&settings) + runScript(&settings) // TODO: start xray service } diff --git a/src/main.go b/src/main.go index 743125c..5caba48 100644 --- a/src/main.go +++ b/src/main.go @@ -7,8 +7,6 @@ import ( "syscall" ) -var preScript []string - func main() { defer func() { if err := recover(); err != nil { @@ -16,11 +14,6 @@ func main() { } }() - for _, script := range preScript { - log.Infof("Run script command -> %s", script) - runCommand("sh", "-c", script) - } - xray := newProcess("xray", "-confdir", "/etc/xproxy/config") xray.startProcess(true, true) subProcess = append(subProcess, xray)