Skip to content

Commit 87a7429

Browse files
committed
fix(tailscale): preserve foreground Serve handlers
1 parent f9071ce commit 87a7429

2 files changed

Lines changed: 134 additions & 7 deletions

File tree

‎packages/tailscale/src/tailscale.test.ts‎

Lines changed: 113 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -68,13 +68,14 @@ function assertCarriesNoSecret(error: object, secret: string): void {
6868
const tailscaleStatusJson = `{"Self":{"DNSName":"desktop.tail.ts.net.","TailscaleIPs":["100.100.100.100","fd7a:115c:a1e0::1","192.168.1.20"]}}`;
6969
const tailscaleStatusWithSingleIpJson = `{"Self":{"DNSName":"desktop.tail.ts.net.","TailscaleIPs":["100.90.1.2"]}}`;
7070
const emptyServeStatusJson = `{"TCP":{},"Web":{}}`;
71+
const serveConfig = (servePort: number, proxy: string) => ({
72+
TCP: { [servePort]: { HTTPS: true } },
73+
Web: {
74+
[`desktop.tail.ts.net:${String(servePort)}`]: { Handlers: { "/": { Proxy: proxy } } },
75+
},
76+
});
7177
const serveStatusJson = (servePort: number, proxy: string) =>
72-
JSON.stringify({
73-
TCP: { [servePort]: { HTTPS: true } },
74-
Web: {
75-
[`desktop.tail.ts.net:${String(servePort)}`]: { Handlers: { "/": { Proxy: proxy } } },
76-
},
77-
});
78+
JSON.stringify(serveConfig(servePort, proxy));
7879

7980
function mockHandle(result: { stdout?: string; stderr?: string; code?: number }) {
8081
return ChildProcessSpawner.makeHandle({
@@ -385,6 +386,112 @@ describe("tailscale", () => {
385386
});
386387
});
387388

389+
it.effect.each([
390+
{
391+
name: "foreground-only",
392+
background: {},
393+
foreground: serveConfig(8443, "http://127.0.0.1:13773"),
394+
},
395+
{
396+
name: "foreground overriding an exact background",
397+
background: serveConfig(8443, "http://127.0.0.1:13773"),
398+
foreground: serveConfig(8443, "http://127.0.0.1:13773"),
399+
},
400+
{
401+
name: "foreground TCP-only",
402+
background: {},
403+
foreground: { TCP: { 8443: { TCPForward: "127.0.0.1:39831" } } },
404+
},
405+
{
406+
name: "foreground web-only",
407+
background: {},
408+
foreground: { Web: serveConfig(8443, "http://127.0.0.1:13773").Web },
409+
},
410+
{
411+
name: "foreground Funnel-only",
412+
background: {},
413+
foreground: { AllowFunnel: { "desktop.tail.ts.net:8443": true } },
414+
},
415+
])("refuses to reuse, replace, or disable a $name handler", ({ background, foreground }) => {
416+
const commands: ReadonlyArray<string>[] = [];
417+
const layer = mockSpawnerLayer((_command, args) => {
418+
commands.push(args);
419+
return {
420+
stdout: JSON.stringify({
421+
...background,
422+
Foreground: { "session-id": foreground },
423+
}),
424+
};
425+
});
426+
427+
return Effect.gen(function* () {
428+
const ensureError = yield* ensureTailscaleServe({
429+
localPort: 13773,
430+
servePort: 8443,
431+
replaceVerifiedHandler: true,
432+
}).pipe(Effect.flip, Effect.provide(layer));
433+
assert.instanceOf(ensureError, TailscaleServePortOccupiedError);
434+
435+
const disableError = yield* disableTailscaleServe({
436+
localPort: 13773,
437+
servePort: 8443,
438+
}).pipe(Effect.flip, Effect.provide(layer));
439+
assert.instanceOf(disableError, TailscaleServePortOccupiedError);
440+
assert.deepEqual(commands, [
441+
["serve", "status", "--json"],
442+
["serve", "status", "--json"],
443+
]);
444+
});
445+
});
446+
447+
it.effect("configures an unused port while another port has a foreground handler", () => {
448+
const commands: ReadonlyArray<string>[] = [];
449+
const layer = mockSpawnerLayer((_command, args) => {
450+
commands.push(args);
451+
return {
452+
stdout: JSON.stringify({
453+
Foreground: { "session-id": serveConfig(443, "http://127.0.0.1:39831") },
454+
}),
455+
};
456+
});
457+
458+
return Effect.gen(function* () {
459+
yield* ensureTailscaleServe({ localPort: 13773, servePort: 8443 }).pipe(
460+
Effect.provide(layer),
461+
);
462+
assert.deepEqual(commands, [
463+
["serve", "status", "--json"],
464+
["serve", "--bg", "--https=8443", "http://127.0.0.1:13773"],
465+
]);
466+
});
467+
});
468+
469+
it.effect(
470+
"disables an exact background handler while another port has a foreground handler",
471+
() => {
472+
const commands: ReadonlyArray<string>[] = [];
473+
const layer = mockSpawnerLayer((_command, args) => {
474+
commands.push(args);
475+
return {
476+
stdout: JSON.stringify({
477+
...serveConfig(8443, "http://127.0.0.1:13773"),
478+
Foreground: { "session-id": serveConfig(443, "http://127.0.0.1:39831") },
479+
}),
480+
};
481+
});
482+
483+
return Effect.gen(function* () {
484+
yield* disableTailscaleServe({ localPort: 13773, servePort: 8443 }).pipe(
485+
Effect.provide(layer),
486+
);
487+
assert.deepEqual(commands, [
488+
["serve", "status", "--json"],
489+
["serve", "--https=8443", "off"],
490+
]);
491+
});
492+
},
493+
);
494+
388495
it.effect("refuses to replace a configured port even when its backend differs", () => {
389496
const layer = mockSpawnerLayer(() => ({
390497
stdout: serveStatusJson(8443, "http://127.0.0.1:39831"),

‎packages/tailscale/src/tailscale.ts‎

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -149,7 +149,7 @@ const TailscaleStatusJson = Schema.Struct({
149149
Self: Schema.optional(TailscaleStatusSelf),
150150
});
151151

152-
const TailscaleServeStatusJson = Schema.Struct({
152+
const TailscaleServeConfigJson = Schema.Struct({
153153
TCP: Schema.optional(
154154
Schema.Record(
155155
Schema.String,
@@ -169,6 +169,11 @@ const TailscaleServeStatusJson = Schema.Struct({
169169
AllowFunnel: Schema.optional(Schema.Record(Schema.String, Schema.Boolean)),
170170
});
171171

172+
const TailscaleServeStatusJson = Schema.Struct({
173+
...TailscaleServeConfigJson.fields,
174+
Foreground: Schema.optional(Schema.Record(Schema.String, TailscaleServeConfigJson)),
175+
});
176+
172177
type TailscaleServeStatusJson = typeof TailscaleServeStatusJson.Type;
173178

174179
export type TailscaleStatusJson = typeof TailscaleStatusJson.Type;
@@ -398,6 +403,21 @@ function servePortState(
398403
status: TailscaleServeStatusJson,
399404
input: { readonly servePort: number; readonly proxy: string },
400405
): TailscaleServePortState {
406+
// Foreground handlers belong to their CLI session and override background mappings.
407+
if (
408+
Object.values(status.Foreground ?? {}).some(
409+
(config) =>
410+
Object.keys(config.TCP ?? {}).some((port) => Number(port) === input.servePort) ||
411+
Object.keys(config.Web ?? {}).some(
412+
(authority) => authorityPort(authority) === input.servePort,
413+
) ||
414+
Object.entries(config.AllowFunnel ?? {}).some(
415+
([authority, enabled]) => authorityPort(authority) === input.servePort && enabled,
416+
),
417+
)
418+
) {
419+
return "occupied";
420+
}
401421
const tcpEntries = Object.entries(status.TCP ?? {}).filter(
402422
([port]) => Number(port) === input.servePort,
403423
);

0 commit comments

Comments
 (0)