Browse Source

feat: exec system command

v1.x.x
dnomd343 2 years ago
parent
commit
7928aaaf02
  1. 1
      src/config.go
  2. 14
      src/load.go
  3. 9
      src/main.go
  4. 19
      src/network.go

1
src/config.go

@ -86,7 +86,6 @@ func loadConfig(rawConfig []byte) {
} }
log.Info("DNS server -> ", dnsServer) 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 for _, address := range config.Network.ByPass { // bypass options
if isIPv4(address, true) { if isIPv4(address, true) {
v4Bypass = append(v4Bypass, address) v4Bypass = append(v4Bypass, address)

14
src/load.go

@ -6,7 +6,9 @@ import (
"io" "io"
"io/ioutil" "io/ioutil"
"os" "os"
"os/exec"
"strings" "strings"
"syscall"
) )
var logConfig = `{ var logConfig = `{
@ -67,10 +69,16 @@ type inboundsSettings struct {
Inbounds []interface{} `json:"inbounds"` Inbounds []interface{} `json:"inbounds"`
} }
func runCommand(command []string) bool { func runCommand(command []string) (int, string) {
log.Debugf("Running system command -> %v", command) log.Debugf("Running system command -> %v", command)
// TODO: run system command process := exec.Command(command[0], command[1:]...)
return true 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 { func isFileExist(filePath string) bool {

9
src/main.go

@ -27,11 +27,14 @@ func main() {
panic(err) panic(err)
} }
loadConfig(content) loadConfig(content)
//loadProxy("/etc/xproxy/config", "/xproxy") loadProxy("/etc/xproxy/config", "/xproxy")
// TODO: load geo assets // TODO: load geo assets
//loadDns() loadDns()
//loadNetwork() loadNetwork()
loadTProxy() loadTProxy()
// TODO: running custom script
// TODO: start xray service
} }

19
src/network.go

@ -3,6 +3,7 @@ package main
import ( import (
log "github.com/sirupsen/logrus" log "github.com/sirupsen/logrus"
"os" "os"
"regexp"
"strconv" "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() { func loadTProxy() {
log.Info("Setting up TProxy of IPv4") log.Info("Setting up TProxy of IPv4")
v4TableNum := strconv.Itoa(v4RouteTable) v4TableNum := strconv.Itoa(v4RouteTable)
runCommand([]string{"ip", "-4", "rule", "add", "fwmark", "1", "table", v4TableNum}) 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{"ip", "-4", "route", "add", "local", "0.0.0.0/0", "dev", "lo", "table", v4TableNum})
runCommand([]string{"iptables", "-t", "mangle", "-N", "XPROXY"}) runCommand([]string{"iptables", "-t", "mangle", "-N", "XPROXY"})
v4SysBypass()
log.Infof("Setting up IPv4 bypass CIDR -> %v", v4Bypass)
for _, cidr := range v4Bypass { for _, cidr := range v4Bypass {
runCommand([]string{"iptables", "-t", "mangle", "-A", "XPROXY", "-d", cidr, "-j", "RETURN"}) 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", "rule", "add", "fwmark", "1", "table", v6TableNum})
runCommand([]string{"ip", "-6", "route", "add", "local", "::/0", "dev", "lo", "table", v6TableNum}) runCommand([]string{"ip", "-6", "route", "add", "local", "::/0", "dev", "lo", "table", v6TableNum})
runCommand([]string{"ip6tables", "-t", "mangle", "-N", "XPROXY6"}) runCommand([]string{"ip6tables", "-t", "mangle", "-N", "XPROXY6"})
v6SysBypass()
log.Infof("Setting up IPv6 bypass CIDR -> %v", v6Bypass)
for _, cidr := range v6Bypass { for _, cidr := range v6Bypass {
runCommand([]string{"ip6tables", "-t", "mangle", "-A", "XPROXY6", "-d", cidr, "-j", "RETURN"}) runCommand([]string{"ip6tables", "-t", "mangle", "-A", "XPROXY6", "-d", cidr, "-j", "RETURN"})
} }

Loading…
Cancel
Save