-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathnb4a.go
More file actions
111 lines (90 loc) · 2.56 KB
/
nb4a.go
File metadata and controls
111 lines (90 loc) · 2.56 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
package libcore
import (
"fmt"
"libcore/device"
"os"
"path/filepath"
"runtime"
"strings"
_ "unsafe"
"log"
"github.com/matsuridayo/libneko/neko_common"
"github.com/matsuridayo/libneko/neko_log"
"github.com/sagernet/sing-box/nekoutils"
"golang.org/x/sys/unix"
)
//go:linkname resourcePaths github.com/sagernet/sing-box/constant.resourcePaths
var resourcePaths []string
func NekoLogPrintln(s string) {
log.Println(s)
}
func NekoLogClear() {
neko_log.LogWriter.Truncate()
}
func ForceGc() {
go runtime.GC()
}
func InitCore(process, cachePath, internalAssets, externalAssets string,
maxLogSizeKb int32, logEnable bool,
if1 NB4AInterface, if2 BoxPlatformInterface,
) {
defer device.DeferPanicToError("InitCore", func(err error) { log.Println(err) })
isBgProcess = strings.HasSuffix(process, ":bg")
neko_common.RunMode = neko_common.RunMode_NekoBoxForAndroid
intfNB4A = if1
intfBox = if2
useProcfs = intfBox.UseProcFS()
// Working dir
tmp := filepath.Join(cachePath, "../no_backup")
os.MkdirAll(tmp, 0755)
os.Chdir(tmp)
// sing-box fs
resourcePaths = append(resourcePaths, externalAssets)
// Set up log
if maxLogSizeKb < 50 {
maxLogSizeKb = 50
}
neko_log.LogWriterDisable = !logEnable
neko_log.TruncateOnStart = isBgProcess
neko_log.SetupLog(int(maxLogSizeKb)*1024, filepath.Join(cachePath, "neko.log"))
// nekoutils
nekoutils.Selector_OnProxySelected = intfNB4A.Selector_OnProxySelected
// Set up some component
go func() {
defer device.DeferPanicToError("InitCore-go", func(err error) { log.Println(err) })
device.GoDebug(process)
// certs
pem, err := os.ReadFile(externalAssets + "ca.pem")
if err == nil {
updateRootCACerts(pem)
}
}()
}
func sendFdToProtect(fd int, path string) error {
socketFd, err := unix.Socket(unix.AF_UNIX, unix.SOCK_STREAM, 0)
if err != nil {
return fmt.Errorf("failed to create unix socket: %w", err)
}
defer unix.Close(socketFd)
var timeout unix.Timeval
timeout.Usec = 100 * 1000
_ = unix.SetsockoptTimeval(socketFd, unix.SOL_SOCKET, unix.SO_RCVTIMEO, &timeout)
_ = unix.SetsockoptTimeval(socketFd, unix.SOL_SOCKET, unix.SO_SNDTIMEO, &timeout)
err = unix.Connect(socketFd, &unix.SockaddrUnix{Name: path})
if err != nil {
return fmt.Errorf("failed to connect: %w", err)
}
err = unix.Sendmsg(socketFd, nil, unix.UnixRights(fd), nil, 0)
if err != nil {
return fmt.Errorf("failed to send: %w", err)
}
dummy := []byte{1}
n, err := unix.Read(socketFd, dummy)
if err != nil {
return fmt.Errorf("failed to receive: %w", err)
}
if n != 1 {
return fmt.Errorf("socket closed unexpectedly")
}
return nil
}