Merge pull request #125 from BlessedRebuS/feat/improve-logging

fix connection exception issue
This commit is contained in:
Patrick Di Fazio
2026-03-09 18:24:08 +01:00
committed by GitHub
2 changed files with 10 additions and 16 deletions

View File

@@ -131,14 +131,7 @@ def create_app() -> FastAPI:
async def access_log_middleware(request: Request, call_next):
from dependencies import get_client_ip
try:
response: Response = await call_next(request)
except ConnectionResetError:
client_ip = get_client_ip(request)
path = request.url.path
method = request.method
get_access_logger().info(f"[BANNED] [{method}] {client_ip} - {path}")
raise
response: Response = await call_next(request)
client_ip = get_client_ip(request)
path = request.url.path

View File

@@ -12,13 +12,6 @@ from starlette.responses import Response
from dependencies import get_client_ip
class ConnectionResetResponse(Response):
"""Response that abruptly closes the connection without sending data."""
async def __call__(self, scope, receive, send):
raise ConnectionResetError()
class BanCheckMiddleware(BaseHTTPMiddleware):
async def dispatch(self, request: Request, call_next):
# Skip ban check for dashboard routes
@@ -31,7 +24,15 @@ class BanCheckMiddleware(BaseHTTPMiddleware):
tracker = request.app.state.tracker
if tracker.is_banned_ip(client_ip):
return ConnectionResetResponse()
from logger import get_access_logger
get_access_logger().info(
f"[BANNED] [{request.method}] {client_ip} - {request.url.path}"
)
transport = request.scope.get("transport")
if transport:
transport.close()
return Response(status_code=500)
response = await call_next(request)
return response