Repository navigation
Expand file tree
/
Copy pathsocks.lua
More file actions
379 lines (359 loc) · 10.6 KB
/
Copy pathsocks.lua
File metadata and controls
379 lines (359 loc) · 10.6 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
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
--[[
This module implements a subset of SOCKS as defined in RFC 1928.
SOCKS5 has different authentication mechanisms,
currently this code only supports username+password auth (defined in RFC 1929).
URI format is taken from curl:
- socks5:// is SOCKS5, resolving the authority locally
- socks5h:// is SOCKS5, but let the proxy resolve the hostname
]]
local cqueues = require "cqueues"
local monotime = cqueues.monotime
local ca = require "cqueues.auxlib"
local ce = require "cqueues.errno"
local cs = require "cqueues.socket"
local spack = string.pack or require "compat53.string".pack -- luacheck: ignore 143
local sunpack = string.unpack or require "compat53.string".unpack -- luacheck: ignore 143
local IPv4 = require "lpeg_patterns.IPv4"
local IPv6 = require "lpeg_patterns.IPv6"
local uri_patts = require "lpeg_patterns.uri"
local http_util = require "http.util"
local EOF = require "lpeg".P(-1)
local IPv4address = require "lpeg_patterns.IPv4".IPv4address
local IPv6address = require "lpeg_patterns.IPv6".IPv6address
local IPaddress = (IPv4address + IPv6address) * EOF
local socks_methods = {}
local socks_mt = {
__name = "http.socks";
__index = socks_methods;
}
local function onerror(socket, op, why, lvl) -- luacheck: ignore 212
return string.format("%s: %s", op, ce.strerror(why)), why
end
local function new()
return setmetatable({
version = 5;
socket = nil;
family = nil;
host = nil;
port = nil;
needs_resolve = false;
available_auth_methods = { "\0", ["\0"] = true; };
username = nil;
password = nil;
dst_family = nil;
dst_host = nil;
dst_port = nil;
}, socks_mt)
end
local function connect(socks_uri)
if type(socks_uri) == "string" then
socks_uri = assert(uri_patts.uri:match(socks_uri), "invalid URI")
end
local self = new()
if socks_uri.scheme == "socks5" then
self.needs_resolve = true
elseif socks_uri.scheme ~= "socks5h" then
error("only SOCKS5 proxys supported")
end
assert(socks_uri.path == nil, "path not expected")
local username, password
if socks_uri.userinfo then
username, password = socks_uri.userinfo:match("^([^:]*):(.*)$")
if username == nil then
error("invalid username/password format")
end
end
self.host = socks_uri.host
self.port = socks_uri.port or 1080
if username then
self:add_username_password_auth(username, password)
end
return self
end
local function fdopen(socket)
local self = new()
socket:onerror(onerror)
self.socket = socket
return self
end
function socks_methods:clone()
if self.socket then
error("cannot clone live http.socks object")
end
local clone = new()
clone.family = self.family
clone.host = self.host
clone.port = self.port
clone.needs_resolve = self.needs_resolve
if self.username then
clone:add_username_password_auth(self.username, self.password)
end
return clone
end
function socks_methods:add_username_password_auth(username, password)
self.username = http_util.decodeURIComponent(username)
self.password = http_util.decodeURIComponent(password)
if not self.available_auth_methods["\2"] then
table.insert(self.available_auth_methods, "\2")
self.available_auth_methods["\2"] = true
end
return true
end
-- RFC 1929
local function username_password_auth(self, deadline)
do
local data = spack("Bs1s1", 1, self.username, self.password)
local ok, err, errno = self.socket:xwrite(data, "bn", deadline and deadline-monotime())
if not ok then
return nil, err, errno
end
end
do
local version, err, errno = self.socket:xread(1, "b", deadline and deadline-monotime())
if not version then
if err == nil then
return nil, "username_password_auth: "..ce.strerror(ce.EPIPE), ce.EPIPE
end
return nil, err, errno
end
if version ~= "\1" then
return nil, "username_password_auth: invalid username/password auth version", ce.EILSEQ
end
end
do
local ok, err, errno = self.socket:xread(1, "b", deadline and deadline-monotime())
if not ok then
if err == nil then
return nil, "username_password_auth: "..ce.strerror(ce.EPIPE), ce.EPIPE
end
return nil, err, errno
end
if ok ~= "\0" then
return nil, "username_password_auth: "..ce.strerror(ce.EACCES), ce.EACCES
end
end
return true
end
function socks_methods:negotiate(host, port, timeout)
local deadline = timeout and monotime()+timeout
assert(host, "host expected")
port = assert(tonumber(port), "numeric port expected")
if self.socket == nil then
assert(self.host)
local socket, err, errno = ca.fileresult(cs.connect {
family = self.family;
host = self.host;
port = self.port;
sendname = false;
nodelay = true;
})
if socket == nil then
return nil, err, errno
end
socket:onerror(onerror)
self.socket = socket
end
local ip = IPaddress:match(host)
if self.needs_resolve and not ip then
-- Waiting on https://github.com/wahern/cqueues/issues/164
error("NYI: need to resolve locally")
end
do
local data = "\5"..string.char(#self.available_auth_methods)..table.concat(self.available_auth_methods)
local ok, err, errno = self.socket:xwrite(data, "bn", deadline and deadline-monotime())
if not ok then
return nil, err, errno
end
end
do
local byte, err, errno = self.socket:xread(1, "b", deadline and deadline-monotime())
if not byte then
if err == nil then
return nil, "socks:negotiate: "..ce.strerror(ce.EPIPE), ce.EPIPE
end
return nil, err, errno
elseif byte ~= "\5" then
return nil, "socks:negotiate: not SOCKS5", ce.EILSEQ
end
end
local auth_method do
local err, errno
auth_method, err, errno = self.socket:xread(1, "b", deadline and deadline-monotime())
if not auth_method then
if err == nil then
return nil, "socks:negotiate: "..ce.strerror(ce.EPIPE), ce.EPIPE
end
return nil, err, errno
end
if self.available_auth_methods[auth_method] == nil then
return nil, "socks:negotiate: unknown authentication method", ce.EILSEQ
end
end
if auth_method == "\0" then -- luacheck: ignore 542
-- do nothing
elseif auth_method == "\2" then
local ok, err, errno = username_password_auth(self, deadline)
if not ok then
return nil, err, errno
end
else
error("unreachable") -- implies `available_auth_methods` was edited while this was in progress
end
do
local data
if getmetatable(ip) == IPv4.IPv4_mt then
data = spack(">BBx Bc4I2", 5, 1, 1, ip:binary(), port)
elseif getmetatable(ip) == IPv6.IPv6_mt then
data = spack(">BBx Bc16I2", 5, 1, 4, ip:binary(), port)
else -- domain name
data = spack(">BBx Bs1I2", 5, 1, 3, host, port)
end
local ok, err, errno = self.socket:xwrite(data, "bn", deadline and deadline-monotime())
if not ok then
return nil, err, errno
end
end
do
local byte, err, errno = self.socket:xread(1, "b", deadline and deadline-monotime())
if not byte then
if err == nil then
return nil, "socks:negotiate: "..ce.strerror(ce.EPIPE), ce.EPIPE
end
return nil, err, errno
elseif byte ~= "\5" then
return nil, "socks:negotiate: not SOCKS5", ce.EILSEQ
end
end
do
local code, err, errno = self.socket:xread(1, "b", deadline and deadline-monotime())
if not code then
if err == nil then
return nil, "socks:negotiate: "..ce.strerror(ce.EPIPE), ce.EPIPE
end
return nil, err, errno
elseif code ~= "\0" then
local num_code = code:byte()
if num_code == 1 then
err = "general SOCKS server failure"
elseif num_code == 2 then
err = "connection not allowed by ruleset"
errno = ce.EACCES
elseif num_code == 3 then
err = "Network unreachable"
errno = ce.ENETUNREACH
elseif num_code == 4 then
err = "Host unreachable"
errno = ce.EHOSTUNREACH
elseif num_code == 5 then
err = "Connection refused"
errno = ce.ECONNREFUSED
elseif num_code == 6 then
err = "TTL expired"
errno = ce.ETIMEDOUT
elseif num_code == 7 then
err = "Command not supported"
errno = ce.EOPNOTSUPP
elseif num_code == 8 then
err = "Address type not supported"
errno = ce.EAFNOSUPPORT
else
err = "Unknown code"
errno = ce.PROTO
end
return nil, string.format("socks:negotiate: remote error %d: %s", num_code, err), errno
end
end
do
local byte, err, errno = self.socket:xread(1, "b", deadline and deadline-monotime())
if not byte then
if err == nil then
return nil, "socks:negotiate: "..ce.strerror(ce.EPIPE), ce.EPIPE
end
return nil, err, errno
elseif byte ~= "\0" then
return nil, "socks:negotiate: reserved field set to non-zero", ce.EILSEQ
end
end
local dst_family, dst_host, dst_port do
local atype, err, errno = self.socket:xread(1, "b", deadline and deadline-monotime())
if not atype then
if err == nil then
return nil, "socks:negotiate: "..ce.strerror(ce.EPIPE), ce.EPIPE
end
return nil, err, errno
end
if atype == "\1" then
local ipv4
ipv4, err, errno = self.socket:xread(4, "b", deadline and deadline-monotime())
if not ipv4 or #ipv4 < 4 then
if err == nil then
return nil, "socks:negotiate: "..ce.strerror(ce.EPIPE), ce.EPIPE
end
return nil, err, errno
end
dst_family = cs.AF_INET
dst_host = string.format("%d.%d.%d.%d", ipv4:byte(1, 4))
elseif atype == "\4" then
local ipv6
ipv6, err, errno = self.socket:xread(16, "b", deadline and deadline-monotime())
if not ipv6 or #ipv6 < 16 then
if err == nil then
return nil, "socks:negotiate: "..ce.strerror(ce.EPIPE), ce.EPIPE
end
return nil, err, errno
end
dst_family = cs.AF_INET6
dst_host = string.format("%02x%02x:%02x%02x:%02x%02x:%02x%02x:%02x%02x:%02x%02x:%02x%02x:%02x%02x",
ipv6:byte(1, 16))
elseif atype == "\3" then
local len
len, err, errno = self.socket:xread(1, "b", deadline and deadline-monotime())
if not len then
if err == nil then
return nil, "socks:negotiate: "..ce.strerror(ce.EPIPE), ce.EPIPE
end
return nil, err, errno
end
dst_family = cs.AF_UNSPEC
len = string.byte(len)
dst_host, err, errno = self.socket:xread(len, "b", deadline and deadline-monotime())
if not dst_host or #dst_host < len then
if err == nil then
return nil, "socks:negotiate: "..ce.strerror(ce.EPIPE), ce.EPIPE
end
return nil, err, errno
end
else
return nil, "socks:negotiate: unknown address type", ce.EAFNOSUPPORT
end
end
do
local dst_port_bin, err, errno = self.socket:xread(2, "b", deadline and deadline-monotime())
if not dst_port_bin then
return nil, err or ce.EPIPE, errno
end
dst_port = sunpack(">I2", dst_port_bin)
end
self.dst_family = dst_family
self.dst_host = dst_host
self.dst_port = dst_port
return true
end
function socks_methods:close()
if self.socket then
self.socket:close()
end
end
function socks_methods:take_socket()
local s = self.socket
if s == nil then
-- already taken
return nil
end
self.socket = nil
return s
end
return {
connect = connect;
fdopen = fdopen;
}