tlshelper.lua
2.3 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
local socket = require "http.sockethelper"
local c = require "ltls.c"
local tlshelper = {}
function tlshelper.init_requestfunc(fd, tls_ctx)
local readfunc = socket.readfunc(fd)
local writefunc = socket.writefunc(fd)
return function ()
local ds1 = tls_ctx:handshake()
writefunc(ds1)
while not tls_ctx:finished() do
local ds2 = readfunc()
local ds3 = tls_ctx:handshake(ds2)
if ds3 then
writefunc(ds3)
end
end
end
end
function tlshelper.init_responsefunc(fd, tls_ctx)
local readfunc = socket.readfunc(fd)
local writefunc = socket.writefunc(fd)
return function ()
while not tls_ctx:finished() do
local ds1 = readfunc()
local ds2 = tls_ctx:handshake(ds1)
if ds2 then
writefunc(ds2)
end
end
local ds3 = tls_ctx:write()
writefunc(ds3)
end
end
function tlshelper.closefunc(tls_ctx)
return function ()
tls_ctx:close()
end
end
function tlshelper.readfunc(fd, tls_ctx)
local readfunc = socket.readfunc(fd)
local read_buff = ""
return function (sz)
if not sz then
local s = ""
if #read_buff == 0 then
local ds = readfunc(sz)
s = tls_ctx:read(ds)
end
s = read_buff .. s
read_buff = ""
return s
else
while #read_buff < sz do
local ds = readfunc()
local s = tls_ctx:read(ds)
read_buff = read_buff .. s
end
local s = string.sub(read_buff, 1, sz)
read_buff = string.sub(read_buff, sz+1, #read_buff)
return s
end
end
end
function tlshelper.writefunc(fd, tls_ctx)
local writefunc = socket.writefunc(fd)
return function (s)
local ds = tls_ctx:write(s)
return writefunc(ds)
end
end
function tlshelper.readallfunc(fd, tls_ctx)
local readfunc = socket.readfunc(fd)
return function ()
local ds = socket.readall(fd)
local s = tls_ctx:read(ds)
return s
end
end
function tlshelper.newctx()
return c.newctx()
end
function tlshelper.newtls(method, ssl_ctx)
return c.newtls(method, ssl_ctx)
end
return tlshelper