from fastapi import FastAPI, HTTPException, Request from fastapi.middleware.cors import CORSMiddleware from fastapi.responses import HTMLResponse from .fingerprint import generate_fingerprint from .runtime import registry from .schemas import FingerprintGenerateRequest, LaunchRequest, RunningResponse, RuntimeConfigUpdate app = FastAPI(title="local-controller", version="0.2.0") app.add_middleware( CORSMiddleware, allow_origins=["*"], allow_credentials=True, allow_methods=["*"], allow_headers=["*"], ) @app.get("/health") def health(): return {"ok": True} @app.get("/diagnostics/fingerprint", response_class=HTMLResponse) def fingerprint_diagnostics_page(): return HTMLResponse( """ Fingerprint Diagnostics

Fingerprint Diagnostics

This page shows what the site can read on the first document load from JavaScript and request headers. Use it to verify whether the browser environment is already masked before the target page opens.

Navigator

UA Client Hints

Loading...

Screen

Loading...

Request Headers

Loading...
""".strip() ) @app.get("/diagnostics/fingerprint/echo") def fingerprint_diagnostics_echo(request: Request): interesting_headers = [ "user-agent", "accept-language", "sec-ch-ua", "sec-ch-ua-mobile", "sec-ch-ua-platform", "sec-ch-ua-full-version", "sec-ch-ua-full-version-list", "upgrade-insecure-requests", ] data = {} for key in interesting_headers: value = request.headers.get(key) if value is not None: data[key] = value return data @app.get("/runtime/status") def runtime_status(): return registry.runtime_status() @app.get("/runtime/config") def runtime_config(): return {"ok": True, "data": registry.runtime_config()} @app.put("/runtime/config") def save_runtime_config(req: RuntimeConfigUpdate): return {"ok": True, "data": registry.save_runtime_config(req)} @app.post("/profiles/{profile_id}/launch") def launch_profile(profile_id: str, req: LaunchRequest): if req.profile_id != profile_id: raise HTTPException(status_code=400, detail="profile_id mismatch") try: instance = registry.launch(req) return {"ok": True, "data": instance.model_dump()} except FileNotFoundError as exc: raise HTTPException(status_code=400, detail=f"runtime not found: {exc}") from exc except RuntimeError as exc: raise HTTPException(status_code=409, detail=str(exc)) from exc @app.post("/profiles/{profile_id}/close") def close_profile(profile_id: str): closed = registry.close(profile_id) return {"ok": closed} @app.get("/profiles/running", response_model=RunningResponse) def running_profiles(): return RunningResponse(items=registry.running()) @app.get("/profiles/{profile_id}/status") def profile_status(profile_id: str): instance = registry.status(profile_id) if not instance: raise HTTPException(status_code=404, detail="profile not running") return {"ok": True, "data": instance.model_dump()} @app.post("/fingerprints/generate") def generate_fingerprint_config(req: FingerprintGenerateRequest): return { "ok": True, "data": generate_fingerprint( req.browser_family, browser_version=req.browser_version, full_version=req.full_version, locale=req.locale, timezone=req.timezone, geo_country=req.geo_country, geo_mode=req.geo_mode, screen_width=req.screen_width, screen_height=req.screen_height, runtime_path=req.runtime_path, ), }