openclaw
Version:
Multi-channel AI gateway with extensible messaging integrations
3 lines • 4.44 kB
JavaScript
import{o as registerSecretValueForRedaction}from"./secret-redaction-registry-DospXE4m.mjs";import{n as NODE_DESKTOP_STREAM_COMMAND}from"./node-desktop-stream-ldsAKscA.mjs";import{s as isNodeCommandAllowed,u as resolveNodeCommandAllowlist}from"./node-command-policy-Qm4E-4M-.mjs";import{t as DesktopCredentialsRequiredError}from"./host-source-errors-OIAs3Kv5.mjs";import{r as mintDesktopObserverToken}from"./observe-bridge-oJ9dsvHx.mjs";
//!src/gateway/desktop/node-source.ts
function createNodeDesktopService(e){const t=/* @__PURE__ */ new Map;const n=/* @__PURE__ */ new Map;const commandAllowed=t=>isNodeCommandAllowed({command:NODE_DESKTOP_STREAM_COMMAND,declaredCommands:t.commands,allowlist:resolveNodeCommandAllowlist(e.getConfig(),t)}).ok;const stopNode=t=>e.desktopRegistry.stop(`node:${t}`);const ensureSession=async r=>{const i=`node:${r.nodeId}`;const a=n.get(r.nodeId);if(a?.connId===r.connId&&a.pairingGeneration===r.pairingGeneration){await e.desktopRegistry.activate({sourceKey:i,ownerEpoch:a.ownerEpoch});return a}const o=(t.get(r.nodeId)??0)+1;t.set(r.nodeId,o);const s={connId:r.connId,pairingGeneration:r.pairingGeneration,ownerEpoch:o,active:/* @__PURE__ */ new Set};n.set(r.nodeId,s);try{await e.desktopRegistry.activate({sourceKey:i,ownerEpoch:o,teardown:async()=>{if(n.get(r.nodeId)===s)n.delete(r.nodeId);await Promise.all([...s.active].map(e=>e.stop()));s.active.clear()}});return s}catch(e){if(n.get(r.nodeId)===s)n.delete(r.nodeId);throw e}};return{stopNode,async reconcileRuntimePolicy(){await Promise.all([...n].map(async([t,n])=>{const r=e.nodeRegistry.get(t);if(!r||r.connId!==n.connId||r.pairingGeneration!==n.pairingGeneration||!commandAllowed(r))await stopNode(t)}))},async observe(t){const r=e.nodeRegistry.get(t.nodeId);if(!r?.pairingGeneration)throw new Error(`node desktop is unavailable; reconnect and approve the node capability`);const i=r.pairingGeneration;const isRequesterCurrent=()=>!t.requester?.signal?.aborted&&t.requester?.isCurrent()!==false;const isAuthorized=()=>e.nodeRegistry.get(t.nodeId)===r&&r.pairingGeneration===i&&commandAllowed(r);const assertAuthorized=()=>{if(!isRequesterCurrent())throw new Error(`Desktop observer connection is no longer current`);if(!isAuthorized())throw new Error(`node desktop is unavailable; enable Desktop Sharing on the node, approve its capability request, and check gateway.nodes.commands.deny`)};assertAuthorized();const a=`node:${t.nodeId}`;const o=await ensureSession({nodeId:t.nodeId,connId:r.connId,pairingGeneration:i});assertAuthorized();const s=e.desktopRegistry.createStream({sourceKey:a,ownerEpoch:o.ownerEpoch,onStopped:()=>{o.active.delete(s)}});if(!s.reserve())throw new Error(`node desktop observer limit reached`);const c=t.requester?.signal?AbortSignal.any([s.signal,t.requester.signal]):s.signal;o.active.add(s);try{const l=e.streamBroker.mint({nodeId:t.nodeId,connId:r.connId,pairingGeneration:i});const u=await s.connect(l,()=>e.nodeRegistry.invoke({nodeId:t.nodeId,expectedConnId:r.connId,expectedPairingGeneration:i,command:NODE_DESKTOP_STREAM_COMMAND,params:{ticket:l.ticket,attachPath:l.attachPath},timeoutMs:0,onProgress:()=>{},signal:c,isDispatchAuthorized:()=>!s.stopped&&n.get(t.nodeId)===o&&isRequesterCurrent()&&isAuthorized()}));if(s.stopped||n.get(t.nodeId)!==o){u.stream.destroy();throw new Error(`node desktop session was superseded before attachment`)}assertAuthorized();let d;if(u.auth===`vnc-password`){const e=u.vncPassword??t.credentials?.password;if(!e)throw new DesktopCredentialsRequiredError(`vnc-password`,`VNC password is required to observe this node`);d={auth:u.auth,credentials:{password:e}}}else{const e=t.credentials?.username?.trim()??``;const n=t.credentials?.password??``;if(!e||!n)throw new DesktopCredentialsRequiredError(`ard-account`,`macOS account credentials are required to observe this node`);d={auth:u.auth,credentials:{username:e,password:n}}}registerSecretValueForRedaction(d.credentials.password);const f=s.publish();if(!f)throw new Error(`node desktop session was superseded before publication`);const p=mintDesktopObserverToken({sourceKey:a,ownerEpoch:o.ownerEpoch,control:t.control,requester:t.requester,attachment:f,preauth:d,onAbandon:s.stop});s.expireAt(p.expiresAtMs);return{transport:`rfb`,wsPath:`/desktop/observe?token=${p.token}`,expiresAtMs:p.expiresAtMs,control:t.control,auth:u.auth,preauthenticated:true}}catch(e){await s.stop();throw e}}}}export{createNodeDesktopService};