From 9559445ef8c2033d44c3ac80e9f7191ccc9d0d3e Mon Sep 17 00:00:00 2001 From: dnomd343 Date: Mon, 15 Aug 2022 21:02:17 +0800 Subject: [PATCH] feat: setting up ip configure --- src/load.go | 6 ++++++ src/main.go | 6 +++++- src/network.go | 26 ++++++++++++++++++++++++++ test.yml | 4 +++- 4 files changed, 40 insertions(+), 2 deletions(-) diff --git a/src/load.go b/src/load.go index bc43081..8237335 100644 --- a/src/load.go +++ b/src/load.go @@ -67,6 +67,12 @@ type inboundsSettings struct { Inbounds []interface{} `json:"inbounds"` } +func runCommand(command []string) bool { + log.Debugf("Running system command -> %v", command) + // TODO: run system command + return true +} + func isFileExist(filePath string) bool { s, err := os.Stat(filePath) if err != nil { // file or folder not exist diff --git a/src/main.go b/src/main.go index 2567365..3e683b0 100644 --- a/src/main.go +++ b/src/main.go @@ -25,5 +25,9 @@ func main() { } loadConfig(content) //loadProxy("/etc/xproxy/config", "/xproxy") - loadDns() + + // TODO: load geo assets + + //loadDns() + loadNetwork() } diff --git a/src/network.go b/src/network.go index b3a2166..9157cc0 100644 --- a/src/network.go +++ b/src/network.go @@ -21,3 +21,29 @@ func loadDns() { panic("Setting up DNS failed") } } + +func loadNetwork() { + log.Info("Enabled IP forward") + runCommand([]string{"sysctl", "-w", "net.ipv4.ip_forward=1"}) + runCommand([]string{"sysctl", "-w", "net.ipv6.conf.all.forwarding=1"}) + + log.Info("Flush system IP configure") + runCommand([]string{"ip", "link", "set", "eth0", "down"}) + runCommand([]string{"ip", "-4", "addr", "flush", "dev", "eth0"}) + runCommand([]string{"ip", "-6", "addr", "flush", "dev", "eth0"}) + runCommand([]string{"ip", "link", "set", "eth0", "down"}) + + log.Info("Setting up system IP configure") + if v4Address != "" { + runCommand([]string{"ip", "-4", "addr", "add", v4Address, "dev", "eth0"}) + } + if v4Gateway != "" { + runCommand([]string{"ip", "-4", "route", "add", "default", "via", v4Gateway}) + } + if v6Address != "" { + runCommand([]string{"ip", "-6", "addr", "add", v6Address, "dev", "eth0"}) + } + if v6Gateway != "" { + runCommand([]string{"ip", "-6", "route", "add", "default", "via", v6Gateway}) + } +} diff --git a/test.yml b/test.yml index 7e25170..5f26ad4 100644 --- a/test.yml +++ b/test.yml @@ -22,7 +22,9 @@ network: ipv4: gateway: 192.168.2.1 address: 192.168.2.2/24 - ipv6: null + ipv6: + gateway: null + address: fc00::2/64 bypass: - 169.254.0.0/16 - fc00::/7