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