Browse Source

feat: Throttle out call attempts to the max number per minute (#7742)

* feat: Make possible to reload config for filter rayo iq.

* feat: Throttle out call attempts to the max number per minute

* squash: Updates comment about config
j8
Дамян Минков 4 years ago
parent
commit
8dcf04897a
No account linked to committer's email address
1 changed files with 26 additions and 10 deletions
  1. 26
    10
      resources/prosody-plugins/mod_filter_iq_rayo.lua

+ 26
- 10
resources/prosody-plugins/mod_filter_iq_rayo.lua View File

1
+local new_throttle = require "util.throttle".create;
1
 local st = require "util.stanza";
2
 local st = require "util.stanza";
2
 
3
 
3
 local token_util = module:require "token/util".new(module);
4
 local token_util = module:require "token/util".new(module);
10
     return;
11
     return;
11
 end
12
 end
12
 
13
 
13
-local LIMIT_OUTGOING_CALLS = module:get_option_number("max_number_outgoing_calls", -1);
14
+-- The maximum number of simultaneous calls,
15
+-- and also the maximum number of new calls per minute that a session is allowed to create.
16
+local limit_outgoing_calls;
17
+local function load_config()
18
+    limit_outgoing_calls = module:get_option_number("max_number_outgoing_calls", -1);
19
+end
20
+load_config();
14
 
21
 
15
 -- Header names to use to push extra data extracted from token, if any
22
 -- Header names to use to push extra data extracted from token, if any
16
 local OUT_INITIATOR_USER_ATTR_NAME = "X-outbound-call-initiator-user";
23
 local OUT_INITIATOR_USER_ATTR_NAME = "X-outbound-call-initiator-user";
17
 local OUT_INITIATOR_GROUP_ATTR_NAME = "X-outbound-call-initiator-group";
24
 local OUT_INITIATOR_GROUP_ATTR_NAME = "X-outbound-call-initiator-group";
25
+local OUTGOING_CALLS_THROTTLE_INTERVAL = 60; -- if max_number_outgoing_calls is enabled it will be
26
+                                             -- the max number of outgoing calls a user can try for a minute
18
 
27
 
19
 -- filters rayo iq in case of requested from not jwt authenticated sessions
28
 -- filters rayo iq in case of requested from not jwt authenticated sessions
20
 -- or if the session has features in user context and it doesn't mention
29
 -- or if the session has features in user context and it doesn't mention
53
             end
61
             end
54
 
62
 
55
             -- now lets check any limits if configured
63
             -- now lets check any limits if configured
56
-            if LIMIT_OUTGOING_CALLS > 0
57
-                and get_concurrent_outgoing_count(
58
-                        session.jitsi_meet_context_user["id"],
59
-                        session.jitsi_meet_context_group) >= LIMIT_OUTGOING_CALLS
60
-            then
61
-                module:log("warn",
62
-                    "Filtering stanza dial, stanza:%s, outgoing calls limit reached", tostring(stanza));
63
-                session.send(st.error_reply(stanza, "cancel", "resource-constraint"));
64
-                return true;
64
+            if limit_outgoing_calls > 0 then
65
+                if not session.dial_out_throttle then
66
+                    module:log("debug", "Enabling dial-out throttle session=%s.", session);
67
+                    session.dial_out_throttle = new_throttle(limit_outgoing_calls, OUTGOING_CALLS_THROTTLE_INTERVAL);
68
+                end
69
+
70
+                if not session.dial_out_throttle:poll(1) -- we first check the throttle so we can mark one incoming dial for the balance
71
+                    or get_concurrent_outgoing_count(session.jitsi_meet_context_user["id"], session.jitsi_meet_context_group)
72
+                            >= limit_outgoing_calls
73
+                then
74
+                    module:log("warn",
75
+                        "Filtering stanza dial, stanza:%s, outgoing calls limit reached", tostring(stanza));
76
+                    session.send(st.error_reply(stanza, "cancel", "resource-constraint"));
77
+                    return true;
78
+                end
65
             end
79
             end
66
 
80
 
67
             -- now lets insert token information if any
81
             -- now lets insert token information if any
163
     return count;
177
     return count;
164
 end
178
 end
165
 
179
 
180
+module:hook_global('config-reloaded', load_config);

Loading…
Cancel
Save