openclaw
Version:
Multi-channel AI gateway with extensible messaging integrations
43 lines (42 loc) • 1.35 kB
JavaScript
import os from "node:os";
//#region src/infra/network-interfaces.ts
function normalizeNetworkInterfaceFamily(e) {
if (e === "IPv4" || e === 4) return "IPv4";
return e === "IPv6" || e === 6 ? "IPv6" : void 0;
}
/** Best-effort interface read that returns undefined when OS inspection fails. */
function safeNetworkInterfaces(e = os.networkInterfaces) {
try {
return e();
} catch {
return;
}
}
function listExternalInterfaceAddresses(e, t) {
const n = [];
for (const [r, i] of Object.entries(e ?? {})) for (const e of i ?? []) {
if (!e || e.internal) continue;
const i = e.address?.trim();
if (!i) continue;
const a = normalizeNetworkInterfaceFamily(e.family);
if (!a || t && a !== t) continue;
n.push({
name: r,
address: i,
family: a
});
}
return n;
}
/** Picks a matching external address, honoring preferred interface names first. */
function pickMatchingExternalInterfaceAddress(e, t) {
const { family: n, preferredNames: r = [], matches = () => true } = t;
const i = listExternalInterfaceAddresses(e, n);
for (const e of r) {
const t = i.find((t) => t.name === e && matches(t.address));
if (t) return t.address;
}
return i.find((e) => matches(e.address))?.address;
}
//#endregion
export { pickMatchingExternalInterfaceAddress as n, safeNetworkInterfaces as r, listExternalInterfaceAddresses as t };