-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutil.go
235 lines (208 loc) · 4.31 KB
/
util.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
package socksy5
import (
"encoding/binary"
"fmt"
"io"
"net"
"net/netip"
"strconv"
)
func err(err error, a ...any) error {
if err == nil && len(a) == 0 {
return nil
}
if err == nil {
return fmt.Errorf("%s", fmt.Sprint(a...))
}
if len(a) == 0 {
return err
}
return fmt.Errorf("%s: %w", fmt.Sprint(a...), err)
}
func readByte(reader io.Reader) (byte, error) {
buf := make([]byte, 1)
if _, err := io.ReadFull(reader, buf); err != nil {
return 0, err
}
return buf[0], nil
}
func readUInt16BigEndian(reader io.Reader) (uint16, error) {
buf := make([]byte, 2)
if _, err := io.ReadFull(reader, buf); err != nil {
return 0, err
}
return binary.BigEndian.Uint16(buf), nil
}
func conn2str(conn net.Conn) string {
laddr := conn.LocalAddr()
raddr := conn.RemoteAddr()
s := ""
if laddr == nil {
s += "<nil>"
} else {
s += laddr.String()
}
s += " -> "
if raddr == nil {
s += "<nil>"
} else {
s += raddr.String()
}
return s
}
func parseUint16(str string) (i uint16, err error) {
d, err := strconv.Atoi(str)
if err != nil {
return
}
if d < 0x00 || d > 0xFFFF {
return 0, fmt.Errorf("%d is not uint16", d)
}
return uint16(d), err
}
func cmd2str(cmd byte) string {
switch cmd {
case CmdCONNECT:
return "CONNECT"
case CmdBIND:
return "BIND"
case CmdASSOC:
return "UDP ASSOCIATE"
default:
return "CMD " + fmt.Sprintf("0x%02X", cmd)
}
}
func method2Str(method byte) string {
switch method {
case MethodNoAuth:
return "no auth"
case MethodGSSAPI:
return "GSS API"
case MethodUsrPwd:
return "USR/PWD"
case MethodCHAP:
return "CHAP"
case byte(0x04):
return "unassigned 0x04"
case MethodCRAM:
return "CRAM"
case MethodSSL:
return "SSL"
case MethodNDS:
return "NDS"
case MethodMAF:
return "MAF"
case MethodJRB:
return "JRB"
case MethodNoAccepted:
return "no accepted"
}
if method >= 0x0A && method <= 0x7F {
return fmt.Sprintf("unassigned 0x%02X", method)
} else {
return fmt.Sprintf("private 0x%02X", method)
}
}
func rep2str(rep byte) string {
switch rep {
case RepSucceeded:
return "succeeded"
case RepGeneralFailure:
return "general failure"
case RepConnNotAllowedByRuleset:
return "connection not allowed by ruleset"
case RepNetworkUnreachable:
return "Network unreachable"
case RepHostUnreachable:
return "Host unreachable"
case RepConnRefused:
return "Connection refused"
case RepTtlExpired:
return "TTL expired"
case RepCmdNotSupported:
return "Command not supported"
case RepAddrTypeNotSupported:
return "Address type not supported"
default:
return fmt.Sprintf("unassigned 0x%02X", rep)
}
}
func relayAddr2str(craddr, claddr, hladdr, hraddr net.Addr) string {
return fmt.Sprintf(
"relay [client %s]<->[%s server %s]<->[%s host]",
craddr, claddr,
hladdr, hraddr,
)
}
func relay2str(cConn net.Conn, hConn net.Conn) string {
return relayAddr2str(cConn.RemoteAddr(), cConn.LocalAddr(), hConn.LocalAddr(), hConn.RemoteAddr())
}
func isByteOneOf(a byte, bytes ...byte) bool {
for _, v := range bytes {
if a == v {
return true
}
}
return false
}
func cpySlice(src []byte) (result []byte) {
result = make([]byte, len(src))
copy(result, src)
return
}
func listenMultipleTCP(ips []net.IP, port string) (ls []net.Listener, err error) {
result := make([]net.Listener, 0, 4)
for _, ip := range ips {
var l net.Listener
l, err := net.Listen("tcp", net.JoinHostPort(ip.String(), port))
if err != nil {
break
}
if port == "0" {
_, port, _ = net.SplitHostPort(l.Addr().String())
}
result = append(result, l)
}
if err != nil {
for _, l := range result {
l.Close()
}
return nil, err
}
return result, nil
}
func parseHostToAddrPort(host string) *AddrPort {
result := new(AddrPort)
if ipAddr, err := netip.ParseAddr(host); err == nil {
if ipAddr.Is4() {
result.Type = ATYPV4
} else {
result.Type = ATYPV6
}
raw, _ := ipAddr.MarshalBinary()
result.Addr = cpySlice(raw)
} else {
result.Type = ATYPDOMAIN
result.Addr = []byte(host)
}
return result
}
type sliceReader struct {
bytes []byte
n int
}
func newSliceReader(b []byte) *sliceReader {
bytes := make([]byte, len(b))
copy(bytes, b)
return &sliceReader{
bytes: bytes,
}
}
func (r *sliceReader) Read(p []byte) (n int, err error) {
if len(r.bytes[r.n:]) == 0 {
return 0, io.EOF
}
n = copy(p, r.bytes[r.n:])
r.n += n
return n, nil
}