Skip to content

Commit

Permalink
Add workaround for golang/go#68760
Browse files Browse the repository at this point in the history
  • Loading branch information
nekohasekai committed Dec 10, 2024
1 parent 3032317 commit 6a0043d
Show file tree
Hide file tree
Showing 7 changed files with 78 additions and 15 deletions.
6 changes: 6 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -201,9 +201,15 @@ test_stdio:
lib_android:
go run ./cmd/internal/build_libbox -target android

lib_android_debug:
go run ./cmd/internal/build_libbox -target android -debug

lib_ios:
go run ./cmd/internal/build_libbox -target ios

lib_ios_debug:
go run ./cmd/internal/build_libbox -target ios -debug

lib:
go run ./cmd/internal/build_libbox -target android
go run ./cmd/internal/build_libbox -target ios
Expand Down
8 changes: 8 additions & 0 deletions cmd/internal/build_libbox/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -80,9 +80,17 @@ func buildAndroid() {
log.Fatal("java version should be openjdk 17")
}

var bindTarget string
if debugEnabled {
bindTarget = "android/arm64"
} else {
bindTarget = "android"
}

args := []string{
"bind",
"-v",
"-target", bindTarget,
"-androidapi", "21",
"-javapkg=io.nekohasekai",
"-libname=box",
Expand Down
8 changes: 8 additions & 0 deletions constant/cgo_android_fix.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
//go:build android && debug

package constant

// TODO: remove after fixed
// https://github.com/golang/go/issues/68760

const WithFixAndroidStack = true
5 changes: 5 additions & 0 deletions constant/cgo_android_fix_stub.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
//go:build !(android || debug)

package constant

const WithFixAndroidStack = false
22 changes: 18 additions & 4 deletions experimental/libbox/command_client.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ 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 @@ -113,11 +114,24 @@ func (c *CommandClient) Connect() error {
if err != nil {
return err
}
c.handler.Connected()
c.handler.InitializeClashMode(newIterator(modeList), currentMode)
if C.WithFixAndroidStack {
go func() {
c.handler.Connected()
c.handler.InitializeClashMode(newIterator(modeList), currentMode)
if len(modeList) == 0 {
conn.Close()
c.handler.Disconnected(os.ErrInvalid.Error())
}
}()
} else {
c.handler.Connected()
c.handler.InitializeClashMode(newIterator(modeList), currentMode)
if len(modeList) == 0 {
conn.Close()
c.handler.Disconnected(os.ErrInvalid.Error())
}
}
if len(modeList) == 0 {
conn.Close()
c.handler.Disconnected(os.ErrInvalid.Error())
return nil
}
go c.handleModeConn(conn)
Expand Down
9 changes: 9 additions & 0 deletions experimental/libbox/monitor.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ 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"
Expand Down Expand Up @@ -97,6 +98,14 @@ func (m *platformDefaultInterfaceMonitor) UnregisterCallback(element *list.Eleme
}

func (m *platformDefaultInterfaceMonitor) UpdateDefaultInterface(interfaceName string, interfaceIndex32 int32) {
if C.WithFixAndroidStack {
go m.updateDefaultInterface(interfaceName, interfaceIndex32)
} else {
m.updateDefaultInterface(interfaceName, interfaceIndex32)
}
}

func (m *platformDefaultInterfaceMonitor) updateDefaultInterface(interfaceName string, interfaceIndex32 int32) {
if interfaceName == "" || interfaceIndex32 == -1 {
m.defaultInterfaceName = ""
m.defaultInterfaceIndex = -1
Expand Down
35 changes: 24 additions & 11 deletions experimental/libbox/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -73,23 +73,36 @@ func NewService(configContent string, platformInterface PlatformInterface) (*Box
}

func (s *BoxService) Start() error {
return s.instance.Start()
if C.WithFixAndroidStack {
var err error
done := make(chan struct{})
go func() {
err = s.instance.Start()
close(done)
}()
<-done
return err
} else {
return s.instance.Start()
}
}

func (s *BoxService) Close() error {
s.cancel()
s.urlTestHistoryStorage.Close()
var err error
done := make(chan struct{})
defer close(done)
go func() {
select {
case <-done:
return
case <-time.After(C.FatalStopTimeout):
os.Exit(1)
}
err = s.instance.Close()
close(done)
}()
s.cancel()
s.urlTestHistoryStorage.Close()
return s.instance.Close()
select {
case <-done:
return err
case <-time.After(C.FatalStopTimeout):
os.Exit(1)
return nil
}
}

func (s *BoxService) NeedWIFIState() bool {
Expand Down

0 comments on commit 6a0043d

Please sign in to comment.