-
-
Notifications
You must be signed in to change notification settings - Fork 80.8k
Expand file tree
/
Copy pathssh-config.ts
More file actions
130 lines (120 loc) · 3.4 KB
/
Copy pathssh-config.ts
File metadata and controls
130 lines (120 loc) · 3.4 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
// Reads effective SSH target config from the local ssh client.
import { spawn } from "node:child_process";
import { parseStrictPositiveInteger } from "./parse-finite-number.js";
import type { SshParsedTarget } from "./ssh-tunnel.js";
export const SSH_CONFIG_OUTPUT_MAX_CHARS = 64 * 1024;
export type SshResolvedConfig = {
user?: string;
host?: string;
port?: number;
identityFiles: string[];
};
type AppendSshConfigOutputResult = { ok: true; value: string } | { ok: false; reason: "too-large" };
function parsePort(value: string | undefined): number | undefined {
if (!value) {
return undefined;
}
const parsed = parseStrictPositiveInteger(value);
if (parsed === undefined || parsed > 65535) {
return undefined;
}
return parsed;
}
export function parseSshConfigOutput(output: string): SshResolvedConfig {
const result: SshResolvedConfig = { identityFiles: [] };
const lines = output.split("\n");
for (const raw of lines) {
const line = raw.trim();
if (!line) {
continue;
}
const [key, ...rest] = line.split(/\s+/);
const value = rest.join(" ").trim();
if (!key || !value) {
continue;
}
switch (key) {
case "user":
result.user = value;
break;
case "hostname":
result.host = value;
break;
case "port":
result.port = parsePort(value);
break;
case "identityfile":
if (value !== "none") {
result.identityFiles.push(value);
}
break;
default:
break;
}
}
return result;
}
export function appendSshConfigOutput(
current: string,
chunk: unknown,
maxChars = SSH_CONFIG_OUTPUT_MAX_CHARS,
): AppendSshConfigOutputResult {
const next = current + String(chunk);
if (next.length > maxChars) {
return { ok: false, reason: "too-large" };
}
return { ok: true, value: next };
}
export async function resolveSshConfig(
target: SshParsedTarget,
opts: { identity?: string; timeoutMs?: number } = {},
): Promise<SshResolvedConfig | null> {
const sshPath = "/usr/bin/ssh";
const args = ["-G"];
if (target.port > 0 && target.port !== 22) {
args.push("-p", String(target.port));
}
if (opts.identity?.trim()) {
args.push("-i", opts.identity.trim());
}
const userHost = target.user ? `${target.user}@${target.host}` : target.host;
// Use "--" so userHost can't be parsed as an ssh option.
args.push("--", userHost);
return await new Promise<SshResolvedConfig | null>((resolve) => {
const child = spawn(sshPath, args, {
stdio: ["ignore", "pipe", "ignore"],
});
let stdout = "";
let outputTooLarge = false;
child.stdout?.setEncoding("utf8");
child.stdout?.on("data", (chunk) => {
const appended = appendSshConfigOutput(stdout, chunk);
if (!appended.ok) {
outputTooLarge = true;
child.kill("SIGKILL");
return;
}
stdout = appended.value;
});
const timeoutMs = Math.max(200, opts.timeoutMs ?? 800);
const timer = setTimeout(() => {
try {
child.kill("SIGKILL");
} finally {
resolve(null);
}
}, timeoutMs);
child.once("error", () => {
clearTimeout(timer);
resolve(null);
});
child.once("exit", (code) => {
clearTimeout(timer);
if (outputTooLarge || code !== 0 || !stdout.trim()) {
resolve(null);
return;
}
resolve(parseSshConfigOutput(stdout));
});
});
}