From 7928aaaf02b75e5492798ab21bace945972ff710 Mon Sep 17 00:00:00 2001 From: dnomd343 Date: Mon, 15 Aug 2022 22:11:21 +0800 Subject: [PATCH] feat: exec system command --- src/config.go | 1 - src/load.go | 14 +++++++++++--- src/main.go | 9 ++++++--- src/network.go | 19 +++++++++++++++++++ 4 files changed, 36 insertions(+), 7 deletions(-) diff --git a/src/config.go b/src/config.go index a15c876..2e0acda 100644 --- a/src/config.go +++ b/src/config.go @@ -86,7 +86,6 @@ func loadConfig(rawConfig []byte) { } log.Info("DNS server -> ", dnsServer) - // TODO: load basic bypass -> ip -4/6 addr | grep -w "inet(6)" | awk '{print $2}' for _, address := range config.Network.ByPass { // bypass options if isIPv4(address, true) { v4Bypass = append(v4Bypass, address) diff --git a/src/load.go b/src/load.go index 144de2d..159d710 100644 --- a/src/load.go +++ b/src/load.go @@ -6,7 +6,9 @@ import ( "io" "io/ioutil" "os" + "os/exec" "strings" + "syscall" ) var logConfig = `{ @@ -67,10 +69,16 @@ type inboundsSettings struct { Inbounds []interface{} `json:"inbounds"` } -func runCommand(command []string) bool { +func runCommand(command []string) (int, string) { log.Debugf("Running system command -> %v", command) - // TODO: run system command - return true + process := exec.Command(command[0], command[1:]...) + output, _ := process.CombinedOutput() + log.Debugf("Command %v -> \n%s", command, string(output)) + code := process.ProcessState.Sys().(syscall.WaitStatus).ExitStatus() + if code != 0 { + log.Warningf("Command %v return code %d", command, code) + } + return code, string(output) } func isFileExist(filePath string) bool { diff --git a/src/main.go b/src/main.go index c890703..7bb367b 100644 --- a/src/main.go +++ b/src/main.go @@ -27,11 +27,14 @@ func main() { panic(err) } loadConfig(content) - //loadProxy("/etc/xproxy/config", "/xproxy") + loadProxy("/etc/xproxy/config", "/xproxy") // TODO: load geo assets - //loadDns() - //loadNetwork() + loadDns() + loadNetwork() loadTProxy() + + // TODO: running custom script + // TODO: start xray service } diff --git a/src/network.go b/src/network.go index 83efb8c..029fcbd 100644 --- a/src/network.go +++ b/src/network.go @@ -3,6 +3,7 @@ package main import ( log "github.com/sirupsen/logrus" "os" + "regexp" "strconv" ) @@ -49,12 +50,28 @@ func loadNetwork() { } } +func v4SysBypass() { + _, output := runCommand([]string{"ip", "-4", "addr"}) + for _, temp := range regexp.MustCompile(`inet (\S+)`).FindAllStringSubmatch(output, -1) { + v4Bypass = append(v4Bypass, temp[1]) + } +} + +func v6SysBypass() { + _, output := runCommand([]string{"ip", "-6", "addr"}) + for _, temp := range regexp.MustCompile(`inet6 (\S+)`).FindAllStringSubmatch(output, -1) { + v6Bypass = append(v6Bypass, temp[1]) + } +} + func loadTProxy() { log.Info("Setting up TProxy of IPv4") v4TableNum := strconv.Itoa(v4RouteTable) runCommand([]string{"ip", "-4", "rule", "add", "fwmark", "1", "table", v4TableNum}) runCommand([]string{"ip", "-4", "route", "add", "local", "0.0.0.0/0", "dev", "lo", "table", v4TableNum}) runCommand([]string{"iptables", "-t", "mangle", "-N", "XPROXY"}) + v4SysBypass() + log.Infof("Setting up IPv4 bypass CIDR -> %v", v4Bypass) for _, cidr := range v4Bypass { runCommand([]string{"iptables", "-t", "mangle", "-A", "XPROXY", "-d", cidr, "-j", "RETURN"}) } @@ -69,6 +86,8 @@ func loadTProxy() { runCommand([]string{"ip", "-6", "rule", "add", "fwmark", "1", "table", v6TableNum}) runCommand([]string{"ip", "-6", "route", "add", "local", "::/0", "dev", "lo", "table", v6TableNum}) runCommand([]string{"ip6tables", "-t", "mangle", "-N", "XPROXY6"}) + v6SysBypass() + log.Infof("Setting up IPv6 bypass CIDR -> %v", v6Bypass) for _, cidr := range v6Bypass { runCommand([]string{"ip6tables", "-t", "mangle", "-A", "XPROXY6", "-d", cidr, "-j", "RETURN"}) }