From 0d2b952b610069996f733700117a2e62d5b73696 Mon Sep 17 00:00:00 2001 From: Matthew Wild Date: Fri, 5 Dec 2008 19:24:01 +0000 Subject: Add TLS socket to readlist before handshake starts, fixes major slow-down on TLS connections --- core/presencemanager.lua | 9 +++++++++ core/sessionmanager.lua | 6 +++++- net/server.lua | 16 ++++++++++++---- net/xmppclient_listener.lua | 2 +- plugins/mod_console.lua | 2 +- plugins/mod_saslauth.lua | 8 ++++++-- 6 files changed, 34 insertions(+), 9 deletions(-) diff --git a/core/presencemanager.lua b/core/presencemanager.lua index 6e27752b..dbcb83d6 100644 --- a/core/presencemanager.lua +++ b/core/presencemanager.lua @@ -21,6 +21,7 @@ local log = require "util.logger".init("presencemanager") +local tostring = tostring; local require = require; local pairs, ipairs = pairs, ipairs; local t_concat = table.concat; @@ -121,6 +122,14 @@ function handle_normal_presence(origin, stanza, core_route_stanza) else log("error", "presence recieved from client with no roster"); end + + if origin.conntimetotal then + local session = origin; + origin.log("BLAH", "***********\n\n\n\n\n\n****************"); + origin.send(st.stanza("message", { from = session.host, to=session.full_jid, type = "normal" }):body("Your login took "..tostring(session.conntimetotal).." seconds")); + origin.conntimetotal = nil; + end + end function send_presence_of_available_resources(user, host, jid, recipient_session, core_route_stanza) diff --git a/core/sessionmanager.lua b/core/sessionmanager.lua index 36111633..bc48d228 100644 --- a/core/sessionmanager.lua +++ b/core/sessionmanager.lua @@ -35,6 +35,8 @@ local uuid_generate = require "util.uuid".generate; local rm_load_roster = require "core.rostermanager".load_roster; local config_get = require "core.configmanager".get; +local gettime = require "socket".gettime; + local st = require "util.stanza"; local newproxy = newproxy; @@ -45,7 +47,7 @@ module "sessionmanager" local open_sessions = 0; function new_session(conn) - local session = { conn = conn, priority = 0, type = "c2s_unauthed" }; + local session = { conn = conn, priority = 0, type = "c2s_unauthed", conntime = gettime() }; if true then session.trace = newproxy(true); getmetatable(session.trace).__gc = function () open_sessions = open_sessions - 1; print("Session got collected, now "..open_sessions.." sessions are allocated") end; @@ -109,6 +111,8 @@ function bind_resource(session, resource) if session.resource then return nil, "cancel", "already-bound", "Cannot bind multiple resources on a single connection"; end -- We don't support binding multiple resources + session.conntimetotal = gettime()-session.conntime; + resource = resource or uuid_generate(); --FIXME: Randomly-generated resources must be unique per-user, and never conflict with existing diff --git a/net/server.lua b/net/server.lua index 9d178cb2..d840d51b 100644 --- a/net/server.lua +++ b/net/server.lua @@ -504,13 +504,24 @@ wraptlsclient = function( listener, socket, ip, serverport, clientport, mode, ss handler.starttls = function (now) if not now then out_put("server.lua: we need to do tls, but delaying until later"); handler.need_tls = true; return; end out_put( "server.lua: attempting to start tls on "..tostring(socket) ) + local oldsocket = socket; socket, err = ssl_wrap( socket, sslctx ) -- wrap socket out_put("sslwrapped socket is "..tostring(socket)); if err then out_put( "server.lua: ssl error: ", err ) return nil, nil, err -- fatal error end - socket:settimeout( 1 ) + socket:settimeout(0); + + -- Add the new socket to our system + socketlist[ socket ] = handler + readlen = readlen + 1 + readlist[ readlen ] = socket + + -- Remove traces of the old socket + readlen = removesocket( readlist, oldsocket, readlen ) + socketlist [ oldsocket ] = nil; + send = socket.send receive = socket.receive close = socket.close @@ -536,9 +547,6 @@ wraptlsclient = function( listener, socket, ip, serverport, clientport, mode, ss handler.receivedata = handler._receivedata -- when handshake is done, replace the handshake function with regular functions handler.dispatchdata = handler._dispatchdata handler.need_tls = nil - socketlist[ client ] = handler - readlen = readlen + 1 - readlist[ readlen ] = client return true; else out_put( "server.lua: error during ssl handshake: ", err ) diff --git a/net/xmppclient_listener.lua b/net/xmppclient_listener.lua index 22af2de4..fe6ec57b 100644 --- a/net/xmppclient_listener.lua +++ b/net/xmppclient_listener.lua @@ -113,7 +113,7 @@ function xmppclient.listener(conn, data) if not session then session = sm_new_session(conn); sessions[conn] = session; - + -- Logging functions -- local mainlog, log = log; diff --git a/plugins/mod_console.lua b/plugins/mod_console.lua index 4ac3c5fe..9b9fc217 100644 --- a/plugins/mod_console.lua +++ b/plugins/mod_console.lua @@ -33,7 +33,7 @@ function console:new_session(conn) local w = conn.write; local session = { conn = conn; send = function (t) w(tostring(t)); end; - print = function (t) w("| "..tostring(t).."\n"); end; + print = function (t) w("| "..tostring(t).."\r\n"); end; disconnect = function () conn.close(); end; }; session.env = setmetatable({}, default_env_mt); diff --git a/plugins/mod_saslauth.lua b/plugins/mod_saslauth.lua index 52ef68c7..d0ba8542 100644 --- a/plugins/mod_saslauth.lua +++ b/plugins/mod_saslauth.lua @@ -24,6 +24,8 @@ local sm_bind_resource = require "core.sessionmanager".bind_resource; local jid local base64 = require "util.encodings".base64; +local gettime = require "socket".gettime; + local usermanager_validate_credentials = require "core.usermanager".validate_credentials; local t_concat, t_insert = table.concat, table.insert; local tostring = tostring; @@ -64,14 +66,14 @@ local function handle_status(session, status) end end -local function password_callback(node, host, mechanism, raw_host) +local function password_callback(node, host, mechanism) local password = (datamanager.load(node, host, "accounts") or {}).password; -- FIXME handle hashed passwords local func = function(x) return x; end; if password then if mechanism == "PLAIN" then return func, password; elseif mechanism == "DIGEST-MD5" then - return func, md5(node..":"..raw_host..":"..password); + return func, md5(node..":"..host..":"..password); end end return func, nil; @@ -142,6 +144,8 @@ module:add_iq_handler("c2s", "urn:ietf:params:xml:ns:xmpp-bind", :tag("bind", { xmlns = xmlns_bind}) :tag("jid"):text(session.full_jid)); end + + session.log("******", "Connection took "..tostring(session.conntimetotal).." seconds"); end); module:add_iq_handler("c2s", "urn:ietf:params:xml:ns:xmpp-session", -- cgit v1.2.3