TypeScript
fix(web): reopening Connections picks up new network addresses (#15840)
Lars Nieuwenhuis55f4c7025642
diff --git a/apps/desktop/src/backend/DesktopServerExposure.test.ts b/apps/desktop/src/backend/DesktopServerExposure.test.tsindex 5e3d25392ef..75e1c8609a6 100644--- a/apps/desktop/src/backend/DesktopServerExposure.test.ts+++ b/apps/desktop/src/backend/DesktopServerExposure.test.ts@@ -40,7 +40,7 @@ const tailnetNetworkInterfaces: DesktopNetworkInterfaces.NetworkInterfaces = {],};-function layerMockSpawner(statusJson = "{}", onSpawn?: () => void) {+function layerMockSpawner(statusJson: string | (() => string) = "{}", onSpawn?: () => void) {return Layer.succeed(ChildProcessSpawner.ChildProcessSpawner,ChildProcessSpawner.make(() =>@@ -53,7 +53,9 @@ function layerMockSpawner(statusJson = "{}", onSpawn?: () => void) {kill: () => Effect.void,unref: Effect.succeed(Effect.void),stdin: Sink.drain,- stdout: Stream.make(encoder.encode(statusJson)),+ stdout: Stream.make(+ encoder.encode(typeof statusJson === "string" ? statusJson : statusJson()),+ ),stderr: Stream.empty,all: Stream.empty,getInputFd: () => Sink.drain,@@ -460,6 +462,66 @@ describe("DesktopServerExposure", () => {),);+ it.effect(+ "refreshes Tailscale status when addresses change and caches unchanged addresses",+ () => {+ const networkInterfaces: Record<+ string,+ readonly DesktopNetworkInterfaces.DesktopNetworkInterfaceInfo[] | undefined+ > = { ...lanNetworkInterfaces };+ let statusJson = "{}";+ let statusReads = 0;++ return withHarness(+ networkInterfaces,+ Effect.gen(function* () {+ const serverExposure = yield* DesktopServerExposure.DesktopServerExposure;+ yield* serverExposure.configureFromSettings({ port: 4173 });+ yield* serverExposure.setMode("network-accessible");++ const initial = yield* serverExposure.getAdvertisedEndpoints;+ assert.deepEqual(+ initial.map((endpoint) => endpoint.httpBaseUrl),+ ["http://127.0.0.1:4173/", "http://192.168.1.20:4173/"],+ );+ assert.equal(statusReads, 1);++ for (const [address, dnsName] of [+ ["100.90.1.2", "desktop.tail.ts.net"],+ ["100.90.1.3", "desktop.other-tail.ts.net"],+ ] as const) {+ networkInterfaces.vpn0 = [{ address, family: "IPv4", internal: false }];+ statusJson = JSON.stringify({+ Self: { DNSName: `${dnsName}.`, TailscaleIPs: [address] },+ });+ const previousReads = statusReads;++ const endpoints = yield* serverExposure.getAdvertisedEndpoints;+ assert.deepEqual(+ endpoints.map((endpoint) => endpoint.httpBaseUrl),+ [+ "http://127.0.0.1:4173/",+ "http://192.168.1.20:4173/",+ `http://${address}:4173/`,+ `https://${dnsName}/`,+ ],+ );+ assert.equal(statusReads, previousReads + 1);+ assert.deepEqual(yield* serverExposure.getAdvertisedEndpoints, endpoints);+ assert.equal(statusReads, previousReads + 1);+ }+ }),+ {},+ layerMockSpawner(+ () => statusJson,+ () => {+ statusReads += 1;+ },+ ),+ );+ },+ );+it.effect("does not spawn the tailscale CLI while server exposure is local-only", () =>withHarness(lanNetworkInterfaces,diff --git a/apps/desktop/src/backend/DesktopServerExposure.ts b/apps/desktop/src/backend/DesktopServerExposure.tsindex b116622576b..f9a69692a25 100644--- a/apps/desktop/src/backend/DesktopServerExposure.ts+++ b/apps/desktop/src/backend/DesktopServerExposure.ts@@ -10,6 +10,7 @@ import {type DesktopServerExposureState,} from "@t3tools/contracts";import { isTailscaleIpv4Address, readTailscaleStatus } from "@t3tools/tailscale";+import * as Cache from "effect/Cache";import * as Context from "effect/Context";import * as Duration from "effect/Duration";import * as Effect from "effect/Effect";@@ -416,6 +417,25 @@ function resolveRuntimeState(input: {};}+const tailscaleIpv4AddressKey = (+ networkInterfaces: DesktopNetworkInterfaces.NetworkInterfaces,+): string =>+ [+ ...new Set(+ Object.values(networkInterfaces)+ .flatMap((addresses) => addresses ?? [])+ .filter(+ (address) =>+ !address.internal &&+ address.family === "IPv4" &&+ isTailscaleIpv4Address(address.address),+ )+ .map((address) => address.address),+ ),+ ]+ .toSorted()+ .join(",");+const requiresBackendRelaunch = (previous: RuntimeState, next: RuntimeState): boolean =>previous.port !== next.port ||previous.bindHost !== next.bindHost ||@@ -434,16 +454,17 @@ export const make = Effect.gen(function* () {// with values it read before that change persisted.const changePermit = yield* Semaphore.make(1);- // Cache the `tailscale status` spawn for the TTL. On macOS, the Mac App- // Store Tailscale CLI lives inside Tailscale's sandbox container, so each- // spawn re-triggers the "Other apps" TCC prompt.- const cachedReadTailscaleStatus = yield* Effect.cachedWithTTL(- readTailscaleStatus.pipe(- Effect.orElseSucceed(() => null),- Effect.provideService(ChildProcessSpawner.ChildProcessSpawner, childProcessSpawner),- ),- TAILSCALE_STATUS_CACHE_TTL,- );+ // Key status by interface addresses so network changes bypass stale DNS and+ // identity. Cache unchanged addresses to avoid repeated macOS TCC prompts.+ const tailscaleStatusCache = yield* Cache.make({+ capacity: 1,+ timeToLive: TAILSCALE_STATUS_CACHE_TTL,+ lookup: (_addresses: string) =>+ readTailscaleStatus.pipe(+ Effect.orElseSucceed(() => null),+ Effect.provideService(ChildProcessSpawner.ChildProcessSpawner, childProcessSpawner),+ ),+ });const readNetworkInterfaces = networkInterfaces.read;@@ -564,7 +585,10 @@ export const make = Effect.gen(function* () {serveEnabled: state.tailscaleServeEnabled,servePort: state.tailscaleServePort,networkInterfaces: currentNetworkInterfaces,- readStatus: cachedReadTailscaleStatus,+ readStatus: Cache.get(+ tailscaleStatusCache,+ tailscaleIpv4AddressKey(currentNetworkInterfaces),+ ),}).pipe(Effect.provideService(ChildProcessSpawner.ChildProcessSpawner, childProcessSpawner),Effect.provideService(HttpClient.HttpClient, httpClient),