-
Notifications
You must be signed in to change notification settings - Fork 155
Expand file tree
/
Copy pathandroid_wrapper.go
More file actions
65 lines (54 loc) · 1.82 KB
/
Copy pathandroid_wrapper.go
File metadata and controls
65 lines (54 loc) · 1.82 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
//go:build android
package libXray
import (
"errors"
c "github.com/xtls/libxray/controller"
"github.com/xtls/libxray/dns"
)
type DialerController interface {
ProtectFd(int) bool
}
// SetDNS installs an Android VPN-aware process resolver. server must be an IP
// endpoint such as 8.8.8.8:53 or [2001:4860:4860::8888]:53.
func SetDNS(controller DialerController, server string) error {
if controller == nil {
return errors.New("dns dialer controller is nil")
}
return dns.SetDNS(server, func(fd uintptr) bool {
return controller.ProtectFd(int(fd))
})
}
// ResetDNS restores the resolver that was active before SetDNS.
func ResetDNS() {
dns.ResetDNS()
}
// ProcessFinder -> implemented by apps to resolve UID from connection details.
// See RegisterProcessFinder.
type ProcessFinder interface {
// FindProcessByConnection -> returns UID owning the connection, or -1.
FindProcessByConnection(network, srcIP string, srcPort int, destIP string, destPort int) int
}
func RegisterDialerController(controller DialerController) {
c.RegisterDialerController(func(fd uintptr) {
controller.ProtectFd(int(fd))
})
}
func RegisterListenerController(controller DialerController) {
c.RegisterListenerController(func(fd uintptr) {
controller.ProtectFd(int(fd))
})
}
// RegisterProcessFinder -> registers process finder for per-app routing.
// Pass nil to unregister.
// sdkVersion = Build.VERSION.SDK_INT.
// On API < 30, /proc/net/* parsing is used automatically; the Java callback
// is only called on API 30+.
func RegisterProcessFinder(finder ProcessFinder, sdkVersion int) {
if finder == nil {
c.RegisterProcessFinder(nil, 0)
return
}
c.RegisterProcessFinder(func(network, srcIP string, srcPort int, destIP string, destPort int) int {
return finder.FindProcessByConnection(network, srcIP, srcPort, destIP, destPort)
}, sdkVersion)
}