feat(backend): block generic user agents
This commit is contained in:
parent
b4634f2a36
commit
fa88fa658e
2 changed files with 15 additions and 1 deletions
|
@ -1,6 +1,11 @@
|
|||
#!/usr/bin/env python3
|
||||
from datetime import datetime
|
||||
|
||||
BAD_BOTS = [
|
||||
'undici',
|
||||
'curl/(.*)',
|
||||
]
|
||||
|
||||
def today() -> str:
|
||||
now = datetime.now()
|
||||
return format_checkin_date(now)
|
||||
|
|
|
@ -2,9 +2,10 @@
|
|||
|
||||
from typing import Union
|
||||
import hashlib
|
||||
import re
|
||||
import sqlite3
|
||||
|
||||
from fastapi import Depends, FastAPI
|
||||
from fastapi import Depends, FastAPI, Request, Response
|
||||
from fastapi.middleware.cors import CORSMiddleware
|
||||
|
||||
import uvicorn
|
||||
|
@ -24,6 +25,14 @@ app.add_middleware(
|
|||
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('/')
|
||||
def index():
|
||||
return {
|
||||
|
|
Loading…
Reference in a new issue