Переглянути джерело

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 роки тому
джерело
коміт
8dcf04897a
Аккаунт користувача з таким Email не знайдено
1 змінених файлів з 26 додано та 10 видалено
  1. 26
    10
      resources/prosody-plugins/mod_filter_iq_rayo.lua

+ 26
- 10
resources/prosody-plugins/mod_filter_iq_rayo.lua Переглянути файл

@@ -1,3 +1,4 @@
1
+local new_throttle = require "util.throttle".create;
1 2
 local st = require "util.stanza";
2 3
 
3 4
 local token_util = module:require "token/util".new(module);
@@ -10,12 +11,19 @@ if token_util == nil then
10 11
     return;
11 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 22
 -- Header names to use to push extra data extracted from token, if any
16 23
 local OUT_INITIATOR_USER_ATTR_NAME = "X-outbound-call-initiator-user";
17 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 28
 -- filters rayo iq in case of requested from not jwt authenticated sessions
20 29
 -- or if the session has features in user context and it doesn't mention
@@ -53,15 +61,21 @@ module:hook("pre-iq/full", function(event)
53 61
             end
54 62
 
55 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 79
             end
66 80
 
67 81
             -- now lets insert token information if any
@@ -163,3 +177,4 @@ function get_concurrent_outgoing_count(context_user, context_group)
163 177
     return count;
164 178
 end
165 179
 
180
+module:hook_global('config-reloaded', load_config);

Завантаження…
Відмінити
Зберегти