diff --git a/adapter/network.go b/adapter/network.go index 22816f07..00ef54b8 100644 --- a/adapter/network.go +++ b/adapter/network.go @@ -51,5 +51,4 @@ type NetworkInterface struct { DNSServers []string Expensive bool Constrained bool - RawNetwork any } diff --git a/constant/cgo_android_fix.go b/constant/cgo_android_fix.go deleted file mode 100644 index cb4023c4..00000000 --- a/constant/cgo_android_fix.go +++ /dev/null @@ -1,8 +0,0 @@ -//go:build android && debug - -package constant - -// TODO: remove after fixed -// https://github.com/golang/go/issues/68760 - -const FixAndroidStack = true diff --git a/constant/cgo_android_fix_stub.go b/constant/cgo_android_fix_stub.go deleted file mode 100644 index 6145f9f5..00000000 --- a/constant/cgo_android_fix_stub.go +++ /dev/null @@ -1,5 +0,0 @@ -//go:build !(android && debug) - -package constant - -const FixAndroidStack = false diff --git a/experimental/libbox/command_client.go b/experimental/libbox/command_client.go index a9b1c131..fff2dbe2 100644 --- a/experimental/libbox/command_client.go +++ b/experimental/libbox/command_client.go @@ -7,7 +7,6 @@ import ( "path/filepath" "time" - C "github.com/sagernet/sing-box/constant" "github.com/sagernet/sing/common" E "github.com/sagernet/sing/common/exceptions" ) @@ -114,7 +113,7 @@ func (c *CommandClient) Connect() error { if err != nil { return err } - if C.FixAndroidStack { + if sFixAndroidStack { go func() { c.handler.Connected() c.handler.InitializeClashMode(newIterator(modeList), currentMode) diff --git a/experimental/libbox/config.go b/experimental/libbox/config.go index 7d0627fe..6a85c963 100644 --- a/experimental/libbox/config.go +++ b/experimental/libbox/config.go @@ -82,10 +82,6 @@ func (s *platformInterfaceStub) Interfaces() ([]adapter.NetworkInterface, error) return nil, os.ErrInvalid } -func (s *platformInterfaceStub) SetUnderlyingNetworks(networks []adapter.NetworkInterface) error { - return os.ErrInvalid -} - func (s *platformInterfaceStub) UnderNetworkExtension() bool { return false } diff --git a/experimental/libbox/monitor.go b/experimental/libbox/monitor.go index f218d8b2..eac0026d 100644 --- a/experimental/libbox/monitor.go +++ b/experimental/libbox/monitor.go @@ -1,7 +1,6 @@ package libbox import ( - C "github.com/sagernet/sing-box/constant" "github.com/sagernet/sing-tun" "github.com/sagernet/sing/common/control" E "github.com/sagernet/sing/common/exceptions" @@ -56,7 +55,7 @@ func (m *platformDefaultInterfaceMonitor) UnregisterCallback(element *list.Eleme } func (m *platformDefaultInterfaceMonitor) UpdateDefaultInterface(interfaceName string, interfaceIndex32 int32, isExpensive bool, isConstrained bool) { - if C.FixAndroidStack { + if sFixAndroidStack { go m.updateDefaultInterface(interfaceName, interfaceIndex32, isExpensive, isConstrained) } else { m.updateDefaultInterface(interfaceName, interfaceIndex32, isExpensive, isConstrained) diff --git a/experimental/libbox/platform.go b/experimental/libbox/platform.go index f4bc7ea6..2503ea44 100644 --- a/experimental/libbox/platform.go +++ b/experimental/libbox/platform.go @@ -18,7 +18,6 @@ type PlatformInterface interface { StartDefaultInterfaceMonitor(listener InterfaceUpdateListener) error CloseDefaultInterfaceMonitor(listener InterfaceUpdateListener) error GetInterfaces() (NetworkInterfaceIterator, error) - SetUnderlyingNetworks(networks RawNetworkIterator) error UnderNetworkExtension() bool IncludeAllNetworks() bool ReadWIFIState() *WIFIState @@ -52,8 +51,6 @@ type NetworkInterface struct { Type int32 DNSServer StringIterator Metered bool - - RawNetwork RawNetwork } type WIFIState struct { @@ -70,11 +67,6 @@ type NetworkInterfaceIterator interface { HasNext() bool } -type RawNetworkIterator interface { - Next() RawNetwork - HasNext() bool -} - type Notification struct { Identifier string TypeName string diff --git a/experimental/libbox/platform/interface.go b/experimental/libbox/platform/interface.go index 1684d88c..eda51b48 100644 --- a/experimental/libbox/platform/interface.go +++ b/experimental/libbox/platform/interface.go @@ -16,7 +16,6 @@ type Interface interface { UpdateRouteOptions(options *tun.Options, platformOptions option.TunPlatformOptions) error CreateDefaultInterfaceMonitor(logger logger.Logger) tun.DefaultInterfaceMonitor Interfaces() ([]adapter.NetworkInterface, error) - SetUnderlyingNetworks(networks []adapter.NetworkInterface) error UnderNetworkExtension() bool IncludeAllNetworks() bool ClearDNSCache() diff --git a/experimental/libbox/raw_network_android.go b/experimental/libbox/raw_network_android.go deleted file mode 100644 index b8d38aaa..00000000 --- a/experimental/libbox/raw_network_android.go +++ /dev/null @@ -1,3 +0,0 @@ -package libbox - -type RawNetwork interface{} diff --git a/experimental/libbox/raw_network_stub.go b/experimental/libbox/raw_network_stub.go deleted file mode 100644 index 6e23135c..00000000 --- a/experimental/libbox/raw_network_stub.go +++ /dev/null @@ -1,7 +0,0 @@ -//go:build !android - -package libbox - -type RawNetwork interface { - stub() -} diff --git a/experimental/libbox/service.go b/experimental/libbox/service.go index e04c040d..4906c571 100644 --- a/experimental/libbox/service.go +++ b/experimental/libbox/service.go @@ -81,7 +81,7 @@ func NewService(configContent string, platformInterface PlatformInterface) (*Box } func (s *BoxService) Start() error { - if C.FixAndroidStack { + if sFixAndroidStack { var err error done := make(chan struct{}) go func() { @@ -220,18 +220,11 @@ func (w *platformInterfaceWrapper) Interfaces() ([]adapter.NetworkInterface, err DNSServers: iteratorToArray[string](netInterface.DNSServer), Expensive: netInterface.Metered || isDefault && w.isExpensive, Constrained: isDefault && w.isConstrained, - RawNetwork: netInterface.RawNetwork, }) } return interfaces, nil } -func (w *platformInterfaceWrapper) SetUnderlyingNetworks(networks []adapter.NetworkInterface) error { - return w.iif.SetUnderlyingNetworks(newIterator(common.Map(networks, func(it adapter.NetworkInterface) RawNetwork { - return it.RawNetwork.(RawNetwork) - }))) -} - func (w *platformInterfaceWrapper) UnderNetworkExtension() bool { return w.iif.UnderNetworkExtension() } diff --git a/experimental/libbox/setup.go b/experimental/libbox/setup.go index 3ea2f404..184d5250 100644 --- a/experimental/libbox/setup.go +++ b/experimental/libbox/setup.go @@ -14,43 +14,55 @@ import ( ) var ( - sBasePath string - sWorkingPath string - sTempPath string - sUserID int - sGroupID int - sTVOS bool + sBasePath string + sWorkingPath string + sTempPath string + sUserID int + sGroupID int + sTVOS bool + sFixAndroidStack bool ) func init() { debug.SetPanicOnFault(true) } -func Setup(basePath string, workingPath string, tempPath string, isTVOS bool) { - sBasePath = basePath - sWorkingPath = workingPath - sTempPath = tempPath - sUserID = os.Getuid() - sGroupID = os.Getgid() - sTVOS = isTVOS - os.MkdirAll(sWorkingPath, 0o777) - os.MkdirAll(sTempPath, 0o777) +type SetupOptions struct { + BasePath string + WorkingPath string + TempPath string + Username string + IsTVOS bool + FixAndroidStack bool } -func SetupWithUsername(basePath string, workingPath string, tempPath string, username string) error { - sBasePath = basePath - sWorkingPath = workingPath - sTempPath = tempPath - sUser, err := user.Lookup(username) - if err != nil { - return err +func Setup(options *SetupOptions) error { + sBasePath = options.BasePath + sWorkingPath = options.WorkingPath + sTempPath = options.TempPath + if options.Username != "" { + sUser, err := user.Lookup(options.Username) + if err != nil { + return err + } + sUserID, _ = strconv.Atoi(sUser.Uid) + sGroupID, _ = strconv.Atoi(sUser.Gid) + } else { + sUserID = os.Getuid() + sGroupID = os.Getgid() } - sUserID, _ = strconv.Atoi(sUser.Uid) - sGroupID, _ = strconv.Atoi(sUser.Gid) + sTVOS = options.IsTVOS + + // TODO: remove after fixed + // https://github.com/golang/go/issues/68760 + sFixAndroidStack = options.FixAndroidStack + os.MkdirAll(sWorkingPath, 0o777) os.MkdirAll(sTempPath, 0o777) - os.Chown(sWorkingPath, sUserID, sGroupID) - os.Chown(sTempPath, sUserID, sGroupID) + if options.Username != "" { + os.Chown(sWorkingPath, sUserID, sGroupID) + os.Chown(sTempPath, sUserID, sGroupID) + } return nil } diff --git a/route/network.go b/route/network.go index 875b6e93..d82701fe 100644 --- a/route/network.go +++ b/route/network.go @@ -240,9 +240,6 @@ func (r *NetworkManager) UpdateInterfaces() error { newInterfaces := common.Filter(interfaces, func(it adapter.NetworkInterface) bool { return it.Flags&net.FlagUp != 0 }) - for _, networkInterface := range newInterfaces { - networkInterface.RawNetwork = nil - } r.networkInterfaces.Store(newInterfaces) if len(newInterfaces) > 0 && !slices.EqualFunc(oldInterfaces, newInterfaces, func(oldInterface adapter.NetworkInterface, newInterface adapter.NetworkInterface) bool { return oldInterface.Interface.Index == newInterface.Interface.Index && @@ -263,15 +260,6 @@ func (r *NetworkManager) UpdateInterfaces() error { } return F.ToString(it.Name, " (", strings.Join(options, ", "), ")") }), ", ")) - if C.IsAndroid { - err = r.platformInterface.SetUnderlyingNetworks(newInterfaces) - if err != nil { - r.logger.Error("set underlying networks: ", err) - } - } - } - for _, networkInterface := range interfaces { - networkInterface.RawNetwork = nil } return nil }