Improved custom throttling

This commit is contained in:
Spitap
2023-01-27 16:55:45 +01:00
parent 0d2f3d05a3
commit 2e9f43d3a7
3 changed files with 12 additions and 22 deletions
@@ -196,14 +196,12 @@ REST_FRAMEWORK = {
'DEFAULT_THROTTLE_CLASSES': [
'rest_framework.throttling.AnonRateThrottle',
'rest_framework.throttling.ScopedRateThrottle',
'modoboa.lib.throttle.UserDosThrottle',
'modoboa.lib.throttle.AnonDosThrottle'
'modoboa.lib.throttle.UserDosThrottle'
],
'DEFAULT_THROTTLE_RATES': {
'anon': '100/hour',
'login': '3/minute',
'ddos': '100/minute',
'anon_ddos': '10/minute',
'password_recovery_request': '6/hour',
'password_recovery_totp_check': '20/hour',
'password_recovery_apply': '20/hour'
+10 -16
View File
@@ -1,23 +1,17 @@
from rest_framework.throttling import UserRateThrottle, AnonRateThrottle
from rest_framework.throttling import SimpleRateThrottle
class UserDosThrottle(UserRateThrottle):
class UserDosThrottle(SimpleRateThrottle):
"""Custom Throttle class for rest_framework. The throttling is applied on a per view basis for authentificated users."""
scope = "ddos"
def get_cache_key(self, request, view):
return "throttle_{viewid}_{indent}".format(
viewid=id(view),
indent=self.get_indent(request)
)
if request.user and request.user.is_authenticated:
ident = request.user.pk
else:
ident = self.get_ident(request)
class AnonDosThrottle(AnonRateThrottle):
"""Custom Throttle class for rest_framework. The throttling is applied on a per view basis for visitors."""
scope = "anon_ddos"
def get_cache_key(self, request, view):
return "throttle_{viewid}_{indent}".format(
viewid=id(view),
indent=self.get_indent(request)
)
return self.cache_format % {
'scope': id(view),
'ident': ident
}
+1 -3
View File
@@ -189,14 +189,12 @@ REST_FRAMEWORK = {
'DEFAULT_THROTTLE_CLASSES': [
'rest_framework.throttling.AnonRateThrottle',
'rest_framework.throttling.ScopedRateThrottle',
'modoboa.lib.throttle.UserDosThrottle',
'modoboa.lib.throttle.AnonDosThrottle'
'modoboa.lib.throttle.UserDosThrottle'
],
'DEFAULT_THROTTLE_RATES': {
'anon': '100/hour',
'login': '3/minute',
'ddos': '100/minute',
'anon_ddos': '10/minute',
'password_recovery_request': '6/hour',
'password_recovery_totp_check': '20/hour',
'password_recovery_apply': '20/hour'