forked from yinqiwen/gsnova
-
Notifications
You must be signed in to change notification settings - Fork 0
/
protected.go
460 lines (414 loc) · 11.3 KB
/
protected.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
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
// +build !windows
// Package protected is used for creating "protected" connections
// that bypass Android's VpnService
package protector
import (
"context"
"errors"
"fmt"
"io"
"log"
"net"
"os"
"strconv"
"sync"
"syscall"
"time"
)
const (
SO_MARK = 0x24
)
var SocketMark int
type ProtectedConnBase struct {
mutex sync.Mutex
isClosed bool
socketFd int
ip net.IP
//ip [4]byte
port int
}
// cleanup is ran whenever we encounter a socket error
// we use a mutex since this connection is active in a variety
// of goroutines and to prevent any possible race conditions
func (conn *ProtectedConnBase) cleanup() {
conn.mutex.Lock()
defer conn.mutex.Unlock()
if conn.socketFd != socketError {
syscall.Close(conn.socketFd)
conn.socketFd = socketError
}
}
// connectSocket makes the connection to the given IP address port
// for the given socket fd
func (conn *ProtectedConnBase) connectSocket() error {
if nil == conn.ip {
return fmt.Errorf("Empty IP to connect")
}
var sockAddr syscall.Sockaddr
//bindAddr := &syscall.SockaddrInet4{Port: 0}
// lip := net.ParseIP("27.38.173.96")
// copy(bindAddr.Addr[:], lip[0:4])
if conn.ip.To4() != nil {
sockAddr4 := &syscall.SockaddrInet4{Port: conn.port}
copy(sockAddr4.Addr[:], conn.ip.To4()[:])
sockAddr = sockAddr4
} else {
sockAddr6 := &syscall.SockaddrInet6{Port: conn.port}
copy(sockAddr6.Addr[:], conn.ip.To16()[0:16])
sockAddr = sockAddr6
//bindAddr = &syscall.SockaddrInet6{Port: 0}
}
// err := syscall.Bind(conn.socketFd, bindAddr)
// if nil != err {
// return err
// }
errCh := make(chan error, 2)
time.AfterFunc(connectTimeOut, func() {
errCh <- errors.New("connect timeout")
})
go func() {
errCh <- syscall.Connect(conn.socketFd, sockAddr)
}()
err := <-errCh
return err
}
type ProtectedPacketConn struct {
ProtectedConnBase
net.PacketConn
rawFile *os.File
}
func (c *ProtectedPacketConn) Write(b []byte) (int, error) {
writer, ok := c.PacketConn.(io.Writer)
if !ok {
return 0, syscall.EINVAL
}
return writer.Write(b)
}
func (c *ProtectedPacketConn) Close() error {
return c.PacketConn.Close()
}
// converts the protected connection specified by
// socket fd to a net.Conn
func (conn *ProtectedPacketConn) convert() error {
conn.mutex.Lock()
file := os.NewFile(uintptr(conn.socketFd), "")
conn.rawFile = file
// dup the fd and return a copy
fileConn, err := net.FilePacketConn(file)
// closes the original fd
file.Close()
conn.socketFd = socketError
if err != nil {
conn.mutex.Unlock()
return err
}
conn.PacketConn = fileConn
conn.mutex.Unlock()
return nil
}
// connectSocket makes the connection to the given IP address port
// for the given socket fd
func (conn *ProtectedPacketConn) listenSocket(ip net.IP, port int, ipv6 bool) error {
var sockAddr syscall.Sockaddr
if !ipv6 {
sockAddr4 := &syscall.SockaddrInet4{Port: conn.port}
copy(sockAddr4.Addr[:], ip.To4()[0:4])
sockAddr = sockAddr4
} else {
sockAddr6 := &syscall.SockaddrInet6{Port: conn.port}
copy(sockAddr6.Addr[:], ip.To16()[0:16])
sockAddr = sockAddr6
}
err := syscall.Bind(conn.socketFd, sockAddr)
if nil != err {
return err
}
// errCh := make(chan error, 2)
// time.AfterFunc(connectTimeOut, func() {
// errCh <- errors.New("connect timeout")
// })
// go func() {
// errCh <- syscall.Listen(conn.socketFd, 512)
// }()
// err = <-errCh
return err
}
type ProtectedConn struct {
net.Conn
ProtectedConnBase
}
// Resolve resolves the given address using a DNS lookup on a UDP socket
// protected by the currnet Protector.
func Resolve(network string, addr string) (*net.TCPAddr, error) {
host, port, err := SplitHostPort(addr)
if err != nil {
return nil, err
}
// Check if we already have the IP address
IPAddr := net.ParseIP(host)
if IPAddr != nil {
return &net.TCPAddr{IP: IPAddr, Port: port}, nil
}
// Create a datagram socket
socketFd, err := syscall.Socket(syscall.AF_INET, syscall.SOCK_DGRAM, 0)
if err != nil {
return nil, fmt.Errorf("Error creating socket: %v", err)
}
defer syscall.Close(socketFd)
// Here we protect the underlying socket from the
// VPN connection by passing the file descriptor
// back to Java for exclusion
err = currentProtect(socketFd)
if err != nil {
return nil, fmt.Errorf("Could not bind socket to system device: %v", err)
}
IPAddr = net.ParseIP(currentDnsServer)
if IPAddr == nil {
return nil, errors.New("invalid IP address")
}
var ip [4]byte
copy(ip[:], IPAddr.To4())
sockAddr := syscall.SockaddrInet4{Addr: ip, Port: dnsPort}
err = syscall.Connect(socketFd, &sockAddr)
if err != nil {
return nil, err
}
fd := uintptr(socketFd)
file := os.NewFile(fd, "")
defer file.Close()
// return a copy of the network connection
// represented by file
fileConn, err := net.FileConn(file)
if err != nil {
log.Printf("Error returning a copy of the network connection: %v", err)
return nil, err
}
setQueryTimeouts(fileConn)
//log.Printf("performing dns lookup...!!")
result, err := DnsLookup(host, fileConn)
if err != nil {
log.Printf("Error doing DNS resolution: %v", err)
return nil, err
}
ipAddr, err := result.PickRandomIP()
if err != nil {
log.Printf("No IP address available: %v", err)
return nil, err
}
return &net.TCPAddr{IP: ipAddr, Port: port}, nil
}
func Dial(network, addr string, timeout time.Duration) (net.Conn, error) {
ctx, _ := context.WithTimeout(context.Background(), timeout)
return DialContext(ctx, network, addr)
}
// Dial creates a new protected connection, it assumes that the address has
// already been resolved to an IPv4 address.
// - syscall API calls are used to create and bind to the
// specified system device (this is primarily
// used for Android VpnService routing functionality)
func DialContext(ctx context.Context, network, addr string) (net.Conn, error) {
host, port, err := SplitHostPort(addr)
if err != nil {
return nil, err
}
conn := &ProtectedConn{}
conn.ProtectedConnBase.port = port
// do DNS query
IPAddr := net.ParseIP(host)
if IPAddr == nil {
log.Printf("Couldn't parse IP address %v while port:%d", host, port)
return nil, err
}
conn.ip = IPAddr
select {
case <-ctx.Done():
return nil, ctx.Err()
default:
}
family := syscall.AF_INET
if IPAddr.To4() == nil {
family = syscall.AF_INET6
}
//copy(conn.ip[:], IPAddr.To4())
var socketFd int
//var err error
switch network {
case "udp", "udp4", "udp6":
socketFd, err = syscall.Socket(family, syscall.SOCK_DGRAM, 0)
default:
socketFd, err = syscall.Socket(family, syscall.SOCK_STREAM, syscall.IPPROTO_TCP)
}
if nil == err && SocketMark > 0 {
err = syscall.SetsockoptInt(socketFd, syscall.SOL_SOCKET, SO_MARK, SocketMark)
}
if err != nil {
log.Printf("Could not create socket: %v", err)
if socketFd > 0 {
syscall.Close(socketFd)
}
return nil, err
}
syscall.SetsockoptInt(socketFd, syscall.SOL_SOCKET, syscall.SO_REUSEADDR, 1)
conn.socketFd = socketFd
defer conn.cleanup()
// Actually protect the underlying socket here
err = currentProtect(conn.socketFd)
if err != nil {
return nil, fmt.Errorf("Could not bind socket to system device: %v", err)
}
err = conn.connectSocket()
if err != nil {
log.Printf("Could not connect to %s socket: %v", addr, err)
return nil, err
}
// finally, convert the socket fd to a net.Conn
err = conn.convert()
if err != nil {
log.Printf("Error converting protected connection: %v", err)
return nil, err
}
//conn.Conn.SetDeadline(time.Now().Add(timeout))
return conn.Conn, nil
}
// converts the protected connection specified by
// socket fd to a net.Conn
func (conn *ProtectedConn) convert() error {
conn.mutex.Lock()
file := os.NewFile(uintptr(conn.socketFd), "")
// dup the fd and return a copy
fileConn, err := net.FileConn(file)
// closes the original fd
file.Close()
conn.socketFd = socketError
if err != nil {
conn.mutex.Unlock()
return err
}
conn.Conn = fileConn
conn.mutex.Unlock()
return nil
}
// Close is used to destroy a protected connection
func (conn *ProtectedConn) Close() (err error) {
conn.mutex.Lock()
defer conn.mutex.Unlock()
if !conn.isClosed {
conn.isClosed = true
if conn.Conn == nil {
if conn.socketFd == socketError {
err = nil
} else {
err = syscall.Close(conn.socketFd)
// update socket fd to socketError
// to make it explicit this connection
// has been closed
conn.socketFd = socketError
}
} else {
err = conn.Conn.Close()
}
}
return err
}
// configure DNS query expiration
func setQueryTimeouts(c net.Conn) {
now := time.Now()
c.SetReadDeadline(now.Add(readDeadline))
c.SetWriteDeadline(now.Add(writeDeadline))
}
// wrapper around net.SplitHostPort that also converts
// uses strconv to convert the port to an int
func SplitHostPort(addr string) (string, int, error) {
host, sPort, err := net.SplitHostPort(addr)
if err != nil {
log.Printf("Could not split network address: %v", err)
return "", 0, err
}
port, err := strconv.Atoi(sPort)
if err != nil {
log.Printf("No port number found %v", err)
return "", 0, err
}
return host, port, nil
}
func ListenUDP(network string, laddr *net.UDPAddr) (net.PacketConn, error) {
conn := &ProtectedPacketConn{}
conn.ProtectedConnBase.port = laddr.Port
family := syscall.AF_INET
if laddr.IP.To4() == nil {
family = syscall.AF_INET6
}
socketFd, err := syscall.Socket(family, syscall.SOCK_DGRAM, 0)
if nil == err && SocketMark > 0 {
err = syscall.SetsockoptInt(socketFd, syscall.SOL_SOCKET, SO_MARK, SocketMark)
}
if err != nil {
log.Printf("Could not create udp socket: %v", err)
if socketFd > 0 {
syscall.Close(socketFd)
}
return nil, err
}
conn.socketFd = socketFd
defer conn.cleanup()
// Actually protect the underlying socket here
err = currentProtect(conn.socketFd)
if err != nil {
return nil, fmt.Errorf("Could not bind socket to system device: %v", err)
}
err = conn.listenSocket(laddr.IP, laddr.Port, family == syscall.AF_INET6)
if err != nil {
log.Printf("Could not listen udp socket: %v", err)
return nil, err
}
// finally, convert the socket fd to a net.Conn
err = conn.convert()
if err != nil {
log.Printf("Error converting protected connection: %v", err)
return nil, err
}
return conn.PacketConn, nil
}
func DialUDP(network string, laddr, raddr *net.UDPAddr) (net.PacketConn, error) {
conn := &ProtectedPacketConn{}
conn.ProtectedConnBase.port = raddr.Port
conn.ip = raddr.IP
family := syscall.AF_INET
if raddr.IP.To4() == nil {
family = syscall.AF_INET6
}
socketFd, err := syscall.Socket(family, syscall.SOCK_DGRAM, 0)
if nil == err && SocketMark > 0 {
err = syscall.SetsockoptInt(socketFd, syscall.SOL_SOCKET, SO_MARK, SocketMark)
}
if err != nil {
log.Printf("Could not create udp socket: %v", err)
if socketFd > 0 {
syscall.Close(socketFd)
}
return nil, err
}
conn.socketFd = socketFd
defer conn.cleanup()
// Actually protect the underlying socket here
err = currentProtect(conn.socketFd)
if err != nil {
return nil, fmt.Errorf("Could not bind socket to system device: %v", err)
}
// if nil != laddr {
// conn.listenSocket(laddr.IP, laddr.Port, family == syscall.AF_INET6)
// }
err = conn.connectSocket()
if err != nil {
log.Printf("Could not connect to %s socket: %v", raddr, err)
return nil, err
}
// finally, convert the socket fd to a net.Conn
err = conn.convert()
if err != nil {
log.Printf("Error converting protected connection: %v", err)
return nil, err
}
//conn.Conn.SetDeadline(time.Now().Add(timeout))
return conn.PacketConn, nil
}