Skip to content

Commit 4def412

Browse files
only21milclaude
andcommitted
fix(desktop): LAN pairing links follow the current network address
A network-accessible desktop backend binds 0.0.0.0, but Connections and the advertised endpoints kept the LAN address read at startup or at the last mode change. After moving to another network, pairing links and QR codes pointed at an address the machine no longer had. Resolve the advertised LAN host from the current interfaces whenever the state or endpoints are read. The binding, mode and any explicit T3CODE_DESKTOP_LAN_HOST override are unchanged. Co-Authored-By: Claude Opus 5.5 <[email protected]>
1 parent d53873b commit 4def412

2 files changed

Lines changed: 64 additions & 2 deletions

File tree

‎apps/desktop/src/backend/DesktopServerExposure.test.ts‎

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -238,6 +238,34 @@ describe("DesktopServerExposure", () => {
238238
),
239239
);
240240

241+
it.effect("advertises the current LAN address after the network changes", () => {
242+
const networkInterfaces: Record<
243+
string,
244+
readonly DesktopNetworkInterfaces.DesktopNetworkInterfaceInfo[] | undefined
245+
> = { ...lanNetworkInterfaces };
246+
247+
return withHarness(
248+
networkInterfaces,
249+
Effect.gen(function* () {
250+
const serverExposure = yield* DesktopServerExposure.DesktopServerExposure;
251+
yield* serverExposure.configureFromSettings({ port: 4173 });
252+
yield* serverExposure.setMode("network-accessible");
253+
254+
networkInterfaces.en0 = [{ address: "10.0.0.5", family: "IPv4", internal: false }];
255+
256+
const state = yield* serverExposure.getState;
257+
assert.equal(state.advertisedHost, "10.0.0.5");
258+
assert.equal(state.endpointUrl, "http://10.0.0.5:4173");
259+
const endpoints = yield* serverExposure.getAdvertisedEndpoints;
260+
assert.deepEqual(
261+
endpoints.map((endpoint) => endpoint.httpBaseUrl),
262+
["http://127.0.0.1:4173/", "http://10.0.0.5:4173/"],
263+
);
264+
assert.equal((yield* serverExposure.backendConfig).bindHost, "0.0.0.0");
265+
}),
266+
);
267+
});
268+
241269
it.effect("persists tailscale serve preferences atomically and reports no-op updates", () =>
242270
withHarness(
243271
emptyNetworkInterfaces,

‎apps/desktop/src/backend/DesktopServerExposure.ts‎

Lines changed: 36 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -417,6 +417,30 @@ function resolveRuntimeState(input: {
417417
};
418418
}
419419

420+
// A network-accessible backend binds every interface, so a new Wi-Fi network
421+
// or DHCP lease changes only the address worth advertising, not the binding.
422+
const withCurrentAdvertisedHost = (
423+
state: RuntimeState,
424+
networkInterfaces: DesktopNetworkInterfaces.NetworkInterfaces,
425+
advertisedHostOverride: Option.Option<string>,
426+
): RuntimeState => {
427+
if (state.mode !== "network-accessible") {
428+
return state;
429+
}
430+
const override = Option.getOrUndefined(advertisedHostOverride);
431+
const exposure = resolveDesktopServerExposure({
432+
mode: state.mode,
433+
port: state.port,
434+
networkInterfaces,
435+
...(override ? { advertisedHostOverride: override } : {}),
436+
});
437+
return {
438+
...state,
439+
endpointUrl: Option.fromNullishOr(exposure.endpointUrl),
440+
advertisedHost: Option.fromNullishOr(exposure.advertisedHost),
441+
};
442+
};
443+
420444
const tailscaleIpv4AddressKey = (
421445
networkInterfaces: DesktopNetworkInterfaces.NetworkInterfaces,
422446
): string =>
@@ -468,7 +492,17 @@ export const make = Effect.gen(function* () {
468492

469493
const readNetworkInterfaces = networkInterfaces.read;
470494

471-
const getState = Ref.get(stateRef).pipe(Effect.map(toContractState));
495+
const readCurrentState = (currentNetworkInterfaces: DesktopNetworkInterfaces.NetworkInterfaces) =>
496+
Ref.get(stateRef).pipe(
497+
Effect.map((state) =>
498+
withCurrentAdvertisedHost(state, currentNetworkInterfaces, config.desktopLanHostOverride),
499+
),
500+
);
501+
502+
const getState = readNetworkInterfaces.pipe(
503+
Effect.flatMap(readCurrentState),
504+
Effect.map(toContractState),
505+
);
472506
const backendConfig = Ref.get(stateRef).pipe(Effect.map(toBackendConfig));
473507

474508
const configureFromSettings = Effect.fn("desktop.serverExposure.configureFromSettings")(
@@ -565,8 +599,8 @@ export const make = Effect.gen(function* () {
565599
);
566600

567601
const getAdvertisedEndpoints = Effect.gen(function* () {
568-
const state = yield* Ref.get(stateRef);
569602
const currentNetworkInterfaces = yield* readNetworkInterfaces;
603+
const state = yield* readCurrentState(currentNetworkInterfaces);
570604
const coreEndpoints = resolveDesktopCoreAdvertisedEndpoints({
571605
port: state.port,
572606
exposure: toResolvedExposure(state),

0 commit comments

Comments
 (0)