Posts: 5
Threads: 2
Joined: Oct 2016
Reputation:
0
31.10.2016, 15:56
(This post was last modified: 31.10.2016, 16:01 by oyvindi .)
Hi,
anybody had any success creating an SSL server listener in LUA ?
There seems to be an issue with the server socket not being able to receive() from the client. Sending from server to client however, works just fine.
Note that I tried using it in a copas handler first, and then as a simple standalone luasec handler (running on my machine, not in LM)
Simple test code looks like this:
Code:
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
local socket =
require (
"socket" )
local ssl =
require (
"ssl" )
local params = {
mode =
"server" ,
protocol =
"tlsv1_2" ,
key =
"C:\\ .... \\serverkey.pem" ,
certificate =
"C:\\ .... \\server.pem" ,
cafile =
"C:\\ .... \\root.pem" ,
verify = {
"peer" ,
"fail_if_no_peer_cert" },
options =
"all" ,
}
local server =
socket.tcp ()
local ctx =
assert (
ssl.newcontext (
params ))
server :
setoption (
'reuseaddr' ,
true )
assert (
server :
bind (
"10.20.2.92" ,
2000 ) )
server :
listen ()
local peer =
server :
accept ()
peer =
assert (
ssl.wrap (
peer ,
ctx ) )
peer :
dohandshake ()
data =
peer :
receive ()
print (
"receieved: " ..
data )
peer :
send (
"Pong!\n" )
peer :
close ()
Posts: 8160
Threads: 43
Joined: Jun 2015
Reputation:
472
SSL server is not yet supported, we'll try to prepare an update this week though
Posts: 5
Threads: 2
Joined: Oct 2016
Reputation:
0
01.11.2016, 09:44
(This post was last modified: 01.11.2016, 12:40 by oyvindi .)
(01.11.2016, 09:30) admin Wrote: SSL server is not yet supported, we'll try to prepare an update this week though
Ok ! I assume it will be via luasec ?
I got my code working though, turned out to be a hickup on the client side.
Posts: 200
Threads: 60
Joined: Jun 2015
Reputation:
7
16.10.2017, 07:47
(This post was last modified: 16.10.2017, 09:37 by rocfusion .)
Hi Admin,
Is there any update on running an ssl server on the lm? Can this be done without a cert?
Thanks,
Roger
Posts: 8160
Threads: 43
Joined: Jun 2015
Reputation:
472
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:
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
if not tcpserver then
require (
'copas' )
sslparams = {
mode =
'server' ,
protocol =
'tlsv12' ,
key =
'/etc/nginx_sha256.key' ,
certificate =
'/etc/nginx_sha256.crt' ,
options =
'all' ,
}
function connhandler (
sock )
local ip ,
port =
sock :
getpeername ()
sock =
copas.wrap (
sock ,
sslparams )
sock :
dohandshake ()
alert (
'[server] connection from %s:%d' ,
ip ,
port )
while true do
local data ,
err =
copas.receive (
sock ,
'*l' )
if err then
alert (
'[server] closed connection from %s:%d' ,
ip ,
port )
return
end
if data ==
'HELLO' then
sock :
send (
'HELLO TO YOU\r\n' )
sock :
close ()
end
end
end
tcpserver =
socket.bind (
'*' ,
12345 )
if not tcpserver then
os.sleep (
5 )
error (
'[server] error: cannot bind' )
end
copas.addserver (
tcpserver ,
connhandler )
end
copas.step (
1 )
Posts: 200
Threads: 60
Joined: Jun 2015
Reputation:
7
Thanks Admin, that works..
Posts: 200
Threads: 60
Joined: Jun 2015
Reputation:
7
Hi Admin,
A couple of questions,
Will this server support multiple clients? I added a basic authentication, that just sets a global variable on connecting and closes the connection if the password is incorrect.
The data received is passed another function parseC. Now I need to be able to respond back to the connected client where I declared a global function in this handler. Is this the right way to do this?
Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
if data then
if (
first ==
true )
then
if (
data ~=
'mypassword' )
then
first =
false
sock :
close ()
end
end
Sclient =
function (
param )
sock :
send (
param )
end
local fd ,
prtd =
pcall (
parseC ,
data )
if (
fd ==
false )
then
alert (
"[elk-client] Error with parsemsg %s " ,
prtd )
end
end
Thanks,
Roger
Posts: 8160
Threads: 43
Joined: Jun 2015
Reputation:
472
Yes, it supports multiple clients.
See this example on how to pass data to all connected clients:
http://openrb.com/example-lm2-as-tcp-ser...-requests/
You can pass any number of arguments via pcall, you can pass both data and socket if you want to send a reply to a specific client.