Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
microsoft
GitHub Repository: microsoft/vscode
Path: blob/main/src/vs/platform/agentHost/common/sshConfigParsing.ts
13394 views
1
/*---------------------------------------------------------------------------------------------
2
* Copyright (c) Microsoft Corporation. All rights reserved.
3
* Licensed under the MIT License. See License.txt in the project root for license information.
4
*--------------------------------------------------------------------------------------------*/
5
6
import type { ISSHResolvedConfig } from './sshRemoteAgentHost.js';
7
8
/** Strip inline comments from an SSH config value. */
9
export function stripSSHComment(s: string): string {
10
const idx = s.indexOf(' #');
11
return idx !== -1 ? s.substring(0, idx).trim() : s;
12
}
13
14
/**
15
* Extract Host aliases from SSH config content (without following Includes).
16
*/
17
export function parseSSHConfigHostEntries(content: string): string[] {
18
const hosts: string[] = [];
19
for (const line of content.split('\n')) {
20
const trimmed = line.trim();
21
if (!trimmed || trimmed.startsWith('#')) {
22
continue;
23
}
24
const hostMatch = trimmed.match(/^Host\s+(.+)$/i);
25
if (hostMatch) {
26
const hostValue = stripSSHComment(hostMatch[1]);
27
for (const h of hostValue.split(/\s+/)) {
28
if (!h.includes('*') && !h.includes('?') && !h.startsWith('!')) {
29
hosts.push(h);
30
}
31
}
32
}
33
}
34
return hosts;
35
}
36
37
/**
38
* Parse `ssh -G` output into a resolved config object.
39
*/
40
export function parseSSHGOutput(stdout: string): ISSHResolvedConfig {
41
const map = new Map<string, string>();
42
const identityFiles: string[] = [];
43
for (const line of stdout.split('\n')) {
44
const spaceIdx = line.indexOf(' ');
45
if (spaceIdx === -1) {
46
continue;
47
}
48
const key = line.substring(0, spaceIdx).toLowerCase();
49
const value = line.substring(spaceIdx + 1).trim();
50
if (key === 'identityfile') {
51
identityFiles.push(value);
52
} else {
53
map.set(key, value);
54
}
55
}
56
57
return {
58
hostname: map.get('hostname') ?? '',
59
user: map.get('user') || undefined,
60
port: parseInt(map.get('port') ?? '22', 10),
61
identityFile: identityFiles,
62
forwardAgent: map.get('forwardagent') === 'yes',
63
};
64
}
65
66