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, field 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 (canonical, 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 progress: asyncio.Event = field(default_factory=asyncio.Event) class ClientIdleTimeout(asyncio.TimeoutError): """No authenticated task progress arrived within the idle timeout.""" 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] = {} self.worker_id = uuid.uuid4().hex self.lifecycle = None self.files = None self.task_cards = None 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"]: continue if plain.get('type') == 'progress': if self.lifecycle and self.lifecycle.progress(data['turn_id'],item.host,item.session,self.worker_id): await self.r.expire(f"hn:inflight:{item.host}:{data['turn_id']}", self.s.request_timeout + 5) item.progress.set() continue if plain.get('type') != 'result': continue result = plain.get("result", "") if not isinstance(result, str) or len(result) > self.s.max_result: continue if self.lifecycle and not self.lifecycle.complete(data["turn_id"],item.host,item.session,self.worker_id): 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 wait_tracked(self, item): # Authenticated native status polls renew idle waiting, never the hard # task deadline. A lost device/worker remains unknown and is not replayed. deadline = asyncio.get_running_loop().time() + self.s.task_tracking_timeout while True: remaining = deadline - asyncio.get_running_loop().time() if remaining <= 0: raise asyncio.TimeoutError() item.progress.clear() progress = asyncio.create_task(item.progress.wait()) try: done, _ = await asyncio.wait({item.future, progress}, timeout=min(self.s.request_timeout, remaining), return_when=asyncio.FIRST_COMPLETED) if item.future in done: return item.future.result() if not done: if remaining<=self.s.request_timeout:raise asyncio.TimeoutError() raise ClientIdleTimeout() finally: progress.cancel() with contextlib.suppress(asyncio.CancelledError): await progress async def dispatch(self, owner, text, turn, *, chat_id="", source_message_id=""): 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"] if self.lifecycle and (proof.get("binding_owner") != owner or proof.get("binding_generation") != device["binding_generation"]): raise ValueError("stale binding session") except Exception: self.store.audit(owner, "dispatch", host, "identity_rejected") raise HTTPException(409, "device identity rejected") from None file_capable=bool(self.files and proof.get('work_files')=='work-files-v1' and chat_id and source_message_id) if self.files and self.files.pending_count(owner,host,device['binding_generation'],chat_id,True): return {'status':'materials_receiving','turn_id':turn,'result':'材料仍在接收,尚未执行任务。请等附件接收回执后再发送任务要求。'} if self.files and self.files.pending_count(owner,host,device['binding_generation'],chat_id) and not file_capable: return {'status':'unavailable','turn_id':turn,'result':'材料已保留,但当前客户端尚未支持附件处理;请升级配套客户端后再发任务要求。'} if not self.store.begin_turn(turn, owner, host): return {"status": "duplicate", "turn_id": turn, "result": "该请求已接收,不会重复执行。"} task = None if self.lifecycle: try: task = self.lifecycle.create_task(turn,owner,host,device["binding_generation"],session, self.worker_id,time.time()+self.s.task_tracking_timeout,chat_id,source_message_id) except ValueError: self.store.finish(turn,"unbound") return {"status":"unbound","turn_id":turn,"result":"绑定已变化,任务未下发。"} if task and file_capable: try:self.files.claim(task) except ValueError: self.store.finish(turn,'not_dispatched') self.lifecycle.transition(turn,'inflight','completed') return {'status':'not_dispatched','turn_id':turn,'result':'材料接收或设备绑定状态发生变化,尚未执行任务;请等附件回执后核对。'} private = x25519.X25519PrivateKey.generate() future = asyncio.get_running_loop().create_future() self.pending[turn] = Pending(future, private, public, host, session) payload = {"type": "instruction", "turn_id": turn, "owner": owner, "text": text, "read_only": True, "channel": "im", "reply_key": raw_public(private.public_key())} if task: if file_capable: payload['file_transfer']='work-files-v1' if source_message_id: payload['result_delivery']='durable-http-v1' payload["task_context"] = {key:task[key] for key in ("environment","app_id","owner","host_id","binding_generation", "conversation_id","chat_id","source_message_id")} payload["task_context"].update(task_id=turn,channel="work-dm") try: envelope = seal(payload, proof["kx"], self.signer,host_id=host,session_id=session) except Exception: self.pending.pop(turn,None) self.store.finish(turn,"unknown") if task: self.lifecycle.transition(turn,"inflight","unknown") raise HTTPException(409,"invalid_device_crypto_session") from None key = f"hn:inflight:{host}:{turn}" try: await self.r.set(key, session, ex=self.s.request_timeout + 5) packet = {"type":"instruction","envelope":envelope} if task: packet["routing"] = sign({"turn_id":turn,"host_id":host,"session_id":session, "environment":self.s.environment,"app_id":self.s.feishu_app_id, "binding_generation":task["binding_generation"], "envelope_hash":hashlib.sha256(canonical(envelope)).hexdigest()}, self.signer) count = await self.r.publish("hn:down:" + host, json.dumps(packet)) if not count: self.store.finish(turn, "offline") if task: self.lifecycle.transition(turn,"inflight","offline") return {"status": "offline", "turn_id": turn, "result": "你的电脑当前离线。"} if task: result = await self.wait_tracked(self.pending[turn]) else: result = await asyncio.wait_for(future, timeout=self.s.request_timeout) self.store.finish(turn, result.get("status","unknown") if task else "completed") if task and not source_message_id: self.lifecycle.transition(turn,"completed","finished") self.store.audit(owner, "instruction", host, result.get("status","unknown") if task else "completed") return result except asyncio.TimeoutError as error: self.store.finish(turn, "unknown") if task: self.lifecycle.transition(turn,"inflight","unknown") if isinstance(error,ClientIdleTimeout) and self.task_cards: self.task_cards.mark_connection_lost(turn) return {"status": "unknown", "turn_id": turn, "result": "等待结果超时,执行状态未知;不要直接重发有副作用的任务。"} finally: self.pending.pop(turn, None) if task: # Cancellation, publish failures and connector shutdown never replay work. with contextlib.suppress(Exception): self.lifecycle.transition(turn,"inflight","unknown") with contextlib.suppress(Exception): 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) if s.credential_mode == "broker": from .credential_api import CredentialAPI from .credential_broker import CredentialBroker from .credential_store import CredentialStore, CredentialKeyring credential_store = CredentialStore(store, CredentialKeyring.from_file(s.credential_keyring_file)) credential_broker = CredentialBroker(credential_store, r, f, environment=s.environment, app_id=s.feishu_app_id) oauth = CredentialAPI(s, r, store, credential_broker) from .binding_state import BindingState from .device_lifecycle import DeviceLifecycle store.lifecycle = BindingState(store,s.environment,s.feishu_app_id) broker.lifecycle = store.lifecycle lifecycle = DeviceLifecycle(store.lifecycle,r,broker) from .task_delivery import TaskDelivery deliveries = TaskDelivery(oauth,store.lifecycle,broker,f) if s.work_files_enabled: from .work_files import WorkFiles broker.files=WorkFiles(s,oauth,store.lifecycle,f) else: oauth = OAuth(s, r, f) oauth.store = store lifecycle = None from .notifications import router as notification_router 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() if lifecycle: await lifecycle.maintain() await deliveries.recover_interrupted_sends() 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) if s.credential_mode == "legacy": async with oauth.lock: oauth.prune() sweep_task = asyncio.create_task(sweep()) async def maintenance(): while True: await asyncio.sleep(5) try: await lifecycle.maintain() await deliveries.recover_interrupted_sends() if broker.files:await broker.files.flush() await deliveries.flush() except Exception: # No credentials or raw exception text in background logs. pass lifecycle_task = asyncio.create_task(maintenance()) if lifecycle else None async def card_maintenance(): while True: await asyncio.sleep(5) with contextlib.suppress(Exception):await deliveries.cards.flush() cards_task=asyncio.create_task(card_maintenance()) if lifecycle and deliveries.cards else None try: yield finally: work = [reply_task,sweep_task,*background] + ([lifecycle_task] if lifecycle_task else []) + ([cards_task] if cards_task else []) for task in work: task.cancel() await asyncio.gather(*work, return_exceptions=True) if lifecycle: with contextlib.suppress(Exception): await r.delete("hn:task-worker:"+broker.worker_id) 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, path_limits={'/work/v2/files/upload':s.work_file_max_bytes}if broker.files else {}) app.include_router(oauth.router) if lifecycle: app.include_router(deliveries.router) app.state.deliveries=deliveries if broker.files: app.include_router(broker.files.router) app.state.files=broker.files app.include_router(notification_router(s, r, store, f, oauth)) 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.state.lifecycle = lifecycle if lifecycle: from .device_lifecycle import AdmissionRequest from .credential_api import DeviceRequest @app.post("/work/v2/device/unbind") async def device_unbind(body: DeviceRequest, request: Request): await oauth.authenticate(body,request) try: store.lifecycle.invalidate_host(body.host_id,"unbind",expected_owner=body.binding_owner, expected_generation=body.binding_generation) except ValueError: raise HTTPException(409,"binding_changed_retry") from None lifecycle.cancel_waiters() propagated = await lifecycle.flush() return {"status":"unbound","binding_generation":store.device(body.host_id)["binding_generation"], "propagation":"published" if propagated else "pending"} @app.post("/internal/devices/{host}/admission") async def admission(host: str, body: AdmissionRequest, request: Request): require_secret(request.headers.get("X-Router-Admission-Secret"),s.router_admission_secret) return sign(await lifecycle.admission(host,body),broker.signer) @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)) if s.credential_mode == "legacy": 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") if lifecycle: try: store.revoke(host) except ValueError: raise HTTPException(409,"binding_changed_retry") from None lifecycle.cancel_waiters() propagated = await lifecycle.flush() return {"status":"revoked","propagation":"published" if propagated else "pending"} # 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) if lifecycle: await lifecycle.maintain() 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 lifecycle: try: store.lifecycle.invalidate_host(host,"unbind") except ValueError: raise HTTPException(409,"binding_changed_retry") from None lifecycle.cancel_waiters() propagated = await lifecycle.flush() return {"status":"unbound","host_id":host,"revoked":bool(device["revoked"]), "propagation":"published" if propagated else "pending"} 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) if lifecycle: try: store.lifecycle.issue_code(owner,code) except ValueError: return "你已绑定电脑,请先发送 /解绑。" else: 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 if lifecycle: try: owner = store.lifecycle.consume_code(body.host_id,code) except LookupError: raise HTTPException(404,"binding code unavailable") from None except (*INTEGRITY_ERRORS,ValueError): raise HTTPException(409,"owner/device already bound; request new code") from None return sign({"host_id":body.host_id,"owner":owner, "binding_generation":store.device(body.host_id)["binding_generation"], "ts":int(time.time())},broker.signer) 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, "binding_generation": store.device(body.host_id)["binding_generation"], "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", "") kind=message.get('message_type') if (message.get("chat_type") != "p2p" or kind not in ({'text','file'}if broker.files else {'text'}) or sender.get("sender_type") != "user" or not isinstance(owner,str) or not 0 < len(owner) <= 128 or any(ord(c) < 32 for c in 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 if lifecycle and (not isinstance(message.get("chat_id"),str) or not 0 < len(message["chat_id"]) <= 128 or any(ord(c) < 32 for c in message["chat_id"])): return try: content=json.loads(message['content']) text = content['text'].strip() if kind=='text' else '' if kind=='text' and (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 kind=='file': try: await broker.files.receive(owner,message['chat_id'],message_id,content) result='材料已接收。可以继续发送其他材料,最后发出本次任务要求;暂未执行模型任务。' except Exception: result='这件附件未接收成功,请核对文件格式、大小、资源权限或设备绑定;其他已接收材料仍保留。' try:await f.reply(message_id,result) finally:store.finish(event_turn,'attachment_processed') return if text == "/绑定": result = await make_binding_code(owner) elif text == "/解绑": old = store.by_owner(owner) store.unbind(owner) if lifecycle: lifecycle.cancel_waiters() await lifecycle.flush() elif 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(), **({"chat_id":message["chat_id"],"source_message_id":message_id} if lifecycle else {})))["result"] else: instruction = text if text.startswith("/助手 ") else "/助手 " + text result = (await broker.dispatch(owner, instruction, "msg_" + hashlib.sha256(message_id.encode()).hexdigest(), **({"chat_id":message["chat_id"],"source_message_id":message_id} if lifecycle else {})))["result"] turn = "msg_"+hashlib.sha256(message_id.encode()).hexdigest() task = store.lifecycle.task(turn) if lifecycle else None if task and not store.lifecycle.claim_reply(turn,owner,message_id): store.finish(event_turn,"result_discarded") return try: await f.reply(message_id, result[:s.max_result]) except Exception: if task: store.lifecycle.transition(turn,"replying","reply_unknown") raise if task: store.lifecycle.transition(turn,"replying","replied") 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