From 8852d4733b4c0d496985a03fabbda78852947b3e Mon Sep 17 00:00:00 2001 From: Kelvin Schoofs Date: Tue, 9 Feb 2021 19:38:41 +0100 Subject: [PATCH] Make instant connections try to use PuTTY --- src/connect.ts | 70 +++++++++++++++++++++++------------------ src/fileSystemConfig.ts | 1 + 2 files changed, 40 insertions(+), 31 deletions(-) diff --git a/src/connect.ts b/src/connect.ts index cf1c8da..43224d7 100644 --- a/src/connect.ts +++ b/src/connect.ts @@ -70,47 +70,55 @@ 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 session = await (await import('./putty')).getSession(config.putty, config.host, config.username, nameOnly); - if (!session) throw new Error(`Couldn't find the requested PuTTY session`); - if (session.protocol !== 'ssh') throw new Error(`The requested PuTTY session isn't a SSH session`); - config.username = config.username || 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; - 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`); - } - config.privateKeyPath = config.privateKeyPath || (!config.agent && session.publickeyfile) || undefined; - switch (session.proxymethod) { - case 0: - break; - case 1: - case 2: - case 3: - if (!session.proxyhost) throw new Error(`Proxymethod is SOCKS 4/5 or HTTP but 'proxyhost' is missing`); - config.proxy = { - host: session.proxyhost, - port: session.proxyport, - type: session.proxymethod === 1 ? 'socks4' : (session.proxymethod === 2 ? 'socks5' : 'http'), - }; - break; - default: - throw new Error(`The requested PuTTY session uses an unsupported proxy method`); + if (session) { + if (session.protocol !== 'ssh') throw new Error(`The requested PuTTY session isn't a SSH session`); + config.username = config.username || 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; + 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`); + } + config.privateKeyPath = config.privateKeyPath || (!config.agent && session.publickeyfile) || undefined; + switch (session.proxymethod) { + case 0: + break; + case 1: + case 2: + case 3: + if (!session.proxyhost) throw new Error(`Proxymethod is SOCKS 4/5 or HTTP but 'proxyhost' is missing`); + config.proxy = { + host: session.proxyhost, + port: session.proxyport, + type: session.proxymethod === 1 ? 'socks4' : (session.proxymethod === 2 ? 'socks5' : 'http'), + }; + break; + default: + throw new Error(`The requested PuTTY session uses an unsupported proxy method`); + } + logging.debug(`\tReading PuTTY configuration lead to the following configuration:\n${JSON.stringify(config, null, 4)}`); + } else if (mandatory) { + throw new Error(`Couldn't find the requested PuTTY session`); + } else { + logging.debug(`\tConfig suggested finding a PuTTY configuration, did not find one`); } - logging.debug(`\tReading PuTTY configuration lead to the following configuration:\n${JSON.stringify(config, null, 4)}`); } if (config.privateKeyPath) { try { diff --git a/src/fileSystemConfig.ts b/src/fileSystemConfig.ts index fc816db..2d7d456 100644 --- a/src/fileSystemConfig.ts +++ b/src/fileSystemConfig.ts @@ -143,5 +143,6 @@ export function parseConnectionString(input: string): [config: FileSystemConfig, username: user || '$USERNAME', _locations: [], //debug: true as any, + putty: process.platform === 'win32' && '', // Since this is like a "quick connect", automatically enable PuTTY support }, path]; }