forked from yinqiwen/gsnova
-
Notifications
You must be signed in to change notification settings - Fork 0
/
net.go
462 lines (414 loc) · 11.9 KB
/
net.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
461
462
package helper
import (
"bufio"
"bytes"
"errors"
"fmt"
"io"
"log"
"math"
"net"
"net/http"
"net/url"
"strconv"
"strings"
"time"
"github.com/getlantern/netx"
)
var ErrTLSIncomplete = errors.New("TLS header incomplete")
var ErrNoSNI = errors.New("No SNI in protocol")
var ErrTLSClientHello = errors.New("Invalid tls client hello")
func TLSReplaceSNI(data []byte, sni string) ([]byte, string, error) {
name, offset, err := tlsParseSNI(data)
if nil == err {
newData := make([]byte, offset)
copy(newData, data[0:offset])
//newData := data[0:offset]
tailData := data[offset+2+len(name):]
sniData := make([]byte, 2+len(sni))
sniData[0] = uint8(uint16(len(sni)) >> 8)
sniData[1] = uint8(uint16(len(sni)) & 0xFF)
copy(sniData[2:], []byte(sni))
newData = append(newData, sniData...)
newData = append(newData, tailData...)
if name == sni {
return data, name, nil
}
return newData, name, nil
}
return data, name, err
}
func TLSParseSNI(data []byte) (string, error) {
name, _, err := tlsParseSNI(data)
return name, err
}
func tlsParseSNI(data []byte) (string, int, error) {
tlsHederLen := 5
if len(data) < tlsHederLen {
return "", 0, ErrTLSIncomplete
}
if (int(data[0])&0x80) != 0 && data[2] == 1 {
return "", 0, ErrNoSNI
}
tlsContentType := int(data[0])
if tlsContentType != 0x16 {
log.Printf("Invaid content type:%d with %v", tlsContentType, ErrTLSClientHello)
return "", 0, ErrTLSClientHello
}
tlsMajorVer := int(data[1])
tlsMinorVer := int(data[2])
if tlsMajorVer < 3 {
log.Printf("Invaid tls ver:%d with %v", tlsMajorVer, ErrNoSNI)
return "", 0, ErrNoSNI
}
tlsLen := (int(data[3]) << 8) + int(data[4]) + tlsHederLen
if tlsLen > len(data) {
return "", 0, ErrTLSIncomplete
}
//log.Printf("####TLS %d %d", tlsLen, len(data))
pos := tlsHederLen
if pos+1 > len(data) {
log.Printf("Less data 1 %v", ErrTLSClientHello)
return "", 0, ErrTLSClientHello
}
tlsHandshakeTypeClientHello := 0x01
if int(data[pos]) != tlsHandshakeTypeClientHello {
log.Printf("Not client hello type:%d with err:%v", data[pos], ErrTLSClientHello)
return "", 0, ErrTLSClientHello
}
/* Skip past fixed length records:
1 Handshake Type
3 Length
2 Version (again)
32 Random
to Session ID Length
*/
pos += 38
if pos+1 > len(data) {
log.Printf("Less data 2 with err:%v", ErrTLSClientHello)
return "", 0, ErrTLSClientHello
}
nextLen := int(data[pos])
pos = pos + 1 + nextLen
if pos+2 > len(data) {
log.Printf("Less data 3 with err:%v", ErrTLSClientHello)
return "", 0, ErrTLSClientHello
}
nextLen = (int(data[pos]) << 8) + int(data[pos+1])
pos = pos + 2 + nextLen
if pos+1 > len(data) {
log.Printf("Less data 4 with err:%v", ErrTLSClientHello)
return "", 0, ErrTLSClientHello
}
nextLen = int(data[pos])
pos = pos + 1 + nextLen
if pos == len(data) && tlsMajorVer == 3 && tlsMinorVer == 0 {
log.Printf("No sni in 3.0 %v", ErrNoSNI)
return "", 0, ErrNoSNI
}
if pos+2 > len(data) {
log.Printf("Less data 5 with err:%v", ErrTLSClientHello)
return "", 0, ErrTLSClientHello
}
nextLen = (int(data[pos]) << 8) + int(data[pos+1])
pos += 2
if pos+nextLen > len(data) {
log.Printf("Less data 6 with err:%v", ErrTLSClientHello)
return "", 0, ErrTLSClientHello
}
return parseExtension(data[pos:], pos)
}
func parseExtension(data []byte, offset int) (string, int, error) {
pos := 0
for (pos + 4) <= len(data) {
nextLen := (int(data[pos+2]) << 8) + int(data[pos+3])
if int(data[pos]) == 0x00 && int(data[pos+1]) == 0x00 {
if pos+4+nextLen > len(data) {
log.Printf("Less data 7 with err:%v", ErrTLSClientHello)
return "", 0, ErrTLSClientHello
}
offset = offset + pos + 4
return parseServerNameExtension(data[pos+4:], offset)
}
pos = pos + 4 + nextLen
}
if pos != len(data) {
log.Printf("Less data 8 with err:%v", ErrTLSClientHello)
return "", 0, ErrTLSClientHello
}
return "", 0, ErrNoSNI
}
func parseServerNameExtension(data []byte, offset int) (string, int, error) {
pos := 2
for pos+3 < len(data) {
nextLen := (int(data[pos+1]) << 8) + int(data[pos+2])
if pos+3+nextLen > len(data) {
log.Printf("Less data 9 with err:%v", ErrTLSClientHello)
return "", 0, ErrTLSClientHello
}
if int(data[pos]) == 0x00 {
offset = offset + pos + 1
name := make([]byte, nextLen)
copy(name, data[pos+3:])
return string(name), offset, nil
}
pos = pos + 3 + nextLen
}
if pos != len(data) {
log.Printf("Less data 10 with err:%v", ErrTLSClientHello)
return "", 0, ErrTLSClientHello
}
return "", 0, ErrNoSNI
}
func Long2IPv4(i uint64) string {
return fmt.Sprintf("%d.%d.%d.%d", (i>>24)&0xFF, (i>>16)&0xFF, (i>>8)&0xFF, i&0xFF)
}
func IPv42Int(ip string) (int64, error) {
addrArray := strings.Split(ip, ".")
var num int64
num = 0
for i := 0; i < len(addrArray); i++ {
power := 3 - i
if v, err := strconv.Atoi(addrArray[i]); nil != err {
return -1, err
} else {
num += (int64(v) % 256 * int64(math.Pow(float64(256), float64(power))))
}
}
return num, nil
}
var privateIPRanges [][]net.IP
func init() {
range1 := []net.IP{net.ParseIP("192.168.0.0"), net.ParseIP("192.168.255.255")}
range2 := []net.IP{net.ParseIP("172.16.0.0"), net.ParseIP("172.31.255.255")}
range3 := []net.IP{net.ParseIP("10.0.0.0"), net.ParseIP("10.255.255.255")}
privateIPRanges = append(privateIPRanges, range1)
privateIPRanges = append(privateIPRanges, range2)
privateIPRanges = append(privateIPRanges, range3)
}
func IsPrivateIP(ip string) bool {
if strings.EqualFold(ip, "localhost") {
return true
}
trial := net.ParseIP(ip)
if trial.To4() == nil {
return false
}
if strings.HasPrefix(ip, "127.0") {
return true
}
for _, r := range privateIPRanges {
if bytes.Compare(trial, r[0]) >= 0 && bytes.Compare(trial, r[1]) <= 0 {
return true
}
}
return false
}
func HTTPProxyConnect(proxyURL *url.URL, c net.Conn, addr string) error {
connReq, err := http.NewRequest("Connect", addr, nil)
if err != nil {
return err
}
err = connReq.Write(c)
if err != nil {
return err
}
connRes, err := http.ReadResponse(bufio.NewReader(c), connReq)
if err != nil {
var tmp bytes.Buffer
connReq.Write(&tmp)
log.Printf("CONNECT %v error with request %s", proxyURL, string(tmp.Bytes()))
return err
}
if nil != connRes.Body {
connRes.Body.Close()
}
if connRes.StatusCode >= 300 {
return fmt.Errorf("Invalid Connect response:%d", connRes.StatusCode)
}
return nil
}
func HTTPProxyDial(proxyURL string, addr string, timeout time.Duration) (net.Conn, error) {
u, err := url.Parse(proxyURL)
if nil != err {
return nil, err
}
c, err := netx.DialTimeout("tcp", u.Host, timeout)
if err != nil {
return nil, err
}
err = HTTPProxyConnect(u, c, addr)
if nil != err {
c.Close()
return nil, err
}
return c, nil
}
const socks5Version = 5
const (
socks5AuthNone = 0
socks5AuthPassword = 2
)
const socks5Connect = 1
const (
socks5IP4 = 1
socks5Domain = 3
socks5IP6 = 4
)
var socks5Errors = []string{
"",
"general failure",
"connection forbidden",
"network unreachable",
"host unreachable",
"connection refused",
"TTL expired",
"command not supported",
"address type not supported",
}
func Socks5ProxyConnect(proxyURL *url.URL, conn net.Conn, addr string) error {
host, portStr, err := net.SplitHostPort(addr)
if err != nil {
return err
}
port, err := strconv.Atoi(portStr)
if err != nil {
return errors.New("proxy: failed to parse port number: " + portStr)
}
if port < 1 || port > 0xffff {
return errors.New("proxy: port number out of range: " + portStr)
}
// the size here is just an estimate
buf := make([]byte, 0, 6+len(host))
buf = append(buf, socks5Version)
if nil != proxyURL.User && len(proxyURL.User.Username()) > 0 && len(proxyURL.User.Username()) < 256 {
buf = append(buf, 2 /* num auth methods */, socks5AuthNone, socks5AuthPassword)
} else {
buf = append(buf, 1 /* num auth methods */, socks5AuthNone)
}
if _, err := conn.Write(buf); err != nil {
return errors.New("proxy: failed to write greeting to SOCKS5 proxy at " + addr + ": " + err.Error())
}
if _, err := io.ReadFull(conn, buf[:2]); err != nil {
return errors.New("proxy: failed to read greeting from SOCKS5 proxy at " + addr + ": " + err.Error())
}
if buf[0] != 5 {
return errors.New("proxy: SOCKS5 proxy at " + addr + " has unexpected version " + strconv.Itoa(int(buf[0])))
}
if buf[1] == 0xff {
return errors.New("proxy: SOCKS5 proxy at " + addr + " requires authentication")
}
if buf[1] == socks5AuthPassword {
buf = buf[:0]
passwd, _ := proxyURL.User.Password()
buf = append(buf, 1 /* password protocol version */)
buf = append(buf, uint8(len(proxyURL.User.Username())))
buf = append(buf, proxyURL.User.Username()...)
buf = append(buf, uint8(len(passwd)))
buf = append(buf, passwd...)
if _, err := conn.Write(buf); err != nil {
return errors.New("proxy: failed to write authentication request to SOCKS5 proxy at " + addr + ": " + err.Error())
}
if _, err := io.ReadFull(conn, buf[:2]); err != nil {
return errors.New("proxy: failed to read authentication reply from SOCKS5 proxy at " + addr + ": " + err.Error())
}
if buf[1] != 0 {
return errors.New("proxy: SOCKS5 proxy at " + addr + " rejected username/password")
}
}
buf = buf[:0]
buf = append(buf, socks5Version, socks5Connect, 0 /* reserved */)
if ip := net.ParseIP(host); ip != nil {
if ip4 := ip.To4(); ip4 != nil {
buf = append(buf, socks5IP4)
ip = ip4
} else {
buf = append(buf, socks5IP6)
}
buf = append(buf, ip...)
} else {
if len(host) > 255 {
return errors.New("proxy: destination hostname too long: " + host)
}
buf = append(buf, socks5Domain)
buf = append(buf, byte(len(host)))
buf = append(buf, host...)
}
buf = append(buf, byte(port>>8), byte(port))
if _, err := conn.Write(buf); err != nil {
return errors.New("proxy: failed to write connect request to SOCKS5 proxy at " + addr + ": " + err.Error())
}
if _, err := io.ReadFull(conn, buf[:4]); err != nil {
return errors.New("proxy: failed to read connect reply from SOCKS5 proxy at " + addr + ": " + err.Error())
}
failure := "unknown error"
if int(buf[1]) < len(socks5Errors) {
failure = socks5Errors[buf[1]]
}
if len(failure) > 0 {
return errors.New("proxy: SOCKS5 proxy at " + addr + " failed to connect: " + failure)
}
bytesToDiscard := 0
switch buf[3] {
case socks5IP4:
bytesToDiscard = net.IPv4len
case socks5IP6:
bytesToDiscard = net.IPv6len
case socks5Domain:
_, err := io.ReadFull(conn, buf[:1])
if err != nil {
return errors.New("proxy: failed to read domain length from SOCKS5 proxy at " + addr + ": " + err.Error())
}
bytesToDiscard = int(buf[0])
default:
return errors.New("proxy: got unknown address type " + strconv.Itoa(int(buf[3])) + " from SOCKS5 proxy at " + addr)
}
if cap(buf) < bytesToDiscard {
buf = make([]byte, bytesToDiscard)
} else {
buf = buf[:bytesToDiscard]
}
if _, err := io.ReadFull(conn, buf); err != nil {
return errors.New("proxy: failed to read address from SOCKS5 proxy at " + addr + ": " + err.Error())
}
// Also need to discard the port number
if _, err := io.ReadFull(conn, buf[:2]); err != nil {
return errors.New("proxy: failed to read port from SOCKS5 proxy at " + addr + ": " + err.Error())
}
return nil
}
func Socks5ProxyDial(proxyURL string, addr string, timeout time.Duration) (net.Conn, error) {
u, err := url.Parse(proxyURL)
if nil != err {
return nil, err
}
c, err := netx.DialTimeout("tcp", u.Host, timeout)
if err != nil {
return nil, err
}
err = Socks5ProxyConnect(u, c, addr)
if nil != err {
c.Close()
return nil, err
}
return c, nil
}
var localIPv4 []string
func GetLocalIPv4() []string {
if len(localIPv4) > 0 {
return localIPv4
}
addrs, err := net.InterfaceAddrs()
if err != nil {
log.Printf("[ERROR]Failed to get local ip:%v", err)
return localIPv4
}
for _, a := range addrs {
if ipnet, ok := a.(*net.IPNet); ok && !ipnet.IP.IsLoopback() {
if ipnet.IP.To4() != nil {
localIPv4 = append(localIPv4, ipnet.IP.String())
}
}
}
return localIPv4
}