-
-
Notifications
You must be signed in to change notification settings - Fork 5.5k
/
Copy pathssh.ts
447 lines (435 loc) · 12.7 KB
/
ssh.ts
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
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
const knownHostRegex = /(?:[a-zA-Z0-9-]+\.)+[a-zA-Z0-9]+/; // will match numerical IPs as well as domains/subdomains
const resolveAbsolutePath = (
path: string,
basePath: string,
home: string
): string => {
if (path.startsWith("/") || path.startsWith("~/") || path === "~") {
return path.replace("~", home);
}
if (
basePath.startsWith("/") ||
basePath.startsWith("~/") ||
basePath === "~"
) {
return (
basePath.replace("~", home) +
(basePath.replace("~", home).endsWith("/") ? "" : "/") +
path
);
}
return basePath + (basePath.endsWith("/") ? "" : "/") + path;
};
const getConfigLines = async (
file: string,
executeShellCommand: Fig.ExecuteCommandFunction,
home: string,
basePath: string
) => {
const absolutePath = resolveAbsolutePath(file, basePath, home);
const { stdout } = await executeShellCommand({
command: "cat",
// eslint-disable-next-line @withfig/fig-linter/no-useless-arrays
args: [absolutePath],
});
const configLines = stdout.split("\n").map((line) => line.trim());
// Get list of includes in the config file
const includes = configLines
.filter((line) => line.toLowerCase().startsWith("include "))
.map((line) => line.split(" ")[1]);
// Get the lines of every include file
const includeLines: string[][] = await Promise.all(
includes.map((file) =>
getConfigLines(file, executeShellCommand, home, basePath)
)
);
// Combine config lines with includes config lines
return [...configLines, ...includeLines.flat()];
};
export const knownHosts: Fig.Generator = {
custom: async (tokens, executeCommand, context) => {
const { stdout } = await executeCommand({
command: "cat",
// eslint-disable-next-line @withfig/fig-linter/no-useless-arrays
args: [`${context.environmentVariables["HOME"]}/.ssh/known_hosts`],
});
return stdout
.split("\n")
.map((line) => {
const match = knownHostRegex.exec(line);
if (match) {
return String(match);
}
})
.filter((value, index, self) => value && self.indexOf(value) === index)
.map((knownHost) => ({
name: (tokens[1].endsWith("@") ? tokens[1] : "") + knownHost, // also suggest when user@ is provided
description: "SSH host",
}));
},
trigger: "@",
};
export const configHosts: Fig.Generator = {
custom: async (tokens, executeShellCommand, context) => {
const configLines = await getConfigLines(
"config",
executeShellCommand,
context.environmentVariables["HOME"],
"~/.ssh"
);
return configLines
.filter(
(line) =>
line.trim().toLowerCase().startsWith("host ") && !line.includes("*")
)
.map((host) => ({
name: host.split(" ")[1],
description: "SSH host",
priority: 90,
}));
},
};
const completionSpec: Fig.Spec = {
name: "ssh",
description: "Log into a remote machine",
args: {
name: "user@hostname",
description: "Address of remote machine to log into",
generators: [knownHosts, configHosts, { template: "history" }],
},
options: [
{
name: "-1",
description: "Forces ssh to try protocol version 1 only",
},
{
name: "-2",
description: "Forces ssh to try protocol version 2 only",
},
{
name: "-4",
description: "Forces ssh to use IPv4 addresses only",
},
{
name: "-6",
description: "Forces ssh to use IPv6 addresses only",
},
{
name: "-A",
description: "Enables forwarding of the authentication agent connection",
},
{
name: "-a",
description: "Disables forwarding of the authentication agent connection",
},
{
name: "-b",
description:
"Use bind_address on the local machine as the source address of the connection",
args: {
name: "bind address",
description: "Source address of the connection",
},
},
{
name: "-C",
description:
"Requests compression of all data (including stdin, stdout, stderr, and data for forwarded X11 and TCP connections)",
},
{
name: "-c",
description:
"Selects the cipher specification for encrypting the session",
args: {
name: "cipher spec",
description: "The selected cipher specification",
},
},
{
name: "-D",
description:
"Specifies a local 'dynamic' application-level port forwarding",
args: {
name: "port",
description: "Port of the bind address",
},
},
{
name: "-e",
description:
"Sets the escape character for sessions with a pty (default: '~')",
args: {
name: "escape char",
description: "Specified escape character",
},
},
{
name: "-F",
description: "Specifies an alternative per-user configuration file",
args: {
name: "configfile",
description: "Path to alternative config file",
template: "filepaths",
},
},
{
name: "-f",
description:
"Requests ssh to go to background just before command execution",
},
{
name: "-g",
description: "Allows remote hosts to connect to local forwarded ports",
},
{
name: "-I",
description:
"Specify the PKCS#11 shared library ssh should use to communicate with a PKCS#11 token providing the user's private RSA key",
args: {
name: "pkcs11",
},
},
{
name: "-i",
description:
"Selects a file from which the identity (private key) for public key authentication is read",
isRepeatable: true,
args: {
name: "identity file",
description: "Path to identity (private key)",
template: "filepaths",
},
},
{
name: "-K",
description:
"Enables GSSAPI-based authentication and forwarding (delegation) of GSSAPI credentials to the server",
},
{
name: "-k",
description:
"Disables forwarding (delegation) of GSSAPI credentials to the server",
},
{
name: "-L",
description:
"Specifies that the given port on the local (client) host is to be forwarded to the given host and port on the remote side",
args: {
name: "port:host:hostport",
description: "Local port, followed by host and host port to forward to",
},
},
{
name: "-l",
description: "Specifies the user to log in as on the remote machine",
args: {
name: "login name",
description: "Name of user logging into remote machine",
},
},
{
name: "-M",
description:
"Places the ssh client into ``master'' mode for connection sharing",
isRepeatable: true,
},
{
name: "-m",
description:
"Additionally, for protocol version 2 a comma-separated list of MAC (message authentication code) algorithms can be specified in order of preference",
args: {
name: "mac spec",
},
},
{
name: "-N",
description: "Do not execute a remote command",
},
{
name: "-n",
description:
"Redirects stdin from /dev/null (actually, prevents reading from stdin)",
},
{
name: "-O",
description: "Control an active connection multiplexing master process",
args: {
name: "ctl cmd",
description: "Command that's passed to the master process",
},
},
{
name: "-o",
description:
"Can be used to give options in the format used in the configuration file",
isRepeatable: true,
args: {
name: "option",
description:
"Options that can be specified in the format of the config file",
suggestions: [
{ name: "AddressFamily" },
{ name: "BatchMode" },
{ name: "BindAddress" },
{ name: "ChallengeResponseAuthentication" },
{ name: "CheckHostIP" },
{ name: "Cipher" },
{ name: "Ciphers" },
{ name: "ClearAllForwardings" },
{ name: "Compression" },
{ name: "CompressionLevel" },
{ name: "ConnectionAttempts" },
{ name: "ConnectTimeout" },
{ name: "ControlMaster" },
{ name: "ControlPath" },
{ name: "ControlPersist" },
{ name: "DynamicForward" },
{ name: "EscapeChar" },
{ name: "ExitOnForwardFailure" },
{ name: "ForwardAgent" },
{ name: "ForwardX11" },
{ name: "ForwardX11Timeout" },
{ name: "ForwardX11Trusted" },
{ name: "GatewayPorts" },
{ name: "GlobalKnownHostsFile" },
{ name: "GSSAPIAuthentication" },
{ name: "GSSAPIDelegateCredentials" },
{ name: "HashKnownHosts" },
{ name: "Host" },
{ name: "HostbasedAuthentication" },
{ name: "HostKeyAlgorithms" },
{ name: "HostKeyAlias" },
{ name: "HostName" },
{ name: "IdentityFile" },
{ name: "IdentitiesOnly" },
{ name: "IPQoS" },
{ name: "KbdInteractiveAuthentication" },
{ name: "KbdInteractiveDevices" },
{ name: "KexAlgorithms" },
{ name: "LocalCommand" },
{ name: "LocalForward" },
{ name: "LogLevel" },
{ name: "MACs" },
{ name: "NoHostAuthenticationForLocalhost" },
{ name: "NumberOfPasswordPrompts" },
{ name: "PasswordAuthentication" },
{ name: "PermitLocalCommand" },
{ name: "PKCS11Provider" },
{ name: "Port" },
{ name: "PreferredAuthentications" },
{ name: "Protocol" },
{ name: "ProxyCommand" },
{ name: "PubkeyAuthentication" },
{ name: "RekeyLimit" },
{ name: "RequestTTY" },
{ name: "RhostsRSAAuthentication" },
{ name: "RSAAuthentication" },
{ name: "SendEnv" },
{ name: "ServerAliveInterval" },
{ name: "ServerAliveCountMax" },
{ name: "StrictHostKeyChecking" },
{ name: "TCPKeepAlive" },
{ name: "Tunnel" },
{ name: "TunnelDevice" },
{ name: "UsePrivilegedPort" },
{ name: "User" },
{ name: "UserKnownHostsFile" },
{ name: "VerifyHostKeyDNS" },
{ name: "VisualHostKey" },
{ name: "XAuthLocation" },
],
},
},
{
name: "-p",
description: "Port to connect to on the remote host",
args: {
name: "port",
description: "Port to connect to",
},
},
{
name: "-q",
description:
"Quiet mode. Causes most warning and diagnostic messages to be suppressed",
},
{
name: "-R",
description:
"Specifies that the given port on the remote (server) host is to be forwarded to the given host and port on the local side",
args: {
name: "port:host:hostport",
description: "Local port, followed by host and host port to forward to",
},
},
{
name: "-S",
description:
"Specifies the location of a control socket for connection sharing, or the string 'none' to disable connection sharing",
args: {
name: "ctl_path",
description: "Location of the control socket",
template: "filepaths",
},
},
{
name: "-s",
description:
"May be used to request invocation of a subsystem on the remote system",
},
{
name: "-T",
description: "Disable pseudo-tty allocation",
},
{
name: "-t",
description: "Force pseudo-tty allocation",
isRepeatable: true,
},
{
name: "-V",
description: "Display the version number and exit",
},
{
name: "-v",
description:
"Verbose mode. Causes ssh to print debugging messages about its progress",
isRepeatable: 3,
},
{
name: "-W",
description:
"Requests that standard input and output on the client be forwarded to host on port over the secure channel",
args: {
name: "host:port",
description: "Host and port to forward to",
},
},
{
name: "-w",
description:
"Requests tunnel device forwarding with the specified tun(4) devices between the client (local_tun) and the server (remote_tun)",
args: {
name: "local tun",
description: "Local device to forward to",
},
},
{
name: "-X",
description: "Enables X11 forwarding",
},
{
name: "-x",
description: "Disables X11 forwarding",
},
{
name: "-Y",
description: "Enables trusted X11 forwarding",
},
{
name: "-y",
description: "Send log information using the syslog(3) system module",
},
],
};
export default completionSpec;