UNPKG

openclaw

Version:

Multi-channel AI gateway with extensible messaging integrations

3 lines • 5.76 kB
import{r as collectErrorGraphCandidates}from"./error-coercion-DlKnt6w-.mjs";import{n as parseBooleanValue}from"./boolean-Dt_JCqyW.mjs";import{t as truncateUtf8Prefix}from"./utf8-truncate-B8GdsL7T.mjs";import{o as registerUncaughtExceptionHandler,s as registerUnhandledRejectionHandler}from"./unhandled-rejections-D3-QfQTZ.mjs";import"./string-coerce-runtime-BYDH9Quw.mjs";import"./text-utility-runtime-CDtGqkrl.mjs";import"./error-runtime-DW-sj0iq.mjs";import"./runtime-BtgUkczr.mjs";import fs from"node:fs";import os from"node:os";import{format}from"node:util"; //!extensions/bonjour/src/advertiser.ts function formatBonjourError(e){if(e instanceof Error){const t=e.message.trim()||e.name||String(e).trim();if(e.name&&e.name!==`Error`)return t===e.name?e.name:`${e.name}: ${t}`;return t}return String(e)}const CIAO_NETMASK_ASSERTION_MESSAGE_RE=/IP ADDRESS VERSION MUST MATCH\.\s+NETMASK CANNOT HAVE A VERSION DIFFERENT FROM THE ADDRESS!?/u;const CIAO_INTERFACE_ENUMERATION_FAILURE_RE=/\bUV_INTERFACE_ADDRESSES\b/u;function classifyCiaoProcessError(e){for(const t of collectErrorGraphCandidates(e,e=>[e.cause,e.reason,e.original,e.error,e.data,...Array.isArray(e.errors)?e.errors:[]])){const e=formatBonjourError(t);const n=e.toUpperCase();if(CIAO_NETMASK_ASSERTION_MESSAGE_RE.test(n))return{kind:`netmask-assertion`,formatted:e};if(CIAO_INTERFACE_ENUMERATION_FAILURE_RE.test(n))return{kind:`interface-enumeration-failure`,formatted:e}}return null}const CIAO_SELF_PROBE_RETRY_FRAGMENT=`failed probing with reason: Error: Can't probe for a service which is announced already.`;const CIAO_MDNS_SOCKET_ERROR_FRAGMENT=`Encountered MDNS socket error on socket`;const CIAO_ENODEV_SOCKET_ERROR_FRAGMENT=`send ENODEV`;function isContainerEnvironment(){if(process.env.FLY_MACHINE_ID?.trim()&&process.env.FLY_APP_NAME?.trim())return true;for(const e of[`/.dockerenv`,`/run/.containerenv`,`/var/run/.containerenv`])if(fs.existsSync(e))return true;try{const e=fs.readFileSync(`/proc/1/cgroup`,`utf8`);return/\/docker\/|cri-containerd-[0-9a-f]|containerd\/[0-9a-f]{64}|\/kubepods[/.]|\blxc\b/u.test(e)}catch{return false}}function resolveSystemMdnsHostname(){let e;try{e=os.hostname()}catch{return null}const t=e.trim().replace(/\.local$/i,``).split(`.`)[0]?.trim()??``;if(!/^[A-Za-z0-9](?:[A-Za-z0-9-]{0,61}[A-Za-z0-9])?$/.test(t))return null;return t}const MAX_DNS_LABEL_BYTES=63;function truncateToDnsLabel(e,t=`OpenClaw`){if(Buffer.byteLength(e)<=MAX_DNS_LABEL_BYTES)return e;return truncateUtf8Prefix(e,MAX_DNS_LABEL_BYTES).replace(/-+$/,``).trim()||t}function serviceSummary(e,t){return`${e} fqdn=${t.getFQDN()} host=${t.getHostname()} port=${t.getPort()} state=${t.serviceState}`}function createCiaoLogger(e){return{log(...t){if(t.some(e=>typeof e===`string`&&e.includes(CIAO_SELF_PROBE_RETRY_FRAGMENT)))return;e.info(`bonjour: ${format(...t)}`)},warn(...t){if(t.some(e=>typeof e===`string`&&e.includes(CIAO_MDNS_SOCKET_ERROR_FRAGMENT)&&e.includes(CIAO_ENODEV_SOCKET_ERROR_FRAGMENT)))return;e.warn(`bonjour: ${format(...t)}`)},error(...t){e.error(`bonjour: ${format(...t)}`)}}}async function startGatewayBonjourAdvertiser(e,t){if(process.env.VITEST||(parseBooleanValue(process.env.OPENCLAW_DISABLE_BONJOUR)??isContainerEnvironment()))return{stop:async()=>{}};let n;let r;function cleanupProcessHandlers(){r?.();n?.()}try{const{getResponder:i}=await import(`@homebridge/ciao/native.js`);const handleCiaoProcessError=e=>{const n=classifyCiaoProcessError(e);if(!n)return false;if(n.kind===`interface-enumeration-failure`)t.warn(`bonjour: disabling mDNS — networkInterfaces() unavailable in this environment: ${n.formatted}`);else t.warn(`bonjour: suppressing ciao netmask assertion: ${n.formatted}`);return true};n=registerUnhandledRejectionHandler(handleCiaoProcessError);r=registerUncaughtExceptionHandler(handleCiaoProcessError);const a=(process.env.OPENCLAW_MDNS_HOSTNAME?.trim()||resolveSystemMdnsHostname()||`openclaw`).replace(/\.local$/i,``);const o=a.indexOf(`.`);const s=o===-1?a.length:o;const c=truncateToDnsLabel(a.slice(0,s).trim()||`openclaw`,`openclaw`);const l=e.instanceName?.trim()||`${c} (OpenClaw)`;const u=l.trim().replace(/\s+/g,` `);const d=u.replace(/\s+\(OpenClaw\)\s*$/i,``).trim()||u;const f={role:`gateway`,gatewayPort:String(e.gatewayPort),lanHost:`${c}.local`,displayName:d};if(e.gatewayTlsEnabled){f.gatewayTls=`1`;if(e.gatewayTlsFingerprintSha256)f.gatewayTlsSha256=e.gatewayTlsFingerprintSha256}if(e.gatewayDirectReachable)f.gatewayDirectReachable=`1`;if(!e.minimal&&e.tailnetDns?.trim())f.tailnetDns=e.tailnetDns.trim();if(!e.minimal&&e.cliPath?.trim())f.cliPath=e.cliPath.trim();f.transport=`gateway`;if(!e.minimal)f.sshPort=String(e.sshPort??22);const p=i({logger:createCiaoLogger(t)});function handleAdvertiseFailure(e,n,r,i){const a=classifyCiaoProcessError(r);if(a){t.warn(`bonjour: advertise ${i} with ciao ${a.kind} (${serviceSummary(e,n)}): ${a.formatted}`);return}t.warn(`bonjour: advertise ${i} (${serviceSummary(e,n)}): ${formatBonjourError(r)}`)}const m=p.createService({name:truncateToDnsLabel(l),type:`openclaw-gw`,port:e.gatewayPort,domain:`local`,hostname:c,txt:f});m.on(`name-change`,e=>{t.warn(`bonjour: gateway name conflict resolved; newName=${JSON.stringify(e)}`)});m.on(`hostname-change`,e=>{t.warn(`bonjour: gateway hostname conflict resolved; newHostname=${JSON.stringify(e)}`)});try{m.advertise().then(()=>{t.info(`bonjour: advertised ${serviceSummary(`gateway`,m)}`)}).catch(e=>{handleAdvertiseFailure(`gateway`,m,e,`failed`)})}catch(e){handleAdvertiseFailure(`gateway`,m,e,`threw`)}let h=null;return{stop:()=>{h??=(async()=>{try{await m.destroy()}catch{}try{await p.shutdown()}catch{}cleanupProcessHandlers()})();return h}}}catch(e){cleanupProcessHandlers();throw e}}export{startGatewayBonjourAdvertiser};