Import ECS deployed relay source and historical patches (2026-10-27)

This commit is contained in:
ex_tianzy
2026-10-27 17:26:58 +08:00
commit 011a7797ee
319 changed files with 34265 additions and 0 deletions
@@ -0,0 +1,506 @@
import asyncio
import base64
import contextlib
import hashlib
import hmac
import json
import re
import secrets
import sqlite3
import time
import uuid
from contextlib import asynccontextmanager
from dataclasses import dataclass
from urllib.parse import unquote, urlsplit
from cryptography.hazmat.primitives import hashes, padding
from cryptography.hazmat.primitives.asymmetric import x25519
from cryptography.hazmat.primitives.ciphers import Cipher, algorithms, modes
from fastapi import FastAPI, HTTPException, Request
from fastapi.responses import HTMLResponse, JSONResponse
from fastapi.exceptions import RequestValidationError
from pydantic import BaseModel, Field
from redis.asyncio import Redis
from .common import BodyLimitMiddleware, no_cache, peer, rate_limit, require_secret
from .config import Settings
from .crypto import (fingerprint, load_private, open_envelope, raw_public, seal,
sign, verified, verify_device)
from .oauth import OAuth
from .platform import Feishu, PlatformError
from .store import Store, INTEGRITY_ERRORS
class Register(BaseModel):
host_id: str = Field(pattern=r"^[a-f0-9]{32}$")
cert: str = Field(max_length=10000)
class Bind(BaseModel):
host_id: str = Field(pattern=r"^[a-f0-9]{32}$")
proof: dict
class Instruction(BaseModel):
open_id: str = Field(min_length=1, max_length=128)
text: str = Field(min_length=1, max_length=8000)
# Stable identity chosen by upstream, not generated again on retries.
turn_id: str = Field(default_factory=lambda: uuid.uuid4().hex, pattern=r"^[A-Za-z0-9_-]{1,128}$")
@dataclass
class Pending:
future: asyncio.Future
private: object
public: object
host: str
session: str
class Broker:
def __init__(self, settings, redis, store):
self.s, self.r, self.store = settings, redis, store
self.signer = load_private(settings.connector_key)
self.pending: dict[str, Pending] = {}
async def replies(self, pub):
async for message in pub.listen():
if message["type"] != "message":
continue
try:
data = json.loads(message["data"])
item = self.pending.get(data["turn_id"])
if not item or item.future.done():
continue
plain = open_envelope(data["envelope"], item.private, item.public,
host_id=item.host, session_id=item.session)
if plain["turn_id"] != data["turn_id"] or plain.get("type") != "result":
continue
result = plain.get("result", "")
if not isinstance(result, str) or len(result) > self.s.max_result:
continue
item.future.set_result({"status": "completed", "turn_id": data["turn_id"], "result": result})
except Exception:
# Invalid packets never consume a pending result; do not log packet contents.
continue
async def dispatch(self, owner, text, turn):
device = self.store.by_owner(owner)
if not device:
return {"status": "unavailable", "turn_id": turn, "result": "未绑定设备或设备不可用。"}
host = device["host_id"]
await rate_limit(self.r, "dispatch:" + owner, 30)
if len(self.pending) >= self.s.max_pending:
raise HTTPException(503, "too many in-flight requests")
presence = await self.r.get("hn:online:" + host)
if not presence or await self.r.exists("hn:revoked:" + host):
return {"status": "offline", "turn_id": turn, "result": "你的电脑当前离线,请开机并连接网络。"}
try:
presence = json.loads(presence)
if fingerprint(presence["cert"]) != device["fingerprint"]:
raise ValueError("certificate changed")
public = verify_device(presence["cert"], self.s.ca_cert.read_bytes(), host)
proof = verified(presence["proof"], public)
if proof["host_id"] != host or proof["challenge"] != presence["challenge"]:
raise ValueError("invalid device session")
session = proof["session_id"]
except Exception:
self.store.audit(owner, "dispatch", host, "identity_rejected")
raise HTTPException(409, "device identity rejected") from None
if not self.store.begin_turn(turn, owner, host):
return {"status": "duplicate", "turn_id": turn, "result": "该请求已接收,不会重复执行。"}
private = x25519.X25519PrivateKey.generate()
future = asyncio.get_running_loop().create_future()
self.pending[turn] = Pending(future, private, public, host, session)
envelope = seal({"type": "instruction", "turn_id": turn, "owner": owner,
"text": text, "read_only": True, "channel": "im",
"reply_key": raw_public(private.public_key())}, proof["kx"], self.signer,
host_id=host, session_id=session)
key = f"hn:inflight:{host}:{turn}"
try:
await self.r.set(key, session, ex=self.s.request_timeout + 5)
count = await self.r.publish("hn:down:" + host, json.dumps({"type": "instruction", "envelope": envelope}))
if not count:
self.store.finish(turn, "offline")
return {"status": "offline", "turn_id": turn, "result": "你的电脑当前离线。"}
result = await asyncio.wait_for(future, timeout=self.s.request_timeout)
self.store.finish(turn, "completed")
self.store.audit(owner, "instruction", host, "completed")
return result
except asyncio.TimeoutError:
self.store.finish(turn, "unknown")
return {"status": "unknown", "turn_id": turn,
"result": "等待结果超时,执行状态未知;不要直接重发有副作用的任务。"}
finally:
self.pending.pop(turn, None)
await self.r.delete(key)
def create_app(settings=None, redis=None, platform=None):
s = settings or Settings()
r = redis or Redis.from_url(s.redis_url, decode_responses=True)
store = Store(s.data_dir / "metadata.sqlite", s.database_url)
f = platform or Feishu(s)
broker = Broker(s, r, store)
oauth = OAuth(s, r, f)
oauth.store = store
background = set()
def spawn(coro):
task = asyncio.create_task(coro)
background.add(task)
task.add_done_callback(background.discard)
# Retrieve errors without displaying possibly sensitive exception text.
task.add_done_callback(lambda t: t.exception() if not t.cancelled() else None)
@asynccontextmanager
async def lifespan(app):
await r.ping()
for row in store.db.execute("SELECT host_id,fingerprint,revoked FROM devices").fetchall():
await r.set("hn:device:" + row["host_id"], row["fingerprint"])
if row["revoked"]:
await r.set("hn:revoked:" + row["host_id"], "1")
pub = r.pubsub()
await pub.subscribe("hn:replies")
reply_task = asyncio.create_task(broker.replies(pub))
app.state.reply_task = reply_task
async def sweep():
while True:
await asyncio.sleep(30)
async with oauth.lock:
oauth.prune()
sweep_task = asyncio.create_task(sweep())
try:
yield
finally:
for task in [reply_task, sweep_task, *background]:
task.cancel()
await asyncio.gather(reply_task, sweep_task, *background, return_exceptions=True)
await pub.aclose()
await f.close()
store.close()
if redis is None:
await r.aclose()
app = FastAPI(title="Hainuo B1 connector", lifespan=lifespan, docs_url=None, redoc_url=None)
app.add_middleware(BodyLimitMiddleware, max_body=s.max_body)
app.include_router(oauth.router)
from .web_session import WebSessions
web_sessions = WebSessions(s, r, store, broker, f)
app.include_router(web_sessions.router)
app.state.store, app.state.broker, app.state.oauth = store, broker, oauth
app.state.platform, app.state.redis = f, r
@app.exception_handler(RequestValidationError)
async def validation_error(request, exc):
# FastAPI's default error response includes invalid input values (possibly tokens).
return JSONResponse({"detail": "invalid request parameters"}, status_code=422)
@app.middleware("http")
async def secure_response(request, call_next):
return no_cache(await call_next(request))
def admin(request):
require_secret(request.headers.get("X-Admin-Secret"), s.admin_secret)
from .enrollment import enrollment_router
app.include_router(enrollment_router(s, r, store, broker.signer, admin))
from .work_api import router as work_router
app.include_router(work_router(s, r, store, f))
@app.get("/health")
async def health():
return {"status": "alive", "role": "connector", "version": "0.1.0"}
@app.get("/ready")
async def ready():
await r.ping()
store.db.execute("SELECT 1")
if not getattr(app.state, "reply_task", None) or app.state.reply_task.done():
raise HTTPException(503, "reply subscriber unavailable")
return {"status": "ready"}
@app.post("/admin/devices", status_code=201)
async def register(body: Register, request: Request):
admin(request)
try:
verify_device(body.cert, s.ca_cert.read_bytes(), body.host_id)
except Exception:
raise HTTPException(400, "invalid device certificate") from None
if store.db.execute("SELECT COUNT(*) AS total FROM devices").fetchone()["total"] >= s.max_devices:
raise HTTPException(503, "device capacity reached")
try:
fp = fingerprint(body.cert)
store.add_device(body.host_id, body.cert, fp)
except INTEGRITY_ERRORS:
raise HTTPException(409, "device already exists") from None
await r.set("hn:device:" + body.host_id, fp)
store.audit("admin", "register", body.host_id, "success")
return {"host_id": body.host_id, "fingerprint": fp}
@app.get("/admin/devices")
async def devices(request: Request):
admin(request)
rows = store.db.execute("SELECT host_id,fingerprint,owner,revoked FROM devices ORDER BY host_id").fetchall()
return {"devices": [{**dict(row), "online": bool(await r.exists("hn:online:" + row["host_id"]))} for row in rows]}
@app.post("/admin/devices/{host}/revoke")
async def revoke(host: str, request: Request):
admin(request)
if not store.device(host):
raise HTTPException(404, "device not found")
# Persistent source of truth first; router block + disconnect follows.
store.revoke(host)
await r.incr("hn:web-generation:" + host)
await r.set("hn:revoked:" + host, "1")
await r.delete("hn:online:" + host)
await r.publish("hn:kill:" + host, "revoke")
for item in broker.pending.values():
if item.host == host and not item.future.done():
item.future.set_result({"status": "revoked", "result": "设备已停用,结果已丢弃。"})
store.audit("admin", "revoke", host, "success")
return {"status": "revoked"}
@app.post("/admin/reconcile")
async def reconcile(request: Request):
"""Rebuild router admission metadata after Redis recovery; no dialogue data."""
admin(request)
rows = store.db.execute("SELECT host_id,fingerprint,revoked FROM devices").fetchall()
for row in rows:
await r.set("hn:device:" + row["host_id"], row["fingerprint"])
if row["revoked"]:
await r.set("hn:revoked:" + row["host_id"], "1")
return {"devices": len(rows)}
@app.post("/admin/devices/{host}/unbind")
async def admin_unbind(host: str, request: Request):
admin(request)
device = store.device(host)
if not device:
raise HTTPException(404, "device not found")
if device["owner"]:
store.unbind(device["owner"])
await r.incr("hn:web-generation:" + host)
await r.delete("hn:online:" + host)
await r.publish("hn:kill:" + host, "unbind")
for item in broker.pending.values():
if item.host == host and not item.future.done():
item.future.set_result({"status": "unbound", "result": "设备已解绑,结果已丢弃。"})
store.audit("admin", "unbind", host, "success")
return {"status": "unbound", "host_id": host, "revoked": bool(device["revoked"])}
async def make_binding_code(owner):
if store.by_owner(owner):
return "你已绑定电脑,请先发送 /解绑。"
await rate_limit(r, "binding:" + owner, 5)
code = secrets.token_urlsafe(12)
await r.set("hn:bind:" + hashlib.sha256(code.encode()).hexdigest(), owner, ex=300)
return "请在本人电脑的桥接客户端输入绑定码(5分钟有效):" + code
@app.post("/device/bind")
async def bind(body: Bind, request: Request):
await rate_limit(r, "bind:" + peer(request), 20)
device = store.device(body.host_id)
if not device or device["revoked"]:
raise HTTPException(404, "device unavailable")
try:
public = verify_device(device["cert"], s.ca_cert.read_bytes(), body.host_id)
proof = verified(body.proof, public)
if proof["host_id"] != body.host_id or abs(time.time() - proof["ts"]) > 90:
raise ValueError("invalid proof")
code = proof["code"]
if not isinstance(code, str) or len(code) > 128:
raise ValueError("invalid code")
except Exception:
raise HTTPException(403, "invalid device proof") from None
key = "hn:bind:" + hashlib.sha256(code.encode()).hexdigest()
owner = await r.get(key)
if not owner:
raise HTTPException(404, "binding code expired or consumed")
# GETDEL is atomic across connectors. Invalid device proofs never reach this point.
owner = await r.getdel(key)
if not owner:
raise HTTPException(404, "binding code consumed")
try:
store.bind(body.host_id, owner)
except (*INTEGRITY_ERRORS, ValueError):
raise HTTPException(409, "owner/device already bound; request new code") from None
await r.incr("hn:web-generation:" + body.host_id)
store.audit(owner, "bind", body.host_id, "success")
return sign({"host_id": body.host_id, "owner": owner, "ts": int(time.time())}, broker.signer)
@app.post("/admin/test/binding-code")
async def test_code(body: Instruction, request: Request):
admin(request)
if s.environment == "production":
raise HTTPException(404, "not found")
return {"message": await make_binding_code(body.open_id)}
@app.post("/admin/test/dispatch")
async def test_dispatch(body: Instruction, request: Request):
admin(request)
if s.environment == "production":
raise HTTPException(404, "not found")
return await broker.dispatch(body.open_id, body.text, body.turn_id)
async def handle_event(data):
header, event = data.get("header", {}), data.get("event", {})
if header.get("event_type") != "im.message.receive_v1":
return
message, sender = event.get("message", {}), event.get("sender", {})
owner = sender.get("sender_id", {}).get("open_id")
message_id = message.get("message_id", "")
if (message.get("chat_type") != "p2p" or message.get("message_type") != "text"
or sender.get("sender_type") != "user" or not owner
or not re.fullmatch(r"[A-Za-z0-9_-]{1,128}", message_id)):
return
if header.get("app_id") != s.feishu_app_id:
return
try:
text = json.loads(message["content"])["text"].strip()
if not text or len(text) > 8000:
return
# Persistent receipt BEFORE any side effect. Restarts do not execute a delivery twice.
event_turn = "event_" + hashlib.sha256(message_id.encode()).hexdigest()
if not store.begin_turn(event_turn, owner, "event"):
return
if text == "/绑定":
result = await make_binding_code(owner)
elif text == "/解绑":
old = store.by_owner(owner)
store.unbind(owner)
if old:
await r.incr("hn:web-generation:" + old["host_id"])
await r.publish("hn:kill:" + old["host_id"], "unbind")
for item in broker.pending.values():
if item.host == old["host_id"] and not item.future.done():
item.future.set_result({"status": "unbound", "result": "设备已解绑,结果已丢弃。"})
result = "已解绑;重新绑定后请更新本机绑定信息。"
elif text == "/主机名" or text.startswith("/回显 "):
result = (await broker.dispatch(owner, text, "msg_" + hashlib.sha256(message_id.encode()).hexdigest()))["result"]
else:
instruction = text if text.startswith("/助手 ") else "/助手 " + text
result = (await broker.dispatch(owner, instruction, "msg_" + hashlib.sha256(message_id.encode()).hexdigest()))["result"]
await f.reply(message_id, result[:s.max_result])
store.finish(event_turn, "replied")
except Exception:
store.audit(owner, "event", message_id, "failed_or_unknown")
@app.post("/internal/events")
async def internal_event(request: Request):
require_secret(request.headers.get("X-Internal-Secret"), s.internal_secret)
if not s.feishu_enabled:
raise HTTPException(503, "platform disabled")
if len(background) >= s.max_pending:
raise HTTPException(503, "busy")
spawn(handle_event(await request.json()))
return {"accepted": True}
@app.post("/feishu/event")
async def webhook(request: Request):
if not s.feishu_enabled:
raise HTTPException(503, "platform disabled")
if not s.feishu_encrypt_key or not s.feishu_verification_token:
raise HTTPException(503, "webhook credentials not configured; use selected event source")
await rate_limit(r, "webhook:" + peer(request), 600)
raw = await request.body()
timestamp, nonce, signature = (request.headers.get(x, "") for x in
("X-Lark-Request-Timestamp", "X-Lark-Request-Nonce", "X-Lark-Signature"))
try:
if abs(time.time() - int(timestamp)) > 300 or not nonce or len(nonce) > 128:
raise ValueError("stale request")
expected = hashlib.sha256((timestamp + nonce + s.feishu_encrypt_key).encode() + raw).hexdigest()
if not hmac.compare_digest(expected, signature):
raise ValueError("bad signature")
data = json.loads(raw)
if "encrypt" in data:
cipher = base64.b64decode(data["encrypt"], validate=True)
decryptor = Cipher(algorithms.AES(hashlib.sha256(s.feishu_encrypt_key.encode()).digest()),
modes.CBC(cipher[:16])).decryptor()
padded = decryptor.update(cipher[16:]) + decryptor.finalize()
unpadder = padding.PKCS7(128).unpadder()
data = json.loads(unpadder.update(padded) + unpadder.finalize())
token = data.get("token", data.get("header", {}).get("token", ""))
if not hmac.compare_digest(token, s.feishu_verification_token):
raise ValueError("invalid verification token")
except Exception:
raise HTTPException(403, "invalid event authentication") from None
if data.get("type") == "url_verification":
return {"challenge": data.get("challenge", "")}
# Same delivery is ACKed again; durable message_id receipt handles execution deduplication.
if len(background) >= s.max_pending:
raise HTTPException(503, "busy")
spawn(handle_event(data))
return {"code": 0}
@app.get("/api/whoami")
async def whoami(request: Request):
if not s.feishu_enabled:
raise HTTPException(503, "platform disabled")
await rate_limit(r, "whoami:" + peer(request))
auth = request.headers.get("Authorization", "")
if not auth.startswith("Bearer ") or len(auth) > 8192:
raise HTTPException(401, "user token required")
try:
info = await f.user_info(auth[7:])
except PlatformError:
raise HTTPException(401, "invalid user token") from None
device = store.by_owner(info["open_id"])
if not device:
raise HTTPException(404, "no device bound")
if not s.device_domain or not re.fullmatch(r"[a-zA-Z0-9.-]+", s.device_domain):
raise HTTPException(503, "device web domain not configured")
return {"host_id": device["host_id"], "url": f"https://h-{device['host_id']}.{s.device_domain}/",
"online": bool(await r.exists("hn:online:" + device["host_id"])),
"device_login_required": True}
@app.get("/h5", response_class=HTMLResponse)
async def h5():
from importlib.resources import files
return HTMLResponse(files("relay").joinpath("web/index.html").read_text(encoding="utf-8"),
headers={"Content-Security-Policy": "default-src 'self'; script-src 'self'; style-src 'self'; frame-ancestors 'self'; base-uri 'none'; form-action 'self'"})
@app.get("/assets/{name}")
async def asset(name: str):
from importlib.resources import files
from starlette.responses import Response
if name not in {"app.js", "style.css"}:
raise HTTPException(404, "not found")
return Response(files("relay").joinpath("web/" + name).read_bytes(),
media_type="text/javascript" if name.endswith(".js") else "text/css")
@app.get("/proxy")
async def proxy(request: Request):
if not s.proxy_enabled or not s.feishu_enabled:
raise HTTPException(404, "not found")
auth = request.headers.get("Authorization", "")
target = request.headers.get("X-Relay-Target", "")
decoded = unquote(target)
parts = urlsplit(target)
allowed = s.proxy_paths.split(",")
if (not auth.startswith("Bearer ") or len(auth) > 8192 or len(target) > 4096
or parts.scheme or parts.netloc or parts.fragment or target.startswith("//")
or any(x in decoded for x in ("..", "\\", "\r", "\n", "#", "%"))
or not any(parts.path == x or parts.path.startswith(x + "/") for x in allowed)):
raise HTTPException(403, "target not allowed")
try:
info = await f.user_info(auth[7:])
await rate_limit(r, "proxy:" + info["open_id"], 60)
# Buffer bounded response; no disk/cache, redirects never followed.
async with f.http.stream("GET", target, headers={"Authorization": auth}) as response:
if response.status_code in {301, 302, 303, 307, 308}:
raise HTTPException(502, "upstream redirect refused")
chunks, size = [], 0
async for chunk in response.aiter_bytes():
size += len(chunk)
if size > 2 * 1024 * 1024:
raise HTTPException(502, "response too large")
chunks.append(chunk)
from starlette.responses import Response
store.audit(info["open_id"], "proxy", parts.path, str(response.status_code))
return Response(b"".join(chunks), status_code=response.status_code,
media_type=response.headers.get("content-type", "application/json"))
except PlatformError:
raise HTTPException(401, "invalid user token") from None
return app