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)
// 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)

14
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 {

9
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
}

19
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"})
}

Loading…
Cancel
Save