Skip to content

Commit e83021c

Browse files
only21milclaude
andauthored
fix(desktop): LAN pairing links follow the current network address (#24474)
Co-authored-by: Claude Opus 5.5 <[email protected]>
1 parent 771afda commit e83021c

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
@@ -409,6 +409,30 @@ function resolveRuntimeState(input: {
409409
};
410410
}
411411

412+
// A network-accessible backend binds every interface, so a new Wi-Fi network
413+
// or DHCP lease changes only the address worth advertising, not the binding.
414+
const withCurrentAdvertisedHost = (
415+
state: RuntimeState,
416+
networkInterfaces: DesktopNetworkInterfaces.NetworkInterfaces,
417+
advertisedHostOverride: Option.Option<string>,
418+
): RuntimeState => {
419+
if (state.mode !== "network-accessible") {
420+
return state;
421+
}
422+
const override = Option.getOrUndefined(advertisedHostOverride);
423+
const exposure = resolveDesktopServerExposure({
424+
mode: state.mode,
425+
port: state.port,
426+
networkInterfaces,
427+
...(override ? { advertisedHostOverride: override } : {}),
428+
});
429+
return {
430+
...state,
431+
endpointUrl: Option.fromNullishOr(exposure.endpointUrl),
432+
advertisedHost: Option.fromNullishOr(exposure.advertisedHost),
433+
};
434+
};
435+
412436
const tailscaleIpv4AddressKey = (
413437
networkInterfaces: DesktopNetworkInterfaces.NetworkInterfaces,
414438
): string =>
@@ -458,7 +482,17 @@ export const make = Effect.gen(function* () {
458482

459483
const readNetworkInterfaces = networkInterfaces.read;
460484

461-
const getState = Ref.get(stateRef).pipe(Effect.map(toContractState));
485+
const readCurrentState = (currentNetworkInterfaces: DesktopNetworkInterfaces.NetworkInterfaces) =>
486+
Ref.get(stateRef).pipe(
487+
Effect.map((state) =>
488+
withCurrentAdvertisedHost(state, currentNetworkInterfaces, config.desktopLanHostOverride),
489+
),
490+
);
491+
492+
const getState = readNetworkInterfaces.pipe(
493+
Effect.flatMap(readCurrentState),
494+
Effect.map(toContractState),
495+
);
462496
const backendConfig = Ref.get(stateRef).pipe(Effect.map(toBackendConfig));
463497

464498
const configureFromSettings = Effect.fn("desktop.serverExposure.configureFromSettings")(
@@ -555,8 +589,8 @@ export const make = Effect.gen(function* () {
555589
);
556590

557591
const getAdvertisedEndpoints = Effect.gen(function* () {
558-
const state = yield* Ref.get(stateRef);
559592
const currentNetworkInterfaces = yield* readNetworkInterfaces;
593+
const state = yield* readCurrentState(currentNetworkInterfaces);
560594
const coreEndpoints = resolveDesktopCoreAdvertisedEndpoints({
561595
port: state.port,
562596
exposure: toResolvedExposure(state),

0 commit comments

Comments
 (0)