forked from xiaokangwang/AndroidLibV2ray
-
Notifications
You must be signed in to change notification settings - Fork 12
/
vpnservice_support.go
127 lines (108 loc) · 2.52 KB
/
vpnservice_support.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
package libv2ray
import (
"errors"
"log"
"net"
"os"
"strings"
"golang.org/x/sys/unix"
)
type vpnProtectedDialer struct {
vp *V2RayPoint
}
func (sDialer *vpnProtectedDialer) Dial(network, Address string) (net.Conn, error) {
if strings.HasPrefix(network, "tcp") {
addr, err := net.ResolveTCPAddr(network, Address)
if err != nil {
return nil, err
}
fd, err := unix.Socket(unix.AF_INET6, unix.SOCK_STREAM, unix.IPPROTO_TCP)
if err != nil {
return nil, err
}
//Protect socket fd!
log.Println("Protecting Sock:", fd)
sDialer.vp.VpnSupportSet.Protect(fd)
sa := new(unix.SockaddrInet6)
sa.Port = addr.Port
sa.ZoneId = uint32(zoneToInt(addr.Zone))
//fmt.Println(addr.IP.To16())
copy(sa.Addr[:], addr.IP.To16())
//fmt.Println(sa.Addr)
err = unix.Connect(fd, sa)
if err != nil {
return nil, err
}
file := os.NewFile(uintptr(fd), "Socket")
conn, err := net.FileConn(file)
if err != nil {
return nil, err
}
return conn, nil
}
if strings.HasPrefix(network, "udp") {
addr, err := net.ResolveUDPAddr(network, Address)
if err != nil {
return nil, err
}
fd, err := unix.Socket(unix.AF_INET6, unix.SOCK_DGRAM, unix.IPPROTO_UDP)
if err != nil {
return nil, err
}
//Protect socket fd!
log.Println("Protecting Sock:", fd)
sDialer.vp.VpnSupportSet.Protect(fd)
sa := new(unix.SockaddrInet6)
sa.Port = addr.Port
sa.ZoneId = uint32(zoneToInt(addr.Zone))
//fmt.Println(addr.IP.To16())
copy(sa.Addr[:], addr.IP.To16())
//fmt.Println(sa.Addr)
err = unix.Connect(fd, sa)
if err != nil {
return nil, err
}
file := os.NewFile(uintptr(fd), "Socket")
conn, err := net.FileConn(file)
if err != nil {
return nil, err
}
return conn, nil
}
return nil, errors.New("Pto udf")
}
// Bigger than we need, not too big to worry about overflow
const big = 0xFFFFFF
// Decimal to integer starting at &s[i0].
// Returns number, new offset, success.
func dtoi(s string, i0 int) (n int, i int, ok bool) {
n = 0
for i = i0; i < len(s) && '0' <= s[i] && s[i] <= '9'; i++ {
n = n*10 + int(s[i]-'0')
if n >= big {
return 0, i, false
}
}
if i == i0 {
return 0, i, false
}
return n, i, true
}
func zoneToInt(zone string) int {
if zone == "" {
return 0
}
if ifi, err := net.InterfaceByName(zone); err == nil {
return ifi.Index
}
n, _, _ := dtoi(zone, 0)
return n
}
/*V2RayVPNServiceSupportsSet To support Android VPN mode*/
type V2RayVPNServiceSupportsSet interface {
GetVPNFd() int
Setup(Conf string) int
Prepare() int
Shutdown() int
Protect(int) int
}