16.10.2017, 11:52
It's possible, but you cannot run it without a certificate. You can reuse web-server's self-signed certificate though. Here's a short example:
Code:
if not tcpserver then
require('copas')
sslparams = {
mode = 'server',
protocol = 'tlsv12',
key = '/etc/nginx_sha256.key',
certificate = '/etc/nginx_sha256.crt',
options = 'all',
}
-- connection handler
function connhandler(sock)
local ip, port = sock:getpeername()
sock = copas.wrap(sock, sslparams)
sock:dohandshake()
alert('[server] connection from %s:%d', ip, port)
-- main reader loop
while true do
-- wait for single line of data (until \n, \r is ignored)
local data, err = copas.receive(sock, '*l')
-- error while receiving
if err then
alert('[server] closed connection from %s:%d', ip, port)
return
end
-- handle data frame
if data == 'HELLO' then
sock:send('HELLO TO YOU\r\n')
sock:close()
end
end
end
-- bind to port 12345
tcpserver = socket.bind('*', 12345)
-- error while binding, try again later
if not tcpserver then
os.sleep(5)
error('[server] error: cannot bind')
end
-- set server connection handler
copas.addserver(tcpserver, connhandler)
end
copas.step(1)