From 4f5a4b33d3eb6a6cc1f0bcc72b3a560ff7d8daf1 Mon Sep 17 00:00:00 2001 From: Matthew Wild Date: Thu, 22 Nov 2012 18:24:09 +0000 Subject: mod_bosh: Add bosh_max_wait config option, to limit the amount of time a client can request for the server to hold open requests --- plugins/mod_bosh.lua | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) (limited to 'plugins') diff --git a/plugins/mod_bosh.lua b/plugins/mod_bosh.lua index 1270a080..249c4792 100644 --- a/plugins/mod_bosh.lua +++ b/plugins/mod_bosh.lua @@ -18,6 +18,7 @@ local core_process_stanza = prosody.core_process_stanza; local st = require "util.stanza"; local logger = require "util.logger"; local log = logger.init("mod_bosh"); +local math_min = math.min; local xmlns_streams = "http://etherx.jabber.org/streams"; local xmlns_xmpp_streams = "urn:ietf:params:xml:ns:xmpp-streams"; @@ -30,6 +31,7 @@ local BOSH_DEFAULT_HOLD = module:get_option_number("bosh_default_hold", 1); local BOSH_DEFAULT_INACTIVITY = module:get_option_number("bosh_max_inactivity", 60); local BOSH_DEFAULT_POLLING = module:get_option_number("bosh_max_polling", 5); local BOSH_DEFAULT_REQUESTS = module:get_option_number("bosh_max_requests", 2); +local bosh_max_wait = module:get_option_number("bosh_max_wait", 120); local consider_bosh_secure = module:get_option_boolean("consider_bosh_secure"); @@ -243,7 +245,7 @@ function stream_callbacks.streamopened(context, attr) sid = new_uuid(); local session = { type = "c2s_unauthed", conn = {}, sid = sid, rid = tonumber(attr.rid)-1, host = attr.to, - bosh_version = attr.ver, bosh_wait = attr.wait, streamid = sid, + bosh_version = attr.ver, bosh_wait = math_min(attr.wait, bosh_max_wait), streamid = sid, bosh_hold = BOSH_DEFAULT_HOLD, bosh_max_inactive = BOSH_DEFAULT_INACTIVITY, requests = { }, send_buffer = {}, reset_stream = bosh_reset_stream, close = bosh_close_stream, dispatch_stanza = core_process_stanza, notopen = true, @@ -278,10 +280,10 @@ function stream_callbacks.streamopened(context, attr) sid = sid; }; if creating_session then - body_attr.wait = attr.wait; body_attr.inactivity = tostring(BOSH_DEFAULT_INACTIVITY); body_attr.polling = tostring(BOSH_DEFAULT_POLLING); body_attr.requests = tostring(BOSH_DEFAULT_REQUESTS); + body_attr.wait = tostring(session.bosh_wait); body_attr.hold = tostring(session.bosh_hold); body_attr.authid = sid; body_attr.secure = "true"; -- cgit v1.2.3 From e23a0f80b506afd905e0bfa734e1e58734d4d7b2 Mon Sep 17 00:00:00 2001 From: Matthew Wild Date: Thu, 22 Nov 2012 18:32:27 +0000 Subject: mod_admin_telnet: Refactor so that command processing is performed in a separate function (usable from other modules) --- plugins/mod_admin_telnet.lua | 118 +++++++++++++++++++++---------------------- 1 file changed, 59 insertions(+), 59 deletions(-) (limited to 'plugins') diff --git a/plugins/mod_admin_telnet.lua b/plugins/mod_admin_telnet.lua index e9d595ba..0af571eb 100644 --- a/plugins/mod_admin_telnet.lua +++ b/plugins/mod_admin_telnet.lua @@ -72,6 +72,64 @@ function console:new_session(conn) return session; end +function console:process_line(session, line) + local useglobalenv; + + if line:match("^>") then + line = line:gsub("^>", ""); + useglobalenv = true; + elseif line == "\004" then + commands["bye"](session, line); + return; + else + local command = line:match("^%w+") or line:match("%p"); + if commands[command] then + commands[command](session, line); + return; + end + end + + session.env._ = line; + + local chunkname = "=console"; + local env = (useglobalenv and redirect_output(_G, session)) or session.env or nil + local chunk, err = envload("return "..line, chunkname, env); + if not chunk then + chunk, err = envload(line, chunkname, env); + if not chunk then + err = err:gsub("^%[string .-%]:%d+: ", ""); + err = err:gsub("^:%d+: ", ""); + err = err:gsub("''", "the end of the line"); + session.print("Sorry, I couldn't understand that... "..err); + return; + end + end + + local ranok, taskok, message = pcall(chunk); + + if not (ranok or message or useglobalenv) and commands[line:lower()] then + commands[line:lower()](session, line); + return; + end + + if not ranok then + session.print("Fatal error while running command, it did not complete"); + session.print("Error: "..taskok); + return; + end + + if not message then + session.print("Result: "..tostring(taskok)); + return; + elseif (not taskok) and message then + session.print("Command completed with a problem"); + session.print("Message: "..tostring(message)); + return; + end + + session.print("OK: "..tostring(message)); +end + local sessions = {}; function console_listener.onconnect(conn) @@ -91,65 +149,7 @@ function console_listener.onincoming(conn, data) end for line in data:gmatch("[^\n]*[\n\004]") do - -- Handle data (loop allows us to break to add \0 after response) - repeat - local useglobalenv; - - if line:match("^>") then - line = line:gsub("^>", ""); - useglobalenv = true; - elseif line == "\004" then - commands["bye"](session, line); - break; - else - local command = line:match("^%w+") or line:match("%p"); - if commands[command] then - commands[command](session, line); - break; - end - end - - session.env._ = line; - - local chunkname = "=console"; - local env = (useglobalenv and redirect_output(_G, session)) or session.env or nil - local chunk, err = envload("return "..line, chunkname, env); - if not chunk then - chunk, err = envload(line, chunkname, env); - if not chunk then - err = err:gsub("^%[string .-%]:%d+: ", ""); - err = err:gsub("^:%d+: ", ""); - err = err:gsub("''", "the end of the line"); - session.print("Sorry, I couldn't understand that... "..err); - break; - end - end - - local ranok, taskok, message = pcall(chunk); - - if not (ranok or message or useglobalenv) and commands[line:lower()] then - commands[line:lower()](session, line); - break; - end - - if not ranok then - session.print("Fatal error while running command, it did not complete"); - session.print("Error: "..taskok); - break; - end - - if not message then - session.print("Result: "..tostring(taskok)); - break; - elseif (not taskok) and message then - session.print("Command completed with a problem"); - session.print("Message: "..tostring(message)); - break; - end - - session.print("OK: "..tostring(message)); - until true - + console:process_line(session, line); session.send(string.char(0)); end session.partial_data = data:match("[^\n]+$"); -- cgit v1.2.3 From c1716e51886cdbd9b3da1ad88b7fc4c1e16c8957 Mon Sep 17 00:00:00 2001 From: Matthew Wild Date: Thu, 22 Nov 2012 19:35:50 +0000 Subject: mod_bosh: Add support for stanza filters to BOSH sessions (needed by some plugins) --- plugins/mod_bosh.lua | 5 +++++ 1 file changed, 5 insertions(+) (limited to 'plugins') diff --git a/plugins/mod_bosh.lua b/plugins/mod_bosh.lua index 249c4792..b54fc9d4 100644 --- a/plugins/mod_bosh.lua +++ b/plugins/mod_bosh.lua @@ -18,6 +18,7 @@ local core_process_stanza = prosody.core_process_stanza; local st = require "util.stanza"; local logger = require "util.logger"; local log = logger.init("mod_bosh"); +local initialize_filters = require "util.filters".initialize; local math_min = math.min; local xmlns_streams = "http://etherx.jabber.org/streams"; @@ -254,6 +255,8 @@ function stream_callbacks.streamopened(context, attr) }; sessions[sid] = session; + local filter = initialize_filters(session); + session.log("debug", "BOSH session created for request from %s", session.ip); log("info", "New BOSH session, assigned it sid '%s'", sid); @@ -267,6 +270,7 @@ function stream_callbacks.streamopened(context, attr) s = st.clone(s); s.attr.xmlns = "jabber:client"; end + s = filter("stanzas/out", s); --log("debug", "Sending BOSH data: %s", tostring(s)); t_insert(session.send_buffer, tostring(s)); @@ -354,6 +358,7 @@ function stream_callbacks.handlestanza(context, stanza) if stanza.attr.xmlns == xmlns_bosh then stanza.attr.xmlns = nil; end + stanza = session.filter("stanzas/in", stanza); core_process_stanza(session, stanza); end end -- cgit v1.2.3 From 731c5f5cb79282a11e8a07a00c0a976803d6b45a Mon Sep 17 00:00:00 2001 From: Matthew Wild Date: Thu, 22 Nov 2012 19:37:30 +0000 Subject: mod_bosh: Add 'Connection: keep-alive' header --- plugins/mod_bosh.lua | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'plugins') diff --git a/plugins/mod_bosh.lua b/plugins/mod_bosh.lua index b54fc9d4..19f191c8 100644 --- a/plugins/mod_bosh.lua +++ b/plugins/mod_bosh.lua @@ -36,7 +36,7 @@ local bosh_max_wait = module:get_option_number("bosh_max_wait", 120); local consider_bosh_secure = module:get_option_boolean("consider_bosh_secure"); -local default_headers = { ["Content-Type"] = "text/xml; charset=utf-8" }; +local default_headers = { ["Content-Type"] = "text/xml; charset=utf-8", ["Connection"] = "keep-alive" }; local cross_domain = module:get_option("cross_domain_bosh", false); if cross_domain then -- cgit v1.2.3