2026-05-20 13:48:24 +02:00
|
|
|
from __future__ import annotations
|
|
|
|
|
|
|
|
|
|
import base64
|
|
|
|
|
import hashlib
|
|
|
|
|
import hmac
|
|
|
|
|
import secrets
|
2026-05-20 23:55:27 +02:00
|
|
|
import time
|
2026-05-20 13:48:24 +02:00
|
|
|
from datetime import UTC, datetime
|
|
|
|
|
from ipaddress import ip_address, ip_network
|
|
|
|
|
|
2026-05-20 17:57:42 +02:00
|
|
|
from fastapi import HTTPException, Request
|
2026-05-20 13:48:24 +02:00
|
|
|
|
2026-05-21 17:23:08 +02:00
|
|
|
from .settings import DdnsSettings
|
2026-05-20 13:48:24 +02:00
|
|
|
|
2026-05-20 23:55:27 +02:00
|
|
|
DASHBOARD_UNLOCK_COOKIE = "ddns_dashboard_unlock"
|
|
|
|
|
DASHBOARD_UNLOCK_DEFAULT_SECONDS = 24 * 60 * 60
|
|
|
|
|
DASHBOARD_UNLOCK_STAY_SIGNED_IN_SECONDS = 30 * 24 * 60 * 60
|
|
|
|
|
|
2026-05-20 13:48:24 +02:00
|
|
|
|
|
|
|
|
def credentials_from_basic_auth(header: str) -> tuple[str | None, str | None]:
|
|
|
|
|
try:
|
|
|
|
|
raw = base64.b64decode(header.split(" ", 1)[1]).decode("utf-8")
|
|
|
|
|
except Exception:
|
|
|
|
|
return None, None
|
|
|
|
|
if ":" not in raw:
|
|
|
|
|
return None, None
|
|
|
|
|
username, password = raw.split(":", 1)
|
|
|
|
|
return username, password
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def hash_secret(token: str) -> str:
|
|
|
|
|
salt = secrets.token_hex(16)
|
|
|
|
|
digest = hashlib.pbkdf2_hmac("sha256", token.encode("utf-8"), salt.encode("ascii"), 200_000)
|
|
|
|
|
return f"pbkdf2_sha256${salt}${digest.hex()}"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def verify_secret(token: str, stored: str) -> bool:
|
|
|
|
|
try:
|
|
|
|
|
algorithm, salt, expected = stored.split("$", 2)
|
|
|
|
|
except ValueError:
|
|
|
|
|
return False
|
|
|
|
|
if algorithm != "pbkdf2_sha256":
|
|
|
|
|
return False
|
|
|
|
|
digest = hashlib.pbkdf2_hmac("sha256", token.encode("utf-8"), salt.encode("ascii"), 200_000)
|
|
|
|
|
return hmac.compare_digest(digest.hex(), expected)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def hash_lookup_token(token: str) -> str:
|
|
|
|
|
return hashlib.sha256(token.encode("utf-8")).hexdigest()
|
|
|
|
|
|
|
|
|
|
|
2026-05-20 23:55:27 +02:00
|
|
|
def sign_dashboard_unlock_cookie(
|
|
|
|
|
hostname: str,
|
|
|
|
|
management_slug: str,
|
|
|
|
|
password_hash: str,
|
2026-05-21 10:05:40 +02:00
|
|
|
signing_secret: str,
|
2026-05-20 23:55:27 +02:00
|
|
|
max_age_seconds: int,
|
|
|
|
|
now: int | None = None,
|
|
|
|
|
) -> str:
|
|
|
|
|
timestamp = int(time.time() if now is None else now)
|
|
|
|
|
bounded_max_age = _bounded_dashboard_unlock_seconds(max_age_seconds)
|
2026-05-21 10:05:40 +02:00
|
|
|
signature = _dashboard_unlock_signature(
|
|
|
|
|
hostname,
|
|
|
|
|
management_slug,
|
|
|
|
|
password_hash,
|
|
|
|
|
signing_secret,
|
|
|
|
|
timestamp,
|
|
|
|
|
bounded_max_age,
|
|
|
|
|
)
|
|
|
|
|
return f"v3:{timestamp}:{bounded_max_age}:{signature}"
|
2026-05-20 23:55:27 +02:00
|
|
|
|
|
|
|
|
|
2026-05-21 10:05:40 +02:00
|
|
|
def verify_dashboard_unlock_cookie(
|
|
|
|
|
hostname: str,
|
|
|
|
|
management_slug: str,
|
|
|
|
|
password_hash: str | None,
|
|
|
|
|
signing_secret: str,
|
|
|
|
|
cookie_value: str | None,
|
|
|
|
|
) -> bool:
|
|
|
|
|
if not password_hash or not signing_secret or not cookie_value:
|
2026-05-20 23:55:27 +02:00
|
|
|
return False
|
|
|
|
|
try:
|
|
|
|
|
version, raw_timestamp, raw_max_age, supplied = cookie_value.split(":", 3)
|
|
|
|
|
timestamp = int(raw_timestamp)
|
|
|
|
|
max_age_seconds = _bounded_dashboard_unlock_seconds(int(raw_max_age))
|
|
|
|
|
except ValueError:
|
|
|
|
|
return False
|
2026-05-21 10:05:40 +02:00
|
|
|
if version != "v3":
|
2026-05-20 23:55:27 +02:00
|
|
|
return False
|
|
|
|
|
now = int(time.time())
|
|
|
|
|
if timestamp > now + 60 or now - timestamp > max_age_seconds:
|
|
|
|
|
return False
|
2026-05-21 10:05:40 +02:00
|
|
|
expected = _dashboard_unlock_signature(
|
|
|
|
|
hostname,
|
|
|
|
|
management_slug,
|
|
|
|
|
password_hash,
|
|
|
|
|
signing_secret,
|
|
|
|
|
timestamp,
|
|
|
|
|
max_age_seconds,
|
|
|
|
|
)
|
2026-05-20 23:55:27 +02:00
|
|
|
return hmac.compare_digest(supplied, expected)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def _dashboard_unlock_signature(
|
|
|
|
|
hostname: str,
|
|
|
|
|
management_slug: str,
|
|
|
|
|
password_hash: str,
|
2026-05-21 10:05:40 +02:00
|
|
|
signing_secret: str,
|
2026-05-20 23:55:27 +02:00
|
|
|
timestamp: int,
|
|
|
|
|
max_age_seconds: int,
|
|
|
|
|
) -> str:
|
2026-05-21 10:05:40 +02:00
|
|
|
payload = f"{hostname}\0{management_slug}\0{password_hash}\0{timestamp}\0{max_age_seconds}".encode()
|
|
|
|
|
return hmac.new(signing_secret.encode(), payload, hashlib.sha256).hexdigest()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def dashboard_cookie_signing_secret(settings: DdnsSettings) -> str:
|
|
|
|
|
return settings.dashboard_cookie_secret or settings.admin_password
|
2026-05-20 23:55:27 +02:00
|
|
|
|
|
|
|
|
|
|
|
|
|
def dashboard_csrf_token(management_slug: str, account: dict[str, str | int | None]) -> str:
|
|
|
|
|
hostname = str(account["hostname"])
|
|
|
|
|
password_hash = str(account.get("dashboard_password_hash") or "")
|
2026-05-20 13:48:24 +02:00
|
|
|
day = datetime.now(UTC).strftime("%Y-%m-%d")
|
2026-05-20 23:55:27 +02:00
|
|
|
key = f"{management_slug}\0{password_hash}".encode()
|
|
|
|
|
return hmac.new(key, f"dashboard:{hostname}:{day}".encode(), hashlib.sha256).hexdigest()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def require_dashboard_csrf(management_slug: str, account: dict[str, str | int | None], supplied: str) -> None:
|
|
|
|
|
if not supplied or not hmac.compare_digest(supplied, dashboard_csrf_token(management_slug, account)):
|
|
|
|
|
raise HTTPException(status_code=403, detail="invalid csrf token")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def _bounded_dashboard_unlock_seconds(max_age_seconds: int) -> int:
|
|
|
|
|
return max(60, min(max_age_seconds, DASHBOARD_UNLOCK_STAY_SIGNED_IN_SECONDS))
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def admin_csrf_token(settings: DdnsSettings) -> str:
|
|
|
|
|
day = datetime.now(UTC).strftime("%Y-%m-%d")
|
|
|
|
|
return hmac.new(settings.admin_password.encode("utf-8"), f"admin:{day}".encode(), hashlib.sha256).hexdigest()
|
2026-05-20 13:48:24 +02:00
|
|
|
|
|
|
|
|
|
|
|
|
|
def require_admin_csrf(settings: DdnsSettings, supplied: str) -> None:
|
|
|
|
|
if not supplied or not hmac.compare_digest(supplied, admin_csrf_token(settings)):
|
|
|
|
|
raise HTTPException(status_code=403, detail="invalid csrf token")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def client_ip(request: Request, settings: DdnsSettings | None = None) -> str:
|
|
|
|
|
peer = request.client.host if request.client else "unknown"
|
2026-05-21 17:34:48 +02:00
|
|
|
if settings and _trusted_proxy(peer, settings.trusted_proxy_ips):
|
|
|
|
|
for header_name in ("cf-connecting-ip", "x-forwarded-for", "x-real-ip"):
|
|
|
|
|
candidate = request.headers.get(header_name, "")
|
|
|
|
|
if candidate:
|
|
|
|
|
return candidate.split(",", 1)[0].strip()
|
2026-05-20 13:48:24 +02:00
|
|
|
return peer
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def is_rate_limited_path(path: str) -> bool:
|
2026-05-21 17:23:08 +02:00
|
|
|
return path == "/" or path.startswith(("/m/", "/api/v1/hostnames", "/api/v1/router/"))
|
2026-05-20 13:48:24 +02:00
|
|
|
|
|
|
|
|
|
2026-05-20 18:33:48 +02:00
|
|
|
def rate_limit_bucket(path: str) -> str:
|
2026-05-21 17:23:08 +02:00
|
|
|
for prefix in ("/m/", "/api/v1/hostnames", "/api/v1/router/"):
|
2026-05-20 18:33:48 +02:00
|
|
|
if path.startswith(prefix):
|
|
|
|
|
return prefix.rstrip("/")
|
|
|
|
|
return path
|
|
|
|
|
|
|
|
|
|
|
2026-05-20 13:48:24 +02:00
|
|
|
def _trusted_proxy(peer: str, trusted: set[str]) -> bool:
|
|
|
|
|
if not trusted:
|
|
|
|
|
return False
|
|
|
|
|
try:
|
|
|
|
|
peer_ip = ip_address(peer)
|
|
|
|
|
except ValueError:
|
|
|
|
|
return False
|
|
|
|
|
for candidate in trusted:
|
|
|
|
|
try:
|
|
|
|
|
if "/" in candidate and peer_ip in ip_network(candidate, strict=False):
|
|
|
|
|
return True
|
|
|
|
|
if "/" not in candidate and peer_ip == ip_address(candidate):
|
|
|
|
|
return True
|
|
|
|
|
except ValueError:
|
|
|
|
|
continue
|
|
|
|
|
return False
|