Mercurial > prosody-modules
annotate mod_throttle_unsolicited/mod_throttle_unsolicited.lua @ 2131:ba42c8882026
mod_firewall: Fix another unprotected use of util.cache
author | Matthew Wild <mwild1@gmail.com> |
---|---|
date | Fri, 18 Mar 2016 09:59:42 +0000 |
parents | f6dcfe263b85 |
children | d6fbb57a216c |
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 sessions = prosody.full_sessions; |
163d55777ad5
mod_throttle_unsolicited: Limit rate of unsolicited messages sent to non-contacts
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
9 |
163d55777ad5
mod_throttle_unsolicited: Limit rate of unsolicited messages sent to non-contacts
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
10 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
|
11 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
|
12 |
163d55777ad5
mod_throttle_unsolicited: Limit rate of unsolicited messages sent to non-contacts
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
13 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
|
14 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
|
15 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
|
16 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
|
17 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
|
18 |
163d55777ad5
mod_throttle_unsolicited: Limit rate of unsolicited messages sent to non-contacts
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
19 -- 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
|
20 local to_bare = jid_bare(stanza.attr.to); |
163d55777ad5
mod_throttle_unsolicited: Limit rate of unsolicited messages sent to non-contacts
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
21 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
|
22 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
|
23 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
|
24 return |
163d55777ad5
mod_throttle_unsolicited: Limit rate of unsolicited messages sent to non-contacts
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
25 end |
163d55777ad5
mod_throttle_unsolicited: Limit rate of unsolicited messages sent to non-contacts
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
26 |
163d55777ad5
mod_throttle_unsolicited: Limit rate of unsolicited messages sent to non-contacts
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
27 -- 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
|
28 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
|
29 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
|
30 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
|
31 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
|
32 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
|
33 end |
163d55777ad5
mod_throttle_unsolicited: Limit rate of unsolicited messages sent to non-contacts
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
34 |
163d55777ad5
mod_throttle_unsolicited: Limit rate of unsolicited messages sent to non-contacts
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
35 local to_user, to_host = jid_split(stanza.attr.to); |
163d55777ad5
mod_throttle_unsolicited: Limit rate of unsolicited messages sent to non-contacts
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
36 local from_jid = jid_bare(stanza.attr.from); |
163d55777ad5
mod_throttle_unsolicited: Limit rate of unsolicited messages sent to non-contacts
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
37 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
|
38 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
|
39 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
|
40 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
|
41 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
|
42 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
|
43 return true; |
163d55777ad5
mod_throttle_unsolicited: Limit rate of unsolicited messages sent to non-contacts
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
44 end |
163d55777ad5
mod_throttle_unsolicited: Limit rate of unsolicited messages sent to non-contacts
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
45 end |
163d55777ad5
mod_throttle_unsolicited: Limit rate of unsolicited messages sent to non-contacts
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
46 end |
163d55777ad5
mod_throttle_unsolicited: Limit rate of unsolicited messages sent to non-contacts
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
47 |
163d55777ad5
mod_throttle_unsolicited: Limit rate of unsolicited messages sent to non-contacts
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
48 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
|
49 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
|
50 |
163d55777ad5
mod_throttle_unsolicited: Limit rate of unsolicited messages sent to non-contacts
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
51 module:depends("track_muc_joins"); |