Mercurial > prosody-modules
annotate mod_throttle_unsolicited/mod_throttle_unsolicited.lua @ 3335:04fe2b41da91
mod_http_index: Tweak header
author | Kim Alvefur <zash@zash.se> |
---|---|
date | Thu, 27 Sep 2018 16:56:44 +0200 |
parents | 231d47e61c81 |
children | 4d8a68557941 |
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); |
2324
1424aa8877f0
mod_throttle_unsolicited: Add support for throttling unsolicited messages on incoming s2s connections
Kim Alvefur <zash@zash.se>
parents:
2143
diff
changeset
|
9 local s2s_max = module:get_option_number("unsolicited_s2s_messages_per_minute"); |
2082
163d55777ad5
mod_throttle_unsolicited: Limit rate of unsolicited messages sent to non-contacts
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
10 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
|
11 |
163d55777ad5
mod_throttle_unsolicited: Limit rate of unsolicited messages sent to non-contacts
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
12 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
|
13 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
|
14 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
|
15 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
|
16 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
|
17 |
2142
d6fbb57a216c
mod_throttle_unsolicited: Skip checking messages to self
Kim Alvefur <zash@zash.se>
parents:
2120
diff
changeset
|
18 local to_orig = stanza.attr.to; |
d6fbb57a216c
mod_throttle_unsolicited: Skip checking messages to self
Kim Alvefur <zash@zash.se>
parents:
2120
diff
changeset
|
19 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
|
20 |
d6fbb57a216c
mod_throttle_unsolicited: Skip checking messages to self
Kim Alvefur <zash@zash.se>
parents:
2120
diff
changeset
|
21 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
|
22 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
|
23 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
|
24 |
2082
163d55777ad5
mod_throttle_unsolicited: Limit rate of unsolicited messages sent to non-contacts
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
25 -- 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
|
26 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
|
27 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
|
28 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
|
29 return |
163d55777ad5
mod_throttle_unsolicited: Limit rate of unsolicited messages sent to non-contacts
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
30 end |
163d55777ad5
mod_throttle_unsolicited: Limit rate of unsolicited messages sent to non-contacts
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
31 |
163d55777ad5
mod_throttle_unsolicited: Limit rate of unsolicited messages sent to non-contacts
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
32 -- 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
|
33 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
|
34 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
|
35 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
|
36 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
|
37 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
|
38 end |
163d55777ad5
mod_throttle_unsolicited: Limit rate of unsolicited messages sent to non-contacts
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
39 |
2142
d6fbb57a216c
mod_throttle_unsolicited: Skip checking messages to self
Kim Alvefur <zash@zash.se>
parents:
2120
diff
changeset
|
40 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
|
41 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
|
42 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
|
43 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
|
44 log("warn", "Sent too many messages to non-contacts, bouncing message"); |
2361
231d47e61c81
mod_throttle_unsolicited: Use existing local reference to origin
Kim Alvefur <zash@zash.se>
parents:
2324
diff
changeset
|
45 origin.firewall_mark_throttle_unsolicited = gettime(); |
231d47e61c81
mod_throttle_unsolicited: Use existing local reference to origin
Kim Alvefur <zash@zash.se>
parents:
2324
diff
changeset
|
46 origin.send(st.error_reply(stanza, "cancel", "service-unavailable")); |
2082
163d55777ad5
mod_throttle_unsolicited: Limit rate of unsolicited messages sent to non-contacts
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
47 return true; |
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 end |
163d55777ad5
mod_throttle_unsolicited: Limit rate of unsolicited messages sent to non-contacts
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
51 |
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/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
|
53 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
|
54 |
2324
1424aa8877f0
mod_throttle_unsolicited: Add support for throttling unsolicited messages on incoming s2s connections
Kim Alvefur <zash@zash.se>
parents:
2143
diff
changeset
|
55 local full_sessions = prosody.full_sessions; |
1424aa8877f0
mod_throttle_unsolicited: Add support for throttling unsolicited messages on incoming s2s connections
Kim Alvefur <zash@zash.se>
parents:
2143
diff
changeset
|
56 |
1424aa8877f0
mod_throttle_unsolicited: Add support for throttling unsolicited messages on incoming s2s connections
Kim Alvefur <zash@zash.se>
parents:
2143
diff
changeset
|
57 -- Rooms and throttle creation will differ for s2s |
1424aa8877f0
mod_throttle_unsolicited: Add support for throttling unsolicited messages on incoming s2s connections
Kim Alvefur <zash@zash.se>
parents:
2143
diff
changeset
|
58 function check_subscribed_s2s(event) |
1424aa8877f0
mod_throttle_unsolicited: Add support for throttling unsolicited messages on incoming s2s connections
Kim Alvefur <zash@zash.se>
parents:
2143
diff
changeset
|
59 local stanza, origin = event.stanza, event.origin; |
1424aa8877f0
mod_throttle_unsolicited: Add support for throttling unsolicited messages on incoming s2s connections
Kim Alvefur <zash@zash.se>
parents:
2143
diff
changeset
|
60 local log = origin.log or module._log; |
1424aa8877f0
mod_throttle_unsolicited: Add support for throttling unsolicited messages on incoming s2s connections
Kim Alvefur <zash@zash.se>
parents:
2143
diff
changeset
|
61 |
1424aa8877f0
mod_throttle_unsolicited: Add support for throttling unsolicited messages on incoming s2s connections
Kim Alvefur <zash@zash.se>
parents:
2143
diff
changeset
|
62 if origin.type ~= "s2sin" then return end |
1424aa8877f0
mod_throttle_unsolicited: Add support for throttling unsolicited messages on incoming s2s connections
Kim Alvefur <zash@zash.se>
parents:
2143
diff
changeset
|
63 |
1424aa8877f0
mod_throttle_unsolicited: Add support for throttling unsolicited messages on incoming s2s connections
Kim Alvefur <zash@zash.se>
parents:
2143
diff
changeset
|
64 local to_orig = stanza.attr.to; |
1424aa8877f0
mod_throttle_unsolicited: Add support for throttling unsolicited messages on incoming s2s connections
Kim Alvefur <zash@zash.se>
parents:
2143
diff
changeset
|
65 local from_orig = stanza.attr.from; |
1424aa8877f0
mod_throttle_unsolicited: Add support for throttling unsolicited messages on incoming s2s connections
Kim Alvefur <zash@zash.se>
parents:
2143
diff
changeset
|
66 local from_bare = jid_bare(from_orig); |
1424aa8877f0
mod_throttle_unsolicited: Add support for throttling unsolicited messages on incoming s2s connections
Kim Alvefur <zash@zash.se>
parents:
2143
diff
changeset
|
67 |
1424aa8877f0
mod_throttle_unsolicited: Add support for throttling unsolicited messages on incoming s2s connections
Kim Alvefur <zash@zash.se>
parents:
2143
diff
changeset
|
68 local target = full_sessions[to_orig]; |
1424aa8877f0
mod_throttle_unsolicited: Add support for throttling unsolicited messages on incoming s2s connections
Kim Alvefur <zash@zash.se>
parents:
2143
diff
changeset
|
69 if target then |
1424aa8877f0
mod_throttle_unsolicited: Add support for throttling unsolicited messages on incoming s2s connections
Kim Alvefur <zash@zash.se>
parents:
2143
diff
changeset
|
70 local rooms = target.rooms_joined; |
1424aa8877f0
mod_throttle_unsolicited: Add support for throttling unsolicited messages on incoming s2s connections
Kim Alvefur <zash@zash.se>
parents:
2143
diff
changeset
|
71 if rooms and rooms[from_bare] then |
1424aa8877f0
mod_throttle_unsolicited: Add support for throttling unsolicited messages on incoming s2s connections
Kim Alvefur <zash@zash.se>
parents:
2143
diff
changeset
|
72 log("debug", "Message to joined room, no limit"); |
1424aa8877f0
mod_throttle_unsolicited: Add support for throttling unsolicited messages on incoming s2s connections
Kim Alvefur <zash@zash.se>
parents:
2143
diff
changeset
|
73 return |
1424aa8877f0
mod_throttle_unsolicited: Add support for throttling unsolicited messages on incoming s2s connections
Kim Alvefur <zash@zash.se>
parents:
2143
diff
changeset
|
74 end |
1424aa8877f0
mod_throttle_unsolicited: Add support for throttling unsolicited messages on incoming s2s connections
Kim Alvefur <zash@zash.se>
parents:
2143
diff
changeset
|
75 end |
1424aa8877f0
mod_throttle_unsolicited: Add support for throttling unsolicited messages on incoming s2s connections
Kim Alvefur <zash@zash.se>
parents:
2143
diff
changeset
|
76 |
1424aa8877f0
mod_throttle_unsolicited: Add support for throttling unsolicited messages on incoming s2s connections
Kim Alvefur <zash@zash.se>
parents:
2143
diff
changeset
|
77 -- Retrieve or create throttle object |
1424aa8877f0
mod_throttle_unsolicited: Add support for throttling unsolicited messages on incoming s2s connections
Kim Alvefur <zash@zash.se>
parents:
2143
diff
changeset
|
78 local lim = origin.throttle_unsolicited; |
1424aa8877f0
mod_throttle_unsolicited: Add support for throttling unsolicited messages on incoming s2s connections
Kim Alvefur <zash@zash.se>
parents:
2143
diff
changeset
|
79 if not lim then |
1424aa8877f0
mod_throttle_unsolicited: Add support for throttling unsolicited messages on incoming s2s connections
Kim Alvefur <zash@zash.se>
parents:
2143
diff
changeset
|
80 log("debug", "New s2s throttle"); |
1424aa8877f0
mod_throttle_unsolicited: Add support for throttling unsolicited messages on incoming s2s connections
Kim Alvefur <zash@zash.se>
parents:
2143
diff
changeset
|
81 lim = throttle.create(s2s_max * multiplier, 60 * multiplier); |
1424aa8877f0
mod_throttle_unsolicited: Add support for throttling unsolicited messages on incoming s2s connections
Kim Alvefur <zash@zash.se>
parents:
2143
diff
changeset
|
82 origin.throttle_unsolicited = lim; |
1424aa8877f0
mod_throttle_unsolicited: Add support for throttling unsolicited messages on incoming s2s connections
Kim Alvefur <zash@zash.se>
parents:
2143
diff
changeset
|
83 end |
1424aa8877f0
mod_throttle_unsolicited: Add support for throttling unsolicited messages on incoming s2s connections
Kim Alvefur <zash@zash.se>
parents:
2143
diff
changeset
|
84 |
1424aa8877f0
mod_throttle_unsolicited: Add support for throttling unsolicited messages on incoming s2s connections
Kim Alvefur <zash@zash.se>
parents:
2143
diff
changeset
|
85 return check_subscribed(event); |
1424aa8877f0
mod_throttle_unsolicited: Add support for throttling unsolicited messages on incoming s2s connections
Kim Alvefur <zash@zash.se>
parents:
2143
diff
changeset
|
86 end |
1424aa8877f0
mod_throttle_unsolicited: Add support for throttling unsolicited messages on incoming s2s connections
Kim Alvefur <zash@zash.se>
parents:
2143
diff
changeset
|
87 |
1424aa8877f0
mod_throttle_unsolicited: Add support for throttling unsolicited messages on incoming s2s connections
Kim Alvefur <zash@zash.se>
parents:
2143
diff
changeset
|
88 if s2s_max then |
1424aa8877f0
mod_throttle_unsolicited: Add support for throttling unsolicited messages on incoming s2s connections
Kim Alvefur <zash@zash.se>
parents:
2143
diff
changeset
|
89 module:hook("message/bare", check_subscribed_s2s, 200); |
1424aa8877f0
mod_throttle_unsolicited: Add support for throttling unsolicited messages on incoming s2s connections
Kim Alvefur <zash@zash.se>
parents:
2143
diff
changeset
|
90 module:hook("message/full", check_subscribed_s2s, 200); |
1424aa8877f0
mod_throttle_unsolicited: Add support for throttling unsolicited messages on incoming s2s connections
Kim Alvefur <zash@zash.se>
parents:
2143
diff
changeset
|
91 end |
1424aa8877f0
mod_throttle_unsolicited: Add support for throttling unsolicited messages on incoming s2s connections
Kim Alvefur <zash@zash.se>
parents:
2143
diff
changeset
|
92 |
2082
163d55777ad5
mod_throttle_unsolicited: Limit rate of unsolicited messages sent to non-contacts
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
93 module:depends("track_muc_joins"); |