mirror of
https://github.com/openclaw/openclaw.git
synced 2026-04-19 12:08:37 +00:00
refactor(infra): reuse lan ip picker
This commit is contained in:
@@ -1,5 +1,6 @@
|
|||||||
import { spawnSync } from "node:child_process";
|
import { spawnSync } from "node:child_process";
|
||||||
import os from "node:os";
|
import os from "node:os";
|
||||||
|
import { pickPrimaryLanIPv4 } from "../gateway/net.js";
|
||||||
|
|
||||||
export type SystemPresence = {
|
export type SystemPresence = {
|
||||||
host?: string;
|
host?: string;
|
||||||
@@ -43,25 +44,7 @@ function normalizePresenceKey(key: string | undefined): string | undefined {
|
|||||||
}
|
}
|
||||||
|
|
||||||
function resolvePrimaryIPv4(): string | undefined {
|
function resolvePrimaryIPv4(): string | undefined {
|
||||||
const nets = os.networkInterfaces();
|
return pickPrimaryLanIPv4() ?? os.hostname();
|
||||||
const prefer = ["en0", "eth0"];
|
|
||||||
const pick = (names: string[]) => {
|
|
||||||
for (const name of names) {
|
|
||||||
const list = nets[name];
|
|
||||||
const entry = list?.find((n) => n.family === "IPv4" && !n.internal);
|
|
||||||
if (entry?.address) {
|
|
||||||
return entry.address;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
for (const list of Object.values(nets)) {
|
|
||||||
const entry = list?.find((n) => n.family === "IPv4" && !n.internal);
|
|
||||||
if (entry?.address) {
|
|
||||||
return entry.address;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return undefined;
|
|
||||||
};
|
|
||||||
return pick(prefer) ?? os.hostname();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
function initSelfPresence() {
|
function initSelfPresence() {
|
||||||
|
|||||||
Reference in New Issue
Block a user