diff --git a/apps/server/src/startupAccess.test.ts b/apps/server/src/startupAccess.test.ts index fd56b4a4c86f..a48bb5263f8e 100644 --- a/apps/server/src/startupAccess.test.ts +++ b/apps/server/src/startupAccess.test.ts @@ -1,9 +1,11 @@ import type * as NodeOS from "node:os"; import { describe, expect, it } from "@effect/vitest"; +import * as NetAddress from "effect/net/NetAddress"; import { acceptsPrivateNetworkConnection, + loopbackUrlHostForAddress, resolveHeadlessConnectionString, } from "./startupAccess.ts"; @@ -55,6 +57,18 @@ it("prefers a LAN address over a tailnet one for a private-network pairing URL", ).toBe("http://192.168.1.42:3773"); }); +it("targets the bound interface when the listener binds a specific address", () => { + const hostFor = (ip: string) => + loopbackUrlHostForAddress(NetAddress.inetAddressFromIpStringUnsafe(ip, 3773)); + + expect(hostFor("0.0.0.0")).toBe("127.0.0.1"); + expect(hostFor("::")).toBe("127.0.0.1"); + expect(hostFor("::1")).toBe("[::1]"); + expect(hostFor("127.0.0.1")).toBe("127.0.0.1"); + expect(hostFor("127.0.0.2")).toBe("127.0.0.2"); + expect(hostFor("100.101.102.103")).toBe("100.101.102.103"); +}); + describe("acceptsPrivateNetworkConnection", () => { const accepts = (localAddress: string | undefined, remoteAddress: string | undefined) => acceptsPrivateNetworkConnection({ localAddress, remoteAddress }); diff --git a/apps/server/src/startupAccess.ts b/apps/server/src/startupAccess.ts index bcbfedff6089..f393faecebf9 100644 --- a/apps/server/src/startupAccess.ts +++ b/apps/server/src/startupAccess.ts @@ -71,9 +71,9 @@ export const acceptsPrivateNetworkConnection = (socket: { ); export const loopbackUrlHostForAddress = (address: NetAddress.InetAddress): string => - address._tag === "InetAddressV6" && NetAddress.isLoopback(address.address) - ? "[::1]" - : "127.0.0.1"; + NetAddress.isUnspecified(address.address) + ? "127.0.0.1" + : NetAddress.formatUrlHostString(NetAddress.formatIp(address.address)); const normalizeHost = (host: string): string => host.startsWith("[") && host.endsWith("]") ? host.slice(1, -1) : host;