From a79dd76fd64721bb22e7d5b991db0d009aa6de5f Mon Sep 17 00:00:00 2001 From: netbyte Date: Tue, 10 May 2022 16:22:50 +0800 Subject: [PATCH] fix global mode --- main.go | 14 ++++++++------ tun/tun.go | 8 ++++---- 2 files changed, 12 insertions(+), 10 deletions(-) diff --git a/main.go b/main.go index a056e54..4cfa2c5 100644 --- a/main.go +++ b/main.go @@ -42,13 +42,15 @@ func main() { } func initConfig(config *config.Config) { - cipher.GenerateKey(config.Key) - os := runtime.GOOS - if os == "linux" { - config.DefaultGateway = netutil.GetLinuxDefaultGateway() - } else if os == "darwin" { - config.DefaultGateway = netutil.GetMacDefaultGateway() + if !config.ServerMode { + os := runtime.GOOS + if os == "linux" { + config.DefaultGateway = netutil.GetLinuxDefaultGateway() + } else if os == "darwin" { + config.DefaultGateway = netutil.GetMacDefaultGateway() + } } + cipher.GenerateKey(config.Key) json, _ := json.Marshal(config) log.Printf("init config:%s", string(json)) } diff --git a/tun/tun.go b/tun/tun.go index c8d9ab4..d7cb15f 100644 --- a/tun/tun.go +++ b/tun/tun.go @@ -33,7 +33,7 @@ func configTun(config config.Config, iface *water.Interface) { netutil.ExecCmd("/sbin/ip", "link", "set", "dev", iface.Name(), "mtu", strconv.Itoa(config.MTU)) netutil.ExecCmd("/sbin/ip", "addr", "add", config.CIDR, "dev", iface.Name()) netutil.ExecCmd("/sbin/ip", "link", "set", "dev", iface.Name(), "up") - if config.GlobalMode { + if !config.ServerMode && config.GlobalMode { physicalIface := netutil.GetPhysicalInterface() serverIP := netutil.LookupIP(strings.Split(config.ServerAddr, ":")[0]) if physicalIface != "" && serverIP != "" { @@ -48,8 +48,8 @@ func configTun(config config.Config, iface *water.Interface) { gateway := ipNet.IP.To4() gateway[3]++ netutil.ExecCmd("ifconfig", iface.Name(), "inet", ip.String(), gateway.String(), "up") - physicalIface := netutil.GetPhysicalInterface() - if config.GlobalMode { + if !config.ServerMode && config.GlobalMode { + physicalIface := netutil.GetPhysicalInterface() serverIP := netutil.LookupIP(strings.Split(config.ServerAddr, ":")[0]) if physicalIface != "" && serverIP != "" { netutil.ExecCmd("route", "add", serverIP, config.DefaultGateway) @@ -67,7 +67,7 @@ func configTun(config config.Config, iface *water.Interface) { func Reset(config config.Config) { os := runtime.GOOS - if os == "darwin" && config.GlobalMode { + if os == "darwin" && !config.ServerMode && config.GlobalMode { netutil.ExecCmd("route", "add", "default", config.DefaultGateway) netutil.ExecCmd("route", "change", "default", config.DefaultGateway) }