UNPKG

openclaw

Version:

Multi-channel AI gateway with extensible messaging integrations

3 lines • 7.14 kB
import{o as normalizeLowercaseStringOrEmpty}from"./string-coerce-CpTFcSwY.mjs";import{t as isContainerEnvironment}from"./container-environment-C0Hd9WfF.mjs";import{S as parseNormalizedIpAddress,b as parseIpAddressOrCidr,f as isLoopbackIpAddress,g as isRfc8215LocalUseNat64Ipv6Address,i as isCanonicalDottedDecimalIPv4,m as isPrivateOrLoopbackIpAddress,p as isParsedIpInCidr,v as normalizeIpAddress}from"./ip-DwojXwyb.mjs";import{a as parseHostForAddressChecks}from"./client-address-utils-CIkL4kpz.mjs";import{n as pickMatchingExternalInterfaceAddress,r as safeNetworkInterfaces}from"./network-interfaces-DP7VnBjC.mjs";import{n as pickPrimaryTailnetIPv4}from"./tailnet-Cz93E_XQ.mjs";import{t as firstHeaderValue}from"./http-header-value-D4bOEsPJ.mjs";import{t as normalizeWebSocketProtocol}from"./websocket-protocol-a7ZCoyTI.mjs";import os from"node:os";import net from"node:net"; //!src/gateway/net.ts function pickPrimaryLanIPv4(){return pickMatchingExternalInterfaceAddress(os.networkInterfaces(),{family:`IPv4`,preferredNames:[`en0`,`eth0`]})}function normalizeHostHeader(e){return normalizeLowercaseStringOrEmpty(e)}function resolveHostName(e){const t=normalizeHostHeader(e);if(!t)return``;if(t.startsWith(`[`)){const e=t.indexOf(`]`);if(e!==-1)return t.slice(1,e)}if(net.isIP(t)===6)return t;const[n]=t.split(`:`);return n??``}function isLoopbackAddress(e){return isLoopbackIpAddress(e)}function hasForwardedRequestHeaders(e){if(!e)return false;const t=e.headers??{};return Object.keys(t).some(e=>{const t=normalizeLowercaseStringOrEmpty(e);return t===`forwarded`||t===`x-real-ip`||t.startsWith(`x-forwarded-`)})}function isLocalDirectRequest(e){return Boolean(e&&!hasForwardedRequestHeaders(e)&&isLoopbackAddress(e.socket?.remoteAddress))}function resolveLocalInterfaceAddressMatch(e){const t=normalizeIpAddress(e);if(!t)return false;const n=safeNetworkInterfaces();if(!n)return;for(const e of Object.values(n))for(const n of e??[])if(normalizeIpAddress(n.address)===t)return true;return false}function isPrivateOrLoopbackAddress(e){return isPrivateOrLoopbackIpAddress(e)&&!isRfc8215LocalUseNat64Ipv6Address(e)}function stripOptionalPort(e){if(e.startsWith(`[`)){const t=e.indexOf(`]`);if(t!==-1)return e.slice(1,t)}if(net.isIP(e))return e;const t=e.lastIndexOf(`:`);if(t>-1&&e.includes(`.`)&&e.indexOf(`:`)===t){const n=e.slice(0,t);if(net.isIP(n)===4)return n}return e}function parseIpLiteral(e){const t=e?.trim();if(!t)return;const n=stripOptionalPort(t);const r=parseNormalizedIpAddress(n);if(!r||net.isIP(r.toString())===0)return;return r}const trustedProxyRanges=/* @__PURE__ */ new WeakMap;function getTrustedProxyRanges(e){if(!e?.length)return;const t=trustedProxyRanges.get(e);if(t&&t.source.length===e.length){let n=0;while(n<e.length&&t.source[n]===e[n])n++;if(n===e.length)return t.ranges}const n=e.map(parseIpAddressOrCidr);trustedProxyRanges.set(e,{source:e.slice(),ranges:n});return n}function isTrustedParsedProxyAddress(e,t){return t?.some(t=>isParsedIpInCidr(e,t))??false}function isTrustedProxyAddress(e,t){const n=parseNormalizedIpAddress(e);return n?isTrustedParsedProxyAddress(n,getTrustedProxyRanges(t)):false}function resolveClientIp(e){const t=parseNormalizedIpAddress(e.remoteAddr);if(!t)return;const n=getTrustedProxyRanges(e.trustedProxies);if(!isTrustedParsedProxyAddress(t,n))return t.toString();const r=e.forwardedFor?.split(`,`)??[];for(let e=r.length-1;e>=0;e--){const t=parseIpLiteral(r[e]);if(t&&t.range()!==`loopback`&&!isTrustedParsedProxyAddress(t,n))return t.toString()}if(e.allowRealIpFallback)return parseIpLiteral(e.realIp)?.toString()}function resolveRequestClientIpFromHeaders(e,t,n=false){if(!e)return;return resolveClientIp({remoteAddr:e.socket?.remoteAddress??``,forwardedFor:firstHeaderValue(e.headers?.[`x-forwarded-for`]),realIp:firstHeaderValue(e.headers?.[`x-real-ip`]),trustedProxies:t,allowRealIpFallback:n})}async function resolveGatewayBindHost(e,t){const n=e??`loopback`;if(n===`loopback`)return`127.0.0.1`;if(n===`tailnet`){const e=pickPrimaryTailnetIPv4();if(e&&await canBindToHost(e))return e}if(n===`tailnet`||n===`auto`&&!isContainerEnvironment())return await canBindToHost(`127.0.0.1`)?`127.0.0.1`:`0.0.0.0`;if(n===`custom`){const e=t?.trim();if(e&&isValidIPv4(e)&&await canBindToHost(e))return e}return`0.0.0.0`}function defaultGatewayBindMode(e){if(e&&e!==`off`)return`loopback`;return isContainerEnvironment()?`auto`:`loopback`}async function canBindToHost(e){return new Promise(t=>{const n=net.createServer();const r=setTimeout(()=>finish(false),3e3);const finish=e=>{clearTimeout(r);n.close();t(e)};n.once(`error`,()=>finish(false));n.once(`listening`,()=>finish(true));try{n.listen(0,e)}catch{finish(false)}})}async function resolveGatewayListenHosts(e){const t=resolveGatewayRequiredListenHosts(e);if(e!==`127.0.0.1`)return t;if(process.platform===`win32`)return[e];if(await canBindToHost(`::1`))return[e,`::1`];return[e]}function resolveGatewayRequiredListenHosts(e){if(!isValidIPv4(e)||e===`0.0.0.0`||e===`127.0.0.1`)return[e];return[e,`127.0.0.1`]}function isValidIPv4(e){return isCanonicalDottedDecimalIPv4(e)}function isLoopbackHost(e){const t=typeof e===`string`?parseHostForAddressChecks(e):null;if(!t)return false;if(t.isLocalhost)return true;return isLoopbackAddress(t.unbracketedHost)}function isLoopbackGatewayUrl(e){try{const t=new URL(e).hostname.toLowerCase();const n=t.startsWith(`[`)&&t.endsWith(`]`)?t.slice(1,-1):t;return n===`localhost`||isLoopbackIpAddress(n)}catch{return false}}function isLocalishHost(e){const t=resolveHostName(e);if(!t)return false;return isLoopbackHost(t)||t.endsWith(`.ts.net`)}function isPrivateOrLoopbackHost(e){const t=typeof e===`string`?parseHostForAddressChecks(e):null;if(!t)return false;if(t.isLocalhost)return true;const n=normalizeIpAddress(t.unbracketedHost);if(!n||!isPrivateOrLoopbackAddress(n))return false;if(net.isIP(n)===6){if(n.startsWith(`ff`))return false;if(n===`::`)return false}return true}function isSecureWebSocketUrl(e,t){let n;try{n=new URL(e)}catch{return false}const r=normalizeWebSocketProtocol(n.protocol);if(r===`wss:`)return true;if(r!==`ws:`)return false;if(isLoopbackHost(n.hostname))return true;if(isTrustedPlaintextWebSocketHost(n.hostname))return true;if(t?.allowPrivateWs){const e=n.hostname.startsWith(`[`)&&n.hostname.endsWith(`]`)?n.hostname.slice(1,-1):n.hostname;return net.isIP(e)===0}return false}function isTrustedPlaintextWebSocketHost(e){if(isPrivateOrLoopbackHost(e))return true;const t=normalizeLowercaseStringOrEmpty(e).replace(/\.+$/,``);return t.endsWith(`.local`)||t.endsWith(`.ts.net`)}export{resolveGatewayListenHosts as _,isLoopbackAddress as a,resolveLocalInterfaceAddressMatch as b,isPrivateOrLoopbackAddress as c,isTrustedProxyAddress as d,isValidIPv4 as f,resolveGatewayBindHost as g,resolveClientIp as h,isLocalishHost as i,isPrivateOrLoopbackHost as l,pickPrimaryLanIPv4 as m,hasForwardedRequestHeaders as n,isLoopbackGatewayUrl as o,normalizeHostHeader as p,isLocalDirectRequest as r,isLoopbackHost as s,defaultGatewayBindMode as t,isSecureWebSocketUrl as u,resolveGatewayRequiredListenHosts as v,resolveRequestClientIpFromHeaders as x,resolveHostName as y};