feat(backend): block generic user agents

This commit is contained in:
Ashhhleyyy 2024-03-11 18:18:49 +00:00
parent b4634f2a36
commit fa88fa658e
Signed by: ash
GPG key ID: 83B789081A0878FB
2 changed files with 15 additions and 1 deletions

View file

@ -1,6 +1,11 @@
#!/usr/bin/env python3 #!/usr/bin/env python3
from datetime import datetime from datetime import datetime
BAD_BOTS = [
'undici',
'curl/(.*)',
]
def today() -> str: def today() -> str:
now = datetime.now() now = datetime.now()
return format_checkin_date(now) return format_checkin_date(now)

View file

@ -2,9 +2,10 @@
from typing import Union from typing import Union
import hashlib import hashlib
import re
import sqlite3 import sqlite3
from fastapi import Depends, FastAPI from fastapi import Depends, FastAPI, Request, Response
from fastapi.middleware.cors import CORSMiddleware from fastapi.middleware.cors import CORSMiddleware
import uvicorn import uvicorn
@ -24,6 +25,14 @@ app.add_middleware(
allow_headers="*", allow_headers="*",
) )
@app.middleware('http')
async def badbots_block(req: Request, call_next):
ua = req.headers.get('User-Agent', '')
for bot in utils.BAD_BOTS:
if re.match(bot, ua):
return Response('forbidden', status_code=403)
return await call_next(req)
@app.get('/') @app.get('/')
def index(): def index():
return { return {