Browse Source

update: complete network package

v1.x.x
dnomd343 2 years ago
parent
commit
832f513054
  1. 10
      cmd/network/dns.go
  2. 23
      cmd/network/main.go
  3. 34
      cmd/network/network.go
  4. 32
      cmd/network/tproxy.go
  5. 8
      main.go

10
cmd/network/dns.go

@ -5,15 +5,15 @@ import (
log "github.com/sirupsen/logrus" log "github.com/sirupsen/logrus"
) )
func loadDns(dnsServer []string) { func loadDns(dns []string) {
if len(dnsServer) == 0 { if len(dns) == 0 { // without dns server
log.Info("Using system DNS server") log.Info("Using system DNS server")
return return
} }
log.Infof("Setting up DNS server -> %v", dnsServer) log.Infof("Setting up DNS server -> %v", dns)
dnsConfig := "" dnsConfig := ""
for _, address := range dnsServer { for _, dnsAddr := range dns {
dnsConfig += "nameserver " + address + "\n" dnsConfig += "nameserver " + dnsAddr + "\n"
} }
common.WriteFile("/etc/resolv.conf", dnsConfig, true) common.WriteFile("/etc/resolv.conf", dnsConfig, true)
} }

23
cmd/network/main.go

@ -1,18 +1,23 @@
package network package network
import "time"
type Config struct { type Config struct {
IPv4 struct {
Address string
Gateway string
RouteTable int RouteTable int
TProxyPort int TProxyPort int
} Address string
V4RouteTable int Gateway string
V6RouteTable int Bypass []string
V4TProxyPort int
V6TProxyPort int
} }
func Load() { func Load(dns []string, ipv4 Config, ipv6 Config) {
loadDns(dns) // init dns server
flushNetwork() // clear network settings
loadV4Network(ipv4)
loadV6Network(ipv6)
time.Sleep(time.Second) // wait 1s for ipv6 (ND protocol)
loadV4TProxy(ipv4, getV4Cidr())
loadV6TProxy(ipv6, getV6Cidr())
} }

34
cmd/network/network.go

@ -24,28 +24,34 @@ func getV6Cidr() []string {
return v6Cidr return v6Cidr
} }
func loadNetwork(v4Address string, v4Gateway string, v6Address string, v6Gateway string) { func flushNetwork() {
log.Info("Enabled IP forward")
common.RunCommand("sysctl", "-w", "net.ipv4.ip_forward=1")
common.RunCommand("sysctl", "-w", "net.ipv6.conf.all.forwarding=1")
log.Info("Flush system IP configure") log.Info("Flush system IP configure")
common.RunCommand("ip", "link", "set", "eth0", "down") common.RunCommand("ip", "link", "set", "eth0", "down")
common.RunCommand("ip", "-4", "addr", "flush", "dev", "eth0") common.RunCommand("ip", "-4", "addr", "flush", "dev", "eth0")
common.RunCommand("ip", "-6", "addr", "flush", "dev", "eth0") common.RunCommand("ip", "-6", "addr", "flush", "dev", "eth0")
common.RunCommand("ip", "link", "set", "eth0", "down") common.RunCommand("ip", "link", "set", "eth0", "down")
}
log.Info("Setting up system IP configure") func loadV4Network(v4 Config) {
if v4Address != "" { log.Info("Enabled IPv4 forward")
common.RunCommand("ip", "-4", "addr", "add", v4Address, "dev", "eth0") common.RunCommand("sysctl", "-w", "net.ipv4.ip_forward=1")
log.Info("Setting up system IPv4 configure")
if v4.Address != "" {
common.RunCommand("ip", "-4", "addr", "add", v4.Address, "dev", "eth0")
} }
if v4Gateway != "" { if v4.Gateway != "" {
common.RunCommand("ip", "-4", "route", "add", "default", "via", v4Gateway) common.RunCommand("ip", "-4", "route", "add", "default", "via", v4.Gateway)
} }
if v6Address != "" { }
common.RunCommand("ip", "-6", "addr", "add", v6Address, "dev", "eth0")
func loadV6Network(v6 Config) {
log.Info("Enabled IPv6 forward")
common.RunCommand("sysctl", "-w", "net.ipv6.conf.all.forwarding=1")
log.Info("Setting up system IPv6 configure")
if v6.Address != "" {
common.RunCommand("ip", "-6", "addr", "add", v6.Address, "dev", "eth0")
} }
if v6Gateway != "" { if v6.Gateway != "" {
common.RunCommand("ip", "-6", "route", "add", "default", "via", v6Gateway) common.RunCommand("ip", "-6", "route", "add", "default", "via", v6.Gateway)
} }
} }

32
cmd/network/tproxy.go

