UNPKG

openclaw

Version:

Multi-channel AI gateway with extensible messaging integrations

92 lines (91 loc) • 3.38 kB
import { t as _usingCtx } from "./usingCtx-rhpPzaNq.mjs"; import { a as getRemoteControlUiIngressContext, r as assertRemoteControlUiIngressCurrent } from "./ingress-attribution-DAAQgd6y.mjs"; import { c as verifyBoardViewTicket, r as BoardGatewayUnavailableError, t as BOARD_HTTP_PATH_PREFIX } from "./board-view-ticket-CRLjSnq1.mjs"; import { i as respondPlainText, n as isReadHttpMethod, r as respondNotFound } from "./control-ui-http-utils-DH5C3tE0.mjs"; import { d as sendMethodNotAllowed } from "./http-common-FfhIIwOW.mjs"; import { t as prepareRemoteIngressSessionRead } from "./remote-ingress-session-read-C1WTYcEK.mjs"; import { t as buildBoardWidgetContentSecurityPolicy } from "./board-sandbox-CCY2jPEI.mjs"; import { t as boardStore } from "./board-store-DbsKOphK.mjs"; import { t as withAuthorizedBoardWidgetView } from "./board-widget-view-B57zgDE_.mjs"; import { h as sessionObserverScopeKey } from "./session-observer-model-CoCBcuix.mjs"; //#region src/gateway/board-http.ts const BOARD_WIDGET_NAME_PATTERN = /^[a-z0-9][a-z0-9._-]{0,63}$/; function parseBoardWidgetPath(e) { const t = /^\/__openclaw__\/board\/([^/]+)\/([^/]+)\/index\.html$/.exec(e); if (!t) return; try { const e = decodeURIComponent(t[1]); const n = decodeURIComponent(t[2]); if (!e || !BOARD_WIDGET_NAME_PATTERN.test(n)) return; return { sessionKey: e, name: n }; } catch { return; } } async function handleBoardHttpRequest(e, t, n = {}) { const r = new URL(e.url ?? "/", "http://localhost"); const i = r.pathname; if (!i.startsWith("/__openclaw__/board/")) return false; t.setHeader("Access-Control-Allow-Origin", "*"); if (!isReadHttpMethod(e.method)) { sendMethodNotAllowed(t, "GET, HEAD"); return true; } const a = parseBoardWidgetPath(i); if (!a) { respondNotFound(t); return true; } const o = r.searchParams.get("bt"); if (!o) { respondPlainText(t, 401, "Unauthorized"); return true; } try { try { var s = _usingCtx(); const r = getRemoteControlUiIngressContext(e); const i = verifyBoardViewTicket(o, { nowMs: n.nowMs }); if (!i) { respondPlainText(t, 401, "Unauthorized"); return true; } const c = s.u(await prepareRemoteIngressSessionRead(r?.resolvePrincipal(), i.sessionKey, i.agentId)); return await withAuthorizedBoardWidgetView(n.store ?? boardStore, o, (n) => { assertRemoteControlUiIngressCurrent(r); c.assertCurrent(); if ((n.agentId ? sessionObserverScopeKey(n.sessionKey, n.agentId) : n.sessionKey) !== a.sessionKey || n.name !== a.name) { respondPlainText(t, 401, "Unauthorized"); return true; } const i = n.document.html; t.statusCode = 200; t.setHeader("Content-Type", "text/html; charset=utf-8"); t.setHeader("Content-Length", String(Buffer.byteLength(i))); t.setHeader("Content-Security-Policy", buildBoardWidgetContentSecurityPolicy(n.document)); t.setHeader("Cache-Control", "no-cache"); t.end(e.method === "HEAD" ? void 0 : i); return true; }, { gatewayContext: n.resolveGatewayContext?.(), nowMs: n.nowMs }); } catch (e) { s.e = e; } finally { s.d(); } } catch (e) { if (e instanceof BoardGatewayUnavailableError) { respondPlainText(t, 503, "Service Unavailable"); return true; } respondPlainText(t, 401, "Unauthorized"); return true; } } //#endregion export { handleBoardHttpRequest };