From 89c1dab9895f0f9a632464d08bf7e716248f1d3a Mon Sep 17 00:00:00 2001 From: armv9 <48624112+arm64v8a@users.noreply.github.com> Date: Sun, 23 Feb 2025 19:23:20 +0900 Subject: [PATCH] Fix native bugs --- .../sagernet/bg/proto/TestInstance.kt | 1 - libcore/.gitignore | 1 + libcore/box.go | 19 +++++++++++++++---- libcore/build.sh | 8 ++++++-- libcore/init.sh | 10 ++++++++-- 5 files changed, 30 insertions(+), 9 deletions(-) diff --git a/app/src/main/java/io/nekohasekai/sagernet/bg/proto/TestInstance.kt b/app/src/main/java/io/nekohasekai/sagernet/bg/proto/TestInstance.kt index 73166bb..ed7fc5a 100644 --- a/app/src/main/java/io/nekohasekai/sagernet/bg/proto/TestInstance.kt +++ b/app/src/main/java/io/nekohasekai/sagernet/bg/proto/TestInstance.kt @@ -47,7 +47,6 @@ class TestInstance(profile: ProxyEntity, val link: String, val timeout: Int) : // don't call destroyAllJsi here if (BuildConfig.DEBUG) Logs.d(config.config) box = Libcore.newSingBoxInstance(config.config) - box.forTest = true } } diff --git a/libcore/.gitignore b/libcore/.gitignore index c93587d..490da99 100644 --- a/libcore/.gitignore +++ b/libcore/.gitignore @@ -5,3 +5,4 @@ binary*.go /debug.go build .build +env_*.sh diff --git a/libcore/box.go b/libcore/box.go index 2880b60..45a074b 100644 --- a/libcore/box.go +++ b/libcore/box.go @@ -10,6 +10,7 @@ import ( "runtime" "runtime/debug" "strings" + "sync" "github.com/matsuridayo/libneko/protect_server" "github.com/matsuridayo/libneko/speedtest" @@ -20,7 +21,6 @@ import ( "github.com/sagernet/sing-box/constant" "github.com/sagernet/sing-box/option" "github.com/sagernet/sing-box/outbound" - "github.com/sagernet/sing/common" "github.com/sagernet/sing/service" "github.com/sagernet/sing/service/pause" ) @@ -59,6 +59,8 @@ func ResetAllConnections(system bool) { } type BoxInstance struct { + access sync.Mutex + *box.Box cancel context.CancelFunc state int @@ -66,8 +68,6 @@ type BoxInstance struct { v2api *boxapi.SbV2rayServer selector *outbound.Selector pauseManager pause.Manager - - ForTest bool } func NewSingBoxInstance(config string) (b *BoxInstance, err error) { @@ -113,6 +113,9 @@ func NewSingBoxInstance(config string) (b *BoxInstance, err error) { } func (b *BoxInstance) Start() (err error) { + b.access.Lock() + defer b.access.Unlock() + defer device.DeferPanicToError("box.Start", func(err_ error) { err = err_ }) if b.state == 0 { @@ -123,6 +126,9 @@ func (b *BoxInstance) Start() (err error) { } func (b *BoxInstance) Close() (err error) { + b.access.Lock() + defer b.access.Unlock() + defer device.DeferPanicToError("box.Close", func(err_ error) { err = err_ }) // no double close @@ -138,7 +144,12 @@ func (b *BoxInstance) Close() (err error) { } // close box - common.Close(b.Box) + if b.cancel != nil { + b.cancel() + } + if b.Box != nil { + b.Box.Close() + } return nil } diff --git a/libcore/build.sh b/libcore/build.sh index 202ef26..a6b90cd 100755 --- a/libcore/build.sh +++ b/libcore/build.sh @@ -1,6 +1,6 @@ #!/bin/bash -source ../../env_go.sh || true +source ./env_java.sh || true source ../buildScript/init/env_ndk.sh BUILD=".build" @@ -10,7 +10,11 @@ rm -rf $BUILD/android \ $BUILD/javac-output \ $BUILD/src -gomobile bind -v -androidapi 21 -cache $(realpath $BUILD) -trimpath -ldflags='-s -w' -tags='with_conntrack,with_gvisor,with_quic,with_wireguard,with_utls,with_clash_api,with_ech' . || exit 1 +if [ -z "$GOPATH" ]; then + GOPATH=$(go env GOPATH) +fi + +"$GOPATH"/bin/gomobile-matsuri bind -v -androidapi 21 -cache "$(realpath $BUILD)" -trimpath -ldflags='-s -w' -tags='with_conntrack,with_gvisor,with_quic,with_wireguard,with_utls,with_clash_api,with_ech' . || exit 1 rm -r libcore-sources.jar proj=../app/libs diff --git a/libcore/init.sh b/libcore/init.sh index 8792fec..86fbbaf 100755 --- a/libcore/init.sh +++ b/libcore/init.sh @@ -3,8 +3,12 @@ chmod -R 777 .build 2>/dev/null rm -rf .build 2>/dev/null +if [ -z "$GOPATH" ]; then + GOPATH=$(go env GOPATH) +fi + # Install gomobile -if [ ! -f "$GOPATH/bin/gomobile" ]; then +if [ ! -f "$GOPATH/bin/gomobile-matsuri" ]; then git clone https://github.com/MatsuriDayo/gomobile.git pushd gomobile/cmd pushd gomobile @@ -15,6 +19,8 @@ if [ ! -f "$GOPATH/bin/gomobile" ]; then popd popd rm -rf gomobile + mv "$GOPATH/bin/gomobile" "$GOPATH/bin/gomobile-matsuri" + mv "$GOPATH/bin/gobind" "$GOPATH/bin/gobind-matsuri" fi -gomobile init +gomobile-matsuri init