diff options
Diffstat (limited to 'net')
-rw-r--r-- | net/adns.lua | 2 | ||||
-rw-r--r-- | net/httpserver.lua | 25 | ||||
-rw-r--r-- | net/server.lua | 32 | ||||
-rw-r--r-- | net/xmppclient_listener.lua | 8 | ||||
-rw-r--r-- | net/xmppserver_listener.lua | 14 |
5 files changed, 54 insertions, 27 deletions
diff --git a/net/adns.lua b/net/adns.lua index 9438bf45..b0c9a625 100644 --- a/net/adns.lua +++ b/net/adns.lua @@ -29,7 +29,7 @@ function lookup(handler, qname, qtype, qclass) log("debug", "Reply for %s (%s)", qname, tostring(coroutine.running())); local ok, err = pcall(handler, dns.peek(qname, qtype, qclass)); if not ok then - log("debug", "Error in DNS response handler: %s", tostring(err)); + log("error", "Error in DNS response handler: %s", tostring(err)); end end)(dns.peek(qname, qtype, qclass)); end diff --git a/net/httpserver.lua b/net/httpserver.lua index 57c8eede..3a54fd62 100644 --- a/net/httpserver.lua +++ b/net/httpserver.lua @@ -61,7 +61,7 @@ local function send_response(request, response) end else -- Response we have is just a string (the body) - log("debug", "Sending response to %s: %s", request.id or "<none>", response or "<none>"); + log("debug", "Sending 200 response to %s", request.id or "<none>"); resp = { "HTTP/1.0 200 OK\r\n" }; t_insert(resp, "Connection: close\r\n"); @@ -89,9 +89,6 @@ local function call_callback(request, err) end callback = (request.server and request.server.handlers[base]) or default_handler; - if callback == default_handler then - log("debug", "Default callback for this request (base: "..tostring(base)..")") - end end if callback then if err then @@ -251,13 +248,27 @@ function new(params) end end -function new_from_config(ports, default_base, handle_request) +function set_default_handler(handler) + default_handler = handler; +end + +function new_from_config(ports, handle_request, default_options) + if type(handle_request) == "string" then -- COMPAT with old plugins + log("warn", "Old syntax of httpserver.new_from_config being used to register %s", handle_request); + handle_request, default_options = default_options, { base = handle_request }; + end for _, options in ipairs(ports) do - local port, base, ssl, interface = 5280, default_base, false, nil; + local port = default_options.port or 5280; + local base = default_options.base; + local ssl = default_options.ssl or false; + local interface = default_options.interface; if type(options) == "number" then port = options; elseif type(options) == "table" then - port, base, ssl, interface = options.port or 5280, options.path or default_base, options.ssl or false, options.interface; + port = options.port or port; + base = options.path or base; + ssl = options.ssl or ssl; + interface = options.interface or interface; elseif type(options) == "string" then base = options; end diff --git a/net/server.lua b/net/server.lua index 3873a5f0..1260a260 100644 --- a/net/server.lua +++ b/net/server.lua @@ -157,6 +157,7 @@ _cleanqueue = false -- clean bufferqueue after using _maxclientsperserver = 1000
+_maxsslhandshake = 30 -- max handshake round-trips
----------------------------------// PRIVATE //--
wrapserver = function( listeners, socket, ip, serverport, pattern, sslctx, maxconnections, startssl ) -- this function wraps a server
@@ -230,6 +231,9 @@ wrapserver = function( listeners, socket, ip, serverport, pattern, sslctx, maxco handler.ssl = function( )
return ssl
end
+ handler.sslctx = function( )
+ return sslctx
+ end
handler.remove = function( )
connections = connections - 1
end
@@ -246,7 +250,7 @@ wrapserver = function( listeners, socket, ip, serverport, pattern, sslctx, maxco _socketlist[ socket ] = nil
handler = nil
socket = nil
- mem_free( )
+ --mem_free( )
out_put "server.lua: closed server handler and removed sockets from list"
end
handler.ip = function( )
@@ -297,6 +301,7 @@ wrapconnection = function( server, listeners, socket, ip, serverport, clientport local ssl
local dispatch = listeners.incoming or listeners.listener
+ local status = listeners.status
local disconnect = listeners.disconnect
local bufferqueue = { } -- buffer array
@@ -336,6 +341,9 @@ wrapconnection = function( server, listeners, socket, ip, serverport, clientport handler.ssl = function( )
return ssl
end
+ handler.sslctx = function ( )
+ return sslctx
+ end
handler.send = function( _, data, i, j )
return send( socket, data, i, j )
end
@@ -377,7 +385,6 @@ wrapconnection = function( server, listeners, socket, ip, serverport, clientport _closelist[ handler ] = nil
handler = nil
end
- mem_free( )
if server then
server.remove( )
end
@@ -476,10 +483,10 @@ wrapconnection = function( server, listeners, socket, ip, serverport, clientport readtraffic = readtraffic + count
_readtraffic = _readtraffic + count
_readtimes[ handler ] = _currenttime
- --out_put( "server.lua: read data '", buffer, "', error: ", err )
+ --out_put( "server.lua: read data '", buffer:gsub("[^%w%p ]", "."), "', error: ", err )
return dispatch( handler, buffer, err )
else -- connections was closed or fatal error
- out_put( "server.lua: client ", tostring(ip), ":", tostring(clientport), " error: ", tostring(err) )
+ out_put( "server.lua: client ", tostring(ip), ":", tostring(clientport), " read error: ", tostring(err) )
fatalerror = true
disconnect( handler, err )
_ = handler and handler.close( )
@@ -516,7 +523,7 @@ wrapconnection = function( server, listeners, socket, ip, serverport, clientport _writetimes[ handler ] = _currenttime
return true
else -- connection was closed during sending or fatal error
- out_put( "server.lua: client ", tostring(ip), ":", tostring(clientport), " error: ", tostring(err) )
+ out_put( "server.lua: client ", tostring(ip), ":", tostring(clientport), " write error: ", tostring(err) )
fatalerror = true
disconnect( handler, err )
_ = handler and handler.close( )
@@ -530,7 +537,7 @@ wrapconnection = function( server, listeners, socket, ip, serverport, clientport local read
local handshake = coroutine_wrap( function( client ) -- create handshake coroutine
local err
- for i = 1, 10 do -- 10 handshake attemps
+ for i = 1, _maxsslhandshake do
_sendlistlen = ( wrote and removesocket( _sendlist, socket, _sendlistlen ) ) or _sendlistlen
_readlistlen = ( read and removesocket( _readlist, socket, _readlistlen ) ) or _readlistlen
read, wrote = nil, nil
@@ -539,7 +546,7 @@ wrapconnection = function( server, listeners, socket, ip, serverport, clientport out_put( "server.lua: ssl handshake done" )
handler.readbuffer = _readbuffer -- when handshake is done, replace the handshake function with regular functions
handler.sendbuffer = _sendbuffer
- -- return dispatch( handler )
+ _ = status and status( handler, "ssl-handshake-complete" )
return true
else
out_put( "server.lua: error during ssl handshake: ", tostring(err) )
@@ -569,7 +576,7 @@ wrapconnection = function( server, listeners, socket, ip, serverport, clientport socket, err = ssl_wrap( socket, sslctx ) -- wrap socket
if err then
out_put( "server.lua: ssl error: ", tostring(err) )
- mem_free( )
+ --mem_free( )
return nil, nil, err -- fatal error
end
socket:settimeout( 0 )
@@ -674,7 +681,7 @@ closesocket = function( socket ) _readlistlen = removesocket( _readlist, socket, _readlistlen )
_socketlist[ socket ] = nil
socket:close( )
- mem_free( )
+ --mem_free( )
end
----------------------------------// PUBLIC //--
@@ -743,11 +750,11 @@ closeall = function( ) _sendlist = { }
_timerlist = { }
_socketlist = { }
- mem_free( )
+ --mem_free( )
end
getsettings = function( )
- return _selecttimeout, _sleeptime, _maxsendlen, _maxreadlen, _checkinterval, _sendtimeout, _readtimeout, _cleanqueue, _maxclientsperserver
+ return _selecttimeout, _sleeptime, _maxsendlen, _maxreadlen, _checkinterval, _sendtimeout, _readtimeout, _cleanqueue, _maxclientsperserver, _maxsslhandshake
end
changesettings = function( new )
@@ -763,6 +770,7 @@ changesettings = function( new ) _readtimeout = tonumber( new.readtimeout ) or _readtimeout
_cleanqueue = new.cleanqueue
_maxclientsperserver = new._maxclientsperserver or _maxclientsperserver
+ _maxsslhandshake = new._maxsslhandshake or _maxsslhandshake
return true
end
@@ -815,7 +823,7 @@ loop = function( ) -- this is the main loop of the program _currenttime = os_time( )
if os_difftime( _currenttime - _timer ) >= 1 then
for i = 1, _timerlistlen do
- _timerlist[ i ]( ) -- fire timers
+ _timerlist[ i ]( _currenttime ) -- fire timers
end
_timer = _currenttime
end
diff --git a/net/xmppclient_listener.lua b/net/xmppclient_listener.lua index ce7788c7..6cea43f2 100644 --- a/net/xmppclient_listener.lua +++ b/net/xmppclient_listener.lua @@ -61,6 +61,7 @@ local function session_reset_stream(session) function session.data(conn, data) local ok, err = parser:parse(data); if ok then return; end + log("debug", "Received invalid XML (%s) %d bytes: %s", tostring(err), #data, data:sub(1, 300):gsub("[\r\n]+", " ")); session:close("xml-not-well-formed"); end @@ -100,7 +101,7 @@ local function session_close(session, reason) end session.send("</stream:stream>"); session.conn.close(); - xmppclient.disconnect(session.conn, (reason and reason.condition) or reason or "session closed"); + xmppclient.disconnect(session.conn, (reason and (reason.text or reason.condition)) or reason or "session closed"); end end @@ -113,11 +114,6 @@ function xmppclient.listener(conn, data) session = sm_new_session(conn); sessions[conn] = session; - -- Logging functions -- - - local conn_name = "c2s"..tostring(conn):match("[a-f0-9]+$"); - session.log = logger.init(conn_name); - session.log("info", "Client connected"); -- Client is using legacy SSL (otherwise mod_tls sets this flag) diff --git a/net/xmppserver_listener.lua b/net/xmppserver_listener.lua index 0b887b87..ccea1170 100644 --- a/net/xmppserver_listener.lua +++ b/net/xmppserver_listener.lua @@ -61,13 +61,14 @@ local function session_reset_stream(session) function session.data(conn, data) local ok, err = parser:parse(data); if ok then return; end + session.log("warn", "Received invalid XML: %s", data); + session.log("warn", "Problem was: %s", err); session:close("xml-not-well-formed"); end return true; end - local stream_xmlns_attr = {xmlns='urn:ietf:params:xml:ns:xmpp-streams'}; local default_stream_attr = { ["xmlns:stream"] = stream_callbacks.stream_tag:gsub("%|[^|]+$", ""), xmlns = stream_callbacks.default_ns, version = "1.0", id = "" }; local function session_close(session, reason) @@ -136,6 +137,17 @@ function xmppserver.listener(conn, data) end end +function xmppserver.status(conn, status) + if status == "ssl-handshake-complete" then + local session = sessions[conn]; + if session and session.direction == "outgoing" then + local format, to_host, from_host = string.format, session.to_host, session.from_host; + session.log("debug", "Sending stream header..."); + session.sends2s(format([[<stream:stream xmlns='jabber:server' xmlns:db='jabber:server:dialback' xmlns:stream='http://etherx.jabber.org/streams' from='%s' to='%s' version='1.0'>]], from_host, to_host)); + end + end +end + function xmppserver.disconnect(conn, err) local session = sessions[conn]; if session then |