comparison plugins/mod_s2s.lua @ 14138:c1469296190d

Merge 13.0->trunk
author Matthew Wild <mwild1@gmail.com>
date Fri, 17 Apr 2026 14:35:32 +0100
parents b5eeb6730721 1e005ba71f0d
children 5aacae4b2ccf
comparison
equal deleted inserted replaced
14121:7008869fcce2 14138:c1469296190d
39 local opt_keepalives = module:get_option_boolean("s2s_tcp_keepalives", module:get_option_boolean("tcp_keepalives", true)); 39 local opt_keepalives = module:get_option_boolean("s2s_tcp_keepalives", module:get_option_boolean("tcp_keepalives", true));
40 local secure_auth = module:get_option_boolean("s2s_secure_auth", false); -- One day... 40 local secure_auth = module:get_option_boolean("s2s_secure_auth", false); -- One day...
41 local secure_domains, insecure_domains = 41 local secure_domains, insecure_domains =
42 module:get_option_set("s2s_secure_domains", {})._items, module:get_option_set("s2s_insecure_domains", {})._items; 42 module:get_option_set("s2s_secure_domains", {})._items, module:get_option_set("s2s_insecure_domains", {})._items;
43 local require_encryption = module:get_option_boolean("s2s_require_encryption", true); 43 local require_encryption = module:get_option_boolean("s2s_require_encryption", true);
44 local stanza_size_limit = module:get_option_integer("s2s_stanza_size_limit", 1024*512, 10000); 44 local unauthed_stanza_size_limit = module:get_option_integer("s2s_unauthed_stanza_size_limit", 10000, 1000);
45 local authed_stanza_size_limit = module:get_option_integer("s2s_stanza_size_limit", 1024*512, 10000);
46 local max_child_elements = module:get_option_integer("s2s_max_child_elements", 25000, 0);
45 local sendq_size = module:get_option_integer("s2s_send_queue_size", 1024*32, 1); 47 local sendq_size = module:get_option_integer("s2s_send_queue_size", 1024*32, 1);
46 local block_retries = module:get_option_boolean("s2s_block_immediate_retries", false); 48 local block_retries = module:get_option_boolean("s2s_block_immediate_retries", false);
47 49
48 local advertised_idle_timeout = 14*60; -- default in all net.server implementations 50 local advertised_idle_timeout = 14*60; -- default in all net.server implementations
49 local network_settings = module:get_option("network_settings"); 51 local network_settings = module:get_option("network_settings");
245 if block_retries then 247 if block_retries then
246 host_session.block_retries = true; 248 host_session.block_retries = true;
247 end 249 end
248 host_session.version = 1; 250 host_session.version = 1;
249 251
252 host_session.stanza_size_limit = unauthed_stanza_size_limit;
253
250 -- Store in buffer 254 -- Store in buffer
251 host_session.bounce_sendq = bounce_sendq; 255 host_session.bounce_sendq = bounce_sendq;
252 host_session.sendq = queue.new(sendq_size); 256 host_session.sendq = queue.new(sendq_size);
253 host_session.sendq:push(st.clone(stanza)); 257 host_session.sendq:push(st.clone(stanza));
254 log("debug", "stanza [%s] queued until connection complete", stanza.name); 258 log("debug", "stanza [%s] queued until connection complete", stanza.name);
289 return nil, "This host has disallow_s2s set"; 293 return nil, "This host has disallow_s2s set";
290 end 294 end
291 module:hook("route/remote", route_to_existing_session, -1); 295 module:hook("route/remote", route_to_existing_session, -1);
292 module:hook("route/remote", route_to_new_session, -10); 296 module:hook("route/remote", route_to_new_session, -10);
293 module:hook("s2sout-stream-features", function (event) 297 module:hook("s2sout-stream-features", function (event)
294 if not (stanza_size_limit or advertised_idle_timeout) then return end 298 local session = event.origin;
299 if not (session.stanza_size_limit or advertised_idle_timeout) then return end
295 local limits = event.features:tag("limits", { xmlns = "urn:xmpp:stream-limits:0" }) 300 local limits = event.features:tag("limits", { xmlns = "urn:xmpp:stream-limits:0" })
296 if stanza_size_limit then 301 if session.stanza_size_limit then
297 limits:text_tag("max-bytes", string.format("%d", stanza_size_limit)); 302 limits:text_tag("max-bytes", string.format("%d", session.stanza_size_limit));
298 end 303 end
299 if advertised_idle_timeout then 304 if advertised_idle_timeout then
300 limits:text_tag("idle-seconds", string.format("%d", advertised_idle_timeout)); 305 limits:text_tag("idle-seconds", string.format("%d", advertised_idle_timeout));
301 end 306 end
302 limits:up(); 307 limits:up();
434 session.type = "s2sin"; 439 session.type = "s2sin";
435 elseif session.type ~= "s2sin" and session.type ~= "s2sout" then 440 elseif session.type ~= "s2sin" and session.type ~= "s2sout" then
436 return false; 441 return false;
437 end 442 end
438 443
444 session.stanza_size_limit = authed_stanza_size_limit;
445 session.stream:set_stanza_size_limit(session.stanza_size_limit);
446
439 if session.incoming and host then 447 if session.incoming and host then
440 if not session.hosts[host] then session.hosts[host] = {}; end 448 if not session.hosts[host] then session.hosts[host] = {}; end
441 session.hosts[host].authed = true; 449 session.hosts[host].authed = true;
442 end 450 end
443 session.log("debug", "connection %s->%s is now authenticated for %s", session.from_host, session.to_host, host); 451 session.log("debug", "connection %s->%s is now authenticated for %s", session.from_host, session.to_host, host);
592 (session.log or log)("warn", "No 'to' on stream header from %s means we can't offer any features", from or session.ip or "unknown host"); 600 (session.log or log)("warn", "No 'to' on stream header from %s means we can't offer any features", from or session.ip or "unknown host");
593 module:fire_event("s2s-stream-features-legacy", { origin = session, features = features }); 601 module:fire_event("s2s-stream-features-legacy", { origin = session, features = features });
594 end 602 end
595 603
596 if ( session.type == "s2sin" or session.type == "s2sout" ) or features.tags[1] then 604 if ( session.type == "s2sin" or session.type == "s2sout" ) or features.tags[1] then
597 if stanza_size_limit or advertised_idle_timeout then 605 if session.stanza_size_limit or advertised_idle_timeout then
598 features:reset(); 606 features:reset();
599 local limits = features:tag("limits", { xmlns = "urn:xmpp:stream-limits:0" }); 607 local limits = features:tag("limits", { xmlns = "urn:xmpp:stream-limits:0" });
600 if stanza_size_limit then 608 if session.stanza_size_limit then
601 limits:text_tag("max-bytes", string.format("%d", stanza_size_limit)); 609 limits:text_tag("max-bytes", string.format("%d", session.stanza_size_limit));
602 end 610 end
603 if advertised_idle_timeout then 611 if advertised_idle_timeout then
604 limits:text_tag("idle-seconds", string.format("%d", advertised_idle_timeout)); 612 limits:text_tag("idle-seconds", string.format("%d", advertised_idle_timeout));
605 end 613 end
606 features:reset(); 614 features:reset();
813 end 821 end
814 end 822 end
815 823
816 -- Session initialization logic shared by incoming and outgoing 824 -- Session initialization logic shared by incoming and outgoing
817 local function initialize_session(session) 825 local function initialize_session(session)
818 local stream = new_xmpp_stream(session, stream_callbacks, stanza_size_limit); 826 local stream = new_xmpp_stream(session, stream_callbacks, session.stanza_size_limit, {
827 max_elements = max_child_elements;
828 });
819 829
820 session.thread = runner(function (item) 830 session.thread = runner(function (item)
821 if st.is_stanza(item) then 831 if st.is_stanza(item) then
822 core_process_stanza(session, item); 832 core_process_stanza(session, item);
823 else 833 else
931 m_accepted_tcp_connections:with_labels():add(1) 941 m_accepted_tcp_connections:with_labels():add(1)
932 else -- Outgoing session connected 942 else -- Outgoing session connected
933 module:fire_event("s2sout-connected", { session = session }) 943 module:fire_event("s2sout-connected", { session = session })
934 session:open_stream(session.from_host, session.to_host); 944 session:open_stream(session.from_host, session.to_host);
935 end 945 end
936 module:fire_event("s2s-connected", { session = session }) 946 module:fire_event("s2s-connected", { session = session, conn = conn })
937 session.ip = conn:ip(); 947 session.ip = conn:ip();
938 end 948 end
939 949
940 function listener.onincoming(conn, data) 950 function listener.onincoming(conn, data)
941 local session = sessions[conn]; 951 local session = sessions[conn];