comparison plugins/mod_c2s.lua @ 14138:c1469296190d

Merge 13.0->trunk
author Matthew Wild <mwild1@gmail.com>
date Fri, 17 Apr 2026 14:35:32 +0100
parents 97d15552d364 836a70deb3c9
children 320e1d49aa9e
comparison
equal deleted inserted replaced
14121:7008869fcce2 14138:c1469296190d
6 -- COPYING file in the source package for more information. 6 -- COPYING file in the source package for more information.
7 -- 7 --
8 8
9 module:set_global(); 9 module:set_global();
10 10
11 local add_task = require "prosody.util.timer".add_task; 11 local timer = require "prosody.util.timer";
12 local add_task = timer.add_task;
13 local stop_task = timer.stop;
12 local new_xmpp_stream = require "prosody.util.xmppstream".new; 14 local new_xmpp_stream = require "prosody.util.xmppstream".new;
13 local nameprep = require "prosody.util.encodings".stringprep.nameprep; 15 local nameprep = require "prosody.util.encodings".stringprep.nameprep;
14 local sessionmanager = require "prosody.core.sessionmanager"; 16 local sessionmanager = require "prosody.core.sessionmanager";
15 local statsmanager = require "prosody.core.statsmanager"; 17 local statsmanager = require "prosody.core.statsmanager";
16 local st = require "prosody.util.stanza"; 18 local st = require "prosody.util.stanza";
23 25
24 local xmlns_xmpp_streams = "urn:ietf:params:xml:ns:xmpp-streams"; 26 local xmlns_xmpp_streams = "urn:ietf:params:xml:ns:xmpp-streams";
25 27
26 local log = module._log; 28 local log = module._log;
27 29
30 local function stop_session_timers(session)
31 if session.c2s_timeout then
32 stop_task(session.c2s_timeout);
33 session.c2s_timeout = nil;
34 end
35 end
36
28 local c2s_timeout = module:get_option_period("c2s_timeout", "5 minutes"); 37 local c2s_timeout = module:get_option_period("c2s_timeout", "5 minutes");
29 local stream_close_timeout = module:get_option_period("c2s_close_timeout", 5); 38 local stream_close_timeout = module:get_option_period("c2s_close_timeout", 5);
30 local opt_keepalives = module:get_option_boolean("c2s_tcp_keepalives", module:get_option_boolean("tcp_keepalives", true)); 39 local opt_keepalives = module:get_option_boolean("c2s_tcp_keepalives", module:get_option_boolean("tcp_keepalives", true));
31 local stanza_size_limit = module:get_option_integer("c2s_stanza_size_limit", 1024*256,10000); 40 local unauthed_stanza_size_limit = module:get_option_integer("c2s_unauthed_stanza_size_limit", 10000,1000);
41 local authed_stanza_size_limit = module:get_option_integer("c2s_stanza_size_limit", 1024*256,10000);
42 local max_child_elements = module:get_option_integer("c2s_max_child_elements", 25000, 0);
32 43
33 local advertised_idle_timeout = 14*60; -- default in all net.server implementations 44 local advertised_idle_timeout = 14*60; -- default in all net.server implementations
34 local network_settings = module:get_option("network_settings"); 45 local network_settings = module:get_option("network_settings");
35 if type(network_settings) == "table" and type(network_settings.read_timeout) == "number" then 46 if type(network_settings) == "table" and type(network_settings.read_timeout) == "number" then
36 advertised_idle_timeout = network_settings.read_timeout; 47 advertised_idle_timeout = network_settings.read_timeout;
135 end 146 end
136 147
137 local features = st.stanza("stream:features"); 148 local features = st.stanza("stream:features");
138 hosts[session.host].events.fire_event("stream-features", { origin = session, features = features, stream = attr }); 149 hosts[session.host].events.fire_event("stream-features", { origin = session, features = features, stream = attr });
139 if features.tags[1] or session.full_jid then 150 if features.tags[1] or session.full_jid then
140 if stanza_size_limit or advertised_idle_timeout then 151 if session.stanza_size_limit or advertised_idle_timeout then
141 features:reset(); 152 features:reset();
142 local limits = features:tag("limits", { xmlns = "urn:xmpp:stream-limits:0" }); 153 local limits = features:tag("limits", { xmlns = "urn:xmpp:stream-limits:0" });
143 if stanza_size_limit then 154 if session.stanza_size_limit then
144 limits:text_tag("max-bytes", string.format("%d", stanza_size_limit)); 155 limits:text_tag("max-bytes", string.format("%d", session.stanza_size_limit));
145 end 156 end
146 if advertised_idle_timeout then 157 if advertised_idle_timeout then
147 limits:text_tag("idle-seconds", string.format("%d", advertised_idle_timeout)); 158 limits:text_tag("idle-seconds", string.format("%d", advertised_idle_timeout));
148 end 159 end
149 limits:reset(); 160 limits:reset();
213 -- Fire event on host if we know it, otherwise fire globally 224 -- Fire event on host if we know it, otherwise fire globally
214 local context = hosts[session.host] and module:context(session.host) or module:context("*"); 225 local context = hosts[session.host] and module:context(session.host) or module:context("*");
215 context:fire_event("pre-session-close", close_event_payload); 226 context:fire_event("pre-session-close", close_event_payload);
216 227
217 reason = close_event_payload.reason; 228 reason = close_event_payload.reason;
229 stop_session_timers(session);
218 if session.conn then 230 if session.conn then
219 if session.notopen then 231 if session.notopen then
220 session:open_stream(); 232 session:open_stream();
221 end 233 end
222 if reason then -- nil == no err, initiated by us, false == initiated by client 234 if reason then -- nil == no err, initiated by us, false == initiated by client
353 conn:setoption("keepalive", opt_keepalives); 365 conn:setoption("keepalive", opt_keepalives);
354 end 366 end
355 367
356 session.close = session_close; 368 session.close = session_close;
357 369
358 local stream = new_xmpp_stream(session, stream_callbacks, stanza_size_limit); 370 session.stanza_size_limit = unauthed_stanza_size_limit;
371
372 local stream = new_xmpp_stream(session, stream_callbacks, session.stanza_size_limit, {
373 max_elements = max_child_elements;
374 });
359 session.stream = stream; 375 session.stream = stream;
360 session.notopen = true; 376 session.notopen = true;
361 377
362 function session.reset_stream() 378 function session.reset_stream()
363 session.notopen = true; 379 session.notopen = true;
407 end 423 end
408 424
409 session.dispatch_stanza = stream_callbacks.handlestanza; 425 session.dispatch_stanza = stream_callbacks.handlestanza;
410 426
411 sessions[conn] = session; 427 sessions[conn] = session;
428
429 module:fire_event("c2s-connected", { conn = conn, session = session });
412 end 430 end
413 431
414 function listener.onincoming(conn, data) 432 function listener.onincoming(conn, data)
415 local session = sessions[conn]; 433 local session = sessions[conn];
416 if session then 434 if session then
420 438
421 function listener.ondisconnect(conn, err) 439 function listener.ondisconnect(conn, err)
422 local session = sessions[conn]; 440 local session = sessions[conn];
423 if session then 441 if session then
424 (session.log or log)("info", "Client disconnected: %s", err or "connection closed"); 442 (session.log or log)("info", "Client disconnected: %s", err or "connection closed");
443 stop_session_timers(session);
425 sm_destroy_session(session, err); 444 sm_destroy_session(session, err);
426 session.conn = nil; 445 session.conn = nil;
427 sessions[conn] = nil; 446 sessions[conn] = nil;
428 end 447 end
429 module:fire_event("c2s-closed", { session = session; conn = conn }); 448 module:fire_event("c2s-closed", { session = session; conn = conn });
461 sessions[conn] = session; 480 sessions[conn] = session;
462 end 481 end
463 482
464 function module.add_host(module) 483 function module.add_host(module)
465 module:hook("c2s-read-timeout", keepalive, -1); 484 module:hook("c2s-read-timeout", keepalive, -1);
485 module:hook("authentication-success", function(event)
486 local session = event.session;
487 if session.stream then
488 session.stanza_size_limit = authed_stanza_size_limit;
489 session.stream:set_stanza_size_limit(session.stanza_size_limit);
490 end
491 end);
466 end 492 end
467 493
468 module:hook("c2s-read-timeout", keepalive, -1); 494 module:hook("c2s-read-timeout", keepalive, -1);
469 495
470 module:hook("server-stopping", function(event) -- luacheck: ignore 212/event 496 module:hook("server-stopping", function(event) -- luacheck: ignore 212/event