ip blocker only call next after validation (#1791)

* only call next after validation

* lets the blocked ips wait :)

* calles suggestion :)
This commit is contained in:
dni ⚡ 2023-07-03 10:26:35 +02:00 committed by GitHub
parent bc55d52ea2
commit fe393b83ab
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -1,3 +1,4 @@
import asyncio
from http import HTTPStatus from http import HTTPStatus
from typing import Any, List, Tuple, Union from typing import Any, List, Tuple, Union
from urllib.parse import parse_qs from urllib.parse import parse_qs
@ -205,19 +206,20 @@ def add_ratelimit_middleware(app: FastAPI):
def add_ip_block_middleware(app: FastAPI): def add_ip_block_middleware(app: FastAPI):
@app.middleware("http") @app.middleware("http")
async def block_allow_ip_middleware(request: Request, call_next): async def block_allow_ip_middleware(request: Request, call_next):
response = await call_next(request)
if not request.client: if not request.client:
return JSONResponse( return JSONResponse(
status_code=429, status_code=403, # Forbidden
content={"detail": "No request client"}, content={"detail": "No request client"},
) )
if request.client.host in settings.lnbits_allowed_ips: if (
return response request.client.host in settings.lnbits_blocked_ips
if request.client.host in settings.lnbits_blocked_ips: and request.client.host not in settings.lnbits_allowed_ips
):
await asyncio.sleep(5)
return JSONResponse( return JSONResponse(
status_code=429, status_code=403, # Forbidden
content={"detail": "IP is blocked"}, content={"detail": "IP is blocked"},
) )
return response return await call_next(request)
app.middleware("http")(block_allow_ip_middleware) app.middleware("http")(block_allow_ip_middleware)