@ -6,42 +6,38 @@ import (
"strconv" "strconv"
) )
type tproxyConfig struct { func loadV4TProxy(v4 Config, v4SysCidr []string) {
routeTable int
tproxyPort int
bypassCidr []string
}
func loadV4TProxy(config tproxyConfig) {
log.Info("Setting up TProxy of IPv4") log.Info("Setting up TProxy of IPv4")
tableNum := strconv.Itoa(config.routeTable) tableNum := strconv.Itoa(v4.RouteTable)
v4Bypass := append(v4SysCidr, v4.Bypass...)
common.RunCommand("ip", "-4", "rule", "add", "fwmark", "1", "table", tableNum) common.RunCommand("ip", "-4", "rule", "add", "fwmark", "1", "table", tableNum)
common.RunCommand("ip", "-4", "route", "add", "local", "0.0.0.0/0", "dev", "lo", "table", tableNum) common.RunCommand("ip", "-4", "route", "add", "local", "0.0.0.0/0", "dev", "lo", "table", tableNum)
common.RunCommand("iptables", "-t", "mangle", "-N", "XPROXY") common.RunCommand("iptables", "-t", "mangle", "-N", "XPROXY")
log.Infof("Setting up IPv4 bypass CIDR -> %v", config.bypassCidr) log.Infof("Setting up IPv4 bypass CIDR -> %v", v4Bypass)
for _, cidr := range config.bypassCidr { for _, cidr := range v4Bypass {
common.RunCommand("iptables", "-t", "mangle", "-A", "XPROXY", "-d", cidr, "-j", "RETURN") common.RunCommand("iptables", "-t", "mangle", "-A", "XPROXY", "-d", cidr, "-j", "RETURN")
} }
common.RunCommand("iptables", "-t", "mangle", "-A", "XPROXY", common.RunCommand("iptables", "-t", "mangle", "-A", "XPROXY",
"-p", "tcp", "-j", "TPROXY", "--on-port", strconv.Itoa(config.tproxyPort), "--tproxy-mark", "1") "-p", "tcp", "-j", "TPROXY", "--on-port", strconv.Itoa(v4.TProxyPort), "--tproxy-mark", "1")
common.RunCommand("iptables", "-t", "mangle", "-A", "XPROXY", common.RunCommand("iptables", "-t", "mangle", "-A", "XPROXY",
"-p", "udp", "-j", "TPROXY", "--on-port", strconv.Itoa(config.tproxyPort), "--tproxy-mark", "1") "-p", "udp", "-j", "TPROXY", "--on-port", strconv.Itoa(v4.TProxyPort), "--tproxy-mark", "1")
common.RunCommand("iptables", "-t", "mangle", "-A", "PREROUTING", "-j", "XPROXY") common.RunCommand("iptables", "-t", "mangle", "-A", "PREROUTING", "-j", "XPROXY")
} }
func loadV6TProxy(config tproxyConfig) { func loadV6TProxy(v6 Config, v6SysCidr []string) {
log.Info("Setting up TProxy of IPv6") log.Info("Setting up TProxy of IPv6")
tableNum := strconv.Itoa(config.routeTable) tableNum := strconv.Itoa(v6.RouteTable)
v6Bypass := append(v6SysCidr, v6.Bypass...)
common.RunCommand("ip", "-6", "rule", "add", "fwmark", "1", "table", tableNum) common.RunCommand("ip", "-6", "rule", "add", "fwmark", "1", "table", tableNum)
common.RunCommand("ip", "-6", "route", "add", "local", "::/0", "dev", "lo", "table", tableNum) common.RunCommand("ip", "-6", "route", "add", "local", "::/0", "dev", "lo", "table", tableNum)
common.RunCommand("ip6tables", "-t", "mangle", "-N", "XPROXY6") common.RunCommand("ip6tables", "-t", "mangle", "-N", "XPROXY6")
log.Infof("Setting up IPv6 bypass CIDR -> %v", config.bypassCidr) log.Infof("Setting up IPv6 bypass CIDR -> %v", v6Bypass)
for _, cidr := range config.bypassCidr { for _, cidr := range v6Bypass {
common.RunCommand("ip6tables", "-t", "mangle", "-A", "XPROXY6", "-d", cidr, "-j", "RETURN") common.RunCommand("ip6tables", "-t", "mangle", "-A", "XPROXY6", "-d", cidr, "-j", "RETURN")
} }
common.RunCommand("ip6tables", "-t", "mangle", "-A", "XPROXY6", common.RunCommand("ip6tables", "-t", "mangle", "-A", "XPROXY6",
"-p", "tcp", "-j", "TPROXY", "--on-port", strconv.Itoa(config.tproxyPort), "--tproxy-mark", "1") "-p", "tcp", "-j", "TPROXY", "--on-port", strconv.Itoa(v6.TProxyPort), "--tproxy-mark", "1")
common.RunCommand("ip6tables", "-t", "mangle", "-A", "XPROXY6", common.RunCommand("ip6tables", "-t", "mangle", "-A", "XPROXY6",
"-p", "udp", "-j", "TPROXY", "--on-port", strconv.Itoa(config.tproxyPort), "--tproxy-mark", "1") "-p", "udp", "-j", "TPROXY", "--on-port", strconv.Itoa(v6.TProxyPort), "--tproxy-mark", "1")
common.RunCommand("ip6tables", "-t", "mangle", "-A", "PREROUTING", "-j", "XPROXY6") common.RunCommand("ip6tables", "-t", "mangle", "-A", "PREROUTING", "-j", "XPROXY6")
} }

8
main.go

@ -1,7 +1,6 @@
package main package main
import ( import (
"XProxy/cmd/network"
"fmt" "fmt"
log "github.com/sirupsen/logrus" log "github.com/sirupsen/logrus"
) )
@ -10,10 +9,5 @@ func main() {
log.SetLevel(log.DebugLevel) log.SetLevel(log.DebugLevel)
fmt.Println("xproxy start") fmt.Println("xproxy start")
//common.CreateFolder("/tmp/test")
//fmt.Println(common.IsFileExist("/tmp/1.jpg"))
//fmt.Println(common.ListFiles("/xproxy/config", ".json"))
net = network.Config{
V4RouteTable: 12,
}
} }

Loading…
Cancel
Save