X-Git-Url: https://git.enpas.org/?a=blobdiff_plain;f=plugins%2Fmod_c2s.lua;h=c638cd039897fc3866eacca4df17c41597182bdb;hb=3c561ed7ab90acb90ea8cec61d912586b91fe56b;hp=3bdffc7d5537c0801032e46bb3f3ab97098d2486;hpb=b7b728b661572e713ab435981d90c982e0d3cf30;p=prosody.git diff --git a/plugins/mod_c2s.lua b/plugins/mod_c2s.lua index 3bdffc7d..c638cd03 100644 --- a/plugins/mod_c2s.lua +++ b/plugins/mod_c2s.lua @@ -28,6 +28,8 @@ local c2s_timeout = module:get_option_number("c2s_timeout"); local stream_close_timeout = module:get_option_number("c2s_close_timeout", 5); local opt_keepalives = module:get_option_boolean("c2s_tcp_keepalives", module:get_option_boolean("tcp_keepalives", true)); +local measure_connections = module:measure("connections", "counter"); + local sessions = module:shared("sessions"); local core_process_stanza = prosody.core_process_stanza; local hosts = prosody.hosts; @@ -38,7 +40,6 @@ local runner_callbacks = {}; --- Stream events handlers local stream_xmlns_attr = {xmlns='urn:ietf:params:xml:ns:xmpp-streams'}; -local default_stream_attr = { ["xmlns:stream"] = "http://etherx.jabber.org/streams", xmlns = stream_callbacks.default_ns, version = "1.0", id = "" }; function stream_callbacks.streamopened(session, attr) local send = session.send; @@ -58,9 +59,7 @@ function stream_callbacks.streamopened(session, attr) return; end - send(""..st.stanza("stream:stream", { - xmlns = 'jabber:client', ["xmlns:stream"] = 'http://etherx.jabber.org/streams'; - id = session.streamid, from = session.host, version = '1.0', ["xml:lang"] = 'en' }):top_tag()); + session:open_stream(); (session.log or log)("debug", "Sent reply to client"); session.notopen = nil; @@ -84,7 +83,12 @@ function stream_callbacks.streamopened(session, attr) local features = st.stanza("stream:features"); hosts[session.host].events.fire_event("stream-features", { origin = session, features = features }); - send(features); + if features.tags[1] or session.full_jid then + send(features); + else + (session.log or log)("warn", "No features to offer"); + session:close{ condition = "undefined-condition", text = "No features to proceed with" }; + end end function stream_callbacks.streamclosed(session) @@ -94,7 +98,7 @@ end function stream_callbacks.error(session, error, data) if error == "no-stream" then - session.log("debug", "Invalid opening stream header"); + session.log("debug", "Invalid opening stream header (%s)", (data:gsub("^([^\1]+)\1", "{%1}"))); session:close("invalid-namespace"); elseif error == "parse-error" then (session.log or log)("debug", "Client XML parse error: %s", tostring(data)); @@ -129,8 +133,7 @@ local function session_close(session, reason) local log = session.log or log; if session.conn then if session.notopen then - session.send(""); - session.send(st.stanza("stream:stream", default_stream_attr):top_tag()); + session:open_stream(); end if reason then -- nil == no err, initiated by us, false == initiated by client local stream_error = st.stanza("stream:error"); @@ -202,6 +205,7 @@ end --- Port listener function listener.onconnect(conn) + measure_connections(1); local session = sm_new_session(conn); sessions[conn] = session; @@ -244,9 +248,9 @@ function listener.onconnect(conn) function session.data(data) -- Parse the data, which will store stanzas in session.pending_stanzas if data then - data = filter("bytes/in", data); - if data then - local ok, err = stream:feed(data); + data = filter("bytes/in", data); + if data then + local ok, err = stream:feed(data); if not ok then log("debug", "Received invalid XML (%s) %d bytes: %s", tostring(err), #data, data:sub(1, 300):gsub("[\r\n]+", " "):gsub("[%z\1-\31]", "_")); session:close("not-well-formed"); @@ -274,6 +278,7 @@ function listener.onincoming(conn, data) end function listener.ondisconnect(conn, err) + measure_connections(-1); local session = sessions[conn]; if session then (session.log or log)("info", "Client disconnected: %s", err or "connection closed"); @@ -308,7 +313,7 @@ module:hook("server-stopping", function(event) for _, session in pairs(sessions) do session:close{ condition = "system-shutdown", text = reason }; end -end, 1000); +end, -100);