forked from quantum1423-dustbin/kirisurf-legacy
-
Notifications
You must be signed in to change notification settings - Fork 0
/
socksproxy.go
86 lines (82 loc) · 2 KB
/
socksproxy.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
package main
import (
"errors"
"fmt"
"io"
)
func socks5_handshake(conn io.ReadWriteCloser) (string, error) {
scratch := make([]byte, 128)
_, err := io.ReadFull(conn, scratch[:2])
if err != nil {
return "", err
}
if scratch[0] != 5 {
return "", errors.New("SOCKS version mismatch")
}
i := scratch[1]
_, err = io.ReadFull(conn, scratch[:i])
if err != nil {
return "", errors.New("Couldn't read auths")
}
scratch[0] = 0x05
scratch[1] = 0x00
_, err = conn.Write(scratch[:2])
if err != nil {
return "", errors.New("Couldn't write auths")
}
_, err = io.ReadFull(conn, scratch[:4])
if err != nil {
return "", errors.New("Couldn't read conn type")
}
conntype := scratch[3]
var thing []byte
var toret string
if conntype == 0x01 {
_, err = io.ReadFull(conn, scratch[:4])
if err != nil {
return "", errors.New("Couldn't read IP address")
}
thing = make([]byte, 4)
copy(thing, scratch)
toret = fmt.Sprintf("%d.%d.%d.%d", scratch[0], scratch[1],
scratch[2], scratch[3])
} else if conntype == 0x03 {
_, err = io.ReadFull(conn, scratch[:1])
if err != nil {
return "", errors.New("Couldn't read domain length")
}
length := scratch[0]
_, err = io.ReadFull(conn, scratch[:length])
if err != nil {
return "", errors.New("Couldn't read domain")
}
thing = make([]byte, length+1)
thing[0] = byte(length)
copy(thing[1:], scratch)
toret = string(thing[1:])
} else {
DEBUG("What? conntype=0x%x??", conntype)
return "", errors.New("conntype mismatch")
}
_, err = io.ReadFull(conn, scratch[:2])
if err != nil {
return "", errors.New("Couldn't read port")
}
portnum := int(scratch[0])*256 + int(scratch[1])
toret = fmt.Sprintf("%s:%d", toret, portnum)
scratch[0] = 0x05
scratch[1] = 0x00
scratch[2] = 0x00
scratch[3] = 0x01
scratch[4] = 0x00
scratch[5] = 0x00
scratch[6] = 0x00
scratch[7] = 0x00
scratch[8] = 0x00
scratch[9] = 0x00
_, err = conn.Write(scratch[:10])
if err != nil {
return "", errors.New("Couldn't complete handshake")
}
return toret, nil
}