Refactor rate limiter and datetime formatting
This commit is contained in:
@@ -14,6 +14,7 @@ from urllib.parse import parse_qs, urlparse
|
||||
|
||||
import argon2
|
||||
|
||||
from . import datetime_str
|
||||
from .cleanup import ExpiredRecordsCleanupThread, RateLimitCleanupThread
|
||||
from .logging import clear_txn_id, set_txn_id
|
||||
from .models import DoesNotExist, get_hostname_for_user, get_user
|
||||
@@ -176,12 +177,12 @@ class DDNSRequestHandler(BaseHTTPRequestHandler):
|
||||
return
|
||||
|
||||
# Bad rate limit check
|
||||
if self.app.rate_limiter:
|
||||
blocked, retry = self.app.rate_limiter.is_blocked_bad(client_ip)
|
||||
if self.app.bad_limiter:
|
||||
blocked, retry_at = self.app.bad_limiter.is_blocked(client_ip)
|
||||
if blocked:
|
||||
logging.warning(
|
||||
f"Rate limited (bad requests): client={client_ip}, "
|
||||
f"retry_after={retry}")
|
||||
f"Rate limited (bad): client={client_ip}, "
|
||||
f"retry_at={datetime_str(retry_at)}")
|
||||
self.respond(429, "abuse")
|
||||
return
|
||||
|
||||
@@ -246,16 +247,18 @@ class DDNSRequestHandler(BaseHTTPRequestHandler):
|
||||
return
|
||||
|
||||
# Good rate limit check
|
||||
if self.app.rate_limiter:
|
||||
blocked, retry = self.app.rate_limiter.is_blocked_good(client_ip)
|
||||
if self.app.good_limiter:
|
||||
blocked, retry_at = self.app.good_limiter.is_blocked(client_ip)
|
||||
if blocked:
|
||||
logging.warning(f"Rate limited: client={client_ip}, retry_after={retry}")
|
||||
self.respond(429, "abuse", retry_after=retry)
|
||||
logging.warning(
|
||||
f"Rate limited: client={client_ip}, "
|
||||
f"retry_at={datetime_str(retry_at)}")
|
||||
self.respond(429, "abuse")
|
||||
return
|
||||
|
||||
# Record good request
|
||||
if self.app.rate_limiter:
|
||||
self.app.rate_limiter.record_good(client_ip)
|
||||
if self.app.good_limiter:
|
||||
self.app.good_limiter.record(client_ip)
|
||||
|
||||
# Determine IPs to update
|
||||
result = self._process_ip_update(hostname, params, endpoint, client_ip)
|
||||
@@ -268,8 +271,8 @@ class DDNSRequestHandler(BaseHTTPRequestHandler):
|
||||
|
||||
def _handle_bad_request(self, client_ip, code, status):
|
||||
"""Handle bad request and record in rate limiter."""
|
||||
if self.app.rate_limiter:
|
||||
self.app.rate_limiter.record_bad(client_ip)
|
||||
if self.app.bad_limiter:
|
||||
self.app.bad_limiter.record(client_ip)
|
||||
self.respond(code, status)
|
||||
|
||||
def _process_ip_update(self, hostname, params, endpoint, client_ip):
|
||||
|
||||
Reference in New Issue
Block a user