UNPKG

openclaw

Version:

Multi-channel AI gateway with extensible messaging integrations

53 lines (52 loc) • 1.78 kB
import { spawnSync } from "node:child_process"; import { readFileSync } from "node:fs"; import { createHmac } from "node:crypto"; import { getEnvironmentData, setEnvironmentData } from "node:worker_threads"; //#region src/infra/current-machine-identity.ts const PROCESS_MACHINE_IDENTITY_KEY = "openclaw.processMachineIdentity"; /** Machine identity cannot change within this process; retain failed probes too. */ function readProcessMachineIdentity() { let e = getEnvironmentData(PROCESS_MACHINE_IDENTITY_KEY); if (e?.platform !== process.platform) { e = { platform: process.platform, value: probeMachineIdentity() }; setEnvironmentData(PROCESS_MACHINE_IDENTITY_KEY, e); } return e.value; } function probeMachineIdentity() { try { let e; if (process.platform === "linux") { const t = readFileSync("/etc/machine-id", "utf8").trim(); if (/^[a-f0-9]{32}$/u.test(t)) e = t; } else if (process.platform === "darwin") { const t = spawnSync("/usr/sbin/ioreg", [ "-rd1", "-c", "IOPlatformExpertDevice" ], { encoding: "utf8", timeout: 1e3, maxBuffer: 65536, killSignal: "SIGKILL", stdio: [ "ignore", "pipe", "ignore" ] }); const n = [...(t.stdout ?? "").matchAll(/"IOPlatformUUID"\s*=\s*"([^"]+)"/gu)]; const r = n.length === 1 ? n[0]?.[1]?.toLowerCase() : void 0; if (!t.error && t.status === 0 && r && /^[a-f0-9]{8}(?:-[a-f0-9]{4}){3}-[a-f0-9]{12}$/u.test(r)) e = r; } if (!e || /^0+$/u.test(e.replaceAll("-", ""))) return null; return createHmac("sha256", "openclaw:state-lease-process-owner:machine:v1").update(`${process.platform}\0${e}`).digest("hex"); } catch { return null; } } //#endregion export { readProcessMachineIdentity as n, PROCESS_MACHINE_IDENTITY_KEY as t };