Annotate

mod_throttle_unsolicited/mod_throttle_unsolicited.lua @ 2297:992e40dab31d

mod_measure_storage: Experimental new module to measure storage API operations (requires 0.10)
author Matthew Wild <mwild1@gmail.com>
date Wed, 31 Aug 2016 16:24:32 +0100
parent 2143:7cab309a26b2
child 2324:1424aa8877f0
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
2082
163d55777ad5 mod_throttle_unsolicited: Limit rate of unsolicited messages sent to non-contacts
Kim Alvefur <zash@zash.se>
parents:
diff changeset
1 local st = require"util.stanza";
163d55777ad5 mod_throttle_unsolicited: Limit rate of unsolicited messages sent to non-contacts
Kim Alvefur <zash@zash.se>
parents:
diff changeset
2 local jid_split = require "util.jid".split;
163d55777ad5 mod_throttle_unsolicited: Limit rate of unsolicited messages sent to non-contacts
Kim Alvefur <zash@zash.se>
parents:
diff changeset
3 local jid_bare = require "util.jid".bare;
163d55777ad5 mod_throttle_unsolicited: Limit rate of unsolicited messages sent to non-contacts
Kim Alvefur <zash@zash.se>
parents:
diff changeset
4 local is_contact_subscribed = require "core.rostermanager".is_contact_subscribed;
163d55777ad5 mod_throttle_unsolicited: Limit rate of unsolicited messages sent to non-contacts
Kim Alvefur <zash@zash.se>
parents:
diff changeset
5 local throttle = require "util.throttle";
2120
f6dcfe263b85 mod_throttle_unsolicited: Mark sessions so they can be matched with 'ORIGIN_MARKED: throttle_unsolicited' by mod_firewall
Kim Alvefur <zash@zash.se>
parents: 2082
diff changeset
6 local gettime = require "socket".gettime;
2082
163d55777ad5 mod_throttle_unsolicited: Limit rate of unsolicited messages sent to non-contacts
Kim Alvefur <zash@zash.se>
parents:
diff changeset
7
163d55777ad5 mod_throttle_unsolicited: Limit rate of unsolicited messages sent to non-contacts
Kim Alvefur <zash@zash.se>
parents:
diff changeset
8 local max = module:get_option_number("unsolicited_messages_per_minute", 10);
163d55777ad5 mod_throttle_unsolicited: Limit rate of unsolicited messages sent to non-contacts
Kim Alvefur <zash@zash.se>
parents:
diff changeset
9 local multiplier = module:get_option_number("throttle_unsolicited_burst", 1);
163d55777ad5 mod_throttle_unsolicited: Limit rate of unsolicited messages sent to non-contacts
Kim Alvefur <zash@zash.se>
parents:
diff changeset
10
163d55777ad5 mod_throttle_unsolicited: Limit rate of unsolicited messages sent to non-contacts
Kim Alvefur <zash@zash.se>
parents:
diff changeset
11 function check_subscribed(event)
163d55777ad5 mod_throttle_unsolicited: Limit rate of unsolicited messages sent to non-contacts
Kim Alvefur <zash@zash.se>
parents:
diff changeset
12 local stanza, origin = event.stanza, event.origin;
163d55777ad5 mod_throttle_unsolicited: Limit rate of unsolicited messages sent to non-contacts
Kim Alvefur <zash@zash.se>
parents:
diff changeset
13 local log = origin.log or module._log;
163d55777ad5 mod_throttle_unsolicited: Limit rate of unsolicited messages sent to non-contacts
Kim Alvefur <zash@zash.se>
parents:
diff changeset
14 log("debug", "check_subscribed(%s)", stanza:top_tag());
163d55777ad5 mod_throttle_unsolicited: Limit rate of unsolicited messages sent to non-contacts
Kim Alvefur <zash@zash.se>
parents:
diff changeset
15 if stanza.attr.type == "error" then return end
163d55777ad5 mod_throttle_unsolicited: Limit rate of unsolicited messages sent to non-contacts
Kim Alvefur <zash@zash.se>
parents:
diff changeset
16
2142
d6fbb57a216c mod_throttle_unsolicited: Skip checking messages to self
Kim Alvefur <zash@zash.se>
parents: 2120
diff changeset
17 local to_orig = stanza.attr.to;
d6fbb57a216c mod_throttle_unsolicited: Skip checking messages to self
Kim Alvefur <zash@zash.se>
parents: 2120
diff changeset
18 if to_orig == nil or to_orig == origin.full_jid then return end -- to self
d6fbb57a216c mod_throttle_unsolicited: Skip checking messages to self
Kim Alvefur <zash@zash.se>
parents: 2120
diff changeset
19
d6fbb57a216c mod_throttle_unsolicited: Skip checking messages to self
Kim Alvefur <zash@zash.se>
parents: 2120
diff changeset
20 local to_bare = jid_bare(to_orig);
d6fbb57a216c mod_throttle_unsolicited: Skip checking messages to self
Kim Alvefur <zash@zash.se>
parents: 2120
diff changeset
21 local from_jid = jid_bare(stanza.attr.from);
d6fbb57a216c mod_throttle_unsolicited: Skip checking messages to self
Kim Alvefur <zash@zash.se>
parents: 2120
diff changeset
22 if to_bare == from_jid then return end -- to own resource
d6fbb57a216c mod_throttle_unsolicited: Skip checking messages to self
Kim Alvefur <zash@zash.se>
parents: 2120
diff changeset
23
2082
163d55777ad5 mod_throttle_unsolicited: Limit rate of unsolicited messages sent to non-contacts
Kim Alvefur <zash@zash.se>
parents:
diff changeset
24 -- Check if it's a message to a joined room
163d55777ad5 mod_throttle_unsolicited: Limit rate of unsolicited messages sent to non-contacts
Kim Alvefur <zash@zash.se>
parents:
diff changeset
25 local rooms = origin.rooms_joined;
163d55777ad5 mod_throttle_unsolicited: Limit rate of unsolicited messages sent to non-contacts
Kim Alvefur <zash@zash.se>
parents:
diff changeset
26 if rooms and rooms[to_bare] then
163d55777ad5 mod_throttle_unsolicited: Limit rate of unsolicited messages sent to non-contacts
Kim Alvefur <zash@zash.se>
parents:
diff changeset
27 log("debug", "Message to joined room, no limit");
163d55777ad5 mod_throttle_unsolicited: Limit rate of unsolicited messages sent to non-contacts
Kim Alvefur <zash@zash.se>
parents:
diff changeset
28 return
163d55777ad5 mod_throttle_unsolicited: Limit rate of unsolicited messages sent to non-contacts
Kim Alvefur <zash@zash.se>
parents:
diff changeset
29 end
163d55777ad5 mod_throttle_unsolicited: Limit rate of unsolicited messages sent to non-contacts
Kim Alvefur <zash@zash.se>
parents:
diff changeset
30
163d55777ad5 mod_throttle_unsolicited: Limit rate of unsolicited messages sent to non-contacts
Kim Alvefur <zash@zash.se>
parents:
diff changeset
31 -- Retrieve or create throttle object
163d55777ad5 mod_throttle_unsolicited: Limit rate of unsolicited messages sent to non-contacts
Kim Alvefur <zash@zash.se>
parents:
diff changeset
32 local lim = origin.throttle_unsolicited;
163d55777ad5 mod_throttle_unsolicited: Limit rate of unsolicited messages sent to non-contacts
Kim Alvefur <zash@zash.se>
parents:
diff changeset
33 if not lim then
163d55777ad5 mod_throttle_unsolicited: Limit rate of unsolicited messages sent to non-contacts
Kim Alvefur <zash@zash.se>
parents:
diff changeset
34 log("debug", "New throttle");
163d55777ad5 mod_throttle_unsolicited: Limit rate of unsolicited messages sent to non-contacts
Kim Alvefur <zash@zash.se>
parents:
diff changeset
35 lim = throttle.create(max * multiplier, 60 * multiplier);
163d55777ad5 mod_throttle_unsolicited: Limit rate of unsolicited messages sent to non-contacts
Kim Alvefur <zash@zash.se>
parents:
diff changeset
36 origin.throttle_unsolicited = lim;
163d55777ad5 mod_throttle_unsolicited: Limit rate of unsolicited messages sent to non-contacts
Kim Alvefur <zash@zash.se>
parents:
diff changeset
37 end
163d55777ad5 mod_throttle_unsolicited: Limit rate of unsolicited messages sent to non-contacts
Kim Alvefur <zash@zash.se>
parents:
diff changeset
38
2142
d6fbb57a216c mod_throttle_unsolicited: Skip checking messages to self
Kim Alvefur <zash@zash.se>
parents: 2120
diff changeset
39 local to_user, to_host = jid_split(to_orig);
2082
163d55777ad5 mod_throttle_unsolicited: Limit rate of unsolicited messages sent to non-contacts
Kim Alvefur <zash@zash.se>
parents:
diff changeset
40 if to_user and not is_contact_subscribed(to_user, to_host, from_jid) then
163d55777ad5 mod_throttle_unsolicited: Limit rate of unsolicited messages sent to non-contacts
Kim Alvefur <zash@zash.se>
parents:
diff changeset
41 log("debug", "%s is not subscribed to %s@%s", from_jid, to_user, to_host);
163d55777ad5 mod_throttle_unsolicited: Limit rate of unsolicited messages sent to non-contacts
Kim Alvefur <zash@zash.se>
parents:
diff changeset
42 if not lim:poll(1) then
163d55777ad5 mod_throttle_unsolicited: Limit rate of unsolicited messages sent to non-contacts
Kim Alvefur <zash@zash.se>
parents:
diff changeset
43 log("warn", "Sent too many messages to non-contacts, bouncing message");
2120
f6dcfe263b85 mod_throttle_unsolicited: Mark sessions so they can be matched with 'ORIGIN_MARKED: throttle_unsolicited' by mod_firewall
Kim Alvefur <zash@zash.se>
parents: 2082
diff changeset
44 event.origin.firewall_mark_throttle_unsolicited = gettime();
2082
163d55777ad5 mod_throttle_unsolicited: Limit rate of unsolicited messages sent to non-contacts
Kim Alvefur <zash@zash.se>
parents:
diff changeset
45 event.origin.send(st.error_reply(stanza, "cancel", "service-unavailable"));
163d55777ad5 mod_throttle_unsolicited: Limit rate of unsolicited messages sent to non-contacts
Kim Alvefur <zash@zash.se>
parents:
diff changeset
46 return true;
163d55777ad5 mod_throttle_unsolicited: Limit rate of unsolicited messages sent to non-contacts
Kim Alvefur <zash@zash.se>
parents:
diff changeset
47 end
163d55777ad5 mod_throttle_unsolicited: Limit rate of unsolicited messages sent to non-contacts
Kim Alvefur <zash@zash.se>
parents:
diff changeset
48 end
163d55777ad5 mod_throttle_unsolicited: Limit rate of unsolicited messages sent to non-contacts
Kim Alvefur <zash@zash.se>
parents:
diff changeset
49 end
163d55777ad5 mod_throttle_unsolicited: Limit rate of unsolicited messages sent to non-contacts
Kim Alvefur <zash@zash.se>
parents:
diff changeset
50
163d55777ad5 mod_throttle_unsolicited: Limit rate of unsolicited messages sent to non-contacts
Kim Alvefur <zash@zash.se>
parents:
diff changeset
51 module:hook("pre-message/bare", check_subscribed, 200);
163d55777ad5 mod_throttle_unsolicited: Limit rate of unsolicited messages sent to non-contacts
Kim Alvefur <zash@zash.se>
parents:
diff changeset
52 module:hook("pre-message/full", check_subscribed, 200);
163d55777ad5 mod_throttle_unsolicited: Limit rate of unsolicited messages sent to non-contacts
Kim Alvefur <zash@zash.se>
parents:
diff changeset
53
163d55777ad5 mod_throttle_unsolicited: Limit rate of unsolicited messages sent to non-contacts
Kim Alvefur <zash@zash.se>
parents:
diff changeset
54 module:depends("track_muc_joins");