diff --git a/src/connect.ts b/src/connect.ts index cb8fbbc..928527e 100644 --- a/src/connect.ts +++ b/src/connect.ts @@ -7,6 +7,7 @@ import * as vscode from 'vscode'; import { getConfig, getFlag } from './config'; import type { FileSystemConfig } from './fileSystemConfig'; import { censorConfig, Logging } from './logging'; +import type { PuttySession } from './putty'; import { toPromise } from './toPromise'; // tslint:disable-next-line:variable-name @@ -29,8 +30,8 @@ const PROMPT_FIELDS: Partial> = { host: ['Host', c => `Host for ${c.name}`, true], username: ['Username', c => `Username for ${c.name}`, true], - password: ['Password', c => `Password for ${c.username}@${c.name}`, false, true], - passphrase: ['Passphrase', c => `Passphrase for provided export/private key for ${c.username}@${c.name}`, false, true], + password: ['Password', c => `Password for '${c.username}' for ${c.name}`, false, true], + passphrase: ['Passphrase', c => `Passphrase for provided export/private key for '${c.username}' for ${c.name}`, false, true], }; async function promptFields(config: FileSystemConfig, ...fields: (keyof FileSystemConfig)[]): Promise { @@ -69,37 +70,48 @@ export async function calculateActualConfig(config: FileSystemConfig): Promise') { - // ^ '' is a special case used in parseConnectionString in fileSystemConfig.ts - await promptFields(config, 'host'); - // TODO: `config.putty === true` without config.host should prompt the user with *all* PuTTY sessions - if (!config.host) throw new Error(`'putty' was true but 'host' is empty/missing`); - mandatory = config.putty === true; - config.putty = config.host; - nameOnly = false; - } else { - config.putty = replaceVariables(config.putty); + const { getCachedFinder } = await import('./putty'); + const getSession = await getCachedFinder(); + const cUsername = config.username === '$USER' ? undefined : config.username; + const tryPutty = config.instantConnection || config.putty === ''; + let session: PuttySession | undefined; + if (tryPutty) { + // If we're trying to find one, we also check whether `config.host` represents the name of a PuTTY session + session = await getSession(config.host); + logging.info(`\ttryPutty is true, tried finding a config named '${config.host}' and found ${session ? `'${session.name}'` : 'no match'}`); + } + if (!session) { + let nameOnly = true; + if (config.putty === true) { + await promptFields(config, 'host'); + // TODO: `config.putty === true` without config.host should prompt the user with *all* PuTTY sessions + if (!config.host) throw new Error(`'putty' was true but 'host' is empty/missing`); + config.putty = config.host; + nameOnly = false; + } else { + config.putty = replaceVariables(config.putty); + } + session = await getSession(config.putty, config.host, cUsername, nameOnly); } - const session = await (await import('./putty')).getSession(config.putty, config.host, config.username, nameOnly); if (session) { if (session.protocol !== 'ssh') throw new Error(`The requested PuTTY session isn't a SSH session`); - config.username = config.username || session.username; + config.username = cUsername || session.username; if (!config.username && session.hostname && session.hostname.indexOf('@') >= 1) { config.username = session.hostname.substr(0, session.hostname.indexOf('@')); } - config.host = config.host || session.hostname; + // Used to be `config.host || session.hostname`, but `config.host` could've been just the session name + config.host = session.hostname.replace(/^.*?@/, ''); config.port = session.portnumber || config.port; config.agent = config.agent || (session.tryagent ? 'pageant' : undefined); - if (session.usernamefromenvironment) { - config.username = process.env.USERNAME || process.env.USER; - if (!config.username) throw new Error(`Trying to use the system username, but process.env.USERNAME or process.env.USER is missing`); - } + if (session.usernamefromenvironment) config.username = '$USER'; config.privateKeyPath = config.privateKeyPath || (!config.agent && session.publickeyfile) || undefined; switch (session.proxymethod) { case 0: @@ -118,7 +130,7 @@ export async function calculateActualConfig(config: FileSystemConfig): Promise', // Since this is like a "quick connect", automatically enable PuTTY support }, path]; }