Annotate

mod_muc_limits/mod_muc_limits.lua @ 3165:7af4776a5dea

mod_http_upload_external: Log file type and size
author Matthew Wild <mwild1@gmail.com>
date Mon, 02 Jul 2018 12:56:51 +0100
parent 3098:a81456a13797
child 3188:5c3f3f5a4377
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
1057
0b41122b19f9 mod_muc_limits: Abort loading on non-MUC hosts (thanks Ge0rG)
Matthew Wild <mwild1@gmail.com>
parents: 1042
diff changeset
1
1768
163967467308 mod_muc_limits: Update to work with both the new and the old MUC API
Kim Alvefur <zash@zash.se>
parents: 1428
diff changeset
2 local mod_muc = module:depends"muc";
163967467308 mod_muc_limits: Update to work with both the new and the old MUC API
Kim Alvefur <zash@zash.se>
parents: 1428
diff changeset
3 local rooms = rawget(mod_muc, "rooms"); -- Old MUC API
1428
091ee76745e8 mod_muc_limits: Make compatible with new MUC API
Vadim Misbakh-Soloviov <mva@mva.name>
parents: 1205
diff changeset
4 if not rooms then
1768
163967467308 mod_muc_limits: Update to work with both the new and the old MUC API
Kim Alvefur <zash@zash.se>
parents: 1428
diff changeset
5 rooms = module:shared"muc/rooms"; -- New MUC API
1057
0b41122b19f9 mod_muc_limits: Abort loading on non-MUC hosts (thanks Ge0rG)
Matthew Wild <mwild1@gmail.com>
parents: 1042
diff changeset
6 end
554
a2b0174b5c48 mod_muc_limits: New module to impose overall rate-limits on a MUC (not on individual users)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
7
1042
5fd0860c86cd mod_muc_limits: Allow stanzas from affiliated users even if they are not in the room
Matthew Wild <mwild1@gmail.com>
parents: 1040
diff changeset
8 local jid_split, jid_bare = require "util.jid".split, require "util.jid".bare;
554
a2b0174b5c48 mod_muc_limits: New module to impose overall rate-limits on a MUC (not on individual users)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
9 local st = require "util.stanza";
a2b0174b5c48 mod_muc_limits: New module to impose overall rate-limits on a MUC (not on individual users)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
10 local new_throttle = require "util.throttle".create;
1038
edb06824a5a4 mod_muc_limits: Condense multiple dropped stanzas into a single log message every 5 seconds
Matthew Wild <mwild1@gmail.com>
parents: 1036
diff changeset
11 local t_insert, t_concat = table.insert, table.concat;
554
a2b0174b5c48 mod_muc_limits: New module to impose overall rate-limits on a MUC (not on individual users)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
12
557
14f39769c9e0 mod_muc_limits: Echo any MUC <x> or <body> in the error reply (required to make Gajim display the error)
Matthew Wild <mwild1@gmail.com>
parents: 556
diff changeset
13 local xmlns_muc = "http://jabber.org/protocol/muc";
14f39769c9e0 mod_muc_limits: Echo any MUC <x> or <body> in the error reply (required to make Gajim display the error)
Matthew Wild <mwild1@gmail.com>
parents: 556
diff changeset
14
554
a2b0174b5c48 mod_muc_limits: New module to impose overall rate-limits on a MUC (not on individual users)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
15 local period = math.max(module:get_option_number("muc_event_rate", 0.5), 0);
a2b0174b5c48 mod_muc_limits: New module to impose overall rate-limits on a MUC (not on individual users)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
16 local burst = math.max(module:get_option_number("muc_burst_factor", 6), 1);
a2b0174b5c48 mod_muc_limits: New module to impose overall rate-limits on a MUC (not on individual users)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
17
1036
a44e755f7579 mod_muc_limits: Add muc_max_nick_length option, defaulting to 23 (bytes)
Matthew Wild <mwild1@gmail.com>
parents: 1035
diff changeset
18 local max_nick_length = module:get_option_number("muc_max_nick_length", 23); -- Default chosen through scientific methods
1038
edb06824a5a4 mod_muc_limits: Condense multiple dropped stanzas into a single log message every 5 seconds
Matthew Wild <mwild1@gmail.com>
parents: 1036
diff changeset
19 local dropped_count = 0;
edb06824a5a4 mod_muc_limits: Condense multiple dropped stanzas into a single log message every 5 seconds
Matthew Wild <mwild1@gmail.com>
parents: 1036
diff changeset
20 local dropped_jids;
edb06824a5a4 mod_muc_limits: Condense multiple dropped stanzas into a single log message every 5 seconds
Matthew Wild <mwild1@gmail.com>
parents: 1036
diff changeset
21
edb06824a5a4 mod_muc_limits: Condense multiple dropped stanzas into a single log message every 5 seconds
Matthew Wild <mwild1@gmail.com>
parents: 1036
diff changeset
22 local function log_dropped()
edb06824a5a4 mod_muc_limits: Condense multiple dropped stanzas into a single log message every 5 seconds
Matthew Wild <mwild1@gmail.com>
parents: 1036
diff changeset
23 module:log("warn", "Dropped %d stanzas from %d JIDs: %s", dropped_count, #dropped_jids, t_concat(dropped_jids, ", "));
edb06824a5a4 mod_muc_limits: Condense multiple dropped stanzas into a single log message every 5 seconds
Matthew Wild <mwild1@gmail.com>
parents: 1036
diff changeset
24 dropped_count = 0;
edb06824a5a4 mod_muc_limits: Condense multiple dropped stanzas into a single log message every 5 seconds
Matthew Wild <mwild1@gmail.com>
parents: 1036
diff changeset
25 dropped_jids = nil;
edb06824a5a4 mod_muc_limits: Condense multiple dropped stanzas into a single log message every 5 seconds
Matthew Wild <mwild1@gmail.com>
parents: 1036
diff changeset
26 end
1036
a44e755f7579 mod_muc_limits: Add muc_max_nick_length option, defaulting to 23 (bytes)
Matthew Wild <mwild1@gmail.com>
parents: 1035
diff changeset
27
3098
a81456a13797 mod_client_proxy: a Jabber Address Translation implementation
Jonas Wielicki <jonas@wielicki.name>
parents: 1768
diff changeset
28 local function get_non_outcast_affiliations(room)
a81456a13797 mod_client_proxy: a Jabber Address Translation implementation
Jonas Wielicki <jonas@wielicki.name>
parents: 1768
diff changeset
29 local nmembers = 0;
a81456a13797 mod_client_proxy: a Jabber Address Translation implementation
Jonas Wielicki <jonas@wielicki.name>
parents: 1768
diff changeset
30 -- this is an evil hack, we probably should not access _affiliations
a81456a13797 mod_client_proxy: a Jabber Address Translation implementation
Jonas Wielicki <jonas@wielicki.name>
parents: 1768
diff changeset
31 -- directly ...
a81456a13797 mod_client_proxy: a Jabber Address Translation implementation
Jonas Wielicki <jonas@wielicki.name>
parents: 1768
diff changeset
32 for _, aff in pairs(room._affiliations) do
a81456a13797 mod_client_proxy: a Jabber Address Translation implementation
Jonas Wielicki <jonas@wielicki.name>
parents: 1768
diff changeset
33 if aff ~= "outcast" then
a81456a13797 mod_client_proxy: a Jabber Address Translation implementation
Jonas Wielicki <jonas@wielicki.name>
parents: 1768
diff changeset
34 nmembers = nmembers + 1;
a81456a13797 mod_client_proxy: a Jabber Address Translation implementation
Jonas Wielicki <jonas@wielicki.name>
parents: 1768
diff changeset
35 end
a81456a13797 mod_client_proxy: a Jabber Address Translation implementation
Jonas Wielicki <jonas@wielicki.name>
parents: 1768
diff changeset
36 end
a81456a13797 mod_client_proxy: a Jabber Address Translation implementation
Jonas Wielicki <jonas@wielicki.name>
parents: 1768
diff changeset
37 return nmembers;
a81456a13797 mod_client_proxy: a Jabber Address Translation implementation
Jonas Wielicki <jonas@wielicki.name>
parents: 1768
diff changeset
38 end
a81456a13797 mod_client_proxy: a Jabber Address Translation implementation
Jonas Wielicki <jonas@wielicki.name>
parents: 1768
diff changeset
39
554
a2b0174b5c48 mod_muc_limits: New module to impose overall rate-limits on a MUC (not on individual users)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
40 local function handle_stanza(event)
a2b0174b5c48 mod_muc_limits: New module to impose overall rate-limits on a MUC (not on individual users)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
41 local origin, stanza = event.origin, event.stanza;
555
2356ad05fdb6 mod_muc_limits: Don't limit room leaving
Matthew Wild <mwild1@gmail.com>
parents: 554
diff changeset
42 if stanza.name == "presence" and stanza.attr.type == "unavailable" then -- Don't limit room leaving
2356ad05fdb6 mod_muc_limits: Don't limit room leaving
Matthew Wild <mwild1@gmail.com>
parents: 554
diff changeset
43 return;
2356ad05fdb6 mod_muc_limits: Don't limit room leaving
Matthew Wild <mwild1@gmail.com>
parents: 554
diff changeset
44 end
1042
5fd0860c86cd mod_muc_limits: Allow stanzas from affiliated users even if they are not in the room
Matthew Wild <mwild1@gmail.com>
parents: 1040
diff changeset
45 local dest_room, dest_host, dest_nick = jid_split(stanza.attr.to);
1428
091ee76745e8 mod_muc_limits: Make compatible with new MUC API
Vadim Misbakh-Soloviov <mva@mva.name>
parents: 1205
diff changeset
46 local room = rooms[dest_room.."@"..dest_host];
554
a2b0174b5c48 mod_muc_limits: New module to impose overall rate-limits on a MUC (not on individual users)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
47 if not room then return; end
a2b0174b5c48 mod_muc_limits: New module to impose overall rate-limits on a MUC (not on individual users)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
48 local from_jid = stanza.attr.from;
a2b0174b5c48 mod_muc_limits: New module to impose overall rate-limits on a MUC (not on individual users)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
49 local occupant = room._occupants[room._jid_nick[from_jid]];
1042
5fd0860c86cd mod_muc_limits: Allow stanzas from affiliated users even if they are not in the room
Matthew Wild <mwild1@gmail.com>
parents: 1040
diff changeset
50 if (occupant and occupant.affiliation) or (not(occupant) and room._affiliations[jid_bare(from_jid)]) then
554
a2b0174b5c48 mod_muc_limits: New module to impose overall rate-limits on a MUC (not on individual users)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
51 module:log("debug", "Skipping stanza from affiliated user...");
a2b0174b5c48 mod_muc_limits: New module to impose overall rate-limits on a MUC (not on individual users)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
52 return;
1058
1255de347dd4 mod_muc_limits: Fix traceback on presence sent to the room's bare JID
Matthew Wild <mwild1@gmail.com>
parents: 1057
diff changeset
53 elseif dest_nick and max_nick_length and stanza.name == "presence" and not room._occupants[stanza.attr.to] and #dest_nick > max_nick_length then
1036
a44e755f7579 mod_muc_limits: Add muc_max_nick_length option, defaulting to 23 (bytes)
Matthew Wild <mwild1@gmail.com>
parents: 1035
diff changeset
54 module:log("debug", "Forbidding long (%d bytes) nick in %s", #dest_nick, dest_room)
a44e755f7579 mod_muc_limits: Add muc_max_nick_length option, defaulting to 23 (bytes)
Matthew Wild <mwild1@gmail.com>
parents: 1035
diff changeset
55 origin.send(st.error_reply(stanza, "modify", "policy-violation", "Your nick name is too long, please use a shorter one")
a44e755f7579 mod_muc_limits: Add muc_max_nick_length option, defaulting to 23 (bytes)
Matthew Wild <mwild1@gmail.com>
parents: 1035
diff changeset
56 :up():tag("x", { xmlns = xmlns_muc }));
a44e755f7579 mod_muc_limits: Add muc_max_nick_length option, defaulting to 23 (bytes)
Matthew Wild <mwild1@gmail.com>
parents: 1035
diff changeset
57 return true;
554
a2b0174b5c48 mod_muc_limits: New module to impose overall rate-limits on a MUC (not on individual users)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
58 end
a2b0174b5c48 mod_muc_limits: New module to impose overall rate-limits on a MUC (not on individual users)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
59 local throttle = room.throttle;
a2b0174b5c48 mod_muc_limits: New module to impose overall rate-limits on a MUC (not on individual users)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
60 if not room.throttle then
a2b0174b5c48 mod_muc_limits: New module to impose overall rate-limits on a MUC (not on individual users)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
61 throttle = new_throttle(period*burst, burst);
a2b0174b5c48 mod_muc_limits: New module to impose overall rate-limits on a MUC (not on individual users)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
62 room.throttle = throttle;
a2b0174b5c48 mod_muc_limits: New module to impose overall rate-limits on a MUC (not on individual users)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
63 end
3098
a81456a13797 mod_client_proxy: a Jabber Address Translation implementation
Jonas Wielicki <jonas@wielicki.name>
parents: 1768
diff changeset
64
a81456a13797 mod_client_proxy: a Jabber Address Translation implementation
Jonas Wielicki <jonas@wielicki.name>
parents: 1768
diff changeset
65 local cost = 1;
a81456a13797 mod_client_proxy: a Jabber Address Translation implementation
Jonas Wielicki <jonas@wielicki.name>
parents: 1768
diff changeset
66 -- we scale the cost by the inverse of the square root of the number of
a81456a13797 mod_client_proxy: a Jabber Address Translation implementation
Jonas Wielicki <jonas@wielicki.name>
parents: 1768
diff changeset
67 -- members; this should effectively raise the limit by a factor of
a81456a13797 mod_client_proxy: a Jabber Address Translation implementation
Jonas Wielicki <jonas@wielicki.name>
parents: 1768
diff changeset
68 -- sqrt(nmembers)
a81456a13797 mod_client_proxy: a Jabber Address Translation implementation
Jonas Wielicki <jonas@wielicki.name>
parents: 1768
diff changeset
69 local nmembers = math.max(get_non_outcast_affiliations(room), 1);
a81456a13797 mod_client_proxy: a Jabber Address Translation implementation
Jonas Wielicki <jonas@wielicki.name>
parents: 1768
diff changeset
70 cost = cost / math.sqrt(nmembers);
a81456a13797 mod_client_proxy: a Jabber Address Translation implementation
Jonas Wielicki <jonas@wielicki.name>
parents: 1768
diff changeset
71
a81456a13797 mod_client_proxy: a Jabber Address Translation implementation
Jonas Wielicki <jonas@wielicki.name>
parents: 1768
diff changeset
72 if not throttle:poll(cost) then
1038
edb06824a5a4 mod_muc_limits: Condense multiple dropped stanzas into a single log message every 5 seconds
Matthew Wild <mwild1@gmail.com>
parents: 1036
diff changeset
73 module:log("debug", "Dropping stanza for %s@%s from %s, over rate limit", dest_room, dest_host, from_jid);
edb06824a5a4 mod_muc_limits: Condense multiple dropped stanzas into a single log message every 5 seconds
Matthew Wild <mwild1@gmail.com>
parents: 1036
diff changeset
74 if not dropped_jids then
edb06824a5a4 mod_muc_limits: Condense multiple dropped stanzas into a single log message every 5 seconds
Matthew Wild <mwild1@gmail.com>
parents: 1036
diff changeset
75 dropped_jids = { [from_jid] = true, from_jid };
edb06824a5a4 mod_muc_limits: Condense multiple dropped stanzas into a single log message every 5 seconds
Matthew Wild <mwild1@gmail.com>
parents: 1036
diff changeset
76 module:add_timer(5, log_dropped);
edb06824a5a4 mod_muc_limits: Condense multiple dropped stanzas into a single log message every 5 seconds
Matthew Wild <mwild1@gmail.com>
parents: 1036
diff changeset
77 elseif not dropped_jids[from_jid] then
edb06824a5a4 mod_muc_limits: Condense multiple dropped stanzas into a single log message every 5 seconds
Matthew Wild <mwild1@gmail.com>
parents: 1036
diff changeset
78 dropped_jids[from_jid] = true;
edb06824a5a4 mod_muc_limits: Condense multiple dropped stanzas into a single log message every 5 seconds
Matthew Wild <mwild1@gmail.com>
parents: 1036
diff changeset
79 t_insert(dropped_jids, from_jid);
edb06824a5a4 mod_muc_limits: Condense multiple dropped stanzas into a single log message every 5 seconds
Matthew Wild <mwild1@gmail.com>
parents: 1036
diff changeset
80 end
edb06824a5a4 mod_muc_limits: Condense multiple dropped stanzas into a single log message every 5 seconds
Matthew Wild <mwild1@gmail.com>
parents: 1036
diff changeset
81 dropped_count = dropped_count + 1;
1205
7d2d440e2fa5 mod_muc_limits: Just drop error stanzas instead of bouncing them with more error stanzas
Kim Alvefur <zash@zash.se>
parents: 1058
diff changeset
82 if stanza.attr.type == "error" then -- We don't want to bounce errors
7d2d440e2fa5 mod_muc_limits: Just drop error stanzas instead of bouncing them with more error stanzas
Kim Alvefur <zash@zash.se>
parents: 1058
diff changeset
83 return true;
7d2d440e2fa5 mod_muc_limits: Just drop error stanzas instead of bouncing them with more error stanzas
Kim Alvefur <zash@zash.se>
parents: 1058
diff changeset
84 end
557
14f39769c9e0 mod_muc_limits: Echo any MUC <x> or <body> in the error reply (required to make Gajim display the error)
Matthew Wild <mwild1@gmail.com>
parents: 556
diff changeset
85 local reply = st.error_reply(stanza, "wait", "policy-violation", "The room is currently overactive, please try again later");
14f39769c9e0 mod_muc_limits: Echo any MUC <x> or <body> in the error reply (required to make Gajim display the error)
Matthew Wild <mwild1@gmail.com>
parents: 556
diff changeset
86 local body = stanza:get_child_text("body");
14f39769c9e0 mod_muc_limits: Echo any MUC <x> or <body> in the error reply (required to make Gajim display the error)
Matthew Wild <mwild1@gmail.com>
parents: 556
diff changeset
87 if body then
14f39769c9e0 mod_muc_limits: Echo any MUC <x> or <body> in the error reply (required to make Gajim display the error)
Matthew Wild <mwild1@gmail.com>
parents: 556
diff changeset
88 reply:up():tag("body"):text(body):up();
14f39769c9e0 mod_muc_limits: Echo any MUC <x> or <body> in the error reply (required to make Gajim display the error)
Matthew Wild <mwild1@gmail.com>
parents: 556
diff changeset
89 end
14f39769c9e0 mod_muc_limits: Echo any MUC <x> or <body> in the error reply (required to make Gajim display the error)
Matthew Wild <mwild1@gmail.com>
parents: 556
diff changeset
90 local x = stanza:get_child("x", xmlns_muc);
14f39769c9e0 mod_muc_limits: Echo any MUC <x> or <body> in the error reply (required to make Gajim display the error)
Matthew Wild <mwild1@gmail.com>
parents: 556
diff changeset
91 if x then
14f39769c9e0 mod_muc_limits: Echo any MUC <x> or <body> in the error reply (required to make Gajim display the error)
Matthew Wild <mwild1@gmail.com>
parents: 556
diff changeset
92 reply:add_child(st.clone(x));
14f39769c9e0 mod_muc_limits: Echo any MUC <x> or <body> in the error reply (required to make Gajim display the error)
Matthew Wild <mwild1@gmail.com>
parents: 556
diff changeset
93 end
14f39769c9e0 mod_muc_limits: Echo any MUC <x> or <body> in the error reply (required to make Gajim display the error)
Matthew Wild <mwild1@gmail.com>
parents: 556
diff changeset
94 origin.send(reply);
554
a2b0174b5c48 mod_muc_limits: New module to impose overall rate-limits on a MUC (not on individual users)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
95 return true;
a2b0174b5c48 mod_muc_limits: New module to impose overall rate-limits on a MUC (not on individual users)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
96 end
a2b0174b5c48 mod_muc_limits: New module to impose overall rate-limits on a MUC (not on individual users)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
97 end
a2b0174b5c48 mod_muc_limits: New module to impose overall rate-limits on a MUC (not on individual users)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
98
556
e50bdbaa7802 mod_muc_limits: Remove throttle object from all rooms on unload (to make sure new settings are applied on reload)
Matthew Wild <mwild1@gmail.com>
parents: 555
diff changeset
99 function module.unload()
1428
091ee76745e8 mod_muc_limits: Make compatible with new MUC API
Vadim Misbakh-Soloviov <mva@mva.name>
parents: 1205
diff changeset
100 for room_jid, room in pairs(rooms) do
556
e50bdbaa7802 mod_muc_limits: Remove throttle object from all rooms on unload (to make sure new settings are applied on reload)
Matthew Wild <mwild1@gmail.com>
parents: 555
diff changeset
101 room.throttle = nil;
e50bdbaa7802 mod_muc_limits: Remove throttle object from all rooms on unload (to make sure new settings are applied on reload)
Matthew Wild <mwild1@gmail.com>
parents: 555
diff changeset
102 end
e50bdbaa7802 mod_muc_limits: Remove throttle object from all rooms on unload (to make sure new settings are applied on reload)
Matthew Wild <mwild1@gmail.com>
parents: 555
diff changeset
103 end
e50bdbaa7802 mod_muc_limits: Remove throttle object from all rooms on unload (to make sure new settings are applied on reload)
Matthew Wild <mwild1@gmail.com>
parents: 555
diff changeset
104
1040
6574303a8169 mod_muc_limits: Increase hook priority
Matthew Wild <mwild1@gmail.com>
parents: 1038
diff changeset
105 module:hook("message/bare", handle_stanza, 501);
6574303a8169 mod_muc_limits: Increase hook priority
Matthew Wild <mwild1@gmail.com>
parents: 1038
diff changeset
106 module:hook("message/full", handle_stanza, 501);
6574303a8169 mod_muc_limits: Increase hook priority
Matthew Wild <mwild1@gmail.com>
parents: 1038
diff changeset
107 module:hook("presence/bare", handle_stanza, 501);
6574303a8169 mod_muc_limits: Increase hook priority
Matthew Wild <mwild1@gmail.com>
parents: 1038
diff changeset
108 module:hook("presence/full", handle_stanza, 501);