Skip to content

Commit

Permalink
Enable fix stack for Android 7 and 9
Browse files Browse the repository at this point in the history
  • Loading branch information
nekohasekai committed Dec 26, 2024
1 parent d7ff5a7 commit b4ae92d
Show file tree
Hide file tree
Showing 6 changed files with 41 additions and 44 deletions.
8 changes: 0 additions & 8 deletions constant/cgo_android_fix.go

This file was deleted.

5 changes: 0 additions & 5 deletions constant/cgo_android_fix_stub.go

This file was deleted.

3 changes: 1 addition & 2 deletions experimental/libbox/command_client.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)
Expand Down Expand Up @@ -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)
Expand Down
3 changes: 1 addition & 2 deletions experimental/libbox/monitor.go
Original file line number Diff line number Diff line change
@@ -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"
Expand Down Expand Up @@ -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)
Expand Down
2 changes: 1 addition & 1 deletion experimental/libbox/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand Down
64 changes: 38 additions & 26 deletions experimental/libbox/setup.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}

Expand Down

0 comments on commit b4ae92d

Please sign in to comment.