Archived
177 lines
6.8 KiB
Python
177 lines
6.8 KiB
Python
from __future__ import annotations
|
|
|
|
from fastapi import APIRouter, Depends, HTTPException, Query, Request, Response
|
|
from fastapi.responses import HTMLResponse, PlainTextResponse, RedirectResponse
|
|
from starlette.concurrency import run_in_threadpool
|
|
|
|
from ..dependencies import ServiceDep, SettingsDep, StoreDep, authenticate_admin
|
|
from ..dns import DnsProviderError
|
|
from ..event_statuses import (
|
|
ADMIN_HOSTNAME_DELETED,
|
|
ADMIN_MANAGEMENT_URL_ROTATED,
|
|
ADMIN_UPDATE_URL_ROTATED,
|
|
)
|
|
from ..security import client_ip, require_admin_csrf
|
|
from ..services.ddns import normalize_hostname
|
|
from ..web.components import _copy_row
|
|
from ..web.forms import first_form_value, request_form
|
|
from ..web.templates_admin import _admin_secret_panel, _render_admin_page
|
|
|
|
ADMIN_TABLE_LIMIT = 100
|
|
ADMIN_JSON_OFFSET_CAP = 10_000
|
|
|
|
|
|
def make_router(admin_path: str = "/admin") -> APIRouter:
|
|
router = APIRouter()
|
|
|
|
def render_admin(
|
|
service: ServiceDep,
|
|
settings: SettingsDep,
|
|
store: StoreDep,
|
|
table_offsets: dict[str, int] | None = None,
|
|
notice: str | None = None,
|
|
) -> str:
|
|
table_offsets = table_offsets or {}
|
|
return _render_admin_page(
|
|
service,
|
|
settings,
|
|
store.list_accounts(limit=ADMIN_TABLE_LIMIT, offset=table_offsets.get("accounts", 0)),
|
|
store.list_cleanup_runs(limit=ADMIN_TABLE_LIMIT, offset=table_offsets.get("cleanup_runs", 0)),
|
|
store.list_scheduled_tasks(),
|
|
store.list_update_events(ADMIN_TABLE_LIMIT, offset=table_offsets.get("update_events", 0)),
|
|
store.get_service_metrics(),
|
|
{
|
|
"accounts": store.count_accounts(),
|
|
"cleanup_runs": store.count_cleanup_runs(),
|
|
"update_events": store.count_update_events(),
|
|
},
|
|
table_offsets,
|
|
ADMIN_TABLE_LIMIT,
|
|
notice,
|
|
)
|
|
|
|
@router.get(admin_path, response_class=HTMLResponse, include_in_schema=False)
|
|
def admin(
|
|
settings: SettingsDep,
|
|
store: StoreDep,
|
|
service: ServiceDep,
|
|
_: None = Depends(authenticate_admin),
|
|
accounts_offset: int = Query(0, ge=0),
|
|
cleanup_runs_offset: int = Query(0, ge=0),
|
|
update_events_offset: int = Query(0, ge=0),
|
|
) -> str:
|
|
return render_admin(
|
|
service,
|
|
settings,
|
|
store,
|
|
{
|
|
"accounts": accounts_offset,
|
|
"cleanup_runs": cleanup_runs_offset,
|
|
"update_events": update_events_offset,
|
|
},
|
|
)
|
|
|
|
@router.post(f"{admin_path}/hostnames/delete", include_in_schema=False)
|
|
async def admin_delete_account(
|
|
request: Request,
|
|
settings: SettingsDep,
|
|
store: StoreDep,
|
|
service: ServiceDep,
|
|
_: None = Depends(authenticate_admin),
|
|
) -> Response:
|
|
form = await request_form(request, settings)
|
|
require_admin_csrf(settings, first_form_value(form, "csrf"))
|
|
hostname = normalize_hostname(first_form_value(form, "hostname"), settings, expand_suffix=False)
|
|
if hostname:
|
|
try:
|
|
await run_in_threadpool(service.delete_account, hostname, "admin")
|
|
except DnsProviderError:
|
|
return PlainTextResponse("DNS delete failed; hostname was not deleted", status_code=500)
|
|
await run_in_threadpool(
|
|
store.log_update_event,
|
|
hostname,
|
|
None,
|
|
None,
|
|
ADMIN_HOSTNAME_DELETED,
|
|
"hostname deleted by admin",
|
|
client_ip(request, settings),
|
|
)
|
|
return RedirectResponse(admin_path, status_code=303)
|
|
|
|
@router.post(f"{admin_path}/hostnames/rotate", include_in_schema=False)
|
|
async def admin_rotate_account(
|
|
request: Request,
|
|
settings: SettingsDep,
|
|
store: StoreDep,
|
|
service: ServiceDep,
|
|
_: None = Depends(authenticate_admin),
|
|
) -> Response:
|
|
form = await request_form(request, settings)
|
|
require_admin_csrf(settings, first_form_value(form, "csrf"))
|
|
hostname = normalize_hostname(first_form_value(form, "hostname"), settings, expand_suffix=False)
|
|
action = first_form_value(form, "action")
|
|
account = await run_in_threadpool(store.get_account_by_hostname, hostname)
|
|
if not account:
|
|
raise HTTPException(status_code=404, detail="hostname not found")
|
|
|
|
notice = None
|
|
if action == "update":
|
|
rotated = await run_in_threadpool(store.rotate_update_slug, hostname)
|
|
await run_in_threadpool(
|
|
store.log_update_event,
|
|
hostname,
|
|
None,
|
|
None,
|
|
ADMIN_UPDATE_URL_ROTATED,
|
|
"router update URL rotated by admin",
|
|
client_ip(request, settings),
|
|
)
|
|
notice = _admin_secret_panel("Update URL changed", [_copy_row("Update-URL:", service.fritz_update_url(rotated), "Secret")]) if rotated else None
|
|
elif action == "management":
|
|
rotated = await run_in_threadpool(store.rotate_management_slug, hostname)
|
|
await run_in_threadpool(
|
|
store.log_update_event,
|
|
hostname,
|
|
None,
|
|
None,
|
|
ADMIN_MANAGEMENT_URL_ROTATED,
|
|
"dashboard link rotated by admin",
|
|
client_ip(request, settings),
|
|
)
|
|
notice = _admin_secret_panel("Dashboard link changed", [_copy_row("Private dashboard:", service.dashboard_url(rotated), "Secret")]) if rotated else None
|
|
else:
|
|
raise HTTPException(status_code=400, detail="invalid rotation action")
|
|
page = await run_in_threadpool(render_admin, service, settings, store, None, notice)
|
|
return HTMLResponse(page)
|
|
|
|
@router.post(f"{admin_path}/cleanup", include_in_schema=False)
|
|
async def admin_run_cleanup(
|
|
request: Request,
|
|
settings: SettingsDep,
|
|
store: StoreDep,
|
|
service: ServiceDep,
|
|
_: None = Depends(authenticate_admin),
|
|
) -> Response:
|
|
form = await request_form(request, settings)
|
|
require_admin_csrf(settings, first_form_value(form, "csrf"))
|
|
result = await run_in_threadpool(
|
|
store.cleanup,
|
|
settings.cleanup_unused_account_hours,
|
|
settings.cleanup_event_history_hours,
|
|
settings.cleanup_pending_account_hours,
|
|
)
|
|
notice = _admin_secret_panel(
|
|
"Cleanup done",
|
|
[
|
|
_copy_row("Unused hostnames removed", str(result["unused_accounts"])),
|
|
_copy_row("Old update events removed", str(result["update_events"])),
|
|
],
|
|
)
|
|
page = await run_in_threadpool(render_admin, service, settings, store, None, notice)
|
|
return HTMLResponse(page)
|
|
|
|
return router
|
|
|
|
|
|
router = make_router()
|