From bc2e3960e4c90f4713efded55cc77d6a2598363c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E4=B8=96=E7=95=8C?= Date: Thu, 26 Dec 2024 11:12:48 +0800 Subject: [PATCH] Enable fix stack for Android 7 and 9 --- constant/cgo_android_fix.go | 8 ---- constant/cgo_android_fix_stub.go | 5 --- experimental/libbox/command_client.go | 3 +- experimental/libbox/monitor.go | 3 +- experimental/libbox/service.go | 2 +- experimental/libbox/setup.go | 64 ++++++++++++++++----------- 6 files changed, 41 insertions(+), 44 deletions(-) delete mode 100644 constant/cgo_android_fix.go delete mode 100644 constant/cgo_android_fix_stub.go 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/monitor.go b/experimental/libbox/monitor.go index d0517973..eb56a314 100644 --- a/experimental/libbox/monitor.go +++ b/experimental/libbox/monitor.go @@ -5,7 +5,6 @@ import ( "net/netip" "sync" - C "github.com/sagernet/sing-box/constant" "github.com/sagernet/sing-tun" "github.com/sagernet/sing/common" E "github.com/sagernet/sing/common/exceptions" @@ -98,7 +97,7 @@ func (m *platformDefaultInterfaceMonitor) UnregisterCallback(element *list.Eleme } func (m *platformDefaultInterfaceMonitor) UpdateDefaultInterface(interfaceName string, interfaceIndex32 int32) { - if C.FixAndroidStack { + if sFixAndroidStack { go m.updateDefaultInterface(interfaceName, interfaceIndex32) } else { m.updateDefaultInterface(interfaceName, interfaceIndex32) diff --git a/experimental/libbox/service.go b/experimental/libbox/service.go index a08d5e07..9bfa8a70 100644 --- a/experimental/libbox/service.go +++ b/experimental/libbox/service.go @@ -73,7 +73,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() { diff --git a/experimental/libbox/setup.go b/experimental/libbox/setup.go index ea941ecf..950cbe4e 100644 --- a/experimental/libbox/setup.go +++ b/experimental/libbox/setup.go @@ -15,43 +15,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 }