Browse Source

update: setting up network

v1.x.x
dnomd343 2 years ago
parent
commit
f013fa28a4
  1. 2
      cmd/controller.go
  2. 28
      cmd/network/main.go
  3. 64
      cmd/network/network.go
  4. 1
      cmd/xproxy.go

2
cmd/controller.go

@ -50,7 +50,7 @@ func loadNetwork(settings *config.Config) {
settings.IPv4.TProxyPort = v4TProxyPort
settings.IPv6.RouteTable = v6RouteTable
settings.IPv6.TProxyPort = v6TProxyPort
network.Load(settings.DNS, &settings.IPv4, &settings.IPv6, settings.Dev)
network.Load(settings.DNS, settings.Dev, &settings.IPv4, &settings.IPv6)
}
func loadProxy(settings *config.Config) {

28
cmd/network/main.go

@ -2,8 +2,6 @@ package network
import (
"XProxy/cmd/common"
log "github.com/sirupsen/logrus"
"time"
)
type Config struct {
@ -17,30 +15,10 @@ type Config struct {
var run = common.RunCommand
func Load(dns []string, ipv4 *Config, ipv6 *Config, dev string) {
func Load(dns []string, dev string, ipv4 *Config, ipv6 *Config) {
loadDns(dns) // init dns server
delay := false
setV4 := ipv4.Address != "" || ipv4.Gateway != ""
setV6 := ipv6.Address != "" || ipv6.Gateway != ""
if setV4 && setV6 { // clear network settings
delay = true
flushNetwork(dev)
loadV4Network(ipv4, dev)
loadV6Network(ipv6, dev)
} else if setV6 {
delay = true
flushV6Network(dev)
loadV6Network(ipv6, dev)
} else if setV4 {
flushV4Network(dev)
loadV4Network(ipv4, dev)
} else {
log.Infof("Skip system IP configure")
}
if delay {
log.Info("Wait 1s for IPv6 setting up")
time.Sleep(time.Second) // wait for ipv6 setting up (ND protocol) -> RA should reply less than 0.5s
}
enableIpForward()
loadNetwork(dev, ipv4, ipv6)
loadV4TProxy(ipv4, getV4Cidr())
loadV6TProxy(ipv6, getV6Cidr())
}

64
cmd/network/network.go

@ -3,9 +3,10 @@ package network
import (
log "github.com/sirupsen/logrus"
"regexp"
"time"
)
func getV4Cidr() []string {
func getV4Cidr() []string { // fetch ipv4 network range
var v4Cidr []string
_, output := run("ip", "-4", "addr")
for _, temp := range regexp.MustCompile(`inet (\S+)`).FindAllStringSubmatch(output, -1) {
@ -14,7 +15,7 @@ func getV4Cidr() []string {
return v4Cidr
}
func getV6Cidr() []string {
func getV6Cidr() []string { // fetch ipv6 network range
var v6Cidr []string
_, output := run("ip", "-6", "addr")
for _, temp := range regexp.MustCompile(`inet6 (\S+)`).FindAllStringSubmatch(output, -1) {
@ -23,31 +24,26 @@ func getV6Cidr() []string {
return v6Cidr
}
func flushNetwork(dev string) {
log.Info("Flush system IP configure")
run("ip", "link", "set", dev, "down")
run("ip", "-4", "addr", "flush", "dev", dev)
run("ip", "-6", "addr", "flush", "dev", dev)
run("ip", "link", "set", dev, "up")
}
func flushV4Network(dev string) {
log.Info("Flush system IPv4 configure")
run("ip", "link", "set", dev, "down")
run("ip", "-4", "addr", "flush", "dev", dev)
run("ip", "link", "set", dev, "up")
func enableIpForward() { // enable ip forward function
log.Info("Enabled IPv4 forward")
run("sysctl", "-w", "net.ipv4.ip_forward=1")
log.Info("Enabled IPv6 forward")
run("sysctl", "-w", "net.ipv6.conf.all.forwarding=1")
}
func flushV6Network(dev string) {
log.Info("Flush system IPv6 configure")
func flushNetwork(dev string, isV4 bool, isV6 bool) { // flush ipv4 and ipv6 network
log.Info("Flush system IP configure")
run("ip", "link", "set", dev, "down")
run("ip", "-6", "addr", "flush", "dev", dev)
if isV4 {
run("ip", "-4", "addr", "flush", "dev", dev)
}
if isV6 {
run("ip", "-6", "addr", "flush", "dev", dev)
}
run("ip", "link", "set", dev, "up")
}
func loadV4Network(v4 *Config, dev string) {
log.Info("Enabled IPv4 forward")
run("sysctl", "-w", "net.ipv4.ip_forward=1")
func loadV4Network(v4 *Config, dev string) { // setting up ipv4 network
log.Info("Setting up system IPv4 configure")
if v4.Address != "" {
run("ip", "-4", "addr", "add", v4.Address, "dev", dev)
@ -57,9 +53,7 @@ func loadV4Network(v4 *Config, dev string) {
}
}
func loadV6Network(v6 *Config, dev string) {
log.Info("Enabled IPv6 forward")
run("sysctl", "-w", "net.ipv6.conf.all.forwarding=1")
func loadV6Network(v6 *Config, dev string) { // setting up ipv6 network
log.Info("Setting up system IPv6 configure")
if v6.Address != "" {
run("ip", "-6", "addr", "add", v6.Address, "dev", dev)
@ -68,3 +62,25 @@ func loadV6Network(v6 *Config, dev string) {
run("ip", "-6", "route", "add", "default", "via", v6.Gateway, "dev", dev)
}
}
func loadNetwork(dev string, v4 *Config, v6 *Config) {
setV4 := v4.Address != "" || v4.Gateway != ""
setV6 := v6.Address != "" || v6.Gateway != ""
if setV4 && setV6 { // load both ipv4 and ipv6
flushNetwork(dev, true, true)
loadV4Network(v4, dev)
loadV6Network(v6, dev)
} else if setV4 { // only load ipv4 network
flushNetwork(dev, true, false)
loadV4Network(v4, dev)
} else if setV6 { // only load ipv6 network
flushNetwork(dev, false, true)
loadV6Network(v6, dev)
} else { // skip network settings
log.Infof("Skip system IP configure")
}
if setV6 {
log.Info("Wait 1s for IPv6 setting up")
time.Sleep(time.Second) // wait for ipv6 setting up (ND protocol) -> RA should reply less than 0.5s
}
}

1
cmd/xproxy.go

@ -35,6 +35,7 @@ func logInit(isDebug bool, logDir string) {
if isDebug {
log.SetLevel(log.DebugLevel)
}
common.CreateFolder(logDir) // confirm log folder exist
logFile, err := os.OpenFile(path.Join(logDir, "xproxy.log"), os.O_CREATE|os.O_WRONLY|os.O_APPEND, 0644)
if err != nil {
log.Errorf("Unable to open log file -> %s", path.Join(logDir, "xproxy.log"))

Loading…
Cancel
Save