Comparison

plugins/mod_register.lua @ 7026:f0dc5cc11d0e

mod_register: Use util.cache to limit the number of per-ip throttles kept
author Kim Alvefur <zash@zash.se>
date Wed, 23 Dec 2015 08:58:34 +0100
parent 7025:236e8d1ee96c
child 7027:77d838ba91c6
comparison
equal deleted inserted replaced
7025:236e8d1ee96c 7026:f0dc5cc11d0e
14 local usermanager_set_password = require "core.usermanager".set_password; 14 local usermanager_set_password = require "core.usermanager".set_password;
15 local usermanager_delete_user = require "core.usermanager".delete_user; 15 local usermanager_delete_user = require "core.usermanager".delete_user;
16 local nodeprep = require "util.encodings".stringprep.nodeprep; 16 local nodeprep = require "util.encodings".stringprep.nodeprep;
17 local jid_bare = require "util.jid".bare; 17 local jid_bare = require "util.jid".bare;
18 local create_throttle = require "util.throttle".create; 18 local create_throttle = require "util.throttle".create;
19 local new_cache = require "util.cache".new;
19 20
20 local compat = module:get_option_boolean("registration_compat", true); 21 local compat = module:get_option_boolean("registration_compat", true);
21 local allow_registration = module:get_option_boolean("allow_registration", false); 22 local allow_registration = module:get_option_boolean("allow_registration", false);
22 local additional_fields = module:get_option("additional_registration_fields", {}); 23 local additional_fields = module:get_option("additional_registration_fields", {});
23 24
169 end 170 end
170 return data; 171 return data;
171 end 172 end
172 end 173 end
173 174
174 local recent_ips = {};
175 local min_seconds_between_registrations = module:get_option_number("min_seconds_between_registrations"); 175 local min_seconds_between_registrations = module:get_option_number("min_seconds_between_registrations");
176 local whitelist_only = module:get_option_boolean("whitelist_registration_only"); 176 local whitelist_only = module:get_option_boolean("whitelist_registration_only");
177 local whitelisted_ips = module:get_option_set("registration_whitelist", { "127.0.0.1" })._items; 177 local whitelisted_ips = module:get_option_set("registration_whitelist", { "127.0.0.1" })._items;
178 local blacklisted_ips = module:get_option_set("registration_blacklist", {})._items; 178 local blacklisted_ips = module:get_option_set("registration_blacklist", {})._items;
179 179
180 local throttle_max = module:get_option_number("registration_throttle_max", min_seconds_between_registrations and 1); 180 local throttle_max = module:get_option_number("registration_throttle_max", min_seconds_between_registrations and 1);
181 local throttle_period = module:get_option_number("registration_throttle_period", min_seconds_between_registrations); 181 local throttle_period = module:get_option_number("registration_throttle_period", min_seconds_between_registrations);
182 local throttle_cache_size = module:get_option_number("registration_throttle_cache_size", 100);
183
184 local throttle_cache = new_cache(throttle_cache_size);
182 185
183 local function check_throttle(ip) 186 local function check_throttle(ip)
184 if not throttle_max then return true end 187 if not throttle_max then return true end
185 local throttle = recent_ips[ip]; 188 local throttle = throttle_cache:get(ip);
186 if not throttle then 189 if not throttle then
187 throttle = create_throttle(throttle_max, throttle_period); 190 throttle = create_throttle(throttle_max, throttle_period);
188 recent_ips[ip] = throttle; 191 end
189 end 192 throttle_cache:set(ip, throttle);
190 return throttle:poll(1); 193 return throttle:poll(1);
191 end 194 end
192 195
193 module:hook("stanza/iq/jabber:iq:register:query", function(event) 196 module:hook("stanza/iq/jabber:iq:register:query", function(event)
194 local session, stanza = event.origin, event.stanza; 197 local session, stanza = event.origin, event.stanza;