fix global mode

This commit is contained in:
netbyte 2022-05-10 16:22:50 +08:00
parent 8d6346e82e
commit a79dd76fd6
2 changed files with 12 additions and 10 deletions

14
main.go
View File

@ -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))
}

View File

@ -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)
}