From e64cf3b7df3a2ead890aabcab1860fd85f8b1924 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E4=B8=96=E7=95=8C?= Date: Mon, 27 Jan 2025 13:40:26 +0800 Subject: [PATCH] Do not set address sets to routes on Apple platforms Network Extension was observed to stop for unknown reasons --- experimental/libbox/config.go | 4 --- experimental/libbox/platform.go | 1 - experimental/libbox/platform/interface.go | 1 - experimental/libbox/service.go | 14 --------- protocol/tun/inbound.go | 38 ++--------------------- 5 files changed, 2 insertions(+), 56 deletions(-) diff --git a/experimental/libbox/config.go b/experimental/libbox/config.go index 6a85c963..159fd8f6 100644 --- a/experimental/libbox/config.go +++ b/experimental/libbox/config.go @@ -66,10 +66,6 @@ func (s *platformInterfaceStub) OpenTun(options *tun.Options, platformOptions op return nil, os.ErrInvalid } -func (s *platformInterfaceStub) UpdateRouteOptions(options *tun.Options, platformInterface option.TunPlatformOptions) error { - return os.ErrInvalid -} - func (s *platformInterfaceStub) UsePlatformDefaultInterfaceMonitor() bool { return true } diff --git a/experimental/libbox/platform.go b/experimental/libbox/platform.go index 2503ea44..d5951cd3 100644 --- a/experimental/libbox/platform.go +++ b/experimental/libbox/platform.go @@ -9,7 +9,6 @@ type PlatformInterface interface { UsePlatformAutoDetectInterfaceControl() bool AutoDetectInterfaceControl(fd int32) error OpenTun(options TunOptions) (int32, error) - UpdateRouteOptions(options TunOptions) error WriteLog(message string) UseProcFS() bool FindConnectionOwner(ipProtocol int32, sourceAddress string, sourcePort int32, destinationAddress string, destinationPort int32) (int32, error) diff --git a/experimental/libbox/platform/interface.go b/experimental/libbox/platform/interface.go index eda51b48..ef37daea 100644 --- a/experimental/libbox/platform/interface.go +++ b/experimental/libbox/platform/interface.go @@ -13,7 +13,6 @@ type Interface interface { UsePlatformAutoDetectInterfaceControl() bool AutoDetectInterfaceControl(fd int) error OpenTun(options *tun.Options, platformOptions option.TunPlatformOptions) (tun.Tun, error) - UpdateRouteOptions(options *tun.Options, platformOptions option.TunPlatformOptions) error CreateDefaultInterfaceMonitor(logger logger.Logger) tun.DefaultInterfaceMonitor Interfaces() ([]adapter.NetworkInterface, error) UnderNetworkExtension() bool diff --git a/experimental/libbox/service.go b/experimental/libbox/service.go index 4906c571..8d42d26e 100644 --- a/experimental/libbox/service.go +++ b/experimental/libbox/service.go @@ -174,20 +174,6 @@ func (w *platformInterfaceWrapper) OpenTun(options *tun.Options, platformOptions return tun.New(*options) } -func (w *platformInterfaceWrapper) UpdateRouteOptions(options *tun.Options, platformOptions option.TunPlatformOptions) error { - if len(options.IncludeUID) > 0 || len(options.ExcludeUID) > 0 { - return E.New("android: unsupported uid options") - } - if len(options.IncludeAndroidUser) > 0 { - return E.New("android: unsupported android_user option") - } - routeRanges, err := options.BuildAutoRouteRanges(true) - if err != nil { - return err - } - return w.iif.UpdateRouteOptions(&tunOptions{options, routeRanges, platformOptions}) -} - func (w *platformInterfaceWrapper) CreateDefaultInterfaceMonitor(logger logger.Logger) tun.DefaultInterfaceMonitor { return &platformDefaultInterfaceMonitor{ platformInterfaceWrapper: w, diff --git a/protocol/tun/inbound.go b/protocol/tun/inbound.go index 34a5db2b..3bbe0421 100644 --- a/protocol/tun/inbound.go +++ b/protocol/tun/inbound.go @@ -305,7 +305,7 @@ func (t *Inbound) Start(stage adapter.StartStage) error { if t.tunOptions.Name == "" { t.tunOptions.Name = tun.CalculateInterfaceName("") } - if t.platformInterface == nil || runtime.GOOS != "android" { + if t.platformInterface == nil { t.routeAddressSet = common.FlatMap(t.routeRuleSet, adapter.RuleSet.ExtractIPSet) for _, routeRuleSet := range t.routeRuleSet { ipSets := routeRuleSet.ExtractIPSet() @@ -421,41 +421,7 @@ func (t *Inbound) Start(stage adapter.StartStage) error { func (t *Inbound) updateRouteAddressSet(it adapter.RuleSet) { t.routeAddressSet = common.FlatMap(t.routeRuleSet, adapter.RuleSet.ExtractIPSet) t.routeExcludeAddressSet = common.FlatMap(t.routeExcludeRuleSet, adapter.RuleSet.ExtractIPSet) - if t.autoRedirect != nil { - t.autoRedirect.UpdateRouteAddressSet() - } else { - tunOptions := t.tunOptions - for _, ipSet := range t.routeAddressSet { - for _, prefix := range ipSet.Prefixes() { - if prefix.Addr().Is4() { - tunOptions.Inet4RouteAddress = append(tunOptions.Inet4RouteAddress, prefix) - } else { - tunOptions.Inet6RouteAddress = append(tunOptions.Inet6RouteAddress, prefix) - } - } - } - for _, ipSet := range t.routeExcludeAddressSet { - for _, prefix := range ipSet.Prefixes() { - if prefix.Addr().Is4() { - tunOptions.Inet4RouteExcludeAddress = append(tunOptions.Inet4RouteExcludeAddress, prefix) - } else { - tunOptions.Inet6RouteExcludeAddress = append(tunOptions.Inet6RouteExcludeAddress, prefix) - } - } - } - if t.platformInterface != nil { - err := t.platformInterface.UpdateRouteOptions(&tunOptions, t.platformOptions) - if err != nil { - t.logger.Error("update route addresses: ", err) - } - } else { - err := t.tunIf.UpdateRouteOptions(tunOptions) - if err != nil { - t.logger.Error("update route addresses: ", err) - } - } - t.logger.Info("updated route addresses") - } + t.autoRedirect.UpdateRouteAddressSet() t.routeAddressSet = nil t.routeExcludeAddressSet = nil }