-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrequest.go
More file actions
139 lines (123 loc) · 3.31 KB
/
request.go
File metadata and controls
139 lines (123 loc) · 3.31 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
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
package socks5
import (
"encoding/binary"
"io"
"net"
)
type ClientRequestMessage struct {
Cmd Command
AddressType AddressType
Address string
Port uint16
}
type Command = byte
const (
CommandConnect Command = 0x01
CommandBind Command = 0x02
CommandUDP Command = 0x03
)
type AddressType = byte
const (
TypeIPv4 AddressType = 0x01
TypeDomain AddressType = 0x03
TypeIPv6 AddressType = 0x04
)
/*
REP Reply field:
o X'00' succeeded
o X'01' general SOCKS server failure
o X'02' connection not allowed by ruleset
o X'03' Network unreachable
o X'04' Host unreachable
o X'05' Connection refused
o X'06' TTL expired
o X'07' Command not supported
o X'08' Address type not supported
o X'09' to X'FF' unassigned
*/
type ReplyType = byte
const (
ReplySuccess ReplyType = iota
ReplyServerFailure
ReplyConnectionNotAllowed
ReplyNetworkUnreachable
ReplyHostUnreachable
ReplyConnectionRefused
ReplyTTLExpired
ReplyCommandNotSupported
ReplyAddressTypeNotSupported
)
func NewClientRequestMessage(conn io.Reader) (*ClientRequestMessage, error) {
// Read Version, CMD, RSV, AddressType
buff := make([]byte, 4)
_, err := io.ReadFull(conn, buff)
if err != nil {
return nil, err
}
version, command, reserved, addrType := buff[0], buff[1], buff[2], buff[3]
if version != SOCKS5Version {
return nil, ErrVersionNotSupported
}
if command != CommandConnect && command != CommandBind && command != CommandUDP {
return nil, ErrCommandNotSupported
}
if reserved != ReservedField {
return nil, ErrInvalidReservedField
}
if addrType != TypeIPv4 && addrType != TypeDomain && addrType != TypeIPv6 {
return nil, ErrAddressTypeNotSupported
}
message := &ClientRequestMessage{
Cmd: command,
AddressType: addrType,
}
// 解析 address
switch addrType {
case TypeIPv6:
buff = make([]byte, 6)
// 继续执行
fallthrough
case TypeIPv4:
// 复用了 buff,因为 ipv4 也是 4 个字节
if _, err := io.ReadFull(conn, buff); err != nil {
return nil, err
}
ip := net.IP(buff)
message.Address = ip.String()
case TypeDomain:
// 读取 1 个字节, 表示域名的长度
if _, err := io.ReadFull(conn, buff[:1]); err != nil {
return nil, err
}
domainLength := buff[0]
domainBuff := make([]byte, domainLength)
if _, err := io.ReadFull(conn, domainBuff); err != nil {
return nil, err
}
message.Address = string(domainBuff)
}
// 读取剩下两个字节,表示端口
if _, err := io.ReadFull(conn, buff[:2]); err != nil {
return nil, err
}
message.Port = binary.BigEndian.Uint16(buff[:2])
return message, nil
}
func WriteRequestSuccessMessage(conn io.Writer, ip net.IP, addressType AddressType, port uint16) error {
if _, err := conn.Write([]byte{SOCKS5Version, ReplySuccess, ReservedField, addressType}); err != nil {
return err
}
// Write bind IP(IPv4/IPv6)
if _, err := conn.Write(ip); err != nil {
return err
}
// Write bind port
buf := make([]byte, 2)
binary.BigEndian.PutUint16(buf, port)
_, err := conn.Write(buf)
return err
}
func WriteRequestFailureMessage(conn io.Writer, replyType ReplyType) error {
_, err := conn.Write([]byte{SOCKS5Version, replyType, ReservedField, TypeIPv4, 0, 0, 0, 0, 0})
return err
}