forked from MatsuriDayo/NekoBoxForAndroid
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathplatform_box.go
136 lines (115 loc) · 3.67 KB
/
platform_box.go
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
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
package libcore
import (
"context"
"encoding/json"
"errors"
"fmt"
"libcore/procfs"
"log"
"net/netip"
"syscall"
"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/option"
tun "github.com/sagernet/sing-tun"
"github.com/sagernet/sing/common/control"
E "github.com/sagernet/sing/common/exceptions"
"github.com/sagernet/sing/common/logger"
N "github.com/sagernet/sing/common/network"
)
var boxPlatformInterfaceInstance platform.Interface = &boxPlatformInterfaceWrapper{}
type boxPlatformInterfaceWrapper struct{}
func (w *boxPlatformInterfaceWrapper) Initialize(ctx context.Context, router adapter.Router) error {
return nil
}
func (w *boxPlatformInterfaceWrapper) UsePlatformAutoDetectInterfaceControl() bool {
return true
}
func (w *boxPlatformInterfaceWrapper) AutoDetectInterfaceControl() control.Func {
// "protect"
return func(network, address string, conn syscall.RawConn) error {
return control.Raw(conn, func(fd uintptr) error {
return intfBox.AutoDetectInterfaceControl(int32(fd))
})
}
}
func (w *boxPlatformInterfaceWrapper) OpenTun(options *tun.Options, platformOptions option.TunPlatformOptions) (tun.Tun, error) {
if len(options.IncludeUID) > 0 || len(options.ExcludeUID) > 0 {
return nil, E.New("android: unsupported uid options")
}
if len(options.IncludeAndroidUser) > 0 {
return nil, E.New("android: unsupported android_user option")
}
a, _ := json.Marshal(options)
b, _ := json.Marshal(platformOptions)
tunFd, err := intfBox.OpenTun(string(a), string(b))
if err != nil {
return nil, fmt.Errorf("intfBox.OpenTun: %v", err)
}
// Do you want to close it?
tunFd, err = syscall.Dup(tunFd)
if err != nil {
return nil, fmt.Errorf("syscall.Dup: %v", err)
}
//
options.FileDescriptor = int(tunFd)
return tun.New(*options)
}
func (w *boxPlatformInterfaceWrapper) CloseTun() error {
return nil
}
func (w *boxPlatformInterfaceWrapper) UsePlatformDefaultInterfaceMonitor() bool {
return true
}
func (w *boxPlatformInterfaceWrapper) CreateDefaultInterfaceMonitor(l logger.Logger) tun.DefaultInterfaceMonitor {
return &interfaceMonitor{}
}
func (w *boxPlatformInterfaceWrapper) UsePlatformInterfaceGetter() bool {
return false
}
func (w *boxPlatformInterfaceWrapper) Interfaces() ([]platform.NetworkInterface, error) {
return nil, errors.New("wtf")
}
// Android not using
func (w *boxPlatformInterfaceWrapper) UnderNetworkExtension() bool {
return false
}
func (w *boxPlatformInterfaceWrapper) ClearDNSCache() {
}
// process.Searcher
func (w *boxPlatformInterfaceWrapper) FindProcessInfo(ctx context.Context, network string, source netip.AddrPort, destination netip.AddrPort) (*process.Info, error) {
var uid int32
if useProcfs {
uid = procfs.ResolveSocketByProcSearch(network, source, destination)
if uid == -1 {
return nil, E.New("procfs: not found")
}
} else {
var ipProtocol int32
switch N.NetworkName(network) {
case N.NetworkTCP:
ipProtocol = syscall.IPPROTO_TCP
case N.NetworkUDP:
ipProtocol = syscall.IPPROTO_UDP
default:
return nil, E.New("unknown network: ", network)
}
var err error
uid, err = intfBox.FindConnectionOwner(ipProtocol, source.Addr().String(), int32(source.Port()), destination.Addr().String(), int32(destination.Port()))
if err != nil {
return nil, err
}
}
packageName, _ := intfBox.PackageNameByUid(uid)
return &process.Info{UserId: uid, PackageName: packageName}, nil
}
// io.Writer
var disableSingBoxLog = false
func (w *boxPlatformInterfaceWrapper) Write(p []byte) (n int, err error) {
// use neko_log
if !disableSingBoxLog {
log.Print(string(p))
}
return len(p), nil
}