Skip to content
Source
Sign inSign up
pingdotgg/t3code
Open History to see the last commit

command.ts

437 lines14.2 KB

1import type { DesktopSshEnvironmentTarget } from "@t3tools/contracts";
2import * as HostProcess from "@t3tools/shared/HostProcess";
3import * as Digest from "effect/crypto/Digest";
4import * as Duration from "effect/Duration";
5import * as Effect from "effect/Effect";
6import * as Hex from "effect/encoding/Hex";
7import * as FileSystem from "effect/FileSystem";
8import * as Option from "effect/Option";
9import * as Path from "effect/Path";
10import * as Scope from "effect/Scope";
11import * as Stream from "effect/Stream";
12import { ChildProcess, ChildProcessSpawner } from "effect/process";
13​
14import { buildSshChildEnvironment, type SshAuthOptions } from "./auth.ts";
15import { SshCommandError, SshInvalidTargetError } from "./errors.ts";
16​
17const DEFAULT_SSH_COMMAND_TIMEOUT_MS = 60_000;
18const MAX_SSH_ERROR_OUTPUT_LENGTH = 4_000;
19​
20/**
21 * ssh is a real executable everywhere (`ssh.exe` on Windows), so it is always
22 * spawned directly — cmd.exe shell mode would re-tokenize arguments such as
23 * identity-file paths containing spaces.
24 */
25const sshCommandForPlatform = (platform: NodeJS.Platform): string =>
26 platform === "win32" ? "ssh.exe" : "ssh";
27​
28export const resolveSshCommand = Effect.map(HostProcess.Platform, sshCommandForPlatform);
29​
30const encoder = new TextEncoder();
31​
32export interface SshCommandResult {
33 readonly stdout: string;
34 readonly stderr: string;
36​
37export interface RunSshCommandOptions extends SshAuthOptions {
38 readonly preHostArgs?: ReadonlyArray<string>;
39 readonly remoteCommandArgs?: ReadonlyArray<string>;
40 readonly stdin?: string;
41 readonly timeoutMs?: number;
42 /** Keep the end of long failed-command stdout, where the final cause usually is. */
43 readonly keepErrorOutputTail?: boolean;
45​
46export function parseSshResolveOutput(alias: string, stdout: string): DesktopSshEnvironmentTarget {
47 const values = new Map<string, string>();
48 for (const line of stdout.split(/\r?\n/u)) {
49 const trimmed = line.trim();
50 if (trimmed.length === 0) {
51 continue;
52 }
53 const [key, ...rest] = trimmed.split(/\s+/u);
54 if (!key || rest.length === 0 || values.has(key)) {
55 continue;
56 }
57 values.set(key, rest.join(" ").trim());
58 }
59​
60 const hostname = values.get("hostname")?.trim() || alias;
61 const username = values.get("user")?.trim() || null;
62 const rawPort = values.get("port")?.trim() ?? "";
63 const parsedPort = Number.parseInt(rawPort, 10);
64​
65 return {
66 alias,
67 hostname,
68 username,
69 port: Number.isInteger(parsedPort) ? parsedPort : null,
70 };
72​
73export function targetConnectionKey(target: DesktopSshEnvironmentTarget): string {
74 return `${target.alias}\u0000${target.hostname}\u0000${target.username ?? ""}\u0000${target.port ?? ""}`;
76​
77export const remoteStateKey = Effect.fn("ssh/command.remoteStateKey")(function* (
78 target: DesktopSshEnvironmentTarget,
79): Effect.fn.Return<string, never, Digest.Digest> {
80 const digestService = yield* Digest.Digest;
81 const digest = yield* digestService.digest(
82 Digest.sha256,
83 encoder.encode(targetConnectionKey(target)),
84 );
85 return Hex.encode(digest).slice(0, 16);
86});
87​
88function buildSshHostSpec(target: DesktopSshEnvironmentTarget): string {
89 const destination = target.alias.trim() || target.hostname.trim();
90 if (destination.length === 0) {
91 throw new Error("SSH target is missing its alias/hostname.");
92 }
93 return target.username ? `${target.username}@${destination}` : destination;
95​
96export const buildSshHostSpecEffect = (
97 target: DesktopSshEnvironmentTarget,
98): Effect.Effect<string, SshInvalidTargetError> =>
99 Effect.try({
100 try: () => buildSshHostSpec(target),
101 catch: (cause) =>
102 new SshInvalidTargetError({
103 message: cause instanceof Error ? cause.message : "SSH target is invalid.",
104 }),
105 });
106​
107export function baseSshArgs(
108 target: DesktopSshEnvironmentTarget,
109 input?: { readonly batchMode?: "yes" | "no" },
110): string[] {
111 return [
112 "-o",
113 `BatchMode=${input?.batchMode ?? "no"}`,
114 "-o",
115 "ConnectTimeout=10",
116 ...(target.port !== null ? ["-p", String(target.port)] : []),
117 ];
119​
120export function getLastNonEmptyOutputLine(stdout: string): string | null {
121 return (
122 stdout
123 .trim()
124 .split(/\r?\n/u)
125 .map((entry) => entry.trim())
126 .findLast((entry) => entry.length > 0) ?? null
127 );
129​
130interface SshCommandOutput {
131 stdout: string;
132 stderr: string;
134​
135// Appends as chunks arrive so a timeout can still report what was printed.
136const collectProcessOutput = <E>(
137 stream: Stream.Stream<Uint8Array, E>,
138 output: SshCommandOutput,
139 key: keyof SshCommandOutput,
140): Effect.Effect<void, E> =>
141 stream.pipe(
142 Stream.decodeText(),
143 Stream.runForEach((chunk) =>
144 Effect.sync(() => {
145 output[key] += chunk;
146 }),
147 ),
148 );
149​
150function redactSshErrorOutput(output: string, keepTail = false): string {
151 const redacted = output.replace(
152 /("(?:access_token|bearerToken|credential|pairingToken|token)"\s*:\s*")[^"]+(")/giu,
153 "$1[redacted]$2",
154 );
155 if (redacted.length <= MAX_SSH_ERROR_OUTPUT_LENGTH) return redacted;
156 return keepTail
157 ? `[truncated]\n${redacted.slice(-MAX_SSH_ERROR_OUTPUT_LENGTH)}`
158 : `${redacted.slice(0, MAX_SSH_ERROR_OUTPUT_LENGTH)}\n[truncated]`;
160​
161export function shellSingleQuote(value: string): string {
162 return `'${value.replaceAll("'", "'\\''")}'`;
164​
165const SAFE_SHELL_ARG = /^[\w@%+=:,./-]+$/u;
166​
167// cmd.exe and PowerShell both read double quotes, but not POSIX single quotes.
168function terminalArg(value: string, platform: NodeJS.Platform): string {
169 if (SAFE_SHELL_ARG.test(value)) return value;
170 return platform === "win32" ? `"${value}"` : shellSingleQuote(value);
172​
173// ssh prints this for unknown and changed host keys when it cannot ask. The app
174// never answers host key prompts, so the user checks the fingerprint in a terminal.
175function hostKeyVerificationMessage(input: {
176 readonly target: DesktopSshEnvironmentTarget;
177 readonly hostSpec: string;
178 readonly stderr: string;
179 readonly platform: NodeJS.Platform;
180}): string | null {
181 const { target, hostSpec, stderr, platform } = input;
182 if (!stderr.includes("Host key verification failed")) return null;
183 const command = [
184 "ssh",
185 ...(target.port !== null ? ["-p", String(target.port)] : []),
186 ...(hostSpec.startsWith("-") ? ["--"] : []),
187 hostSpec,
188 ]
189 .map((arg) => terminalArg(arg, platform))
190 .join(" ");
191 const guidance = stderr.includes("REMOTE HOST IDENTIFICATION HAS CHANGED")
192 ? `The host key for ${hostSpec} has changed since it was trusted. Only if you expect this change, remove the old key with the \`ssh-keygen -R\` command in the output below, run \`${command}\` in a terminal, check that the new host key fingerprint is the one you expect, then try again.`
193 : `SSH could not verify the host key for ${hostSpec}. Run \`${command}\` in a terminal, check that the host key fingerprint is the one you expect, then try again.`;
194 return `${guidance}\n\n${stderr.trim()}`;
196​
197function normalizeSshErrorMessage(input: {
198 readonly stdout?: string;
199 readonly stderr: string;
200 readonly fallbackMessage: string;
201}): string {
202 const cleanedStderr = input.stderr.trim();
203 if (cleanedStderr.length > 0) {
204 return cleanedStderr;
205 }
206​
207 const cleanedStdout = input.stdout?.trim() ?? "";
208 return cleanedStdout.length > 0 ? cleanedStdout : input.fallbackMessage;
210​
211export function sshTargetLogFields(target: DesktopSshEnvironmentTarget) {
212 return {
213 alias: target.alias,
214 hostname: target.hostname,
215 username: target.username,
216 port: target.port,
217 };
219​
220function stdinStream(input: string | undefined) {
221 return input === undefined ? Stream.empty : Stream.make(encoder.encode(input));
223​
224const runSshCommandInScope = Effect.fn("ssh/command.runSshCommand.inScope")(function* (
225 target: DesktopSshEnvironmentTarget,
226 input: RunSshCommandOptions,
227 commandScope: Scope.Scope,
228 output: SshCommandOutput,
229): Effect.fn.Return<
230 SshCommandResult,
231 SshCommandError | SshInvalidTargetError,
232 ChildProcessSpawner.ChildProcessSpawner | FileSystem.FileSystem | Path.Path
233> {
234 const hostSpec = yield* buildSshHostSpecEffect(target);
235 const environment = yield* buildSshChildEnvironment({
236 ...(input.interactiveAuth === undefined ? {} : { interactiveAuth: input.interactiveAuth }),
237 ...(input.authSecret === undefined ? {} : { authSecret: input.authSecret }),
238 }).pipe(
239 Effect.mapError(
240 (cause) =>
241 new SshCommandError({
242 command: ["ssh"],
243 exitCode: null,
244 stderr: "",
245 message: "Failed to prepare SSH authentication helpers.",
246 cause,
247 }),
248 ),
249 );
250 const args = [
251 ...baseSshArgs(target, {
252 batchMode: input.batchMode ?? (input.interactiveAuth ? "no" : "yes"),
253 }),
254 // Cold installs need a long command budget; a lost connection must fail sooner.
255 "-o",
256 "ServerAliveInterval=15",
257 "-o",
258 "ServerAliveCountMax=3",
259 ...(input.preHostArgs ?? []),
260 "--",
261 hostSpec,
262 ...(input.remoteCommandArgs ?? []),
263 ];
264 const spawner = yield* ChildProcessSpawner.ChildProcessSpawner;
265 const sshCommand = yield* resolveSshCommand;
266 yield* Effect.logDebug("ssh.command.start", {
267 ...sshTargetLogFields(target),
268 command: [sshCommand, ...args],
269 hasStdin: input.stdin !== undefined,
270 timeoutMs: input.timeoutMs ?? DEFAULT_SSH_COMMAND_TIMEOUT_MS,
271 });
272 const child = yield* spawner
273 .spawn(
274 ChildProcess.make(sshCommand, args, {
275 env: environment,
276 extendEnv: true,
277 stdin: {
278 stream: stdinStream(input.stdin),
279 endOnDone: true,
280 },
281 }),
282 )
283 .pipe(
284 Effect.provideService(Scope.Scope, commandScope),
285 Effect.mapError(
286 (cause) =>
287 new SshCommandError({
288 command: [sshCommand, ...args],
289 exitCode: null,
290 stderr: "",
291 message:
292 cause instanceof Error
293 ? cause.message
294 : `Failed to spawn SSH command for ${hostSpec}.`,
295 cause,
296 }),
297 ),
298 );
299​
300 const [, , exitCode] = yield* Effect.all(
301 [
302 collectProcessOutput(child.stdout, output, "stdout"),
303 collectProcessOutput(child.stderr, output, "stderr"),
304 child.exitCode.pipe(Effect.map(Number)),
305 ],
306 { concurrency: "unbounded" },
307 ).pipe(
308 Effect.mapError(
309 (cause) =>
310 new SshCommandError({
311 command: ["ssh", ...args],
312 exitCode: null,
313 stderr: "",
314 message:
315 cause instanceof Error ? cause.message : `Failed to run SSH command for ${hostSpec}.`,
316 cause,
317 }),
318 ),
319 );
320​
321 const { stdout, stderr } = output;
322 if (exitCode !== 0) {
323 const diagnosticStdout = redactSshErrorOutput(stdout, input.keepErrorOutputTail);
324 yield* Effect.logWarning("ssh.command.failed", {
325 ...sshTargetLogFields(target),
326 command: ["ssh", ...args],
327 exitCode,
328 stdout: diagnosticStdout,
329 stderr,
330 });
331 return yield* new SshCommandError({
332 command: ["ssh", ...args],
333 exitCode,
334 stdout: diagnosticStdout,
335 stderr,
336 message:
337 hostKeyVerificationMessage({
338 target,
339 hostSpec,
340 stderr,
341 platform: yield* HostProcess.Platform,
342 }) ??
343 normalizeSshErrorMessage({
344 stdout: diagnosticStdout,
345 stderr,
346 fallbackMessage: `SSH command failed for ${hostSpec} (exit ${exitCode}).`,
347 }),
348 });
349 }
350​
351 yield* Effect.logDebug("ssh.command.succeeded", {
352 ...sshTargetLogFields(target),
353 command: ["ssh", ...args],
354 });
355 return { stdout, stderr };
356});
357​
358export const runSshCommand = Effect.fn("ssh/command.runSshCommand")(function* (
359 target: DesktopSshEnvironmentTarget,
360 input: RunSshCommandOptions = {},
361): Effect.fn.Return<
362 SshCommandResult,
363 SshCommandError | SshInvalidTargetError,
364 ChildProcessSpawner.ChildProcessSpawner | FileSystem.FileSystem | Path.Path
365> {
366 const output: SshCommandOutput = { stdout: "", stderr: "" };
367 return yield* Effect.scopedWith((commandScope) =>
368 runSshCommandInScope(target, input, commandScope, output),
369 ).pipe(
370 Effect.timeoutOption(Duration.millis(input.timeoutMs ?? DEFAULT_SSH_COMMAND_TIMEOUT_MS)),
371 Effect.flatMap((result) =>
372 Option.match(result, {
373 onSome: Effect.succeed,
374 // The enclosing operation owns the span for this inline handler.
375 onNone: Effect.fnUntraced(function* () {
376 const diagnosticStdout = redactSshErrorOutput(output.stdout, input.keepErrorOutputTail);
377 const { stderr } = output;
378 yield* Effect.logWarning("ssh.command.timedOut", {
379 ...sshTargetLogFields(target),
380 timeoutMs: input.timeoutMs ?? DEFAULT_SSH_COMMAND_TIMEOUT_MS,
381 remoteCommandArgs: input.remoteCommandArgs ?? [],
382 preHostArgs: input.preHostArgs ?? [],
383 hasStdin: input.stdin !== undefined,
384 stdout: diagnosticStdout,
385 stderr,
386 });
387 return yield* new SshCommandError({
388 command: ["ssh"],
389 exitCode: null,
390 stdout: diagnosticStdout,
391 stderr,
392 message: `SSH command timed out after ${input.timeoutMs ?? DEFAULT_SSH_COMMAND_TIMEOUT_MS}ms.`,
393 });
394 }),
395 }),
396 ),
397 );
398});
399​
400export const resolveSshTarget = Effect.fn("ssh/command.resolveSshTarget")(function* (
401 alias: string,
402): Effect.fn.Return<
403 DesktopSshEnvironmentTarget,
404 SshCommandError | SshInvalidTargetError,
405 ChildProcessSpawner.ChildProcessSpawner | FileSystem.FileSystem | Path.Path
406> {
407 const trimmedAlias = alias.trim();
408 if (trimmedAlias.length === 0) {
409 return yield* new SshInvalidTargetError({ message: "SSH host alias is required." });
410 }
411​
412 yield* Effect.logDebug("ssh.target.resolve.start", { alias: trimmedAlias });
413 return yield* runSshCommand(
414 {
415 alias: trimmedAlias,
416 hostname: trimmedAlias,
417 username: null,
418 port: null,
419 },
420 { preHostArgs: ["-G"] },
421 ).pipe(
422 Effect.map((result) => parseSshResolveOutput(trimmedAlias, result.stdout)),
423 Effect.tap((target) =>
424 Effect.logDebug("ssh.target.resolve.succeeded", sshTargetLogFields(target)),
425 ),
426 Effect.catch((cause) =>
427 Effect.logDebug("ssh.target.resolve.fallback", { alias: trimmedAlias, cause }).pipe(
428 Effect.as({
429 alias: trimmedAlias,
430 hostname: trimmedAlias,
431 username: null,
432 port: null,
433 }),
434 ),
435 ),
436 );
437});