Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
39 changes: 18 additions & 21 deletions apps/server/src/server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -233,6 +233,7 @@ import * as RelayClient from "@t3tools/shared/relayClient";
import {
disableTailscaleServe,
ensureTailscaleServe,
ensureTailscaleServeAtStartup,
readTailscaleServeRootHolder,
readTailscaleServeRoutesTo,
readTailscaleStatus,
Expand Down Expand Up @@ -1014,31 +1015,26 @@ const layerMakeServer = Layer.unwrap(
localHost: localHost.replace(/^\[|\]$/gu, ""),
servePort: config.tailscaleServePort,
};
const holder = yield* tailscaleServeRootHolder({
host: route.localHost,
port: localPort,
}).pipe(Effect.orElseSucceed(() => "unreadable" as const));
if (holder === "other" || holder === "unreadable") {
yield* Effect.logWarning(
holder === "other"
? "Tailscale Serve left off; its port already serves another server or app"
: "Tailscale Serve left off; its current routes could not be read",
{ localPort, servePort: config.tailscaleServePort },
);
return route;
}
return yield* ensureTailscaleServe({
return yield* ensureTailscaleServeAtStartup({
localPort,
servePort: config.tailscaleServePort,
localHost,
localHost: route.localHost,
serveHost: localHost,
}).pipe(
Effect.as(route),
Effect.tap(() =>
Effect.logInfo("Tailscale Serve configured", {
localPort,
servePort: config.tailscaleServePort,
}),
Effect.flatMap((outcome) =>
outcome === "occupied"
? Effect.logWarning(
"Tailscale Serve left off; its port already serves another server or app",
{ localPort, servePort: config.tailscaleServePort },
)
: outcome === "unknown"
? Effect.void
: Effect.logInfo("Tailscale Serve configured", {
localPort,
servePort: config.tailscaleServePort,
}),
),
Effect.as(route),
Effect.catch((cause) =>
Effect.logWarning(
cause._tag === "TailscaleCommandExitError" &&
Expand Down Expand Up @@ -1099,6 +1095,7 @@ const layerMakeServer = Layer.unwrap(
),
)
: Effect.void,
{ interruptible: true },
).pipe(
// Startup is not the only time the route can go stale, so whoever finds it on a dead
// port takes it back.
Expand Down
105 changes: 102 additions & 3 deletions packages/tailscale/src/tailscale.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,11 @@ import { HttpClient, HttpClientResponse } from "effect/http";
import * as TestClock from "effect/testing/TestClock";
import { ChildProcessSpawner } from "effect/process";
import * as HostProcess from "@t3tools/shared/HostProcess";
import * as NetService from "@t3tools/shared/Net";

import {
ensureTailscaleServe,
ensureTailscaleServeAtStartup,
parseTailscaleExitNode,
parseTailscaleServeRootHolders,
parseTailscaleServeRoutesTo,
Expand Down Expand Up @@ -59,10 +61,13 @@ function assertCarriesNoSecret(error: object, secret: string): void {
}
const tailscaleStatusJson = `{"Self":{"DNSName":"desktop.tail.ts.net.","TailscaleIPs":["100.100.100.100","fd7a:115c:a1e0::1","192.168.1.20"]}}`;

function mockHandle(result: { stdout?: string; stderr?: string; code?: number }) {
function mockHandle(result: { stdout?: string; stderr?: string; code?: number; hang?: boolean }) {
return ChildProcessSpawner.makeHandle({
pid: ChildProcessSpawner.ProcessId(1),
exitCode: Effect.succeed(ChildProcessSpawner.ExitCode(result.code ?? 0)),
exitCode:
result.hang === true
? Effect.never
: Effect.succeed(ChildProcessSpawner.ExitCode(result.code ?? 0)),
isRunning: Effect.succeed(false),
kill: () => Effect.void,
unref: Effect.succeed(Effect.void),
Expand All @@ -88,7 +93,7 @@ function layerMockSpawner(
handler: (
command: string,
args: ReadonlyArray<string>,
) => { stdout?: string; stderr?: string; code?: number },
) => { stdout?: string; stderr?: string; code?: number; hang?: boolean },
) {
return layerSpawner(
ChildProcessSpawner.make((command) => {
Expand Down Expand Up @@ -495,4 +500,98 @@ describe("tailscale", () => {
assert.deepEqual(killGraces, [500, 500, 500, 500]);
});
});

describe("ensureTailscaleServeAtStartup", () => {
const startupInput = {
localPort: 45_678,
servePort: 443,
localHost: "127.0.0.1",
serveHost: "127.0.0.1",
};

function serveHarness(
serveStatusResults: ReadonlyArray<{ code?: number; hang?: boolean; stdout?: string }>,
) {
const state = { serveStatusReads: 0, serveEnabled: false };
const layer = Layer.merge(
layerMockSpawner((_command, args) => {
if (args[0] === "status") {
return { stdout: tailscaleStatusJson };
}
if (args[0] === "serve" && args[1] === "status") {
const result = serveStatusResults[
Math.min(state.serveStatusReads, serveStatusResults.length - 1)
] ?? { code: 1 };
state.serveStatusReads += 1;
return result.hang === true || (result.code ?? 0) !== 0
? result
: { stdout: result.stdout ?? "null" };
}
if (args[0] === "serve" && args[1] === "--bg") {
state.serveEnabled = true;
}
return { stdout: "" };
}),
NetService.layer,
);
return { state, layer };
}

it.effect("enables Serve after the first route read hangs past its timeout", () => {
const { state, layer } = serveHarness([{ hang: true }, {}]);
return Effect.gen(function* () {
const fiber = yield* ensureTailscaleServeAtStartup(startupInput).pipe(
Effect.provide(layer),
Effect.forkChild,
);
yield* TestClock.adjust(Duration.seconds(10));
const outcome = yield* Fiber.join(fiber);
assert.strictEqual(outcome, "configured");
assert.strictEqual(state.serveStatusReads, 2);
assert.isTrue(state.serveEnabled);
});
});

it.effect("enables Serve after several failed route reads", () => {
const { state, layer } = serveHarness([{ code: 1 }, { code: 1 }, { code: 1 }, {}]);
return Effect.gen(function* () {
const fiber = yield* ensureTailscaleServeAtStartup(startupInput).pipe(
Effect.provide(layer),
Effect.forkChild,
);
yield* TestClock.adjust(Duration.seconds(10));
const outcome = yield* Fiber.join(fiber);
assert.strictEqual(outcome, "configured");
assert.strictEqual(state.serveStatusReads, 4);
assert.isTrue(state.serveEnabled);
});
});

it.effect("enables Serve anyway when every route read fails", () => {
const { state, layer } = serveHarness([{ hang: true }]);
return Effect.gen(function* () {
const fiber = yield* ensureTailscaleServeAtStartup(startupInput).pipe(
Effect.provide(layer),
Effect.forkChild,
);
yield* TestClock.adjust(Duration.seconds(120));
const outcome = yield* Fiber.join(fiber);
assert.strictEqual(outcome, "configured");
assert.isAbove(state.serveStatusReads, 2);
assert.isTrue(state.serveEnabled);
});
});

it.effect("leaves Serve off when the route read succeeds with output it cannot parse", () => {
const { state, layer } = serveHarness([{ stdout: "not json" }]);
return Effect.gen(function* () {
const outcome = yield* ensureTailscaleServeAtStartup(startupInput).pipe(
Effect.provide(layer),
);
assert.strictEqual(outcome, "unknown");
assert.strictEqual(state.serveStatusReads, 1);
assert.isFalse(state.serveEnabled);
});
});
});
});
69 changes: 64 additions & 5 deletions packages/tailscale/src/tailscale.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import * as Duration from "effect/Duration";
import * as Effect from "effect/Effect";
import * as Option from "effect/Option";
import * as Schema from "effect/Schema";
import * as Schedule from "effect/Schedule";
import * as Stream from "effect/Stream";
import { HttpClient, HttpClientRequest, HttpClientResponse } from "effect/http";
import { ChildProcess, ChildProcessSpawner } from "effect/process";
Expand All @@ -27,6 +28,8 @@ const TAILSCALE_STATUS_TIMEOUT = Duration.millis(3_500);
const TAILSCALE_KILL_GRACE = Duration.millis(500);
const TAILSCALE_SERVE_TIMEOUT = Duration.seconds(10);
const TAILSCALE_PROBE_TIMEOUT = Duration.millis(2_500);
const TAILSCALE_STARTUP_READ_TIMEOUT = Duration.seconds(8);
const TAILSCALE_STARTUP_READ_RETRY_WINDOW = Duration.seconds(30);

const tailscaleCommandForPlatform = (platform: NodeJS.Platform): "tailscale" | "tailscale.exe" =>
platform === "win32" ? "tailscale.exe" : "tailscale";
Expand Down Expand Up @@ -244,6 +247,7 @@ export const parseTailscaleStatus = (
const readTailscaleOutput = Effect.fn("tailscale.readTailscaleOutput")(function* (
subcommand: "status" | "serve" | "debug",
args: readonly string[],
timeout: Duration.Input = TAILSCALE_STATUS_TIMEOUT,
) {
const spawner = yield* ChildProcessSpawner.ChildProcessSpawner;
const hostPlatform = yield* HostProcess.Platform;
Expand Down Expand Up @@ -290,13 +294,13 @@ const readTailscaleOutput = Effect.fn("tailscale.readTailscaleOutput")(function*
return stdout;
}).pipe(
Effect.scoped,
Effect.timeout(TAILSCALE_STATUS_TIMEOUT),
Effect.timeout(timeout),
Effect.catchTags({
TimeoutError: (cause) =>
Effect.fail(
new TailscaleCommandTimeoutError({
...commandContext,
timeoutMs: Duration.toMillis(TAILSCALE_STATUS_TIMEOUT),
timeoutMs: Duration.toMillis(Duration.fromInputUnsafe(timeout)),
cause,
}),
),
Expand Down Expand Up @@ -730,8 +734,9 @@ export const parseTailscaleServeRootHolders = (
const readTailscaleServeRootHolders = (
servePort: number,
target: { readonly host: string; readonly port: number },
timeout?: Duration.Input,
) =>
readTailscaleOutput("serve", ["serve", "status", "--json"]).pipe(
readTailscaleOutput("serve", ["serve", "status", "--json"], timeout).pipe(
Effect.flatMap((status) => {
const holders = parseTailscaleServeRootHolders(status, servePort, target);
return holders === undefined
Expand All @@ -745,9 +750,13 @@ const readTailscaleServeRootHolders = (
* unclean exit leaves one), or another live server or app, whose route must be left alone.
*/
export const readTailscaleServeRootHolder = Effect.fn("tailscale.readTailscaleServeRootHolder")(
function* (servePort: number, target: { readonly host: string; readonly port: number }) {
function* (
servePort: number,
target: { readonly host: string; readonly port: number },
readTimeout?: Duration.Input,
) {
const net = yield* NetService.NetService;
const holders = yield* readTailscaleServeRootHolders(servePort, target);
const holders = yield* readTailscaleServeRootHolders(servePort, target, readTimeout);
if (holders.length === 0) return "none" as const;
if (holders.some((holder) => holder.ours)) return "ours" as const;
const live = yield* Effect.forEach(holders, (holder) =>
Expand All @@ -759,6 +768,56 @@ export const readTailscaleServeRootHolder = Effect.fn("tailscale.readTailscaleSe
},
);

const isSlowTailscaledError = (error: { readonly _tag: string }) =>
error._tag === "TailscaleCommandTimeoutError" || error._tag === "TailscaleCommandExitError";

export const ensureTailscaleServeAtStartup = Effect.fn("tailscale.ensureTailscaleServeAtStartup")(
function* (input: {
readonly localPort: number;
readonly servePort: number;
readonly localHost: string;
readonly serveHost: string;
}) {
const holder = yield* readTailscaleServeRootHolder(
input.servePort,
{ host: input.localHost, port: input.localPort },
TAILSCALE_STARTUP_READ_TIMEOUT,
).pipe(
Effect.retry({
while: isSlowTailscaledError,
schedule: Schedule.exponential(Duration.seconds(1)).pipe(
Schedule.upTo({ duration: TAILSCALE_STARTUP_READ_RETRY_WINDOW }),
),
}),
Effect.catch((cause) =>
isSlowTailscaledError(cause)
? Effect.logWarning("Tailscale Serve routes could not be read; applying Serve anyway", {
cause,
localPort: input.localPort,
servePort: input.servePort,
}).pipe(Effect.as("unreadable" as const))
: Effect.logWarning("Tailscale Serve routes could not be checked; leaving Serve off", {
cause,
localPort: input.localPort,
servePort: input.servePort,
}).pipe(Effect.as("unknown" as const)),
),
);
if (holder === "other") {
return "occupied" as const;
}
if (holder === "unknown") {
return "unknown" as const;
}
yield* ensureTailscaleServe({
localPort: input.localPort,
servePort: input.servePort,
localHost: input.serveHost,
});
return "configured" as const;
},
);

export const probeTailscaleHttpsEndpoint = Effect.fn("tailscale.probeTailscaleHttpsEndpoint")(
function* (input: {
readonly baseUrl: string;
Expand Down
Loading