Comparison

mod_throttle_unsolicited/mod_throttle_unsolicited.lua @ 2324:1424aa8877f0

mod_throttle_unsolicited: Add support for throttling unsolicited messages on incoming s2s connections
author Kim Alvefur <zash@zash.se>
date Tue, 04 Oct 2016 16:18:06 +0200
parent 2143:7cab309a26b2
child 2361:231d47e61c81
comparison
equal deleted inserted replaced
2323:35ae59a8196d 2324:1424aa8877f0
4 local is_contact_subscribed = require "core.rostermanager".is_contact_subscribed; 4 local is_contact_subscribed = require "core.rostermanager".is_contact_subscribed;
5 local throttle = require "util.throttle"; 5 local throttle = require "util.throttle";
6 local gettime = require "socket".gettime; 6 local gettime = require "socket".gettime;
7 7
8 local max = module:get_option_number("unsolicited_messages_per_minute", 10); 8 local max = module:get_option_number("unsolicited_messages_per_minute", 10);
9 local s2s_max = module:get_option_number("unsolicited_s2s_messages_per_minute");
9 local multiplier = module:get_option_number("throttle_unsolicited_burst", 1); 10 local multiplier = module:get_option_number("throttle_unsolicited_burst", 1);
10 11
11 function check_subscribed(event) 12 function check_subscribed(event)
12 local stanza, origin = event.stanza, event.origin; 13 local stanza, origin = event.stanza, event.origin;
13 local log = origin.log or module._log; 14 local log = origin.log or module._log;
49 end 50 end
50 51
51 module:hook("pre-message/bare", check_subscribed, 200); 52 module:hook("pre-message/bare", check_subscribed, 200);
52 module:hook("pre-message/full", check_subscribed, 200); 53 module:hook("pre-message/full", check_subscribed, 200);
53 54
55 local full_sessions = prosody.full_sessions;
56
57 -- Rooms and throttle creation will differ for s2s
58 function check_subscribed_s2s(event)
59 local stanza, origin = event.stanza, event.origin;
60 local log = origin.log or module._log;
61
62 if origin.type ~= "s2sin" then return end
63
64 local to_orig = stanza.attr.to;
65 local from_orig = stanza.attr.from;
66 local from_bare = jid_bare(from_orig);
67
68 local target = full_sessions[to_orig];
69 if target then
70 local rooms = target.rooms_joined;
71 if rooms and rooms[from_bare] then
72 log("debug", "Message to joined room, no limit");
73 return
74 end
75 end
76
77 -- Retrieve or create throttle object
78 local lim = origin.throttle_unsolicited;
79 if not lim then
80 log("debug", "New s2s throttle");
81 lim = throttle.create(s2s_max * multiplier, 60 * multiplier);
82 origin.throttle_unsolicited = lim;
83 end
84
85 return check_subscribed(event);
86 end
87
88 if s2s_max then
89 module:hook("message/bare", check_subscribed_s2s, 200);
90 module:hook("message/full", check_subscribed_s2s, 200);
91 end
92
54 module:depends("track_muc_joins"); 93 module:depends("track_muc_joins");