From b2b56ffcf8550386130601dda3f9e24f581a6438 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E4=B8=96=E7=95=8C?= Date: Wed, 6 Nov 2024 19:39:18 +0800 Subject: [PATCH] Fix gateway select --- tun.go | 32 ++++++++++++++++++++++++++------ 1 file changed, 26 insertions(+), 6 deletions(-) diff --git a/tun.go b/tun.go index f1688fa..5c0663a 100644 --- a/tun.go +++ b/tun.go @@ -89,10 +89,20 @@ func (o *Options) Inet4GatewayAddr() netip.Addr { return o.Inet4Gateway } if len(o.Inet4Address) > 0 { - if HasNextAddress(o.Inet4Address[0], 1) { - return o.Inet4Address[0].Addr().Next() - } else if runtime.GOOS != "linux" { + switch runtime.GOOS { + case "android": + case "linux": + if HasNextAddress(o.Inet4Address[0], 1) { + return o.Inet4Address[0].Addr().Next() + } + case "darwin": return o.Inet4Address[0].Addr() + default: + if HasNextAddress(o.Inet4Address[0], 1) { + return o.Inet4Address[0].Addr().Next() + } else { + return o.Inet4Address[0].Addr() + } } } return netip.IPv4Unspecified() @@ -103,10 +113,20 @@ func (o *Options) Inet6GatewayAddr() netip.Addr { return o.Inet6Gateway } if len(o.Inet6Address) > 0 { - if HasNextAddress(o.Inet6Address[0], 1) { - return o.Inet6Address[0].Addr().Next() - } else if runtime.GOOS != "linux" { + switch runtime.GOOS { + case "android": + case "linux": + if HasNextAddress(o.Inet6Address[0], 1) { + return o.Inet6Address[0].Addr().Next() + } + case "darwin": return o.Inet6Address[0].Addr() + default: + if HasNextAddress(o.Inet6Address[0], 1) { + return o.Inet6Address[0].Addr().Next() + } else { + return o.Inet6Address[0].Addr() + } } } return netip.IPv6Unspecified()