@devries-cameron20 / t3code Mirror · read-only
Fork 0

The best way to control Claude Code, Codex and more from any computer or phone

Mirrored from https://github.com/Camo67/t3code.git

Synced Nov 21, 03:37 UTC

main
ts 2,280 lines 82.5 KB
Raw
1 import {
2 DESKTOP_UPDATE_IN_PROGRESS_MARKER_FILE,
3 DESKTOP_UPDATE_IN_PROGRESS_TTL_MS,
4 } from "@t3tools/contracts";
5 import type {
6 DesktopSshEnvironmentBootstrap,
7 DesktopSshEnvironmentTarget,
8 } from "@t3tools/contracts";
9 import { waitForHttpReady as waitForHttpReadyShared } from "@t3tools/shared/httpReadiness";
10 import { cliReleaseDownloadBaseUrl } from "@t3tools/shared/cliRelease";
11 import * as KeyedLock from "@t3tools/shared/KeyedLock";
12 import * as NetService from "@t3tools/shared/Net";
13 import { extractJsonObject, fromLenientJson } from "@t3tools/shared/schemaJson";
14 import { satisfiesSemverRange } from "@t3tools/shared/semver";
15 import * as Context from "effect/Context";
16 import * as Digest from "effect/crypto/Digest";
17 import * as Duration from "effect/Duration";
18 import * as Effect from "effect/Effect";
19 import * as Exit from "effect/Exit";
20 import * as Fiber from "effect/Fiber";
21 import * as FileSystem from "effect/FileSystem";
22 import * as Layer from "effect/Layer";
23 import * as Path from "effect/Path";
24 import * as Schedule from "effect/Schedule";
25 import * as Schema from "effect/Schema";
26 import * as Scope from "effect/Scope";
27 import * as Stream from "effect/Stream";
28 import { HttpClient } from "effect/http";
29 import { ChildProcess, ChildProcessSpawner } from "effect/process";
30
31 import * as SshAuth from "./auth.ts";
32 import {
33 baseSshArgs,
34 buildSshHostSpecEffect,
35 controlMasterArgs,
36 findLiveControlPath,
37 getLastNonEmptyOutputLine,
38 remoteStateKey,
39 resolveSshCommand,
40 resolveSshTarget,
41 runSshCommand,
42 shellSingleQuote,
43 sshTargetLogFields,
44 targetConnectionKey,
45 } from "./command.ts";
46 import {
47 SshCommandError,
48 SshHttpBridgeError,
49 SshInvalidTargetError,
50 SshLaunchError,
51 SshPairingError,
52 SshPasswordPromptError,
53 SshReadinessError,
54 } from "./errors.ts";
55
56 const DEFAULT_REMOTE_PORT = 3773;
57 const REMOTE_PORT_SCAN_WINDOW = 200;
58 const SSH_READY_TIMEOUT_MS = 20_000;
59 const SSH_READY_PROBE_TIMEOUT_MS = 1_000;
60 // Probes through the forward pay several link round trips, about 4s at a 1s RTT.
61 const SSH_TUNNEL_READY_PROBE_TIMEOUT_MS = 5_000;
62 const TUNNEL_SHUTDOWN_TIMEOUT_MS = 2_000;
63 const TUNNEL_STDERR_TAIL_CHARS = 16_384;
64 const REMOTE_READY_TIMEOUT_MS = 60_000;
65 const REMOTE_LAUNCH_BASE_TIMEOUT_MS = 90_000;
66 // Matches the node-script launch command's ssh run without the launch lock wait.
67 const SSH_TUNNEL_CONNECT_TIMEOUT_MS = REMOTE_LAUNCH_BASE_TIMEOUT_MS;
68 const SSH_TUNNEL_FORWARD_PROBE_INTERVAL_MS = 100;
69 // Cold archive launches download and extract ~70 MB and may wait on an install lock.
70 // Lock waits must outlast both downloads and extraction; SSH must outlast install or wait plus readiness.
71 // Leave slack for verification and extraction, which have no timeout.
72 const REMOTE_ARCHIVE_CHECKSUMS_SECONDS = 30;
73 const REMOTE_ARCHIVE_DOWNLOAD_SECONDS = 240;
74 const REMOTE_ARCHIVE_LOCK_WAIT_SECONDS = 360;
75 const REMOTE_ARCHIVE_LAUNCH_TIMEOUT_MS = 900_000;
76 const REMOTE_REUSE_READY_TIMEOUT_MS = 2_000;
77 const REMOTE_PID_EXIT_WAIT_MS = 2_000;
78 // The server answers HTTP before it writes server-runtime.json, which is how
79 // a launch for another alias finds it.
80 const REMOTE_RUNTIME_RECORD_WAIT_SECONDS = 10;
81 // The desktop update wait runs under the home lock. Its last poll and the
82 // adoption after it each add a reuse probe, and the poll adds one sleep.
83 const REMOTE_UPDATE_WAIT_MS =
84 DESKTOP_UPDATE_IN_PROGRESS_TTL_MS +
85 2 * (REMOTE_REUSE_READY_TIMEOUT_MS + SSH_READY_PROBE_TIMEOUT_MS) +
86 1_000;
87 // A launch waits this long for a server that holds the home to answer; its
88 // last poll adds a reuse probe.
89 const REMOTE_OWNER_WAIT_MS =
90 REMOTE_READY_TIMEOUT_MS + REMOTE_REUSE_READY_TIMEOUT_MS + SSH_READY_PROBE_TIMEOUT_MS;
91 // A launch holds the home lock through up to two reuse probes, readiness, two
92 // server exit waits, the runtime record wait, the update wait, and the owner
93 // wait; each probe may overrun its deadline by one request.
94 const REMOTE_LAUNCH_LOCK_WAIT_SECONDS = Math.ceil(
95 (REMOTE_READY_TIMEOUT_MS +
96 2 * REMOTE_REUSE_READY_TIMEOUT_MS +
97 2 * REMOTE_PID_EXIT_WAIT_MS +
98 REMOTE_RUNTIME_RECORD_WAIT_SECONDS * 1000 +
99 4 * SSH_READY_PROBE_TIMEOUT_MS +
100 REMOTE_UPDATE_WAIT_MS +
101 REMOTE_OWNER_WAIT_MS) /
102 1000,
103 );
104 const REMOTE_LAUNCH_TIMEOUT_MS =
105 REMOTE_LAUNCH_LOCK_WAIT_SECONDS * 1000 +
106 REMOTE_UPDATE_WAIT_MS +
107 REMOTE_OWNER_WAIT_MS +
108 REMOTE_LAUNCH_BASE_TIMEOUT_MS;
109
110 export interface RemoteT3RunnerOptions {
111 readonly nodeScriptPath?: string | null;
112 readonly nodeEngineRange?: string | null;
113 readonly archiveVersion?: string | null;
114 readonly releaseBaseUrl?: string | null;
115 }
116
117 export interface SshEnvironmentManagerOptions {
118 readonly resolveCliRunner?: Effect.Effect<RemoteT3RunnerOptions>;
119 // Identifies this install to the remote across edits of a connection; null when unknown.
120 readonly resolveLauncherId?: Effect.Effect<string | null>;
121 }
122
123 interface SshTunnelEntry {
124 readonly key: string;
125 readonly target: DesktopSshEnvironmentTarget;
126 readonly remotePort: number;
127 readonly remoteServerKind: "external" | "managed" | null;
128 readonly localPort: number;
129 readonly httpBaseUrl: string;
130 readonly wsBaseUrl: string;
131 readonly process: ChildProcessSpawner.ChildProcessHandle;
132 readonly scope: Scope.Closeable;
133 }
134
135 type SshEnvironmentEffectContext =
136 | ChildProcessSpawner.ChildProcessSpawner
137 | Digest.Digest
138 | FileSystem.FileSystem
139 | Path.Path
140 | HttpClient.HttpClient
141 | NetService.NetService
142 | SshAuth.SshPasswordPrompt;
143
144 type SshEnvironmentEffectError =
145 | SshCommandError
146 | SshInvalidTargetError
147 | SshLaunchError
148 | SshPairingError
149 | SshReadinessError
150 | SshPasswordPromptError
151 | NetService.NetError;
152
153 function isNodeScriptRunner(runner: RemoteT3RunnerOptions | undefined): boolean {
154 return Boolean(runner?.nodeScriptPath?.trim());
155 }
156
157 function sshRunnerLogFields(runner: RemoteT3RunnerOptions | undefined) {
158 if (runner?.nodeScriptPath?.trim()) {
159 return { runner: "node-script", nodeScriptPath: runner.nodeScriptPath.trim() };
160 }
161 if (runner?.archiveVersion?.trim()) {
162 return { runner: "archive", archiveVersion: runner.archiveVersion.trim() };
163 }
164 return { runner: "archive" };
165 }
166
167 interface SshAuthOperationInput<T> {
168 readonly key: string;
169 readonly target: DesktopSshEnvironmentTarget;
170 readonly operation: (
171 authOptions: SshAuth.SshAuthOptions,
172 ) => Effect.Effect<T, SshEnvironmentEffectError, SshEnvironmentEffectContext>;
173 }
174
175 interface SshAuthAttemptInput<T> extends SshAuthOperationInput<T> {
176 readonly promptCount: number;
177 readonly authSecret: string | null;
178 }
179
180 export interface SshEnvironmentManagerShape {
181 readonly ensureEnvironment: (
182 target: DesktopSshEnvironmentTarget,
183 options?: { readonly issuePairingToken?: boolean },
184 ) => Effect.Effect<
185 DesktopSshEnvironmentBootstrap,
186 SshEnvironmentEffectError,
187 SshEnvironmentEffectContext
188 >;
189 readonly disconnectEnvironment: (
190 target: DesktopSshEnvironmentTarget,
191 ) => Effect.Effect<void, SshEnvironmentEffectError, SshEnvironmentEffectContext>;
192 /** Closes every tunnel, as the manager's scope finalizer does. */
193 readonly closeAllTunnels: Effect.Effect<void>;
194 }
195
196 const RemoteLaunchResult = Schema.Struct({
197 remotePort: Schema.Number,
198 serverKind: Schema.optional(Schema.Literals(["external", "managed"])),
199 });
200
201 const RemotePairingResult = Schema.Struct({
202 credential: Schema.String,
203 });
204
205 const decodeRemoteLaunchResult = Schema.decodeEffect(fromLenientJson(RemoteLaunchResult));
206 const decodeRemotePairingResult = Schema.decodeEffect(fromLenientJson(RemotePairingResult));
207
208 const decodeRemoteJsonOutput = <A, E>(
209 stdout: string,
210 decode: (input: string) => Effect.Effect<A, E>,
211 ): Effect.Effect<A, E> =>
212 decode(stdout).pipe(
213 Effect.catch((error) =>
214 Effect.gen(function* () {
215 const jsonObject = extractJsonObject(stdout);
216 if (jsonObject === stdout.trim()) {
217 return yield* Effect.fail(error);
218 }
219 const exit = yield* Effect.exit(decode(jsonObject));
220 if (Exit.isSuccess(exit)) {
221 return exit.value;
222 }
223 return yield* Effect.fail(error);
224 }),
225 ),
226 );
227
228 const decodeRemoteLaunchOutput = (stdout: string) =>
229 decodeRemoteJsonOutput(stdout, decodeRemoteLaunchResult);
230
231 const decodeRemotePairingOutput = (stdout: string) =>
232 decodeRemoteJsonOutput(stdout, decodeRemotePairingResult);
233
234 const remoteNodeEngineCheckMain = function remoteNodeEngineCheckMain() {
235 const range = process.argv[2] || "";
236 const rawVersion =
237 process.versions && process.versions.node ? process.versions.node : process.version;
238
239 if (!satisfiesSemverRange(rawVersion, range)) {
240 process.stderr.write(
241 "Remote node " + rawVersion + " does not satisfy required range " + range + ".\n",
242 );
243 process.exit(1);
244 }
245 };
246
247 function buildRemoteNodeEngineCheckScript(): string {
248 return `${satisfiesSemverRange.toString()}
249 (${remoteNodeEngineCheckMain.toString()})();`;
250 }
251
252 function normalizeSshErrorMessage(stderr: string, fallbackMessage: string): string {
253 const cleaned = stderr.trim();
254 return cleaned.length > 0 ? cleaned : fallbackMessage;
255 }
256
257 function stripTrailingNewlines(value: string): string {
258 return value.replace(/\n+$/u, "");
259 }
260
261 function applyScriptPlaceholders(
262 template: string,
263 replacements: Readonly<Record<string, string>>,
264 ): string {
265 let result = template;
266 for (const [token, value] of Object.entries(replacements)) {
267 // A replacer function keeps `$$` and other `$` patterns in the value literal.
268 result = result.replaceAll(`@@${token}@@`, () => value);
269 }
270 return result;
271 }
272
273 const REMOTE_PICK_PORT_SCRIPT = `const fs = require("node:fs");
274 const net = require("node:net");
275 const filePath = process.argv[2] ?? "";
276 const defaultPort = Number.parseInt(process.argv[3] ?? "", 10);
277 const scanWindow = Number.parseInt(process.argv[4] ?? "", 10);
278 const raw = fs.existsSync(filePath) ? fs.readFileSync(filePath, "utf8").trim() : "";
279 const preferred = Number.parseInt(raw, 10);
280 const start = Number.isInteger(preferred) ? preferred : defaultPort;
281 const end = start + scanWindow;
282
283 function canBind(port) {
284 return new Promise((resolve) => {
285 const server = net.createServer();
286 server.unref();
287 server.once("error", () => resolve(false));
288 server.listen(port, "127.0.0.1", () => {
289 server.close((error) => resolve(!error));
290 });
291 });
292 }
293
294 // macOS lets the bind above succeed while another process listens on
295 // *:<port>, so also skip any port that already answers on loopback.
296 function answersOnLoopback(port) {
297 return new Promise((resolve) => {
298 const socket = net.connect({ port, host: "127.0.0.1" });
299 socket.setTimeout(1000);
300 socket.once("connect", () => {
301 socket.destroy();
302 resolve(true);
303 });
304 socket.once("timeout", () => {
305 socket.destroy();
306 resolve(true);
307 });
308 socket.once("error", () => resolve(false));
309 });
310 }
311
312 (async () => {
313 for (let port = start; port < end; port += 1) {
314 const available = (await canBind(port)) && !(await answersOnLoopback(port));
315 if (available) {
316 process.stdout.write(String(port));
317 return;
318 }
319 }
320 process.exit(1);
321 })().catch(() => process.exit(1));
322 `;
323
324 const REMOTE_WAIT_READY_SCRIPT = `const http = require("node:http");
325 const port = Number.parseInt(process.argv[2] ?? "", 10);
326 const timeoutMs = Number.parseInt(process.argv[3] ?? "", 10);
327 const probeTimeoutMs = Number.parseInt(process.argv[4] ?? "", 10);
328 if (!Number.isInteger(port) || !Number.isInteger(timeoutMs) || !Number.isInteger(probeTimeoutMs)) {
329 process.exit(1);
330 }
331 const deadline = Date.now() + timeoutMs;
332
333 function sleep(ms) {
334 return new Promise((resolve) => setTimeout(resolve, ms));
335 }
336
337 function probe() {
338 return new Promise((resolve) => {
339 const request = http.get(
340 {
341 hostname: "127.0.0.1",
342 port,
343 path: "/",
344 timeout: probeTimeoutMs,
345 },
346 (response) => {
347 response.resume();
348 response.once("end", () => {
349 resolve(response.statusCode >= 200 && response.statusCode < 300 ? "ready" : "not-ready");
350 });
351 },
352 );
353 request.once("timeout", () => {
354 request.destroy();
355 resolve("not-ready");
356 });
357 request.once("error", (error) => resolve(error?.code === "ECONNREFUSED" ? "refused" : "not-ready"));
358 });
359 }
360
361 (async () => {
362 let refusedOnly = true;
363 while (Date.now() < deadline) {
364 const result = await probe();
365 if (result === "ready") {
366 process.exit(0);
367 }
368 refusedOnly &&= result === "refused";
369 await sleep(100);
370 }
371 process.exit(refusedOnly ? 2 : 1);
372 })().catch(() => process.exit(1));
373 `;
374
375 const REMOTE_NODE_ENV_SCRIPT = `prepend_path_if_dir() {
376 if [ -d "$1" ]; then
377 case ":$PATH:" in
378 *":$1:"*) ;;
379 *) PATH="$1:$PATH" ;;
380 esac
381 fi
382 }
383
384 remote_node_satisfies_engine() {
385 T3_NODE_ENGINE_RANGE=@@T3_NODE_ENGINE_RANGE@@
386 if [ -z "$T3_NODE_ENGINE_RANGE" ]; then
387 return 0
388 fi
389 node - "$T3_NODE_ENGINE_RANGE" <<'NODE'
390 @@T3_NODE_ENGINE_CHECK_SCRIPT@@
391 NODE
392 }
393
394 ensure_remote_node_path() {
395 if command -v node >/dev/null 2>&1 && remote_node_satisfies_engine >/dev/null 2>&1; then
396 return 0
397 fi
398
399 prepend_path_if_dir "$HOME/.local/bin"
400 prepend_path_if_dir "$HOME/bin"
401 prepend_path_if_dir "/opt/homebrew/bin"
402 prepend_path_if_dir "/home/linuxbrew/.linuxbrew/bin"
403 prepend_path_if_dir "/usr/local/bin"
404 prepend_path_if_dir "/usr/bin"
405 prepend_path_if_dir "/bin"
406
407 if [ -z "\${VOLTA_HOME:-}" ]; then
408 VOLTA_HOME="$HOME/.volta"
409 fi
410 export VOLTA_HOME
411 prepend_path_if_dir "$VOLTA_HOME/bin"
412
413 prepend_path_if_dir "$HOME/.asdf/shims"
414 prepend_path_if_dir "$HOME/.asdf/bin"
415 if [ ! -x "$HOME/.asdf/shims/node" ] && [ -s "$HOME/.asdf/asdf.sh" ]; then
416 # shellcheck disable=SC1090
417 . "$HOME/.asdf/asdf.sh"
418 fi
419
420 prepend_path_if_dir "$HOME/.local/share/mise/shims"
421 prepend_path_if_dir "$HOME/.mise/shims"
422 if ! command -v node >/dev/null 2>&1 && command -v mise >/dev/null 2>&1; then
423 eval "$(mise activate sh)" >/dev/null 2>&1 || true
424 fi
425
426 if [ -z "\${FNM_DIR:-}" ]; then
427 FNM_DIR="$HOME/.local/share/fnm"
428 fi
429 export FNM_DIR
430 prepend_path_if_dir "$FNM_DIR"
431 prepend_path_if_dir "$HOME/.fnm"
432 if ! command -v node >/dev/null 2>&1 && command -v fnm >/dev/null 2>&1; then
433 eval "$(fnm env --shell bash)" >/dev/null 2>&1 || true
434 fnm use --silent-if-unchanged >/dev/null 2>&1 || fnm use default >/dev/null 2>&1 || true
435 fi
436
437 prepend_path_if_dir "$HOME/.nodenv/bin"
438 prepend_path_if_dir "$HOME/.nodenv/shims"
439 if ! command -v node >/dev/null 2>&1 && command -v nodenv >/dev/null 2>&1; then
440 eval "$(nodenv init -)" >/dev/null 2>&1 || true
441 fi
442
443 if [ -z "\${NVM_DIR:-}" ]; then
444 NVM_DIR="$HOME/.nvm"
445 fi
446 export NVM_DIR
447
448 if [ -s "$NVM_DIR/nvm.sh" ]; then
449 # shellcheck disable=SC1090
450 . "$NVM_DIR/nvm.sh"
451 if ! command -v node >/dev/null 2>&1 && command -v nvm >/dev/null 2>&1; then
452 nvm use --silent default >/dev/null 2>&1 || nvm use --silent node >/dev/null 2>&1 || nvm use --silent --lts >/dev/null 2>&1 || true
453 fi
454 fi
455
456 if ! command -v node >/dev/null 2>&1 && [ -d "$NVM_DIR/versions/node" ]; then
457 for T3_NODE_BIN in "$NVM_DIR"/versions/node/*/bin; do
458 if [ -x "$T3_NODE_BIN/node" ]; then
459 PATH="$T3_NODE_BIN:$PATH"
460 export PATH
461 fi
462 done
463 fi
464
465 command -v node >/dev/null 2>&1 && remote_node_satisfies_engine
466 }
467 `;
468
469 const REMOTE_RUNNER_SCRIPT = `#!/bin/sh
470 set -eu
471 @@T3_NODE_ENV_SCRIPT@@
472 T3_NODE_SCRIPT_PATH=@@T3_NODE_SCRIPT_PATH@@
473 if [ -n "$T3_NODE_SCRIPT_PATH" ]; then
474 # Dev mode: a source checkout on the remote. This is the only path that
475 # needs Node, so Node discovery runs here and nowhere else.
476 ensure_remote_node_path || true
477 if ! command -v node >/dev/null 2>&1; then
478 printf 'Remote host is missing node on PATH. Install Node or configure a supported version manager for non-interactive shells.\\n' >&2
479 exit 1
480 fi
481 exec node "$T3_NODE_SCRIPT_PATH" "$@"
482 fi
483 T3_ARCHIVE_VERSION=@@T3_ARCHIVE_VERSION@@
484 if [ -z "$T3_ARCHIVE_VERSION" ]; then
485 printf 'No t3 release version was provided for the remote runtime.\\n' >&2
486 exit 1
487 fi
488 T3_SYSTEM_EXECUTABLE="$(command -v t3 2>/dev/null || true)"
489 if [ -n "$T3_SYSTEM_EXECUTABLE" ] && T3_SYSTEM_VERSION="$("$T3_SYSTEM_EXECUTABLE" --version 2>/dev/null)" && [ "$T3_SYSTEM_VERSION" = "t3 v$T3_ARCHIVE_VERSION" ]; then
490 # A matching host-provided runtime supports platforms such as musl Linux,
491 # where the portable release archive cannot run.
492 exec "$T3_SYSTEM_EXECUTABLE" "$@"
493 fi
494 # Self-contained release archive: no Node, npm, or compiler on the remote.
495 # Unpacked into the pinned-runtime layout so \`t3 service install\` reuses it.
496 T3_RELEASE_BASE_URL=@@T3_RELEASE_BASE_URL@@
497 T3_RUNTIME_DIR="$HOME/.t3/runtime/versions/$T3_ARCHIVE_VERSION"
498 t3_runtime_ready() {
499 [ -x "$T3_RUNTIME_DIR/t3" ] && [ "$(cat "$T3_RUNTIME_DIR/.install-complete" 2>/dev/null)" = "$T3_ARCHIVE_VERSION" ]
500 }
501 if ! t3_runtime_ready; then
502 mkdir -p "$HOME/.t3/runtime/versions"
503 T3_LOCK="$HOME/.t3/runtime/versions/.$T3_ARCHIVE_VERSION.install.lock"
504 # uutils mkdir can report success on EEXIST, so use shell noclobber.
505 # An empty lock gets a short grace period for its owner to publish a PID.
506 T3_LOCK_WAITED=0
507 T3_LOCK_UNOWNED=0
508 while ! (set -C; printf '%s\\n' "$$" > "$T3_LOCK") 2>/dev/null; do
509 T3_LOCK_OWNER="$(cat "$T3_LOCK" 2>/dev/null || true)"
510 if [ -n "$T3_LOCK_OWNER" ]; then
511 T3_LOCK_UNOWNED=0
512 if ! kill -0 "$T3_LOCK_OWNER" 2>/dev/null; then
513 rm -rf "$T3_LOCK"
514 continue
515 fi
516 else
517 T3_LOCK_UNOWNED=$((T3_LOCK_UNOWNED + 1))
518 if [ "$T3_LOCK_UNOWNED" -ge 5 ]; then
519 rm -rf "$T3_LOCK"
520 continue
521 fi
522 fi
523 if [ "$T3_LOCK_WAITED" -ge @@T3_ARCHIVE_LOCK_WAIT_SECONDS@@ ]; then
524 printf 'Another t3 %s installation has held %s for too long.\\n' "$T3_ARCHIVE_VERSION" "$T3_LOCK" >&2
525 exit 1
526 fi
527 sleep 1
528 T3_LOCK_WAITED=$((T3_LOCK_WAITED + 1))
529 done
530 trap 'rm -rf "$T3_LOCK"' EXIT
531 fi
532 if ! t3_runtime_ready; then
533 # Keep aligned with CLI_ARCHIVE_PLATFORM_KEYS in packages/shared/src/cliRelease.ts.
534 case "$(uname -s) $(uname -m)" in
535 "Darwin arm64") T3_PLATFORM_KEY="darwin-arm64" ;;
536 "Linux arm64" | "Linux aarch64") T3_PLATFORM_KEY="linux-arm64" ;;
537 "Linux x86_64" | "Linux amd64") T3_PLATFORM_KEY="linux-x64" ;;
538 *) printf 'Remote host %s has no t3 release archive.\\n' "$(uname -s) $(uname -m)" >&2; exit 1 ;;
539 esac
540 T3_ARCHIVE="t3-$T3_ARCHIVE_VERSION-$T3_PLATFORM_KEY.tar.gz"
541 if ! command -v tar >/dev/null 2>&1; then
542 printf 'Remote host needs tar to unpack %s. Install tar on it and try again.\\n' "$T3_ARCHIVE" >&2
543 exit 1
544 fi
545 T3_STAGING="$(mktemp -d "$HOME/.t3/runtime/versions/.staging-XXXXXX")"
546 trap 'rm -rf "$T3_STAGING" "$T3_LOCK"' EXIT
547 t3_fetch() {
548 if command -v curl >/dev/null 2>&1; then curl -fsSL --connect-timeout 30 --max-time "$3" "$1" -o "$2"
549 elif command -v wget >/dev/null 2>&1; then wget -q --timeout=30 --tries=1 "$1" -O "$2"
550 else printf 'Remote host needs curl or wget to download %s.\\n' "$T3_ARCHIVE" >&2; exit 1
551 fi || {
552 printf 'Could not download t3 %s from %s. If this host cannot reach it, install t3 %s on it with https://t3.codes/install.sh (T3CODE_VERSION=%s; T3CODE_RELEASE_BASE_URL selects a mirror).\\n' "$T3_ARCHIVE_VERSION" "$1" "$T3_ARCHIVE_VERSION" "$T3_ARCHIVE_VERSION" >&2
553 exit 1
554 }
555 }
556 t3_fetch "$T3_RELEASE_BASE_URL/v$T3_ARCHIVE_VERSION/SHA256SUMS" "$T3_STAGING/SHA256SUMS" @@T3_ARCHIVE_CHECKSUMS_SECONDS@@
557 t3_fetch "$T3_RELEASE_BASE_URL/v$T3_ARCHIVE_VERSION/$T3_ARCHIVE" "$T3_STAGING/$T3_ARCHIVE" @@T3_ARCHIVE_DOWNLOAD_SECONDS@@
558 T3_EXPECTED="$(grep " \\*\\{0,1\\}$T3_ARCHIVE$" "$T3_STAGING/SHA256SUMS" | cut -d' ' -f1)"
559 if command -v sha256sum >/dev/null 2>&1; then
560 T3_ACTUAL="$(sha256sum < "$T3_STAGING/$T3_ARCHIVE" | cut -d' ' -f1)"
561 else
562 T3_ACTUAL="$(shasum -a 256 < "$T3_STAGING/$T3_ARCHIVE" | cut -d' ' -f1)"
563 fi
564 if [ -z "$T3_EXPECTED" ] || [ "$T3_ACTUAL" != "$T3_EXPECTED" ]; then
565 printf 'Checksum mismatch for %s.\\n' "$T3_ARCHIVE" >&2; exit 1
566 fi
567 if ! T3_TAR_ERROR="$(tar -xzf "$T3_STAGING/$T3_ARCHIVE" -C "$T3_STAGING" --strip-components=1 2>&1)"; then
568 # Some tars (BusyBox) gunzip internally, so gzip is only blamed once extraction fails.
569 if command -v gzip >/dev/null 2>&1; then printf '%s\\n' "$T3_TAR_ERROR" >&2
570 else printf 'Remote host needs gzip to unpack %s. Install gzip on it and try again.\\n' "$T3_ARCHIVE" >&2
571 fi
572 exit 1
573 fi
574 rm -f "$T3_STAGING/$T3_ARCHIVE" "$T3_STAGING/SHA256SUMS"
575 # Prove the binary runs here (libc, arch) before marking it ready, or every
576 # later launch would exec a broken install instead of retrying.
577 if ! "$T3_STAGING/t3" --version >/dev/null 2>&1; then
578 printf 'The t3 %s executable does not run on this host.\\n' "$T3_ARCHIVE_VERSION" >&2; exit 1
579 fi
580 printf '%s\\n' "$T3_ARCHIVE_VERSION" > "$T3_STAGING/.install-complete"
581 rm -rf "$T3_RUNTIME_DIR"
582 mv "$T3_STAGING" "$T3_RUNTIME_DIR"
583 fi
584 if [ -n "\${T3_LOCK:-}" ]; then
585 rm -rf "$T3_LOCK"
586 trap - EXIT
587 fi
588 exec "$T3_RUNTIME_DIR/t3" "$@"
589 `;
590
591 // kill -0 also passes for a zombie, a reused PID and, on Linux, a thread ID,
592 // which ps does not list. Only the serve command the launcher started counts;
593 // where ps cannot look up a PID at all, trust kill -0.
594 const REMOTE_MANAGED_SERVER_RUNNING_SCRIPT = `managed_server_running() {
595 [ -n "$1" ] && kill -0 "$1" 2>/dev/null || return 1
596 if MANAGED_ARGS="$(ps -ww -o args= -p "$1" 2>/dev/null)"; then
597 case "$MANAGED_ARGS" in
598 *" serve --host 127.0.0.1 --port "[0-9]*) return 0 ;;
599 esac
600 return 1
601 fi
602 ! ps -ww -o args= -p "$$" >/dev/null 2>&1
603 }`;
604
605 // NFS-shared homes can link ~/.t3 to a directory that does not exist yet, and
606 // mkdir -p fails with EEXIST on a dangling symlink, so create its target first.
607 const REMOTE_ENSURE_T3_HOME_SCRIPT = `if [ -L "$HOME/.t3" ] && [ ! -e "$HOME/.t3" ]; then
608 (cd "$HOME" && mkdir -p "$(readlink .t3)")
609 fi`;
610
611 const REMOTE_LAUNCH_SCRIPT = `set -eu
612 @@T3_NODE_ENV_SCRIPT@@
613 STATE_KEY="$1"
614 LAUNCHER_ID="\${2:-}"
615 STATE_DIR="$HOME/.t3/ssh-launch/$STATE_KEY"
616 DEFAULT_SERVER_HOME="$HOME/.t3"
617 DEFAULT_RUNTIME_FILE="$DEFAULT_SERVER_HOME/userdata/server-runtime.json"
618 PORT_FILE="$STATE_DIR/port"
619 PID_FILE="$STATE_DIR/pid"
620 MANAGED_FILE="$STATE_DIR/managed"
621 LOG_FILE="$STATE_DIR/server.log"
622 RUNNER_FILE="$STATE_DIR/run-t3.sh"
623 RUNNER_NEXT="$STATE_DIR/run-t3.next.$$"
624 LAUNCH_LOCK="$DEFAULT_SERVER_HOME/ssh-launch/.launch.lock"
625 LAUNCH_BREAK_LOCK="$LAUNCH_LOCK.break"
626 @@T3_ENSURE_T3_HOME_SCRIPT@@
627 mkdir -p "$STATE_DIR"
628 cleanup_launch() {
629 rm -f "$RUNNER_NEXT"
630 for OWNED_LOCK in "$LAUNCH_LOCK" "$LAUNCH_BREAK_LOCK"; do
631 if [ "$(cat "$OWNED_LOCK" 2>/dev/null || true)" = "$$" ]; then
632 rm -f "$OWNED_LOCK"
633 fi
634 done
635 }
636 trap cleanup_launch EXIT
637 # A dropped SSH session signals the launch (PIPE on its next write); exit so
638 # the lock is released.
639 trap 'exit 1' HUP INT PIPE TERM
640 cat >"$RUNNER_NEXT" <<'SH'
641 @@T3_RUNNER_SCRIPT@@
642 SH
643 chmod 700 "$RUNNER_NEXT"
644 T3_ARCHIVE_MODE=@@T3_ARCHIVE_MODE@@
645 if [ "$T3_ARCHIVE_MODE" = "1" ]; then
646 # The archive ships the helpers below inside the executable; the remote
647 # needs no Node at all. Resolving the runner once here also downloads the
648 # archive before the port and readiness probes rely on it.
649 "$RUNNER_NEXT" --version >/dev/null
650 elif ! ensure_remote_node_path; then
651 if ! command -v node >/dev/null 2>&1; then
652 printf 'Remote host is missing node on PATH. Install Node or configure a supported version manager for non-interactive shells.\\n' >&2
653 fi
654 exit 1
655 fi
656 # Recovery runs under its own lock and re-reads the owner there, so a waiter
657 # that judged an earlier lock stale cannot remove its successor's lock. No
658 # launch removes another's recovery lock: only SIGKILL inside these lines
659 # leaves one behind, and the wait below then fails naming it.
660 discard_stale_launch_lock() {
661 (set -C; printf '%s\\n' "$$" >"$LAUNCH_BREAK_LOCK") 2>/dev/null || return 1
662 if [ "$(cat "$LAUNCH_LOCK" 2>/dev/null || true)" = "$1" ]; then
663 rm -f "$LAUNCH_LOCK"
664 fi
665 rm -f "$LAUNCH_BREAK_LOCK"
666 }
667 # Concurrent launches share this home's owner lock and may share this record.
668 # Hold the lock from comparing the runner until the server is recorded and
669 # ready, so a waiting launch reuses that server instead of starting or
670 # replacing one.
671 LAUNCH_LOCK_WAITED=0
672 LAUNCH_LOCK_UNOWNED=0
673 while ! (set -C; printf '%s\\n' "$$" >"$LAUNCH_LOCK") 2>/dev/null; do
674 LAUNCH_LOCK_OWNER="$(cat "$LAUNCH_LOCK" 2>/dev/null || true)"
675 if [ -n "$LAUNCH_LOCK_OWNER" ]; then
676 LAUNCH_LOCK_UNOWNED=0
677 if ! kill -0 "$LAUNCH_LOCK_OWNER" 2>/dev/null && discard_stale_launch_lock "$LAUNCH_LOCK_OWNER"; then
678 continue
679 fi
680 else
681 LAUNCH_LOCK_UNOWNED=$((LAUNCH_LOCK_UNOWNED + 1))
682 if [ "$LAUNCH_LOCK_UNOWNED" -ge 5 ] && discard_stale_launch_lock ""; then
683 LAUNCH_LOCK_UNOWNED=0
684 continue
685 fi
686 fi
687 if [ "$LAUNCH_LOCK_WAITED" -ge @@T3_LAUNCH_LOCK_WAIT_SECONDS@@ ]; then
688 if [ -e "$LAUNCH_BREAK_LOCK" ]; then
689 printf 'A T3 server launch stopped while recovering %s. If no launch is running, delete %s.\\n' "$LAUNCH_LOCK" "$LAUNCH_BREAK_LOCK" >&2
690 else
691 printf 'Another T3 server launch has held %s for too long.\\n' "$LAUNCH_LOCK" >&2
692 fi
693 exit 1
694 fi
695 sleep 1
696 LAUNCH_LOCK_WAITED=$((LAUNCH_LOCK_WAITED + 1))
697 done
698 RUNNER_CHANGED=0
699 if [ ! -f "$RUNNER_FILE" ] || ! cmp -s "$RUNNER_NEXT" "$RUNNER_FILE"; then
700 RUNNER_CHANGED=1
701 fi
702 mv "$RUNNER_NEXT" "$RUNNER_FILE"
703 if [ -n "$LAUNCHER_ID" ]; then
704 printf '%s\\n' "$LAUNCHER_ID" >"$STATE_DIR/launcher"
705 fi
706 pick_port() {
707 if [ "$T3_ARCHIVE_MODE" = "1" ]; then
708 "$RUNNER_FILE" __ssh-helper pick-port "$PORT_FILE" "@@T3_DEFAULT_REMOTE_PORT@@" "@@T3_REMOTE_PORT_SCAN_WINDOW@@"
709 return
710 fi
711 node - "$PORT_FILE" "@@T3_DEFAULT_REMOTE_PORT@@" "@@T3_REMOTE_PORT_SCAN_WINDOW@@" <<'NODE'
712 @@T3_PICK_PORT_SCRIPT@@
713 NODE
714 }
715 wait_ready() {
716 if [ "$T3_ARCHIVE_MODE" = "1" ]; then
717 "$RUNNER_FILE" __ssh-helper wait-ready "$REMOTE_PORT" "$1" "@@T3_READY_PROBE_TIMEOUT_MS@@"
718 return
719 fi
720 node - "$REMOTE_PORT" "$1" "@@T3_READY_PROBE_TIMEOUT_MS@@" <<'NODE'
721 @@T3_WAIT_READY_SCRIPT@@
722 NODE
723 }
724 wait_for_pid_exit() {
725 PID_TO_WAIT="$1"
726 WAIT_COUNT=0
727 while kill -0 "$PID_TO_WAIT" 2>/dev/null && [ "$WAIT_COUNT" -lt @@T3_PID_EXIT_WAIT_TICKS@@ ]; do
728 WAIT_COUNT=$((WAIT_COUNT + 1))
729 sleep 0.1
730 done
731 }
732 # kill -0 also succeeds for a server that exited but was not reaped yet, so
733 # a zombie does not count as running. Without ps to tell, trust kill -0.
734 server_running() {
735 [ -n "$1" ] && kill -0 "$1" 2>/dev/null || return 1
736 case "$(ps -o stat= -p "$1" 2>/dev/null || true)" in
737 *Z*) return 1 ;;
738 esac
739 }
740 # An older client replacing a same-or-newer server would run below the database's migrations.
741 keeps_running_server() {
742 [ "$T3_ARCHIVE_MODE" = "1" ] &&
743 "$RUNNER_FILE" __ssh-helper keep-server "$DEFAULT_RUNTIME_FILE" "$REMOTE_PID" >/dev/null 2>&1
744 }
745 @@T3_MANAGED_SERVER_RUNNING_SCRIPT@@
746 # The server publishes its runtime record only after it answers, so one that
747 # has published and now refuses every connection has closed its listener to
748 # shut down; a starting server has not published yet. Exit 2 means refused.
749 managed_server_stopping() {
750 [ "$DEFAULT_RUNTIME_PID" = "$REMOTE_PID" ] || return 1
751 READY_STATUS=0
752 wait_ready "@@T3_REUSE_READY_TIMEOUT_MS@@" || READY_STATUS=$?
753 [ "$READY_STATUS" -eq 2 ]
754 }
755 # Both are SQLite locks the OS drops with their holder: a server holds
756 # server-owner.sqlite, and a boot service holds service-serving.lock from
757 # before it starts a server, between two of them during its own update, until
758 # it stops or idles behind another server.
759 home_owned() {
760 if [ "$T3_ARCHIVE_MODE" = "1" ]; then
761 "$RUNNER_FILE" __ssh-helper home-owned "$DEFAULT_SERVER_HOME" 2>/dev/null
762 return
763 fi
764 node - "$DEFAULT_SERVER_HOME" 2>/dev/null <<'NODE'
765 const fs = require("node:fs");
766 const path = require("node:path");
767 const { DatabaseSync } = require("node:sqlite");
768 const held = (file) => {
769 if (!fs.existsSync(file)) return false;
770 const db = new DatabaseSync(file);
771 try {
772 db.exec("PRAGMA busy_timeout = 0; BEGIN EXCLUSIVE;");
773 return false;
774 } catch (error) {
775 return error?.errcode === 5;
776 } finally {
777 db.close();
778 }
779 };
780 const home = process.argv[2];
781 process.exitCode =
782 held(path.join(home, "runtime", "service-serving.lock")) ||
783 held(path.join(home, "userdata", "server-owner.sqlite"))
784 ? 0
785 : 1;
786 NODE
787 }
788 resolve_default_runtime_port() {
789 if [ "$T3_ARCHIVE_MODE" = "1" ]; then
790 "$RUNNER_FILE" __ssh-helper runtime-port "$DEFAULT_RUNTIME_FILE"
791 return
792 fi
793 node - "$DEFAULT_RUNTIME_FILE" <<'NODE'
794 const fs = require("node:fs");
795 const runtimePath = process.argv[2] ?? "";
796 try {
797 const runtime = JSON.parse(fs.readFileSync(runtimePath, "utf8"));
798 const pid = Number(runtime.pid);
799 const port = Number(runtime.port);
800 if (!Number.isInteger(pid) || pid <= 0 || !Number.isInteger(port)) {
801 process.exit(1);
802 }
803 const origin = new URL(String(runtime.origin ?? ""));
804 if (origin.protocol !== "http:" || !["127.0.0.1", "localhost"].includes(origin.hostname)) {
805 process.exit(1);
806 }
807 process.kill(pid, 0);
808 process.stdout.write(\`\${pid} \${port}\`);
809 } catch {
810 process.exit(1);
811 }
812 NODE
813 }
814 # This script ships with the SSH client; older clients do not honor this marker.
815 update_remaining() {
816 if [ "$T3_ARCHIVE_MODE" = "1" ]; then
817 "$RUNNER_FILE" __ssh-helper update-remaining "$DEFAULT_SERVER_HOME/runtime/@@T3_UPDATE_MARKER_FILE@@"
818 return
819 fi
820 node - "$DEFAULT_SERVER_HOME/runtime/@@T3_UPDATE_MARKER_FILE@@" <<'NODE'
821 const fs = require("node:fs");
822 const path = process.argv[2];
823 try {
824 const { startedAt, expiresAt } = JSON.parse(fs.readFileSync(path, "utf8"));
825 const now = Date.now();
826 if (Number.isFinite(startedAt) && Number.isFinite(expiresAt) &&
827 startedAt <= now && expiresAt > now && expiresAt > startedAt) {
828 process.stdout.write(String(Math.min(expiresAt - now, @@T3_UPDATE_TTL_MS@@)));
829 }
830 } catch {}
831 NODE
832 }
833 REMOTE_PID="$(cat "$PID_FILE" 2>/dev/null || true)"
834 REMOTE_PORT="$(cat "$PORT_FILE" 2>/dev/null || true)"
835 REMOTE_MANAGED="$(cat "$MANAGED_FILE" 2>/dev/null || true)"
836 adopt_default_server() {
837 DEFAULT_RUNTIME_INFO="$(resolve_default_runtime_port 2>/dev/null || true)"
838 DEFAULT_RUNTIME_PID=""
839 DEFAULT_REMOTE_PORT=""
840 ADOPTED_PID=""
841 if [ -n "$DEFAULT_RUNTIME_INFO" ]; then
842 DEFAULT_RUNTIME_PID="\${DEFAULT_RUNTIME_INFO%% *}"
843 DEFAULT_REMOTE_PORT="\${DEFAULT_RUNTIME_INFO#* }"
844 fi
845 # The managed server runs on the default home, so it writes the default
846 # runtime file too. When that file names the PID this launcher recorded as
847 # managed, it describes our own server and its real port, not one to adopt.
848 if [ -n "$DEFAULT_REMOTE_PORT" ] && [ "$REMOTE_MANAGED" = "managed" ] && [ "$DEFAULT_RUNTIME_PID" = "$REMOTE_PID" ]; then
849 REMOTE_PORT="$DEFAULT_REMOTE_PORT"
850 printf '%s\\n' "$REMOTE_PORT" >"$PORT_FILE"
851 return 0
852 elif [ -n "$DEFAULT_REMOTE_PORT" ]; then
853 REMOTE_PORT="$DEFAULT_REMOTE_PORT"
854 if wait_ready "@@T3_REUSE_READY_TIMEOUT_MS@@"; then
855 ADOPTED_PID="$DEFAULT_RUNTIME_PID"
856 if [ "$REMOTE_MANAGED" = "managed" ]; then
857 if managed_server_running "$REMOTE_PID"; then
858 kill "$REMOTE_PID" 2>/dev/null || true
859 wait_for_pid_exit "$REMOTE_PID"
860 fi
861 REMOTE_PID=""
862 REMOTE_PORT="$DEFAULT_REMOTE_PORT"
863 REMOTE_MANAGED="external"
864 rm -f "$PID_FILE"
865 printf '%s\\n' "$REMOTE_PORT" >"$PORT_FILE"
866 printf 'external\\n' >"$MANAGED_FILE"
867 else
868 printf '%s\\n' "$REMOTE_PORT" >"$PORT_FILE"
869 printf 'external\\n' >"$MANAGED_FILE"
870 REMOTE_PID=""
871 REMOTE_MANAGED="external"
872 fi
873 return 0
874 else
875 REMOTE_PID="$(cat "$PID_FILE" 2>/dev/null || true)"
876 REMOTE_PORT="$(cat "$PORT_FILE" 2>/dev/null || true)"
877 REMOTE_MANAGED="$(cat "$MANAGED_FILE" 2>/dev/null || true)"
878 fi
879 fi
880 return 1
881 }
882 # Editing a connection's alias, host, user or port changes its key, so the
883 # adopted server may be one this desktop started under another key. Ownership
884 # then moves here, leaving one managed record, so updates keep replacing it.
885 # Another desktop's server stays external. Only this first adoption runs
886 # before the replace decision, which the previous key's runner settles.
887 if adopt_default_server && [ -n "$ADOPTED_PID" ] && [ -n "$LAUNCHER_ID" ]; then
888 for SIBLING_DIR in "$DEFAULT_SERVER_HOME"/ssh-launch/*; do
889 [ "$SIBLING_DIR" != "$STATE_DIR" ] && [ "$(cat "$SIBLING_DIR/managed" 2>/dev/null || true)" = "managed" ] || continue
890 [ "$(cat "$SIBLING_DIR/pid" 2>/dev/null || true)" = "$ADOPTED_PID" ] && managed_server_running "$ADOPTED_PID" || continue
891 [ "$(cat "$SIBLING_DIR/launcher" 2>/dev/null || true)" = "$LAUNCHER_ID" ] || break
892 REMOTE_PID="$ADOPTED_PID"
893 REMOTE_MANAGED="managed"
894 printf '%s\\n' "$REMOTE_PID" >"$PID_FILE"
895 printf 'managed\\n' >"$MANAGED_FILE"
896 printf 'external\\n' >"$SIBLING_DIR/managed"
897 rm -f "$SIBLING_DIR/pid"
898 RUNNER_CHANGED=1
899 if cmp -s "$RUNNER_FILE" "$SIBLING_DIR/run-t3.sh"; then
900 RUNNER_CHANGED=0
901 fi
902 break
903 done
904 fi
905 if [ "$REMOTE_MANAGED" = "external" ]; then
906 # A server adopted above has just answered. While it is still alive, probing
907 # it again could catch a momentary stall and launch a second server beside it.
908 if server_running "$ADOPTED_PID"; then
909 :
910 elif [ -z "$REMOTE_PORT" ] || ! wait_ready "@@T3_REUSE_READY_TIMEOUT_MS@@"; then
911 REMOTE_PID=""
912 REMOTE_PORT=""
913 REMOTE_MANAGED=""
914 fi
915 elif [ -n "$REMOTE_PORT" ] && managed_server_running "$REMOTE_PID"; then
916 # Reconnect owns the local tunnel, not the lifetime of an already running
917 # server. A changed runner is the update path and replaces an older server.
918 if [ "$RUNNER_CHANGED" -eq 1 ] && ! keeps_running_server; then
919 kill "$REMOTE_PID" 2>/dev/null || true
920 wait_for_pid_exit "$REMOTE_PID"
921 REMOTE_PID=""
922 REMOTE_PORT=""
923 REMOTE_MANAGED=""
924 elif managed_server_stopping; then
925 wait_for_pid_exit "$REMOTE_PID"
926 if managed_server_running "$REMOTE_PID"; then
927 printf 'The previous remote T3 server (PID %s) is still shutting down. Retry once it has exited.\\n' "$REMOTE_PID" >&2
928 exit 1
929 fi
930 REMOTE_PID=""
931 REMOTE_PORT=""
932 REMOTE_MANAGED=""
933 elif ! wait_ready "@@T3_READY_TIMEOUT_MS@@"; then
934 printf 'Remote T3 server is still running but did not become ready. Retry the connection; the server was left running.\\n' >&2
935 exit 1
936 fi
937 else
938 REMOTE_PID=""
939 REMOTE_PORT=""
940 REMOTE_MANAGED=""
941 fi
942 # The server may answer long before it publishes its runtime record, or never
943 # publish one; another alias's launch record still names it. Only one server
944 # owns the home, so the first live one decides.
945 if [ -z "$REMOTE_PORT" ] && [ -z "$DEFAULT_RUNTIME_INFO" ]; then
946 for SIBLING_DIR in "$DEFAULT_SERVER_HOME"/ssh-launch/*; do
947 [ "$SIBLING_DIR" != "$STATE_DIR" ] && [ "$(cat "$SIBLING_DIR/managed" 2>/dev/null || true)" = "managed" ] || continue
948 managed_server_running "$(cat "$SIBLING_DIR/pid" 2>/dev/null || true)" || continue
949 REMOTE_PORT="$(cat "$SIBLING_DIR/port" 2>/dev/null || true)"
950 if [ -n "$REMOTE_PORT" ] && wait_ready "@@T3_REUSE_READY_TIMEOUT_MS@@"; then
951 REMOTE_MANAGED="external"
952 rm -f "$PID_FILE"
953 printf '%s\\n' "$REMOTE_PORT" >"$PORT_FILE"
954 printf 'external\\n' >"$MANAGED_FILE"
955 else
956 REMOTE_PORT=""
957 fi
958 break
959 done
960 fi
961 if [ -z "$REMOTE_PORT" ]; then
962 # The sleep count bounds a host clock that stops advancing.
963 UPDATE_WAITED=0
964 UPDATE_SAW_MARKER=0
965 while [ "$UPDATE_WAITED" -le @@T3_UPDATE_TTL_SECONDS@@ ]; do
966 UPDATE_REMAINING="$(update_remaining 2>/dev/null || true)"
967 [ -n "$UPDATE_REMAINING" ] || break
968 UPDATE_SAW_MARKER=1
969 [ "$UPDATE_WAITED" -lt @@T3_UPDATE_TTL_SECONDS@@ ] || break
970 if adopt_default_server; then break; fi
971 REMOTE_PID=""
972 REMOTE_PORT=""
973 REMOTE_MANAGED=""
974 sleep 1
975 UPDATE_WAITED=$((UPDATE_WAITED + 1))
976 done
977 # The desktop clears the marker after publishing runtime, possibly before the first marker read,
978 # so refresh once more before falling back. A cleanly stopped server leaves no runtime file.
979 if { [ "$UPDATE_SAW_MARKER" -eq 1 ] || [ -f "$DEFAULT_RUNTIME_FILE" ]; } && [ -z "$REMOTE_PORT" ] && ! adopt_default_server; then
980 REMOTE_PID=""
981 REMOTE_PORT=""
982 REMOTE_MANAGED=""
983 fi
984 fi
985 # A server that holds the home without answering yet is still starting, and a
986 # boot service holds it between two servers while it updates. A second server
987 # would lose the home to it or take the home from it, so wait while the home is
988 # held and start one only once it is free. The managed server an update just
989 # stopped may still be exiting. The sleep count bounds a host clock that stops.
990 OWNER_WAITED=0
991 OWNER_DEADLINE=$(($(date +%s) + @@T3_READY_TIMEOUT_SECONDS@@))
992 while [ -z "$REMOTE_PORT" ] && ! managed_server_running "$(cat "$PID_FILE" 2>/dev/null || true)" && home_owned; do
993 if adopt_default_server; then break; fi
994 REMOTE_PID=""
995 REMOTE_PORT=""
996 REMOTE_MANAGED=""
997 if [ "$OWNER_WAITED" -ge @@T3_READY_TIMEOUT_SECONDS@@ ] || [ "$(date +%s)" -ge "$OWNER_DEADLINE" ]; then
998 printf 'A T3 server or boot service holds %s but did not become ready, so no second server was started. Retry once it has started.\\n' "$DEFAULT_SERVER_HOME" >&2
999 exit 1
1000 fi
1001 sleep 1
1002 OWNER_WAITED=$((OWNER_WAITED + 1))
1003 done
1004 if [ -z "$REMOTE_PORT" ]; then
1005 REMOTE_PORT="$(pick_port)" || true
1006 if [ -z "$REMOTE_PORT" ]; then
1007 if [ "$T3_ARCHIVE_MODE" = "1" ]; then
1008 printf 'Failed to find an available port on the remote host.\\n' >&2
1009 else
1010 printf 'Failed to find an available port on the remote host. Ensure node is available on PATH.\\n' >&2
1011 fi
1012 exit 1
1013 fi
1014 # Start this launch's log empty. Unlink rather than truncate: a process from an
1015 # earlier launch can still hold the old log open and keep writing to it.
1016 rm -f "$LOG_FILE"
1017 nohup env T3CODE_NO_BROWSER=1 "$RUNNER_FILE" serve --host 127.0.0.1 --port "$REMOTE_PORT" --base-dir "$DEFAULT_SERVER_HOME" >>"$LOG_FILE" 2>&1 < /dev/null &
1018 REMOTE_PID="$!"
1019 printf '%s\\n' "$REMOTE_PID" >"$PID_FILE"
1020 printf '%s\\n' "$REMOTE_PORT" >"$PORT_FILE"
1021 printf 'managed\\n' >"$MANAGED_FILE"
1022 # A stop keeps the launcher file; it must not name this server's owner.
1023 if [ -z "$LAUNCHER_ID" ]; then
1024 rm -f "$STATE_DIR/launcher"
1025 fi
1026 if ! wait_ready "@@T3_READY_TIMEOUT_MS@@"; then
1027 printf 'Remote T3 server did not become ready on 127.0.0.1:%s.\\n' "$REMOTE_PORT" >&2
1028 if [ -s "$LOG_FILE" ]; then
1029 tail -n 80 "$LOG_FILE" >&2 2>/dev/null || true
1030 else
1031 printf 'It wrote nothing to %s, so it exited before producing any output.\\n' "$LOG_FILE" >&2
1032 fi
1033 kill "$REMOTE_PID" 2>/dev/null || true
1034 wait_for_pid_exit "$REMOTE_PID"
1035 rm -f "$PID_FILE" "$PORT_FILE" "$MANAGED_FILE"
1036 exit 1
1037 fi
1038 # Keep the lock until another alias's launch can find this server, or it
1039 # would start a second one that loses the home's owner lock.
1040 RECORD_DEADLINE=$(($(date +%s) + @@T3_RUNTIME_RECORD_WAIT_SECONDS@@))
1041 while [ "$(date +%s)" -lt "$RECORD_DEADLINE" ]; do
1042 DEFAULT_RUNTIME_INFO="$(resolve_default_runtime_port 2>/dev/null || true)"
1043 [ "\${DEFAULT_RUNTIME_INFO%% *}" = "$REMOTE_PID" ] && break
1044 # A host wrapper can spawn the server instead of replacing itself.
1045 RUNTIME_PID="\${DEFAULT_RUNTIME_INFO%% *}"
1046 if [ "\${DEFAULT_RUNTIME_INFO#* }" = "$REMOTE_PORT" ] && managed_server_running "$RUNTIME_PID"; then
1047 RUNTIME_ANCESTOR="$RUNTIME_PID"
1048 while [ -n "$RUNTIME_ANCESTOR" ] && [ "$RUNTIME_ANCESTOR" != "1" ] && [ "$RUNTIME_ANCESTOR" != "$REMOTE_PID" ]; do
1049 RUNTIME_ANCESTOR="$(ps -o ppid= -p "$RUNTIME_ANCESTOR" 2>/dev/null | tr -d '[:space:]')"
1050 done
1051 if [ "$RUNTIME_ANCESTOR" = "$REMOTE_PID" ]; then
1052 REMOTE_PID="$RUNTIME_PID"
1053 printf '%s\\n' "$REMOTE_PID" >"$PID_FILE"
1054 break
1055 fi
1056 fi
1057 sleep 0.1
1058 done
1059 fi
1060 printf '{"remotePort":%s,"serverKind":"%s"}\\n' "$REMOTE_PORT" "\${REMOTE_MANAGED:-managed}"
1061 `;
1062
1063 const REMOTE_PAIRING_SCRIPT = `set -eu
1064 STATE_DIR="$HOME/.t3/ssh-launch/@@T3_STATE_KEY@@"
1065 DEFAULT_SERVER_HOME="$HOME/.t3"
1066 RUNNER_FILE="$STATE_DIR/run-t3.sh"
1067 @@T3_ENSURE_T3_HOME_SCRIPT@@
1068 mkdir -p "$STATE_DIR"
1069 cat >"$RUNNER_FILE" <<'SH'
1070 @@T3_RUNNER_SCRIPT@@
1071 SH
1072 chmod 700 "$RUNNER_FILE"
1073 PAIRING_BASE_DIR="$DEFAULT_SERVER_HOME"
1074 "$RUNNER_FILE" auth pairing create --base-dir "$PAIRING_BASE_DIR" --json
1075 `;
1076
1077 const REMOTE_STOP_SCRIPT = `set -eu
1078 STATE_DIR="$HOME/.t3/ssh-launch/@@T3_STATE_KEY@@"
1079 PID_FILE="$STATE_DIR/pid"
1080 PORT_FILE="$STATE_DIR/port"
1081 MANAGED_FILE="$STATE_DIR/managed"
1082 REMOTE_MANAGED="$(cat "$MANAGED_FILE" 2>/dev/null || true)"
1083 REMOTE_PID="$(cat "$PID_FILE" 2>/dev/null || true)"
1084 @@T3_MANAGED_SERVER_RUNNING_SCRIPT@@
1085 if [ "$REMOTE_MANAGED" != "external" ] && managed_server_running "$REMOTE_PID"; then
1086 kill "$REMOTE_PID" 2>/dev/null || true
1087 WAIT_COUNT=0
1088 while kill -0 "$REMOTE_PID" 2>/dev/null && [ "$WAIT_COUNT" -lt 20 ]; do
1089 WAIT_COUNT=$((WAIT_COUNT + 1))
1090 sleep 0.1
1091 done
1092 if kill -0 "$REMOTE_PID" 2>/dev/null; then
1093 printf 'Remote T3 server with PID %s did not stop within 2 seconds. Its ownership files were kept.\\n' "$REMOTE_PID" >&2
1094 exit 1
1095 fi
1096 fi
1097 rm -f "$PID_FILE" "$PORT_FILE" "$MANAGED_FILE"
1098 printf '{"stopped":true}\\n'
1099 `;
1100
1101 const REMOTE_LOG_TAIL_SCRIPT = `set -eu
1102 STATE_DIR="$HOME/.t3/ssh-launch/@@T3_STATE_KEY@@"
1103 LOG_FILE="$STATE_DIR/server.log"
1104 if [ -f "$LOG_FILE" ]; then
1105 tail -n 80 "$LOG_FILE" 2>/dev/null || true
1106 fi
1107 `;
1108
1109 export class SshInvalidArchiveVersionError extends Schema.TaggedError<SshInvalidArchiveVersionError>()(
1110 "SshInvalidArchiveVersionError",
1111 { archiveVersion: Schema.String },
1112 ) {
1113 override get message(): string {
1114 return `'${this.archiveVersion}' is not an exact t3 version and cannot name a runtime directory.`;
1115 }
1116 }
1117
1118 // The version becomes a directory name the runner removes and recreates, so
1119 // it must be one exact SemVer segment: no separators, no `..`, no shell
1120 // metacharacters beyond what SemVer allows.
1121 const EXACT_ARCHIVE_VERSION =
1122 /^(?:0|[1-9]\d*)\.(?:0|[1-9]\d*)\.(?:0|[1-9]\d*)(?:-[0-9A-Za-z-]+(?:\.[0-9A-Za-z-]+)*)?$/u;
1123
1124 export class SshMissingRunnerError extends Schema.TaggedError<SshMissingRunnerError>()(
1125 "SshMissingRunnerError",
1126 {},
1127 ) {
1128 override get message(): string {
1129 return "A remote t3 runner needs an archive version or a node script path.";
1130 }
1131 }
1132
1133 export function buildRemoteT3RunnerScript(input?: RemoteT3RunnerOptions): string {
1134 const nodeScriptPath = input?.nodeScriptPath?.trim() || "";
1135 const archiveVersion = input?.archiveVersion?.trim() || "";
1136 if (nodeScriptPath === "" && archiveVersion === "") {
1137 throw new SshMissingRunnerError();
1138 }
1139 if (archiveVersion !== "" && !EXACT_ARCHIVE_VERSION.test(archiveVersion)) {
1140 throw new SshInvalidArchiveVersionError({ archiveVersion });
1141 }
1142 const releaseBaseUrl = cliReleaseDownloadBaseUrl("", input?.releaseBaseUrl ?? undefined).replace(
1143 /\/v$/u,
1144 "",
1145 );
1146 return stripTrailingNewlines(
1147 applyScriptPlaceholders(REMOTE_RUNNER_SCRIPT, {
1148 T3_NODE_SCRIPT_PATH: shellSingleQuote(nodeScriptPath),
1149 T3_ARCHIVE_VERSION: shellSingleQuote(archiveVersion),
1150 T3_RELEASE_BASE_URL: shellSingleQuote(releaseBaseUrl),
1151 T3_ARCHIVE_LOCK_WAIT_SECONDS: String(REMOTE_ARCHIVE_LOCK_WAIT_SECONDS),
1152 T3_ARCHIVE_DOWNLOAD_SECONDS: String(REMOTE_ARCHIVE_DOWNLOAD_SECONDS),
1153 T3_ARCHIVE_CHECKSUMS_SECONDS: String(REMOTE_ARCHIVE_CHECKSUMS_SECONDS),
1154 T3_NODE_ENV_SCRIPT: buildRemoteNodeEnvScript(input),
1155 }),
1156 );
1157 }
1158
1159 export function buildRemoteNodeEnvScript(input?: RemoteT3RunnerOptions): string {
1160 return stripTrailingNewlines(
1161 applyScriptPlaceholders(REMOTE_NODE_ENV_SCRIPT, {
1162 T3_NODE_ENGINE_RANGE: shellSingleQuote(input?.nodeEngineRange?.trim() || ""),
1163 T3_NODE_ENGINE_CHECK_SCRIPT: stripTrailingNewlines(buildRemoteNodeEngineCheckScript()),
1164 }),
1165 );
1166 }
1167
1168 export function buildRemoteLaunchScript(input?: RemoteT3RunnerOptions): string {
1169 return applyScriptPlaceholders(REMOTE_LAUNCH_SCRIPT, {
1170 T3_UPDATE_MARKER_FILE: DESKTOP_UPDATE_IN_PROGRESS_MARKER_FILE,
1171 T3_UPDATE_TTL_MS: String(DESKTOP_UPDATE_IN_PROGRESS_TTL_MS),
1172 T3_UPDATE_TTL_SECONDS: String(DESKTOP_UPDATE_IN_PROGRESS_TTL_MS / 1000),
1173 T3_ARCHIVE_MODE: isNodeScriptRunner(input) ? "0" : "1",
1174 T3_NODE_ENV_SCRIPT: buildRemoteNodeEnvScript(input),
1175 T3_RUNNER_SCRIPT: stripTrailingNewlines(buildRemoteT3RunnerScript(input)),
1176 T3_PICK_PORT_SCRIPT: stripTrailingNewlines(REMOTE_PICK_PORT_SCRIPT),
1177 T3_WAIT_READY_SCRIPT: stripTrailingNewlines(REMOTE_WAIT_READY_SCRIPT),
1178 T3_MANAGED_SERVER_RUNNING_SCRIPT: REMOTE_MANAGED_SERVER_RUNNING_SCRIPT,
1179 T3_ENSURE_T3_HOME_SCRIPT: REMOTE_ENSURE_T3_HOME_SCRIPT,
1180 T3_DEFAULT_REMOTE_PORT: String(DEFAULT_REMOTE_PORT),
1181 T3_REMOTE_PORT_SCAN_WINDOW: String(REMOTE_PORT_SCAN_WINDOW),
1182 T3_READY_TIMEOUT_MS: String(REMOTE_READY_TIMEOUT_MS),
1183 T3_READY_TIMEOUT_SECONDS: String(REMOTE_READY_TIMEOUT_MS / 1000),
1184 T3_REUSE_READY_TIMEOUT_MS: String(REMOTE_REUSE_READY_TIMEOUT_MS),
1185 T3_READY_PROBE_TIMEOUT_MS: String(SSH_READY_PROBE_TIMEOUT_MS),
1186 T3_PID_EXIT_WAIT_TICKS: String(REMOTE_PID_EXIT_WAIT_MS / 100),
1187 T3_LAUNCH_LOCK_WAIT_SECONDS: String(REMOTE_LAUNCH_LOCK_WAIT_SECONDS),
1188 T3_RUNTIME_RECORD_WAIT_SECONDS: String(REMOTE_RUNTIME_RECORD_WAIT_SECONDS),
1189 });
1190 }
1191
1192 function buildRemotePairingScript(stateKey: string, input?: RemoteT3RunnerOptions): string {
1193 return applyScriptPlaceholders(REMOTE_PAIRING_SCRIPT, {
1194 T3_STATE_KEY: stateKey,
1195 T3_ENSURE_T3_HOME_SCRIPT: REMOTE_ENSURE_T3_HOME_SCRIPT,
1196 T3_RUNNER_SCRIPT: stripTrailingNewlines(buildRemoteT3RunnerScript(input)),
1197 });
1198 }
1199
1200 function buildRemoteStopScript(stateKey: string): string {
1201 return applyScriptPlaceholders(REMOTE_STOP_SCRIPT, {
1202 T3_STATE_KEY: stateKey,
1203 T3_MANAGED_SERVER_RUNNING_SCRIPT: REMOTE_MANAGED_SERVER_RUNNING_SCRIPT,
1204 });
1205 }
1206
1207 function buildRemoteLogTailScript(stateKey: string): string {
1208 return applyScriptPlaceholders(REMOTE_LOG_TAIL_SCRIPT, {
1209 T3_STATE_KEY: stateKey,
1210 });
1211 }
1212
1213 // Shells known to take `-l -c` with a POSIX-style `exec` read their own login
1214 // profiles first; any other shell (csh, pwsh, ...) keeps the plain `sh -l` launch.
1215 const remoteLoginShellArgs = (args = "") => [
1216 "sh",
1217 "-c",
1218 shellSingleQuote(
1219 `case "\${SHELL##*/}" in bash|zsh|ksh|fish|nu) exec "$SHELL" -l -c "exec sh -s${args ? ` -- ${args}` : ""}" ;; *) exec sh -l -s${args ? ` -- ${args}` : ""} ;; esac`,
1220 ),
1221 ];
1222
1223 export const launchOrReuseRemoteServer = Effect.fn("ssh/tunnel.launchOrReuseRemoteServer")(
1224 function* (
1225 target: DesktopSshEnvironmentTarget,
1226 input?: SshAuth.SshAuthOptions,
1227 runner?: RemoteT3RunnerOptions,
1228 launcherId?: string | null,
1229 ): Effect.fn.Return<
1230 { readonly remotePort: number; readonly remoteServerKind: "external" | "managed" | null },
1231 SshCommandError | SshInvalidTargetError | SshLaunchError,
1232 ChildProcessSpawner.ChildProcessSpawner | Digest.Digest | FileSystem.FileSystem | Path.Path
1233 > {
1234 const stateKey = yield* remoteStateKey(target);
1235 // The launcher ID is interpolated into the remote shell command, so only hex passes.
1236 const launchArgs =
1237 typeof launcherId === "string" && /^[0-9a-f]{32}$/u.test(launcherId)
1238 ? `${stateKey} ${launcherId}`
1239 : stateKey;
1240 yield* Effect.logInfo("ssh.remoteServer.launch.start", {
1241 ...sshTargetLogFields(target),
1242 ...sshRunnerLogFields(runner),
1243 stateKey,
1244 });
1245 const result = yield* runSshCommand(target, {
1246 remoteCommandArgs: remoteLoginShellArgs(launchArgs),
1247 stdin: buildRemoteLaunchScript(runner),
1248 timeoutMs: isNodeScriptRunner(runner)
1249 ? REMOTE_LAUNCH_TIMEOUT_MS
1250 : REMOTE_ARCHIVE_LAUNCH_TIMEOUT_MS,
1251 ...(input?.authSecret === undefined ? {} : { authSecret: input.authSecret }),
1252 ...(input?.batchMode === undefined ? {} : { batchMode: input.batchMode }),
1253 ...(input?.interactiveAuth === undefined ? {} : { interactiveAuth: input.interactiveAuth }),
1254 });
1255 if (!getLastNonEmptyOutputLine(result.stdout)) {
1256 return yield* new SshLaunchError({
1257 message: "SSH launch did not return a remote port.",
1258 stdout: result.stdout,
1259 });
1260 }
1261 const parsed = yield* decodeRemoteLaunchOutput(result.stdout).pipe(
1262 Effect.mapError(
1263 (cause) =>
1264 new SshLaunchError({
1265 message: "SSH launch returned unparseable output.",
1266 stdout: result.stdout,
1267 cause,
1268 }),
1269 ),
1270 );
1271 if (!Number.isInteger(parsed.remotePort)) {
1272 return yield* new SshLaunchError({
1273 message: `SSH launch returned an invalid remote port: ${String(parsed.remotePort)}.`,
1274 stdout: result.stdout,
1275 });
1276 }
1277 yield* Effect.logInfo("ssh.remoteServer.launch.ready", {
1278 ...sshTargetLogFields(target),
1279 remotePort: parsed.remotePort,
1280 remoteServerKind: parsed.serverKind ?? null,
1281 stateKey,
1282 });
1283 return {
1284 remotePort: parsed.remotePort,
1285 remoteServerKind: parsed.serverKind ?? null,
1286 };
1287 },
1288 );
1289
1290 export const issueRemotePairingToken = Effect.fn("ssh/tunnel.issueRemotePairingToken")(function* (
1291 target: DesktopSshEnvironmentTarget,
1292 input?: SshAuth.SshAuthOptions,
1293 runner?: RemoteT3RunnerOptions,
1294 ): Effect.fn.Return<
1295 {
1296 readonly credential: string;
1297 },
1298 SshCommandError | SshInvalidTargetError | SshPairingError,
1299 ChildProcessSpawner.ChildProcessSpawner | Digest.Digest | FileSystem.FileSystem | Path.Path
1300 > {
1301 const stateKey = yield* remoteStateKey(target);
1302 yield* Effect.logDebug("ssh.remoteServer.pairingToken.start", {
1303 ...sshTargetLogFields(target),
1304 stateKey,
1305 });
1306 const result = yield* runSshCommand(target, {
1307 remoteCommandArgs: remoteLoginShellArgs(),
1308 stdin: buildRemotePairingScript(stateKey, runner),
1309 ...(isNodeScriptRunner(runner) ? {} : { timeoutMs: REMOTE_ARCHIVE_LAUNCH_TIMEOUT_MS }),
1310 ...(input?.authSecret === undefined ? {} : { authSecret: input.authSecret }),
1311 ...(input?.batchMode === undefined ? {} : { batchMode: input.batchMode }),
1312 ...(input?.interactiveAuth === undefined ? {} : { interactiveAuth: input.interactiveAuth }),
1313 });
1314 if (!getLastNonEmptyOutputLine(result.stdout)) {
1315 return yield* new SshPairingError({
1316 message: "SSH pairing did not return a credential.",
1317 stdout: result.stdout,
1318 });
1319 }
1320 const parsed = yield* decodeRemotePairingOutput(result.stdout).pipe(
1321 Effect.mapError(
1322 (cause) =>
1323 new SshPairingError({
1324 message: "SSH pairing returned unparseable output.",
1325 stdout: result.stdout,
1326 cause,
1327 }),
1328 ),
1329 );
1330 if (parsed.credential.trim().length === 0) {
1331 return yield* new SshPairingError({
1332 message: "SSH pairing command returned an invalid credential.",
1333 stdout: result.stdout,
1334 });
1335 }
1336 yield* Effect.logDebug("ssh.remoteServer.pairingToken.created", {
1337 ...sshTargetLogFields(target),
1338 stateKey,
1339 });
1340 return {
1341 credential: parsed.credential,
1342 };
1343 });
1344
1345 const stopRemoteServer = Effect.fn("ssh/tunnel.stopRemoteServer")(function* (
1346 target: DesktopSshEnvironmentTarget,
1347 input?: SshAuth.SshAuthOptions,
1348 ): Effect.fn.Return<
1349 void,
1350 SshCommandError | SshInvalidTargetError,
1351 ChildProcessSpawner.ChildProcessSpawner | Digest.Digest | FileSystem.FileSystem | Path.Path
1352 > {
1353 const stateKey = yield* remoteStateKey(target);
1354 yield* Effect.logInfo("ssh.remoteServer.stop.start", {
1355 ...sshTargetLogFields(target),
1356 stateKey,
1357 });
1358 yield* runSshCommand(target, {
1359 remoteCommandArgs: ["sh", "-s"],
1360 stdin: buildRemoteStopScript(stateKey),
1361 ...(input?.authSecret === undefined ? {} : { authSecret: input.authSecret }),
1362 ...(input?.batchMode === undefined ? {} : { batchMode: input.batchMode }),
1363 ...(input?.interactiveAuth === undefined ? {} : { interactiveAuth: input.interactiveAuth }),
1364 });
1365 yield* Effect.logInfo("ssh.remoteServer.stop.succeeded", {
1366 ...sshTargetLogFields(target),
1367 stateKey,
1368 });
1369 });
1370
1371 const readRemoteServerLogTail = Effect.fn("ssh/tunnel.readRemoteServerLogTail")(function* (
1372 target: DesktopSshEnvironmentTarget,
1373 input?: SshAuth.SshAuthOptions,
1374 ): Effect.fn.Return<
1375 string,
1376 SshCommandError | SshInvalidTargetError,
1377 ChildProcessSpawner.ChildProcessSpawner | Digest.Digest | FileSystem.FileSystem | Path.Path
1378 > {
1379 const result = yield* runSshCommand(target, {
1380 remoteCommandArgs: ["sh", "-s"],
1381 stdin: buildRemoteLogTailScript(yield* remoteStateKey(target)),
1382 timeoutMs: 10_000,
1383 ...(input?.authSecret === undefined ? {} : { authSecret: input.authSecret }),
1384 ...(input?.batchMode === undefined ? {} : { batchMode: input.batchMode }),
1385 ...(input?.interactiveAuth === undefined ? {} : { interactiveAuth: input.interactiveAuth }),
1386 });
1387 return result.stdout.trim();
1388 });
1389
1390 const waitForHttpReady = (input: {
1391 readonly baseUrl: string;
1392 readonly timeoutMs?: number;
1393 readonly intervalMs?: number;
1394 readonly probeTimeoutMs?: number;
1395 readonly path?: string;
1396 }): Effect.Effect<void, SshReadinessError, HttpClient.HttpClient> =>
1397 waitForHttpReadyShared({
1398 baseUrl: input.baseUrl,
1399 ...(input.path === undefined ? {} : { path: input.path }),
1400 ...(input.timeoutMs === undefined ? {} : { timeoutMs: input.timeoutMs }),
1401 ...(input.intervalMs === undefined ? {} : { intervalMs: input.intervalMs }),
1402 probeTimeoutMs: input.probeTimeoutMs ?? SSH_TUNNEL_READY_PROBE_TIMEOUT_MS,
1403 makeError: ({ requestUrl, probeTimeoutMs, cause }) => {
1404 if (typeof cause === "object" && cause !== null && "kind" in cause) {
1405 const kind = (cause as { readonly kind?: unknown }).kind;
1406 if (kind === "probe-timeout") {
1407 return new SshReadinessError({
1408 message: `Backend readiness probe exceeded ${probeTimeoutMs}ms at ${requestUrl}.`,
1409 cause,
1410 });
1411 }
1412 if (kind === "overall-timeout") {
1413 const overall = cause as unknown as {
1414 readonly baseUrl: string;
1415 readonly timeoutMs: number;
1416 readonly lastFailure: unknown;
1417 };
1418 return new SshReadinessError({
1419 message: `Timed out waiting ${overall.timeoutMs}ms for backend readiness at ${overall.baseUrl}.`,
1420 cause: overall.lastFailure,
1421 });
1422 }
1423 }
1424 return new SshReadinessError({
1425 message: `Backend readiness probe failed at ${requestUrl}.`,
1426 cause,
1427 });
1428 },
1429 });
1430
1431 function isLoopbackHostname(hostname: string): boolean {
1432 const normalized = hostname
1433 .trim()
1434 .toLowerCase()
1435 .replace(/^\[(.*)\]$/, "$1");
1436 return normalized === "127.0.0.1" || normalized === "::1" || normalized === "localhost";
1437 }
1438
1439 export const resolveLoopbackSshHttpBaseUrl = Effect.fn("ssh/tunnel.resolveLoopbackSshHttpBaseUrl")(
1440 function* (rawHttpBaseUrl: unknown): Effect.fn.Return<string, SshHttpBridgeError> {
1441 return yield* Effect.try({
1442 try: () => {
1443 if (typeof rawHttpBaseUrl !== "string" || rawHttpBaseUrl.trim().length === 0) {
1444 throw new Error("Invalid SSH forwarded http base URL.");
1445 }
1446 const baseUrl = new URL(rawHttpBaseUrl);
1447 if (!isLoopbackHostname(baseUrl.hostname)) {
1448 throw new Error("SSH desktop bridge only supports loopback forwarded URLs.");
1449 }
1450 return baseUrl.toString();
1451 },
1452 catch: (cause) =>
1453 new SshHttpBridgeError({
1454 message: cause instanceof Error ? cause.message : "Invalid SSH forwarded http base URL.",
1455 cause,
1456 }),
1457 });
1458 },
1459 );
1460
1461 const reserveLocalTunnelPort = Effect.fn("ssh/tunnel.reserveLocalTunnelPort")(function* () {
1462 const net = yield* NetService.NetService;
1463 return yield* net.reserveLoopbackPort();
1464 });
1465
1466 // ssh only binds the forward after it connects, which can take ConnectTimeout per dead address.
1467 const waitForTunnelForward = Effect.fn("ssh/tunnel.waitForTunnelForward")(function* (input: {
1468 readonly target: DesktopSshEnvironmentTarget;
1469 readonly localPort: number;
1470 }) {
1471 const net = yield* NetService.NetService;
1472 yield* net.hasListenerOnHost(input.localPort, "127.0.0.1").pipe(
1473 Effect.repeat({
1474 schedule: Schedule.spaced(Duration.millis(SSH_TUNNEL_FORWARD_PROBE_INTERVAL_MS)),
1475 until: (listening) => listening,
1476 }),
1477 Effect.timeoutOrElse({
1478 duration: Duration.millis(SSH_TUNNEL_CONNECT_TIMEOUT_MS),
1479 orElse: () =>
1480 Effect.fail(
1481 new SshReadinessError({
1482 message: `SSH connection to ${input.target.alias} did not come up within ${SSH_TUNNEL_CONNECT_TIMEOUT_MS}ms.`,
1483 }),
1484 ),
1485 }),
1486 );
1487 });
1488
1489 const startSshTunnel = Effect.fn("ssh/tunnel.startSshTunnel")(function* (input: {
1490 readonly key: string;
1491 readonly resolvedTarget: DesktopSshEnvironmentTarget;
1492 readonly remotePort: number;
1493 readonly localPort: number;
1494 readonly httpBaseUrl: string;
1495 readonly wsBaseUrl: string;
1496 readonly authOptions: SshAuth.SshAuthOptions;
1497 /** Forward through this live ControlMaster instead of signing in again. */
1498 readonly controlPath: string | null;
1499 readonly remoteServerKind: "external" | "managed" | null;
1500 readonly scope: Scope.Closeable;
1501 }): Effect.fn.Return<
1502 SshTunnelEntry,
1503 SshCommandError | SshInvalidTargetError | SshReadinessError,
1504 | ChildProcessSpawner.ChildProcessSpawner
1505 | Digest.Digest
1506 | FileSystem.FileSystem
1507 | Path.Path
1508 | HttpClient.HttpClient
1509 | NetService.NetService
1510 > {
1511 const hostSpec = yield* buildSshHostSpecEffect(input.resolvedTarget);
1512 const childEnvironment = yield* SshAuth.buildSshChildEnvironment({
1513 ...(input.authOptions.authSecret === undefined
1514 ? {}
1515 : { authSecret: input.authOptions.authSecret }),
1516 ...(input.authOptions.interactiveAuth === undefined
1517 ? {}
1518 : { interactiveAuth: input.authOptions.interactiveAuth }),
1519 }).pipe(
1520 Effect.mapError(
1521 (cause) =>
1522 new SshCommandError({
1523 command: ["ssh"],
1524 exitCode: null,
1525 stderr: "",
1526 message: "Failed to prepare SSH authentication helpers.",
1527 cause,
1528 }),
1529 ),
1530 );
1531 const directArgs = [
1532 ...baseSshArgs(input.resolvedTarget, {
1533 batchMode: input.authOptions.batchMode ?? "no",
1534 }),
1535 "-o",
1536 "ExitOnForwardFailure=yes",
1537 "-o",
1538 "ControlMaster=no",
1539 "-o",
1540 "ControlPath=none",
1541 "-o",
1542 "ControlPersist=no",
1543 "-o",
1544 "ServerAliveInterval=15",
1545 "-o",
1546 "ServerAliveCountMax=3",
1547 "-n",
1548 "-N",
1549 "-L",
1550 `${input.localPort}:127.0.0.1:${input.remotePort}`,
1551 "--",
1552 hostSpec,
1553 ];
1554 const { controlPath, scope } = input;
1555 // The master's own GatewayPorts would otherwise decide the bind address.
1556 const controlMasterForward = `127.0.0.1:${input.localPort}:127.0.0.1:${input.remotePort}`;
1557 const { args, stdin } =
1558 controlPath === null
1559 ? { args: directArgs, stdin: { stream: Stream.empty, endOnDone: true } }
1560 : {
1561 // A mux client with -N exits once the master takes the forward, so hold a session
1562 // open on stdin to keep a process whose exit means the tunnel is gone.
1563 args: [
1564 ...baseSshArgs(input.resolvedTarget, { batchMode: "yes" }),
1565 ...controlMasterArgs(controlPath),
1566 "-o",
1567 "ExitOnForwardFailure=yes",
1568 "-o",
1569 "ControlMaster=no",
1570 "-T",
1571 "-L",
1572 controlMasterForward,
1573 "--",
1574 hostSpec,
1575 "sh -c 'exec cat >/dev/null'",
1576 ],
1577 stdin: "pipe" as const,
1578 };
1579 const sshCommand = yield* resolveSshCommand;
1580 const tunnelCommand = [sshCommand, ...args];
1581 const spawner = yield* ChildProcessSpawner.ChildProcessSpawner;
1582 if (controlPath !== null) {
1583 const fileSystemService = yield* FileSystem.FileSystem;
1584 const pathService = yield* Path.Path;
1585 // The master keeps a forward after the mux client that asked for it exits.
1586 yield* Scope.addFinalizer(
1587 scope,
1588 runSshCommand(input.resolvedTarget, {
1589 preHostArgs: [
1590 ...controlMasterArgs(controlPath),
1591 "-O",
1592 "cancel",
1593 "-L",
1594 controlMasterForward,
1595 ],
1596 }).pipe(
1597 Effect.provideService(ChildProcessSpawner.ChildProcessSpawner, spawner),
1598 Effect.provideService(FileSystem.FileSystem, fileSystemService),
1599 Effect.provideService(Path.Path, pathService),
1600 Effect.ignore,
1601 ),
1602 );
1603 }
1604 yield* Effect.logDebug("ssh.tunnel.spawn.start", {
1605 ...sshTargetLogFields(input.resolvedTarget),
1606 command: tunnelCommand,
1607 localPort: input.localPort,
1608 remotePort: input.remotePort,
1609 remoteServerKind: input.remoteServerKind,
1610 httpBaseUrl: input.httpBaseUrl,
1611 });
1612 const child = yield* spawner
1613 .spawn(
1614 ChildProcess.make(sshCommand, args, {
1615 env: childEnvironment,
1616 extendEnv: true,
1617 stdin,
1618 }),
1619 )
1620 .pipe(
1621 Effect.provideService(Scope.Scope, scope),
1622 Effect.mapError(
1623 (cause) =>
1624 new SshCommandError({
1625 command: tunnelCommand,
1626 exitCode: null,
1627 stderr: "",
1628 message:
1629 cause instanceof Error
1630 ? cause.message
1631 : `Failed to spawn SSH tunnel for ${input.resolvedTarget.alias}.`,
1632 cause,
1633 }),
1634 ),
1635 );
1636 yield* Effect.logDebug("ssh.tunnel.spawn.succeeded", {
1637 ...sshTargetLogFields(input.resolvedTarget),
1638 command: tunnelCommand,
1639 pid: child.pid,
1640 localPort: input.localPort,
1641 remotePort: input.remotePort,
1642 httpBaseUrl: input.httpBaseUrl,
1643 });
1644 const tunnelEntry: SshTunnelEntry = {
1645 key: input.key,
1646 target: input.resolvedTarget,
1647 remotePort: input.remotePort,
1648 remoteServerKind: input.remoteServerKind,
1649 localPort: input.localPort,
1650 httpBaseUrl: input.httpBaseUrl,
1651 wsBaseUrl: input.wsBaseUrl,
1652 process: child,
1653 scope,
1654 };
1655 // Readiness cancels the exit waiter, but diagnostics must drain until the tunnel closes.
1656 let stderrTail = "";
1657 const stderrDrain = yield* child.stderr.pipe(
1658 Stream.decodeText(),
1659 Stream.runForEach((chunk) =>
1660 Effect.sync(() => {
1661 stderrTail = (stderrTail + chunk).slice(-TUNNEL_STDERR_TAIL_CHARS);
1662 }),
1663 ),
1664 // Diagnostics are best-effort: a failed read must not hide the real exit.
1665 Effect.ignore,
1666 Effect.forkIn(scope),
1667 );
1668 const exitFailure = Effect.all(
1669 [Fiber.join(stderrDrain), child.exitCode.pipe(Effect.map(Number))],
1670 { concurrency: "unbounded" },
1671 ).pipe(
1672 Effect.mapError(
1673 (cause) =>
1674 new SshCommandError({
1675 command: tunnelCommand,
1676 exitCode: null,
1677 stderr: "",
1678 message:
1679 cause instanceof Error
1680 ? cause.message
1681 : `Failed to monitor SSH tunnel for ${input.resolvedTarget.alias}.`,
1682 cause,
1683 }),
1684 ),
1685 Effect.flatMap(([, exitCode]) => {
1686 const stderr = stderrTail;
1687 const error = new SshCommandError({
1688 command: tunnelCommand,
1689 exitCode,
1690 stderr,
1691 message: normalizeSshErrorMessage(
1692 stderr,
1693 `SSH tunnel exited unexpectedly for ${input.resolvedTarget.alias} (exit ${exitCode}).`,
1694 ),
1695 });
1696 return Effect.logWarning("ssh.tunnel.process.exited", {
1697 ...sshTargetLogFields(input.resolvedTarget),
1698 command: tunnelCommand,
1699 pid: child.pid,
1700 localPort: input.localPort,
1701 remotePort: input.remotePort,
1702 httpBaseUrl: input.httpBaseUrl,
1703 exitCode,
1704 stderr,
1705 }).pipe(Effect.andThen(Effect.fail(error)));
1706 }),
1707 );
1708 yield* Effect.raceFirst(
1709 waitForTunnelForward({ target: input.resolvedTarget, localPort: input.localPort }).pipe(
1710 Effect.andThen(
1711 waitForHttpReady({
1712 baseUrl: input.httpBaseUrl,
1713 timeoutMs: SSH_READY_TIMEOUT_MS,
1714 }),
1715 ),
1716 ),
1717 exitFailure,
1718 ).pipe(
1719 Effect.tap(() =>
1720 Effect.logInfo("ssh.tunnel.ready", {
1721 ...sshTargetLogFields(input.resolvedTarget),
1722 command: tunnelCommand,
1723 pid: child.pid,
1724 localPort: input.localPort,
1725 remotePort: input.remotePort,
1726 httpBaseUrl: input.httpBaseUrl,
1727 }),
1728 ),
1729 Effect.tapError((cause) =>
1730 Effect.gen(function* () {
1731 const net = yield* NetService.NetService;
1732 const processRunningExit = yield* Effect.exit(child.isRunning);
1733 const localPortAvailableExit = yield* Effect.exit(
1734 net.canListenOnHost(input.localPort, "127.0.0.1"),
1735 );
1736 const remoteLogTailExit = yield* Effect.exit(
1737 readRemoteServerLogTail(input.resolvedTarget, input.authOptions),
1738 );
1739 const processRunning = Exit.isSuccess(processRunningExit) ? processRunningExit.value : null;
1740 const localPortAvailable = Exit.isSuccess(localPortAvailableExit)
1741 ? localPortAvailableExit.value
1742 : null;
1743 const remoteLogTail = Exit.isSuccess(remoteLogTailExit)
1744 ? remoteLogTailExit.value || null
1745 : null;
1746 yield* Effect.logWarning("ssh.tunnel.ready.failed", {
1747 ...sshTargetLogFields(input.resolvedTarget),
1748 command: tunnelCommand,
1749 pid: child.pid,
1750 processRunning,
1751 ...(Exit.isSuccess(processRunningExit)
1752 ? {}
1753 : { processRunningError: processRunningExit.cause }),
1754 localPort: input.localPort,
1755 localPortListening: localPortAvailable === null ? null : !localPortAvailable,
1756 remotePort: input.remotePort,
1757 httpBaseUrl: input.httpBaseUrl,
1758 ...(Exit.isSuccess(localPortAvailableExit)
1759 ? {}
1760 : { localPortProbeError: localPortAvailableExit.cause }),
1761 ...(remoteLogTail === null ? {} : { remoteLogTail }),
1762 ...(Exit.isSuccess(remoteLogTailExit)
1763 ? {}
1764 : { remoteLogTailError: remoteLogTailExit.cause }),
1765 cause,
1766 });
1767 }),
1768 ),
1769 Effect.onExit((exit) =>
1770 Exit.isSuccess(exit)
1771 ? Effect.void
1772 : child
1773 .kill({
1774 killSignal: "SIGTERM",
1775 forceKillAfter: TUNNEL_SHUTDOWN_TIMEOUT_MS,
1776 })
1777 .pipe(Effect.ignore),
1778 ),
1779 );
1780 return tunnelEntry;
1781 });
1782
1783 const makeSshEnvironmentManager = Effect.fn("ssh/tunnel.SshEnvironmentManager.make")(function* (
1784 options: SshEnvironmentManagerOptions = {},
1785 ): Effect.fn.Return<SshEnvironmentManagerShape, never, Scope.Scope> {
1786 const managerScope = yield* Scope.Scope;
1787 const tunnels = new Map<string, SshTunnelEntry>();
1788 const targetLocks = yield* KeyedLock.make<string>();
1789 const authSecrets = new Map<string, string>();
1790
1791 // Keep one lock per target so reconnect cannot reuse a server while stop is pending.
1792 const withTargetLock = Effect.fn("ssh/tunnel.withTargetLock")(function* <A, E, R>(
1793 key: string,
1794 effect: Effect.Effect<A, E, R>,
1795 ): Effect.fn.Return<A, E, R> {
1796 return yield* targetLocks.withLock(key, effect);
1797 });
1798
1799 const closeTunnelEntry = Effect.fn("ssh/tunnel.closeTunnelEntry")(function* (
1800 entry: SshTunnelEntry,
1801 ) {
1802 yield* Effect.logDebug("ssh.tunnel.close.start", {
1803 ...sshTargetLogFields(entry.target),
1804 key: entry.key,
1805 localPort: entry.localPort,
1806 remotePort: entry.remotePort,
1807 });
1808 yield* Scope.close(entry.scope, Exit.void).pipe(Effect.ignore);
1809 yield* Effect.logInfo("ssh.tunnel.close.succeeded", {
1810 ...sshTargetLogFields(entry.target),
1811 key: entry.key,
1812 localPort: entry.localPort,
1813 remotePort: entry.remotePort,
1814 });
1815 });
1816
1817 const closeAllTunnels = Effect.sync(() => [...tunnels.values()]).pipe(
1818 Effect.flatMap((entries) =>
1819 Effect.forEach(entries, closeTunnelEntry, { concurrency: "unbounded" }),
1820 ),
1821 Effect.ignore,
1822 );
1823
1824 yield* Scope.addFinalizer(managerScope, closeAllTunnels);
1825
1826 const promptForPassword = Effect.fn("ssh/tunnel.promptForPassword")(function* (
1827 target: DesktopSshEnvironmentTarget,
1828 attempt: number,
1829 ): Effect.fn.Return<
1830 string,
1831 SshInvalidTargetError | SshPasswordPromptError,
1832 SshAuth.SshPasswordPrompt
1833 > {
1834 const promptService = yield* SshAuth.SshPasswordPrompt;
1835 const hostSpec = yield* buildSshHostSpecEffect(target);
1836 if (!promptService.isAvailable) {
1837 yield* Effect.logWarning("ssh.auth.passwordPrompt.unavailable", {
1838 ...sshTargetLogFields(target),
1839 attempt,
1840 });
1841 return yield* new SshPasswordPromptError({
1842 message: `SSH authentication failed for ${hostSpec}.`,
1843 });
1844 }
1845
1846 yield* Effect.logInfo("ssh.auth.passwordPrompt.request", {
1847 ...sshTargetLogFields(target),
1848 attempt,
1849 });
1850 const password = yield* promptService.request({
1851 attempt,
1852 destination: target.alias.trim() || target.hostname.trim(),
1853 username: target.username,
1854 prompt: `Enter the SSH password or verification code for ${hostSpec}.`,
1855 });
1856 if (password === null) {
1857 yield* Effect.logWarning("ssh.auth.passwordPrompt.cancelled", {
1858 ...sshTargetLogFields(target),
1859 attempt,
1860 });
1861 return yield* new SshPasswordPromptError({
1862 message: `SSH authentication cancelled for ${hostSpec}.`,
1863 });
1864 }
1865 yield* Effect.logInfo("ssh.auth.passwordPrompt.received", {
1866 ...sshTargetLogFields(target),
1867 attempt,
1868 });
1869 return password;
1870 });
1871
1872 const handleSshAuthFailure = Effect.fn("ssh/tunnel.runWithSshAuthAttempt.handleFailure")(
1873 function* <T>(
1874 input: SshAuthAttemptInput<T> & {
1875 readonly error: SshEnvironmentEffectError;
1876 },
1877 ): Effect.fn.Return<T, SshEnvironmentEffectError, SshEnvironmentEffectContext> {
1878 if (!SshAuth.isSshAuthFailure(input.error)) {
1879 return yield* input.error;
1880 }
1881
1882 yield* Effect.logWarning("ssh.auth.failed", {
1883 ...sshTargetLogFields(input.target),
1884 key: input.key,
1885 promptCount: input.promptCount,
1886 cause: input.error,
1887 });
1888 const promptService = yield* SshAuth.SshPasswordPrompt;
1889 if (!promptService.isAvailable) {
1890 return yield* input.error;
1891 }
1892 if (input.authSecret !== null) {
1893 authSecrets.delete(input.key);
1894 }
1895 if (input.promptCount >= 2 || SshAuth.isSshAgentSigningFailure(input.error)) {
1896 return yield* input.error;
1897 }
1898
1899 const nextPromptCount = input.promptCount + 1;
1900 const nextAuthSecret = yield* promptForPassword(input.target, nextPromptCount);
1901 authSecrets.set(input.key, nextAuthSecret);
1902 return yield* runWithSshAuthAttempt({
1903 ...input,
1904 promptCount: nextPromptCount,
1905 authSecret: nextAuthSecret,
1906 });
1907 },
1908 );
1909
1910 const runWithSshAuthAttempt = Effect.fn("ssh/tunnel.runWithSshAuthAttempt")(function* <T>(
1911 input: SshAuthAttemptInput<T>,
1912 ): Effect.fn.Return<T, SshEnvironmentEffectError, SshEnvironmentEffectContext> {
1913 const promptService = yield* SshAuth.SshPasswordPrompt;
1914 const authOptions =
1915 input.authSecret === null
1916 ? {
1917 batchMode: promptService.isAvailable ? ("yes" as const) : ("no" as const),
1918 interactiveAuth: !promptService.isAvailable,
1919 }
1920 : {
1921 authSecret: input.authSecret,
1922 batchMode: "no" as const,
1923 interactiveAuth: true,
1924 };
1925
1926 return yield* input
1927 .operation(authOptions)
1928 .pipe(Effect.catch((error) => handleSshAuthFailure({ ...input, error })));
1929 });
1930
1931 const runWithSshAuth = Effect.fn("ssh/tunnel.runWithSshAuth")(function* <T>(
1932 input: SshAuthOperationInput<T>,
1933 ): Effect.fn.Return<T, SshEnvironmentEffectError, SshEnvironmentEffectContext> {
1934 return yield* runWithSshAuthAttempt({
1935 ...input,
1936 promptCount: 0,
1937 authSecret: authSecrets.get(input.key) ?? null,
1938 });
1939 });
1940
1941 const createTunnelEntry = Effect.fn("ssh/tunnel.ensureTunnelEntry.create")(function* (input: {
1942 readonly key: string;
1943 readonly resolvedTarget: DesktopSshEnvironmentTarget;
1944 readonly runner?: RemoteT3RunnerOptions;
1945 }): Effect.fn.Return<SshTunnelEntry, SshEnvironmentEffectError, SshEnvironmentEffectContext> {
1946 yield* Effect.logDebug("ssh.environment.tunnel.create.start", {
1947 ...sshTargetLogFields(input.resolvedTarget),
1948 ...sshRunnerLogFields(input.runner),
1949 key: input.key,
1950 });
1951 const launcherId =
1952 options.resolveLauncherId === undefined ? null : yield* options.resolveLauncherId;
1953 const remoteLaunch = yield* runWithSshAuth({
1954 key: input.key,
1955 target: input.resolvedTarget,
1956 operation: (authOptions) =>
1957 launchOrReuseRemoteServer(input.resolvedTarget, authOptions, input.runner, launcherId),
1958 });
1959 const remotePort = remoteLaunch.remotePort;
1960 yield* Effect.logDebug("ssh.environment.remotePort.ready", {
1961 ...sshTargetLogFields(input.resolvedTarget),
1962 key: input.key,
1963 remotePort,
1964 remoteServerKind: remoteLaunch.remoteServerKind,
1965 });
1966 const localPort = yield* reserveLocalTunnelPort();
1967 const httpBaseUrl = `http://127.0.0.1:${localPort}/`;
1968 const wsBaseUrl = `ws://127.0.0.1:${localPort}/`;
1969 yield* Effect.logDebug("ssh.environment.localPort.reserved", {
1970 ...sshTargetLogFields(input.resolvedTarget),
1971 key: input.key,
1972 localPort,
1973 remotePort,
1974 });
1975 const entryScope = yield* Scope.make("sequential");
1976 const tunnelEntry = yield* runWithSshAuth({
1977 key: input.key,
1978 target: input.resolvedTarget,
1979 operation: (authOptions) => {
1980 const start = (controlPath: string | null) =>
1981 startSshTunnel({
1982 key: input.key,
1983 resolvedTarget: input.resolvedTarget,
1984 remotePort,
1985 localPort,
1986 httpBaseUrl,
1987 wsBaseUrl,
1988 authOptions,
1989 controlPath,
1990 remoteServerKind: remoteLaunch.remoteServerKind,
1991 scope: entryScope,
1992 });
1993 // A fresh login cannot answer a second factor such as Duo, but the user's signed-in
1994 // ControlMaster already has. Without one, the auth failure goes on to the password prompt.
1995 return start(null).pipe(
1996 Effect.catchIf(SshAuth.isSshAuthFailure, (error) =>
1997 findLiveControlPath(input.resolvedTarget).pipe(
1998 Effect.flatMap((controlPath) =>
1999 controlPath === null ? Effect.fail(error) : start(controlPath),
2000 ),
2001 ),
2002 ),
2003 );
2004 },
2005 }).pipe(
2006 Effect.onExit((exit) =>
2007 Exit.isSuccess(exit) ? Effect.void : Scope.close(entryScope, Exit.void).pipe(Effect.ignore),
2008 ),
2009 );
2010 tunnels.set(input.key, tunnelEntry);
2011 const spawnerService = yield* ChildProcessSpawner.ChildProcessSpawner;
2012 const digestService = yield* Digest.Digest;
2013 const fileSystemService = yield* FileSystem.FileSystem;
2014 const pathService = yield* Path.Path;
2015 yield* Scope.addFinalizer(
2016 entryScope,
2017 Effect.gen(function* () {
2018 const stopRemote = tunnels.get(tunnelEntry.key) === tunnelEntry;
2019 if (stopRemote) {
2020 tunnels.delete(tunnelEntry.key);
2021 }
2022 yield* tunnelEntry.process
2023 .kill({
2024 killSignal: "SIGTERM",
2025 forceKillAfter: TUNNEL_SHUTDOWN_TIMEOUT_MS,
2026 })
2027 .pipe(Effect.ignore);
2028 if (!stopRemote) {
2029 return;
2030 }
2031 yield* Effect.logDebug("ssh.environment.tunnel.finalizer.start", {
2032 ...sshTargetLogFields(tunnelEntry.target),
2033 key: tunnelEntry.key,
2034 localPort: tunnelEntry.localPort,
2035 remotePort: tunnelEntry.remotePort,
2036 });
2037 const authSecret = authSecrets.get(tunnelEntry.key) ?? null;
2038 yield* stopRemoteServer(
2039 tunnelEntry.target,
2040 authSecret === null
2041 ? {
2042 batchMode: "yes",
2043 interactiveAuth: false,
2044 }
2045 : {
2046 authSecret,
2047 batchMode: "no",
2048 interactiveAuth: true,
2049 },
2050 ).pipe(
2051 Effect.provideService(ChildProcessSpawner.ChildProcessSpawner, spawnerService),
2052 Effect.provideService(Digest.Digest, digestService),
2053 Effect.provideService(FileSystem.FileSystem, fileSystemService),
2054 Effect.provideService(Path.Path, pathService),
2055 );
2056 yield* Effect.logDebug("ssh.environment.tunnel.finalizer.succeeded", {
2057 ...sshTargetLogFields(tunnelEntry.target),
2058 key: tunnelEntry.key,
2059 localPort: tunnelEntry.localPort,
2060 remotePort: tunnelEntry.remotePort,
2061 });
2062 }).pipe(Effect.ignore),
2063 );
2064 yield* Effect.logDebug("ssh.environment.tunnel.create.succeeded", {
2065 ...sshTargetLogFields(input.resolvedTarget),
2066 key: input.key,
2067 localPort,
2068 remotePort,
2069 });
2070 return tunnelEntry;
2071 });
2072
2073 const ensureTunnelEntry = Effect.fn("ssh/tunnel.ensureTunnelEntry")(function* (
2074 key: string,
2075 resolvedTarget: DesktopSshEnvironmentTarget,
2076 runner?: RemoteT3RunnerOptions,
2077 ): Effect.fn.Return<SshTunnelEntry, SshEnvironmentEffectError, SshEnvironmentEffectContext> {
2078 const entry = tunnels.get(key) ?? null;
2079
2080 if (entry !== null) {
2081 yield* Effect.logDebug("ssh.environment.tunnel.existing.check", {
2082 ...sshTargetLogFields(resolvedTarget),
2083 key,
2084 localPort: entry.localPort,
2085 remotePort: entry.remotePort,
2086 });
2087 let answered = false;
2088 const markAnswered = Effect.sync(() => {
2089 answered = true;
2090 });
2091 const client = yield* HttpClient.HttpClient;
2092 const readinessExit = yield* Effect.exit(
2093 waitForHttpReady({
2094 baseUrl: entry.httpBaseUrl,
2095 timeoutMs: SSH_TUNNEL_READY_PROBE_TIMEOUT_MS,
2096 }).pipe(
2097 Effect.provideService(
2098 HttpClient.HttpClient,
2099 client.pipe(
2100 HttpClient.transform((request) =>
2101 request.pipe(
2102 Effect.tap(() => markAnswered),
2103 Effect.tapError(() => markAnswered),
2104 ),
2105 ),
2106 ),
2107 ),
2108 ),
2109 );
2110 // ssh closes the channel when nothing listens remotely, so a forward that
2111 // is up but got no answer leads to a live server that is only slow.
2112 const keepSlowForward =
2113 Exit.isFailure(readinessExit) &&
2114 !answered &&
2115 (yield* entry.process.isRunning.pipe(Effect.orElseSucceed(() => false))) &&
2116 (yield* (yield* NetService.NetService).hasListenerOnHost(entry.localPort, "127.0.0.1"));
2117 if (keepSlowForward) {
2118 yield* Effect.logWarning("ssh.environment.tunnel.existing.slow", {
2119 ...sshTargetLogFields(resolvedTarget),
2120 key,
2121 localPort: entry.localPort,
2122 remotePort: entry.remotePort,
2123 });
2124 return entry;
2125 }
2126 if (Exit.isSuccess(readinessExit)) {
2127 yield* Effect.logDebug("ssh.environment.tunnel.reused", {
2128 ...sshTargetLogFields(resolvedTarget),
2129 key,
2130 localPort: entry.localPort,
2131 remotePort: entry.remotePort,
2132 });
2133 return entry;
2134 }
2135 yield* Effect.logWarning("ssh.environment.tunnel.existing.stale", {
2136 ...sshTargetLogFields(resolvedTarget),
2137 key,
2138 localPort: entry.localPort,
2139 remotePort: entry.remotePort,
2140 cause: readinessExit.cause,
2141 });
2142 // Closing a stale forward must not run the manager-owned remote stop.
2143 tunnels.delete(key);
2144 yield* closeTunnelEntry(entry);
2145 }
2146
2147 return yield* createTunnelEntry({
2148 key,
2149 resolvedTarget,
2150 ...(runner === undefined ? {} : { runner }),
2151 }).pipe(
2152 Effect.tapError((cause) =>
2153 Effect.logWarning("ssh.environment.tunnel.create.failed", {
2154 ...sshTargetLogFields(resolvedTarget),
2155 key,
2156 cause,
2157 }),
2158 ),
2159 );
2160 });
2161
2162 const ensureEnvironment = Effect.fn("ssh/tunnel.ensureEnvironment")(function* (
2163 target: DesktopSshEnvironmentTarget,
2164 requestOptions?: { readonly issuePairingToken?: boolean },
2165 ): Effect.fn.Return<
2166 DesktopSshEnvironmentBootstrap,
2167 SshEnvironmentEffectError,
2168 SshEnvironmentEffectContext
2169 > {
2170 yield* Effect.logInfo("ssh.environment.ensure.start", {
2171 ...sshTargetLogFields(target),
2172 issuePairingToken: requestOptions?.issuePairingToken === true,
2173 });
2174 const baseResolved = yield* resolveSshTarget(target.alias || target.hostname);
2175 const resolvedTarget: DesktopSshEnvironmentTarget = {
2176 ...baseResolved,
2177 ...(target.username !== null ? { username: target.username } : {}),
2178 ...(target.port !== null ? { port: target.port } : {}),
2179 };
2180 const key = targetConnectionKey(resolvedTarget);
2181 yield* Effect.logDebug("ssh.environment.target.resolved", {
2182 ...sshTargetLogFields(resolvedTarget),
2183 key,
2184 });
2185 const runner =
2186 options.resolveCliRunner === undefined ? undefined : yield* options.resolveCliRunner;
2187 yield* Effect.logDebug("ssh.environment.runner.resolved", {
2188 ...sshTargetLogFields(resolvedTarget),
2189 ...sshRunnerLogFields(runner),
2190 key,
2191 });
2192 return yield* withTargetLock(
2193 key,
2194 Effect.gen(function* () {
2195 const entry = yield* ensureTunnelEntry(key, resolvedTarget, runner);
2196
2197 const pairingResult = requestOptions?.issuePairingToken
2198 ? yield* runWithSshAuth({
2199 key,
2200 target: entry.target,
2201 operation: (authOptions) =>
2202 issueRemotePairingToken(entry.target, authOptions, runner),
2203 })
2204 : null;
2205 const pairingToken = pairingResult?.credential ?? null;
2206
2207 yield* Effect.logInfo("ssh.environment.ensure.succeeded", {
2208 ...sshTargetLogFields(entry.target),
2209 key,
2210 localPort: entry.localPort,
2211 remotePort: entry.remotePort,
2212 remoteServerKind: entry.remoteServerKind,
2213 issuedPairingToken: pairingToken !== null,
2214 });
2215 return {
2216 // Callers save this target; a port ssh -G resolved would pin the alias's current Port.
2217 target: { ...entry.target, port: target.port },
2218 httpBaseUrl: entry.httpBaseUrl,
2219 wsBaseUrl: entry.wsBaseUrl,
2220 pairingToken,
2221 remotePort: entry.remotePort,
2222 ...(entry.remoteServerKind ? { remoteServerKind: entry.remoteServerKind } : {}),
2223 };
2224 }),
2225 );
2226 });
2227
2228 const disconnectEnvironment = Effect.fn("ssh/tunnel.disconnectEnvironment")(function* (
2229 target: DesktopSshEnvironmentTarget,
2230 ): Effect.fn.Return<void, SshEnvironmentEffectError, SshEnvironmentEffectContext> {
2231 yield* Effect.logInfo("ssh.environment.disconnect.start", sshTargetLogFields(target));
2232 const baseResolved = yield* resolveSshTarget(target.alias || target.hostname);
2233 const resolvedTarget: DesktopSshEnvironmentTarget = {
2234 ...baseResolved,
2235 ...(target.username !== null ? { username: target.username } : {}),
2236 ...(target.port !== null ? { port: target.port } : {}),
2237 };
2238 const key = targetConnectionKey(resolvedTarget);
2239 yield* withTargetLock(
2240 key,
2241 Effect.gen(function* () {
2242 const entry = tunnels.get(key) ?? null;
2243 yield* Effect.logDebug("ssh.environment.disconnect.targetResolved", {
2244 ...sshTargetLogFields(resolvedTarget),
2245 key,
2246 hasTunnel: entry !== null,
2247 });
2248 if (entry !== null) {
2249 // Explicit disconnect owns the remote stop so its failure reaches the caller.
2250 yield* Effect.gen(function* () {
2251 tunnels.delete(key);
2252 yield* closeTunnelEntry(entry);
2253 }).pipe(Effect.uninterruptible);
2254 }
2255 yield* runWithSshAuth({
2256 key,
2257 target: resolvedTarget,
2258 operation: (authOptions) => stopRemoteServer(resolvedTarget, authOptions),
2259 });
2260 yield* Effect.logInfo("ssh.environment.disconnect.succeeded", {
2261 ...sshTargetLogFields(resolvedTarget),
2262 key,
2263 });
2264 }),
2265 );
2266 });
2267
2268 return SshEnvironmentManager.of({ ensureEnvironment, disconnectEnvironment, closeAllTunnels });
2269 });
2270
2271 /**
2272 * @effect-expect-leaking ChildProcessSpawner | Digest | FileSystem | HttpClient | NetService | Path | SshPasswordPrompt
2273 */
2274 export class SshEnvironmentManager extends Context.Service<
2275 SshEnvironmentManager,
2276 SshEnvironmentManagerShape
2277 >()("@t3tools/ssh/tunnel/SshEnvironmentManager") {
2278 static readonly layer = (options: SshEnvironmentManagerOptions = {}) =>
2279 Layer.effect(SshEnvironmentManager, makeSshEnvironmentManager(options));
2280 }