Skip to content

Build your personal knowledge base with Trilium Notes

fix(setup): build sync URLs server-side with the real protocol and port

Elian Doran00816d1ccdb0
diff --git a/apps/client/src/setup.tsx b/apps/client/src/setup.tsx
index 40aa644ac2..890f190de8 100644
--- a/apps/client/src/setup.tsx
+++ b/apps/client/src/setup.tsx
@@ -551,9 +551,12 @@ async function getNetworkAddresses(): Promise<string[]> {
}
// Node's `os` module isn't available in the renderer (node integration is
- // disabled), so the desktop's network interfaces are enumerated server-side.
+ // disabled), and the desktop renderer's `location` points at the internal
+ // `trilium-app://` protocol rather than the real HTTP listener. So the
+ // server enumerates its interfaces and builds the reachable URLs (correct
+ // protocol and port included).
const { addresses } = await server.get<{ addresses: string[] }>("network-addresses");
- return addresses.map((addr) => `${location.protocol}//${addr}:${location.port}`);
+ return addresses;
}
function onSetupFinished() {
diff --git a/apps/server/src/routes/api/system_info.spec.ts b/apps/server/src/routes/api/system_info.spec.ts
index 22ea4ce8f8..909e75476c 100644
--- a/apps/server/src/routes/api/system_info.spec.ts
+++ b/apps/server/src/routes/api/system_info.spec.ts
@@ -28,7 +28,7 @@ vi.mock("os", () => ({
networkInterfaces: () => ({})
}));
-import systemInfoRoute, { collectNetworkAddresses, isCpuArchMismatch } from "./system_info.js";
+import systemInfoRoute, { buildNetworkUrl, collectNetworkAddresses, isCpuArchMismatch } from "./system_info.js";
type NetworkInterfaces = Parameters<typeof collectNetworkAddresses>[0];
@@ -92,4 +92,15 @@ describe("System info API", () => {
expect(collectNetworkAddresses({ lo: undefined } as unknown as NetworkInterfaces)).toEqual([]);
});
});
+
+ describe("buildNetworkUrl", () => {
+ it("builds a URL with the given protocol and port", () => {
+ expect(buildNetworkUrl("http", "192.168.1.20", 37840)).toBe("http://192.168.1.20:37840");
+ expect(buildNetworkUrl("https", "10.0.0.4", 8080)).toBe("https://10.0.0.4:8080");
+ });
+
+ it("wraps IPv6 literals in brackets", () => {
+ expect(buildNetworkUrl("http", "fd00::1", 37840)).toBe("http://[fd00::1]:37840");
+ });
+ });
});
diff --git a/apps/server/src/routes/api/system_info.ts b/apps/server/src/routes/api/system_info.ts
index e1d7a4f90b..eff0a304fe 100644
--- a/apps/server/src/routes/api/system_info.ts
+++ b/apps/server/src/routes/api/system_info.ts
@@ -1,6 +1,8 @@
import { execSync } from "child_process";
import { isMac, isWindows } from "../../services/utils";
import { arch, cpus, networkInterfaces } from "os";
+import config from "../../services/config.js";
+import port from "../../services/port.js";
function systemChecks() {
return {
@@ -9,14 +11,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).
+ * Returns the reachable URLs another device can use to sync with this host.
+ * Consumed by the setup "sync from desktop" screen. This lives server-side
+ * because the renderer can't reach Node's `os` module (node integration is
+ * disabled in the Electron renderer), and because the desktop renderer's
+ * `location` points at the internal `trilium-app://` protocol rather than the
+ * real HTTP listener — so the protocol and port must be resolved here too.
*/
function getNetworkAddresses() {
+ const protocol = config["Network"]["https"] ? "https" : "http";
+
return {
- addresses: collectNetworkAddresses(networkInterfaces())
+ addresses: collectNetworkAddresses(networkInterfaces()).map((addr) => buildNetworkUrl(protocol, addr, port))
};
}
@@ -82,6 +88,16 @@ function networkScore(addr: string): number {
return 3;
}
+/**
+ * Builds a connectable URL from a host address, wrapping IPv6 literals in
+ * brackets as required by the URL authority syntax. Pure and exported for
+ * unit testing.
+ */
+export function buildNetworkUrl(protocol: string, address: string, port: number): string {
+ const host = address.includes(":") ? `[${address}]` : address;
+ return `${protocol}://${host}:${port}`;
+}
+
export default {
systemChecks,
getNetworkAddresses