-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathinit.lua
356 lines (285 loc) · 7.81 KB
/
init.lua
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
--[[
MIT License
Copyright (c) 2022 nikneym
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
]]
local socket = require "socket"
local buffer = require "string.buffer"
local tls = require "mbedtls.ssl"
local picodnsOk, picodns = pcall(require, "picodns")
local co_is_yieldable = coroutine.isyieldable
local co_yield = coroutine.yield
local gettime = socket.gettime
local function read(h, n)
local data, err, part = h:receive(n)
if err == "closed" then
return error "closed"
end
if data or part then
return data or part
end
return ""
end
local function write(h, s)
local bytes, err = h:send(s)
if err == "closed" then
return error "closed"
end
if bytes ~= nil and err == nil then
return bytes
end
if not bytes or bytes < #s then
return 0, err
end
end
--- @class TLSSocket
--- @field handle userdata
--- @field readBuffer userdata
--- @field context userdata
local TLSSocket = {
version = "2.0.3",
cfg = tls.newconfig "tls-client"
}
TLSSocket.__index = TLSSocket
-- create a picodns resolver if exists
if picodnsOk then
TLSSocket.resolver = picodns.newResolver()
end
--- Creates a new `TLSSocket` object.
--- @return TLSSocket
function TLSSocket.new()
local handle = socket.tcp()
return setmetatable({
handle = handle,
readBuffer = buffer.new(8192),
context = tls.newcontext(TLSSocket.cfg, read, write, handle),
}, TLSSocket)
end
function TLSSocket:__tostring()
return tostring(self.handle)
end
--- Closes the socket and releases the TLS context and bytes buffer.
function TLSSocket:close()
self.readBuffer:free()
self.context:reset()
self.handle:close()
end
local function is_ipv4(host)
return host:find("^%d+%.%d+%.%d+%.%d+$")
end
local function is_ipv6(host)
return host:find("^[%a%d]+%:?[%a%d]+%:?[%a%d]+%:?[%a%d]+%:?[%a%d]+%:?[%a%d]+%:?[%a%d]+%:?[%a%d]+%:?$")
end
--- Connects the socket to the given host and port.
--- @param host string
--- @param port? number 443 is default
--- @return boolean ok
--- @return string|nil err
function TLSSocket:connect(host, port)
assert(type(host) == "string", "host name is not specified")
-- for host name verification
self.context:sethostname(host)
-- if called in a coroutine, make this socket non-blocking
if co_is_yieldable() then
if self.handle:gettimeout() ~= 0 then
self.handle:settimeout(0)
end
end
local hostCheck = host == "localhost"
or is_ipv4(host)
or is_ipv6(host)
if not hostCheck then
-- resolve host address with picodns if exists
if picodnsOk then
local answers, err = TLSSocket.resolver:query(host)
if not answers then
return false, "no such domain"
end
host = answers[1].content
end
end
local startTime = gettime()
repeat
local ok, err = self.handle:connect(host, port or 443)
if gettime() - startTime >= 3 then
return false, "connection failed"
end
if co_is_yieldable() then
co_yield()
end
until ok == 1
return true, nil
end
--- Sends a message through a socket.
--- @param str any
--- @return number|nil bytesWritten
--- @return string|nil err
function TLSSocket:send(str)
assert(str, "a buffer must be provided to send")
if co_is_yieldable() then
local len = #str
repeat
local bytesWritten, err = self.context:write(str)
if err == "closed" then
return nil, "connection closed"
end
co_yield()
until bytesWritten == len
return len, nil
end
return self.context:write(str)
end
local function read_by_length(self, length)
-- got enough bytes in the buffer
if #self.readBuffer >= length then
return self.readBuffer:get(length), nil
end
-- not enough bytes, receive 'till get the full
local total = length
repeat
local msg, err = self.context:read(length - #self.readBuffer)
if err == "closed" then
return nil, "connection closed"
end
if msg then
self.readBuffer:put(msg)
end
if co_is_yieldable() then
co_yield()
end
until #self.readBuffer >= length
return self.readBuffer:get(total), nil
end
local function read_line_zero_buffer(self)
local msg, err
repeat
msg, err = self.context:read(8192)
if err == "closed" then
return nil, "connection closed"
end
if co_is_yieldable() then
co_yield()
end
until msg
local msgLen = #msg
-- temporary buffer for searching line
local tempBuffer = buffer.new(msgLen)
tempBuffer:set(msg)
local ptr = tempBuffer:ref()
local line
for i = 0, msgLen do
-- found a line feed '\n'
if ptr[i] == 10 then
if ptr[i - 1] == 13 then -- '\r'
line = tempBuffer:get(i - 1)
tempBuffer:skip(2)
break
end
line = tempBuffer:get(i)
tempBuffer:skip(1)
break
end
end
-- put the rest back to buffer
self.readBuffer:put(tempBuffer)
tempBuffer:free()
if line then
return line, nil
end
return nil, "line not found"
end
local function read_line_filled_buffer(self)
local ptr = self.readBuffer:ref()
local line
for i = 0, #self.readBuffer do
if ptr[i] == 10 then
if ptr[i - 1] == 13 then
line = self.readBuffer:get(i - 1)
self.readBuffer:skip(2)
break
end
line = self.readBuffer:get(i)
self.readBuffer:skip(1)
break
end
end
if line then
return line, nil
end
-- TODO: optimize and cleanup
local msg, err
repeat
msg, err = self.context:read(8192)
if err == "closed" then
return nil, "connection closed"
end
if co_is_yieldable() then
co_yield()
end
until msg
self.readBuffer:put(msg)
return nil, "failed to read line"
end
local function read_by_line(self)
local bufferLen = #self.readBuffer
-- got bytes in the buffer
if bufferLen > 0 then
local line, err = read_line_filled_buffer(self)
if err then
return read_by_line(self)
end
return line, nil
end
-- no bytes in the buffer, let's read some
if bufferLen == 0 then
local line, err = read_line_zero_buffer(self)
if err then
return read_by_line(self)
end
return line, nil
end
end
local function read_all(self)
-- reads 'till close err is received
repeat
local msg, err = self.context:read(8192)
if msg then
self.readBuffer:put(msg)
end
if co_is_yieldable() then
co_yield()
end
until err == "closed"
return self.readBuffer:tostring(), nil
end
--- Receives `x` bytes or a line `*l` or the full buffer `*a` from the socket.
--- @param pattern? number|string Receives a line by default
--- @return string|nil message
--- @return string|nil err
function TLSSocket:receive(pattern)
pattern = pattern or "*l"
if type(pattern) == "number" then
return read_by_length(self, pattern)
end
if pattern == "*l" then
return read_by_line(self)
end
if pattern == "*a" then
return read_all(self)
end
end
return TLSSocket