diff --git a/common/config/config.go b/common/config/config.go index 9c51c37..5c2b775 100644 --- a/common/config/config.go +++ b/common/config/config.go @@ -11,7 +11,6 @@ type Config struct { LocalAddr string ServerAddr string CIDR string - Gateway string Key string Protocol string ServerMode bool diff --git a/common/netutil/netutil.go b/common/netutil/netutil.go index 1279092..e4ab23b 100644 --- a/common/netutil/netutil.go +++ b/common/netutil/netutil.go @@ -100,7 +100,7 @@ func getAllPhysicalInterfaces() []net.Interface { } func isPhysicalInterface(addr string) bool { - prefixArray := []string{"ens", "enp", "enx", "eth", "wlan", "wlp"} + prefixArray := []string{"ens", "enp", "enx", "eth", "en0", "wlan", "wlp"} for _, pref := range prefixArray { if strings.HasPrefix(strings.ToLower(addr), pref) { return true diff --git a/tun/tun.go b/tun/tun.go index ea91af5..1f38237 100644 --- a/tun/tun.go +++ b/tun/tun.go @@ -38,20 +38,30 @@ func configTun(config config.Config, iface *water.Interface) { physicalIface, gateway, _ := netutil.GetPhysicalInterface() serverIP := netutil.LookupIP(strings.Split(config.ServerAddr, ":")[0]) if physicalIface != "" && serverIP != "" { - execCmd("/sbin/ip", "route", "add", "0.0.0.0/0", "dev", iface.Name()) + execCmd("/sbin/ip", "route", "add", "0.0.0.0/1", "dev", iface.Name()) + execCmd("/sbin/ip", "route", "add", "128.0.0.0/1", "dev", iface.Name()) execCmd("/sbin/ip", "route", "delete", strings.Join([]string{serverIP, "32"}, "/"), "via", gateway, "dev", physicalIface) execCmd("/sbin/ip", "route", "add", strings.Join([]string{serverIP, "32"}, "/"), "via", gateway, "dev", physicalIface) } } } else if os == "darwin" { - execCmd("ifconfig", iface.Name(), "inet", ip.String(), config.Gateway, "up") + gateway := ipNet.IP.To4() + gateway[3]++ + execCmd("ifconfig", iface.Name(), "inet", ip.String(), gateway.String(), "up") + physicalIface, localGateway, _ := netutil.GetPhysicalInterface() if config.GlobalMode { - _, _, localNetwork := netutil.GetPhysicalInterface() - if localNetwork != "" { - execCmd("route", "add", "0.0.0.0/0", "-interface", iface.Name()) - execCmd("route", "add", localNetwork, "-interface", ipNet.IP.To4().String()) + serverIP := netutil.LookupIP(strings.Split(config.ServerAddr, ":")[0]) + if physicalIface != "" && serverIP != "" { + execCmd("route", "add", serverIP, localGateway) + execCmd("route", "add", "0.0.0.0/1", "-interface", iface.Name()) + execCmd("route", "add", "128.0.0.0/1", "-interface", iface.Name()) + execCmd("route", "add", "default", gateway.String()) + execCmd("route", "change", "default", gateway.String()) } + } else { + execCmd("route", "add", "default", localGateway) + execCmd("route", "change", "default", localGateway) } } else { log.Printf("not support os:%v", os)