mirror of
https://github.com/jwinterm/modoboa.git
synced 2026-10-18 05:51:26 +02:00
Improved custom throttling
This commit is contained in:
@@ -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
@@ -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
|
||||
}
|
||||
@@ -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'
|
||||
|
||||
Reference in New Issue
Block a user