import asyncio import contextlib import json import secrets import time from contextlib import asynccontextmanager from fastapi import FastAPI, WebSocket, WebSocketDisconnect from redis.asyncio import Redis from redis.exceptions import WatchError from .config import Settings from .crypto import fingerprint, raw_public, verified, verify_device async def compare_delete(redis, key, expected): async with redis.pipeline() as pipe: try: await pipe.watch(key) if await pipe.get(key) != expected: return False pipe.multi() pipe.delete(key) await pipe.execute() return True except WatchError: return False async def compare_expire(redis, key, expected, ttl): async with redis.pipeline() as pipe: try: await pipe.watch(key) if await pipe.get(key) != expected: return False pipe.multi() pipe.expire(key, ttl) await pipe.execute() return True except WatchError: return False def create_app(settings=None, redis=None): s = settings or Settings(role="router") r = redis or Redis.from_url(s.redis_url, decode_responses=True) ca = s.ca_cert.read_bytes() active = set() @asynccontextmanager async def lifespan(app): await r.ping() yield if redis is None: await r.aclose() app = FastAPI(title="Hainuo B2 router", lifespan=lifespan, docs_url=None, redoc_url=None) app.state.redis = r @app.get("/health") async def health(): return {"status": "alive", "role": "router"} @app.get("/ready") async def ready(): await r.ping() return {"status": "ready", "connections": len(active)} @app.websocket("/device/ws") async def device(ws: WebSocket): from .common import rate_limit from fastapi import HTTPException await ws.accept() if len(active) >= s.max_connections: await ws.close(1013) return ip = ws.client.host if ws.client else "unknown" try: await rate_limit(r, "ws:" + ip, 30) except HTTPException: await ws.close(1008) return challenge = secrets.token_urlsafe(32) host = session = presence = None tasks = [] pub = None try: await ws.send_json({"type": "challenge", "challenge": challenge}) hello = await asyncio.wait_for(ws.receive_json(), timeout=10) if len(json.dumps(hello)) > 20000: raise ValueError("oversized hello") host = hello["host_id"] if not isinstance(host, str) or len(host) != 32 or any(c not in "0123456789abcdef" for c in host): raise ValueError("invalid host") public = verify_device(hello["cert"], ca, host) proof = verified(hello["proof"], public) if proof["challenge"] != challenge or proof["host_id"] != host or abs(time.time() - proof["ts"]) > 90: raise ValueError("invalid proof") if len(proof["kx"]) > 64 or len(proof["session_id"]) > 64: raise ValueError("invalid session") fp = fingerprint(hello["cert"]) if await r.get("hn:device:" + host) != fp or await r.exists("hn:revoked:" + host): raise ValueError("unregistered/revoked") session = proof["session_id"] presence = json.dumps({"cert": hello["cert"], "proof": hello["proof"], "challenge": challenge}) if not await r.set("hn:online:" + host, presence, nx=True, ex=30): raise ValueError("already connected") active.add(session) pub = r.pubsub() await pub.subscribe("hn:down:" + host, "hn:kill:" + host) await ws.send_json({"type": "connected", "session_id": session}) async def downstream(): async for msg in pub.listen(): if msg["type"] != "message": continue if msg["channel"] == "hn:kill:" + host: await ws.close(1008) return await ws.send_text(msg["data"]) async def heartbeat(): while True: await asyncio.sleep(8) if await r.exists("hn:revoked:" + host) or not await compare_expire(r, "hn:online:" + host, presence, 30): await ws.close(1008) return async def upstream(): while True: text = await ws.receive_text() if len(text.encode()) > s.max_body: raise ValueError("oversized frame") data = json.loads(text) if data.get("type") == "ping": continue await rate_limit(r, "ws:reply:" + host, 120) turn = data.get("turn_id", "") if data.get("type") != "reply" or await r.get(f"hn:inflight:{host}:{turn}") != session: raise ValueError("unsolicited reply") envelope = data["envelope"] verified(envelope, public) if envelope["host_id"] != host or envelope["session_id"] != session: raise ValueError("wrong reply destination") await r.publish("hn:replies", json.dumps(data)) tasks = [asyncio.create_task(x()) for x in (downstream, heartbeat, upstream)] done, _ = await asyncio.wait(tasks, return_when=asyncio.FIRST_COMPLETED) for task in done: task.result() except (WebSocketDisconnect, asyncio.TimeoutError, ValueError, KeyError, TypeError): with contextlib.suppress(Exception): await ws.close(1008) except Exception: with contextlib.suppress(Exception): await ws.close(1011) finally: for task in tasks: task.cancel() await asyncio.gather(*tasks, return_exceptions=True) if pub: await pub.aclose() if presence: await compare_delete(r, "hn:online:" + host, presence) active.discard(session) return app