Skip to content

Commit

Permalink
test
Browse files Browse the repository at this point in the history
  • Loading branch information
Restia-Ashbell committed Oct 21, 2024
1 parent f301b90 commit 80b0f0a
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 25 deletions.
12 changes: 3 additions & 9 deletions libcore/box.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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()
Expand All @@ -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 {
Expand Down
1 change: 0 additions & 1 deletion libcore/nb4a.go
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand Down
30 changes: 17 additions & 13 deletions libcore/platform_box.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -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(), ",")
Expand All @@ -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
}

Expand Down Expand Up @@ -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)
}

Expand Down Expand Up @@ -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")
Expand All @@ -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))
}
2 changes: 0 additions & 2 deletions libcore/platform_java.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down

0 comments on commit 80b0f0a

Please sign in to comment.