refactor(infra): reuse lan ip picker

This commit is contained in:
Peter Steinberger
2026-02-15 04:47:16 +00:00
parent 28014de974
commit cc15b8c6ad

View File

@@ -1,5 +1,6 @@
import { spawnSync } from "node:child_process";
import os from "node:os";
import { pickPrimaryLanIPv4 } from "../gateway/net.js";
export type SystemPresence = {
host?: string;
@@ -43,25 +44,7 @@ function normalizePresenceKey(key: string | undefined): string | undefined {
}
function resolvePrimaryIPv4(): string | undefined {
const nets = os.networkInterfaces();
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();
return pickPrimaryLanIPv4() ?? os.hostname();
}
function initSelfPresence() {