Enable fix stack for Android 7 and 9

This commit is contained in:
世界 2024-12-26 11:12:48 +08:00
parent 9c4ab0bf33
commit bc2e3960e4
No known key found for this signature in database
GPG key ID: CD109927C34A63C4
6 changed files with 41 additions and 44 deletions

View file

@ -1,8 +0,0 @@
//go:build android && debug
package constant
// TODO: remove after fixed
// https://github.com/golang/go/issues/68760
const FixAndroidStack = true

View file

@ -1,5 +0,0 @@
//go:build !(android && debug)
package constant
const FixAndroidStack = false

View file

@ -7,7 +7,6 @@ import (
"path/filepath" "path/filepath"
"time" "time"
C "github.com/sagernet/sing-box/constant"
"github.com/sagernet/sing/common" "github.com/sagernet/sing/common"
E "github.com/sagernet/sing/common/exceptions" E "github.com/sagernet/sing/common/exceptions"
) )
@ -114,7 +113,7 @@ func (c *CommandClient) Connect() error {
if err != nil { if err != nil {
return err return err
} }
if C.FixAndroidStack { if sFixAndroidStack {
go func() { go func() {
c.handler.Connected() c.handler.Connected()
c.handler.InitializeClashMode(newIterator(modeList), currentMode) c.handler.InitializeClashMode(newIterator(modeList), currentMode)

View file

@ -5,7 +5,6 @@ import (
"net/netip" "net/netip"
"sync" "sync"
C "github.com/sagernet/sing-box/constant"
"github.com/sagernet/sing-tun" "github.com/sagernet/sing-tun"
"github.com/sagernet/sing/common" "github.com/sagernet/sing/common"
E "github.com/sagernet/sing/common/exceptions" 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) { func (m *platformDefaultInterfaceMonitor) UpdateDefaultInterface(interfaceName string, interfaceIndex32 int32) {
if C.FixAndroidStack { if sFixAndroidStack {
go m.updateDefaultInterface(interfaceName, interfaceIndex32) go m.updateDefaultInterface(interfaceName, interfaceIndex32)
} else { } else {
m.updateDefaultInterface(interfaceName, interfaceIndex32) m.updateDefaultInterface(interfaceName, interfaceIndex32)

View file

@ -73,7 +73,7 @@ func NewService(configContent string, platformInterface PlatformInterface) (*Box
} }
func (s *BoxService) Start() error { func (s *BoxService) Start() error {
if C.FixAndroidStack { if sFixAndroidStack {
var err error var err error
done := make(chan struct{}) done := make(chan struct{})
go func() { go func() {

View file

@ -15,43 +15,55 @@ import (
) )
var ( var (
sBasePath string sBasePath string
sWorkingPath string sWorkingPath string
sTempPath string sTempPath string
sUserID int sUserID int
sGroupID int sGroupID int
sTVOS bool sTVOS bool
sFixAndroidStack bool
) )
func init() { func init() {
debug.SetPanicOnFault(true) debug.SetPanicOnFault(true)
} }
func Setup(basePath string, workingPath string, tempPath string, isTVOS bool) { type SetupOptions struct {
sBasePath = basePath BasePath string
sWorkingPath = workingPath WorkingPath string
sTempPath = tempPath TempPath string
sUserID = os.Getuid() Username string
sGroupID = os.Getgid() IsTVOS bool
sTVOS = isTVOS FixAndroidStack bool
os.MkdirAll(sWorkingPath, 0o777)
os.MkdirAll(sTempPath, 0o777)
} }
func SetupWithUsername(basePath string, workingPath string, tempPath string, username string) error { func Setup(options *SetupOptions) error {
sBasePath = basePath sBasePath = options.BasePath
sWorkingPath = workingPath sWorkingPath = options.WorkingPath
sTempPath = tempPath sTempPath = options.TempPath
sUser, err := user.Lookup(username) if options.Username != "" {
if err != nil { sUser, err := user.Lookup(options.Username)
return err 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) sTVOS = options.IsTVOS
sGroupID, _ = strconv.Atoi(sUser.Gid)
// TODO: remove after fixed
// https://github.com/golang/go/issues/68760
sFixAndroidStack = options.FixAndroidStack
os.MkdirAll(sWorkingPath, 0o777) os.MkdirAll(sWorkingPath, 0o777)
os.MkdirAll(sTempPath, 0o777) os.MkdirAll(sTempPath, 0o777)
os.Chown(sWorkingPath, sUserID, sGroupID) if options.Username != "" {
os.Chown(sTempPath, sUserID, sGroupID) os.Chown(sWorkingPath, sUserID, sGroupID)
os.Chown(sTempPath, sUserID, sGroupID)
}
return nil return nil
} }