Server: Cache blocklist to file + disable httpx debug logs
- Blocklist now cached to data/blocklist_cache.txt - Only downloads once, then reuses cache - Disable httpx/httpcore debug logs to reduce noise
This commit is contained in:
+31
-3
@@ -1,4 +1,5 @@
|
|||||||
import re
|
import re
|
||||||
|
import logging
|
||||||
from contextlib import asynccontextmanager
|
from contextlib import asynccontextmanager
|
||||||
from datetime import datetime
|
from datetime import datetime
|
||||||
from pathlib import Path
|
from pathlib import Path
|
||||||
@@ -12,6 +13,10 @@ from fastapi import Depends, FastAPI, HTTPException, Request, Response
|
|||||||
from fastapi.responses import FileResponse, JSONResponse
|
from fastapi.responses import FileResponse, JSONResponse
|
||||||
from uvicorn.protocols.http.httptools_impl import HttpToolsProtocol
|
from uvicorn.protocols.http.httptools_impl import HttpToolsProtocol
|
||||||
|
|
||||||
|
# Disable httpx debug logging
|
||||||
|
logging.getLogger("httpx").setLevel(logging.WARNING)
|
||||||
|
logging.getLogger("httpcore").setLevel(logging.WARNING)
|
||||||
|
|
||||||
from pack_manager import DATA_DIR, scan_pack, get_cached_manifest, PACKS_DIR
|
from pack_manager import DATA_DIR, scan_pack, get_cached_manifest, PACKS_DIR
|
||||||
from models import PackMeta
|
from models import PackMeta
|
||||||
from middleware import LoggingMiddleware
|
from middleware import LoggingMiddleware
|
||||||
@@ -37,6 +42,9 @@ import middleware as mw
|
|||||||
# Public blocklists are loaded in lifespan (once, not per-worker)
|
# Public blocklists are loaded in lifespan (once, not per-worker)
|
||||||
MANUAL_BLOCKED_IPS = set(os.environ.get("BLOCKED_IPS", "").split(",")) - {""}
|
MANUAL_BLOCKED_IPS = set(os.environ.get("BLOCKED_IPS", "").split(",")) - {""}
|
||||||
|
|
||||||
|
# Cache file for blocklist (load once)
|
||||||
|
BLOCKLIST_CACHE_FILE = Path("data/blocklist_cache.txt")
|
||||||
|
|
||||||
|
|
||||||
@asynccontextmanager
|
@asynccontextmanager
|
||||||
async def lifespan(app: FastAPI):
|
async def lifespan(app: FastAPI):
|
||||||
@@ -45,12 +53,32 @@ async def lifespan(app: FastAPI):
|
|||||||
# Initialize logging
|
# Initialize logging
|
||||||
init_logging()
|
init_logging()
|
||||||
|
|
||||||
# Load public blocklists (only once, in main process)
|
# Load public blocklists (use cache if available)
|
||||||
USE_PUBLIC_BLOCKLIST = os.environ.get("PUBLIC_BLOCKLIST", "true").lower() == "true"
|
USE_PUBLIC_BLOCKLIST = os.environ.get("PUBLIC_BLOCKLIST", "true").lower() == "true"
|
||||||
all_blocked = set(MANUAL_BLOCKED_IPS)
|
all_blocked = set(MANUAL_BLOCKED_IPS)
|
||||||
|
|
||||||
if USE_PUBLIC_BLOCKLIST:
|
if USE_PUBLIC_BLOCKLIST:
|
||||||
public_ips = mw.load_public_blocklists()
|
cached_ips = set()
|
||||||
all_blocked.update(public_ips)
|
if BLOCKLIST_CACHE_FILE.exists():
|
||||||
|
try:
|
||||||
|
cached_ips = set(BLOCKLIST_CACHE_FILE.read_text().strip().splitlines())
|
||||||
|
logger.info(f"Loaded {len(cached_ips)} IPs from blocklist cache")
|
||||||
|
except Exception as e:
|
||||||
|
logger.warning(f"Failed to load blocklist cache: {e}")
|
||||||
|
cached_ips = set()
|
||||||
|
|
||||||
|
if not cached_ips:
|
||||||
|
cached_ips = mw.load_public_blocklists()
|
||||||
|
if cached_ips:
|
||||||
|
try:
|
||||||
|
DATA_DIR.mkdir(exist_ok=True)
|
||||||
|
BLOCKLIST_CACHE_FILE.write_text("\n".join(cached_ips))
|
||||||
|
logger.info(f"Saved {len(cached_ips)} IPs to blocklist cache")
|
||||||
|
except Exception as e:
|
||||||
|
logger.warning(f"Failed to save blocklist cache: {e}")
|
||||||
|
|
||||||
|
all_blocked.update(cached_ips)
|
||||||
|
|
||||||
mw.set_ip_config(blocked=all_blocked)
|
mw.set_ip_config(blocked=all_blocked)
|
||||||
logger.info(f"IP blocklist loaded: {len(all_blocked)} IPs")
|
logger.info(f"IP blocklist loaded: {len(all_blocked)} IPs")
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user