Skip to content

Build your personal knowledge base with Trilium Notes

fix(setup): enumerate network addresses server-side for the sync screen

Elian Doran7ca8cd096bb1
diff --git a/apps/client/src/setup.tsx b/apps/client/src/setup.tsx
index e24353e50e..40aa644ac2 100644
--- a/apps/client/src/setup.tsx
+++ b/apps/client/src/setup.tsx
@@ -410,7 +410,11 @@ function SyncFromServer({ setState }: { setState: (state: State) => void }) {
}
function SyncFromDesktop({ setState }: { setState: (state: State) => void }) {
- const networkAddresses = getNetworkAddresses();
+ const [ networkAddresses, setNetworkAddresses ] = useState<string[]>([]);
+
+ useEffect(() => {
+ getNetworkAddresses().then(setNetworkAddresses);
+ }, []);
useEffect(() => {
const interval = setInterval(async () => {
@@ -541,39 +545,17 @@ function SetupPage({ title, description, className, illustration, children, foot
);
}
-function getNetworkAddresses(): string[] {
+async function getNetworkAddresses(): Promise<string[]> {
if (!isElectron()) {
return [`${location.protocol}//${location.host}`];
}
- // eslint-disable-next-line @typescript-eslint/no-require-imports
- const os = require("os") as typeof import("os");
- const interfaces = os.networkInterfaces();
- const addresses: string[] = [];
-
- for (const nets of Object.values(interfaces)) {
- if (!nets) continue;
- for (const net of nets) {
- if (net.internal) continue;
- if (net.family === "IPv6" && net.scopeid !== 0) continue;
- addresses.push(net.address);
- }
- }
-
- // Sort by likelihood of being the local network address.
- addresses.sort((a, b) => networkScore(a) - networkScore(b));
-
+ // Node's `os` module isn't available in the renderer (node integration is
+ // disabled), so the desktop's network interfaces are enumerated server-side.
+ const { addresses } = await server.get<{ addresses: string[] }>("network-addresses");
return addresses.map((addr) => `${location.protocol}//${addr}:${location.port}`);
}
-function networkScore(addr: string): number {
- if (addr.startsWith("192.168.")) return 0;
- if (addr.startsWith("10.")) return 1;
- if (/^172\.(1[6-9]|2\d|3[01])\./.test(addr)) return 2;
- if (addr.includes(":")) return 4; // IPv6
- return 3;
-}
-
function onSetupFinished() {
if (isElectron()) {
// On Electron we need to use the setup route because it handles the closing of the setup window and opening the main app window.
diff --git a/apps/server/src/routes/api/system_info.spec.ts b/apps/server/src/routes/api/system_info.spec.ts
index faad54aa4e..22ea4ce8f8 100644
--- a/apps/server/src/routes/api/system_info.spec.ts
+++ b/apps/server/src/routes/api/system_info.spec.ts
@@ -24,10 +24,13 @@ vi.mock("child_process", () => ({
vi.mock("os", () => ({
arch: () => state.arch,
- cpus: () => [{ model: state.cpuModel }]
+ cpus: () => [{ model: state.cpuModel }],
+ networkInterfaces: () => ({})
}));
-import systemInfoRoute, { isCpuArchMismatch } from "./system_info.js";
+import systemInfoRoute, { collectNetworkAddresses, isCpuArchMismatch } from "./system_info.js";
+
+type NetworkInterfaces = Parameters<typeof collectNetworkAddresses>[0];
describe("System info API", () => {
afterEach(() => {
@@ -62,4 +65,31 @@ describe("System info API", () => {
state.cpuModel = "Intel";
expect(isCpuArchMismatch()).toBe(false);
});
+
+ describe("collectNetworkAddresses", () => {
+ it("drops internal and scope-bound IPv6 addresses, then sorts by LAN likelihood", () => {
+ const interfaces = {
+ lo: [{ address: "127.0.0.1", internal: true, family: "IPv4" }],
+ eth0: [
+ { address: "203.0.113.5", internal: false, family: "IPv4" },
+ { address: "192.168.1.20", internal: false, family: "IPv4" },
+ { address: "10.0.0.4", internal: false, family: "IPv4" },
+ { address: "fe80::1", internal: false, family: "IPv6", scopeid: 4 },
+ { address: "fd00::1", internal: false, family: "IPv6", scopeid: 0 }
+ ]
+ } as unknown as NetworkInterfaces;
+
+ expect(collectNetworkAddresses(interfaces)).toEqual([
+ "192.168.1.20", // 192.168.* ranks first
+ "10.0.0.4", // then 10.*
+ "203.0.113.5", // then other IPv4
+ "fd00::1" // IPv6 last; fe80::1 dropped (scopeid !== 0)
+ ]);
+ });
+
+ it("returns an empty list when no usable interfaces exist", () => {
+ expect(collectNetworkAddresses({})).toEqual([]);
+ expect(collectNetworkAddresses({ lo: undefined } as unknown as NetworkInterfaces)).toEqual([]);
+ });
+ });
});
diff --git a/apps/server/src/routes/api/system_info.ts b/apps/server/src/routes/api/system_info.ts
index 32c7f91f7f..e1d7a4f90b 100644
--- a/apps/server/src/routes/api/system_info.ts
+++ b/apps/server/src/routes/api/system_info.ts
@@ -1,6 +1,6 @@
import { execSync } from "child_process";
import { isMac, isWindows } from "../../services/utils";
-import { arch, cpus } from "os";
+import { arch, cpus, networkInterfaces } from "os";
function systemChecks() {
return {
@@ -8,6 +8,18 @@ function systemChecks() {
}
}
+/**
+ * Returns this host's non-internal network addresses. Consumed by the setup
+ * "sync from desktop" screen so the user can see which addresses another device
+ * should connect to. This lives server-side because the renderer can't reach
+ * Node's `os` module (node integration is disabled in the Electron renderer).
+ */
+function getNetworkAddresses() {
+ return {
+ addresses: collectNetworkAddresses(networkInterfaces())
+ };
+}
+
/**
* Detects if the application is running under emulation on Apple Silicon or Windows on ARM.
* This happens when an x64 version of the app is run on an M1/M2/M3 Mac or on a Windows Snapdragon chip.
@@ -40,6 +52,37 @@ export const isCpuArchMismatch = () => {
}
};
+/**
+ * Collects this host's non-internal network addresses, sorted by how likely
+ * each is to be the reachable LAN address. Pure and exported so it can be
+ * unit-tested without touching the real OS interfaces.
+ */
+export function collectNetworkAddresses(interfaces: ReturnType<typeof networkInterfaces>): string[] {
+ const addresses: string[] = [];
+
+ for (const nets of Object.values(interfaces)) {
+ if (!nets) continue;
+ for (const net of nets) {
+ if (net.internal) continue;
+ if (net.family === "IPv6" && net.scopeid !== 0) continue;
+ addresses.push(net.address);
+ }
+ }
+
+ addresses.sort((a, b) => networkScore(a) - networkScore(b));
+
+ return addresses;
+}
+
+function networkScore(addr: string): number {
+ if (addr.startsWith("192.168.")) return 0;
+ if (addr.startsWith("10.")) return 1;
+ if (/^172\.(1[6-9]|2\d|3[01])\./.test(addr)) return 2;
+ if (addr.includes(":")) return 4; // IPv6
+ return 3;
+}
+
export default {
- systemChecks
+ systemChecks,
+ getNetworkAddresses
};
diff --git a/apps/server/src/routes/routes.ts b/apps/server/src/routes/routes.ts
index 93d794db23..85fa7059b0 100644
--- a/apps/server/src/routes/routes.ts
+++ b/apps/server/src/routes/routes.ts
@@ -127,6 +127,7 @@ function register(app: express.Application) {
apiRoute(GET, "/api/metrics", metricsRoute.getMetrics);
apiRoute(GET, "/api/system-checks", systemInfoRoute.systemChecks);
+ apiRoute(GET, "/api/network-addresses", systemInfoRoute.getNetworkAddresses);
// docker health check
route(GET, "/api/health-check", [], () => ({ status: "ok" }), apiResultHandler);