From 80b0f0a8f561166c3827436f65fa6732daade8e9 Mon Sep 17 00:00:00 2001 From: Restia-Ashbell <107416976+Restia-Ashbell@users.noreply.github.com> Date: Tue, 22 Oct 2024 00:35:12 +0800 Subject: [PATCH] test --- libcore/box.go | 12 +++--------- libcore/nb4a.go | 1 - libcore/platform_box.go | 30 +++++++++++++++++------------- libcore/platform_java.go | 2 -- 4 files changed, 20 insertions(+), 25 deletions(-) diff --git a/libcore/box.go b/libcore/box.go index 0e1d895d..d59138bc 100644 --- a/libcore/box.go +++ b/libcore/box.go @@ -11,7 +11,6 @@ import ( "runtime/debug" "strings" - "github.com/matsuridayo/libneko/neko_log" "github.com/matsuridayo/libneko/protect_server" "github.com/matsuridayo/libneko/speedtest" "github.com/sagernet/sing-box/boxapi" @@ -83,10 +82,12 @@ func NewSingBoxInstance(config string) (b *BoxInstance, err error) { // create box ctx, cancel := context.WithCancel(context.Background()) ctx = service.ContextWithDefaultRegistry(ctx) + platformWrapper := &boxPlatformInterfaceWrapper{useProcFS: intfBox.UseProcFS()} instance, err := box.New(box.Options{ Options: options, Context: ctx, - PlatformInterface: boxPlatformInterfaceInstance, + PlatformInterface: platformWrapper, + PlatformLogWriter: platformWrapper, }) if err != nil { cancel() @@ -99,13 +100,6 @@ func NewSingBoxInstance(config string) (b *BoxInstance, err error) { pauseManager: service.FromContext[pause.Manager](ctx), } - b.SetLogWritter(neko_log.LogWriter) - - // fuck your sing-box platformFormatter - pf := instance.GetLogPlatformFormatter() - pf.DisableColors = true - pf.DisableLineBreak = false - // selector if proxy, ok := b.Router().Outbound("proxy"); ok { if selector, ok := proxy.(*outbound.Selector); ok { diff --git a/libcore/nb4a.go b/libcore/nb4a.go index f8d744bd..db3fd4d8 100644 --- a/libcore/nb4a.go +++ b/libcore/nb4a.go @@ -39,7 +39,6 @@ func InitCore(process, cachePath, internalAssets, externalAssets string, neko_common.RunMode = neko_common.RunMode_NekoBoxForAndroid intfNB4A = if1 intfBox = if2 - useProcfs = intfBox.UseProcFS() // Working dir tmp := filepath.Join(cachePath, "../no_backup") diff --git a/libcore/platform_box.go b/libcore/platform_box.go index 11ae9217..4718e5c3 100644 --- a/libcore/platform_box.go +++ b/libcore/platform_box.go @@ -6,14 +6,15 @@ import ( "errors" "fmt" "libcore/procfs" - "log" "net/netip" + "runtime" "strings" "syscall" + "github.com/matsuridayo/libneko/neko_log" "github.com/sagernet/sing-box/adapter" "github.com/sagernet/sing-box/common/process" - "github.com/sagernet/sing-box/experimental/libbox/platform" + "github.com/sagernet/sing-box/log" "github.com/sagernet/sing-box/option" tun "github.com/sagernet/sing-tun" "github.com/sagernet/sing/common/control" @@ -22,9 +23,10 @@ import ( N "github.com/sagernet/sing/common/network" ) -var boxPlatformInterfaceInstance platform.Interface = &boxPlatformInterfaceWrapper{} - -type boxPlatformInterfaceWrapper struct{} +type boxPlatformInterfaceWrapper struct { + useProcFS bool + router adapter.Router +} func (w *boxPlatformInterfaceWrapper) ReadWIFIState() adapter.WIFIState { state := strings.Split(intfBox.WIFIState(), ",") @@ -35,6 +37,7 @@ func (w *boxPlatformInterfaceWrapper) ReadWIFIState() adapter.WIFIState { } func (w *boxPlatformInterfaceWrapper) Initialize(ctx context.Context, router adapter.Router) error { + w.router = router return nil } @@ -70,7 +73,7 @@ func (w *boxPlatformInterfaceWrapper) OpenTun(options *tun.Options, platformOpti return nil, fmt.Errorf("syscall.Dup: %v", err) } // - options.FileDescriptor = int(tunFd) + options.FileDescriptor = tunFd return tun.New(*options) } @@ -111,7 +114,7 @@ func (w *boxPlatformInterfaceWrapper) ClearDNSCache() { func (w *boxPlatformInterfaceWrapper) FindProcessInfo(ctx context.Context, network string, source netip.AddrPort, destination netip.AddrPort) (*process.Info, error) { var uid int32 - if useProcfs { + if w.useProcFS { uid = procfs.ResolveSocketByProcSearch(network, source, destination) if uid == -1 { return nil, E.New("procfs: not found") @@ -138,12 +141,13 @@ func (w *boxPlatformInterfaceWrapper) FindProcessInfo(ctx context.Context, netwo // io.Writer -var disableSingBoxLog = false +func (w *boxPlatformInterfaceWrapper) DisableColors() bool { + return runtime.GOOS != "android" +} -func (w *boxPlatformInterfaceWrapper) Write(p []byte) (n int, err error) { - // use neko_log - if !disableSingBoxLog { - log.Print(string(p)) +func (w *boxPlatformInterfaceWrapper) WriteMessage(level log.Level, message string) { + if !strings.HasSuffix(message, "\n") { + message += "\n" } - return len(p), nil + neko_log.LogWriter.Write([]byte(message)) } diff --git a/libcore/platform_java.go b/libcore/platform_java.go index 1b76c2df..2c419b45 100644 --- a/libcore/platform_java.go +++ b/libcore/platform_java.go @@ -3,8 +3,6 @@ package libcore var intfBox BoxPlatformInterface var intfNB4A NB4AInterface -var useProcfs bool - type NB4AInterface interface { UseOfficialAssets() bool Selector_OnProxySelected(selectorTag string, tag string)