From 0378c63617fe0f46a1d3d23280eb93de9222f3fd Mon Sep 17 00:00:00 2001 From: butlerx Date: Sat, 12 Jan 2019 17:34:02 +0000 Subject: [PATCH] ensure command is passed correctly --- .babelrc | 2 +- index.js | 2 +- src/server/command.ts | 26 +++++++++++++++++--------- 3 files changed, 19 insertions(+), 11 deletions(-) diff --git a/.babelrc b/.babelrc index b3ff3b4..6e53860 100644 --- a/.babelrc +++ b/.babelrc @@ -3,7 +3,7 @@ "@babel/preset-typescript", ["@babel/env"] ], - "compact": true + "compact": true, "plugins": [ "lodash" ] diff --git a/index.js b/index.js index d27ce01..fcbb72d 100644 --- a/index.js +++ b/index.js @@ -79,7 +79,7 @@ if (require.main === module) { alias: 'c', description: 'command to run in shell', type: 'string', - default: process.env.COMMAND || 'command', + default: process.env.COMMAND || 'login', }, help: { demand: false, diff --git a/src/server/command.ts b/src/server/command.ts index fba6651..8f0bd6d 100644 --- a/src/server/command.ts +++ b/src/server/command.ts @@ -33,7 +33,7 @@ export default ( ? loginOptions(command, remoteAddress) : sshOptions( urlArgs(referer, { - ssh: address(referer, user, host), + host: address(referer, user, host), port: `${port}`, pass, command, @@ -48,29 +48,37 @@ export default ( address(referer, user, host).includes('@'), }); +function parseCommand(command: string, path?: string): string { + if (command === 'login' && path === undefined) return ''; + return path !== undefined + ? `$SHELL -c "cd ${path};${command === 'login' ? '$SHELL' : command}"` + : command; +} + function sshOptions( - { pass, path, command, ssh, port, auth }: { [s: string]: string }, + { pass, path, command, host, port, auth }: { [s: string]: string }, key?: string ): string[] { + const cmd = parseCommand(command, path); const sshRemoteOptsBase = [ 'ssh', - ssh, + host, '-t', '-p', port, '-o', `PreferredAuthentications=${auth}`, - path !== undefined - ? `$SHELL -c "cd ${path};${command === 'login' ? '$SHELL' : command}"` - : command, ]; if (key) { - return sshRemoteOptsBase.concat(['-i', key]); + return sshRemoteOptsBase.concat(['-i', key, cmd]); } if (pass) { - return ['sshpass', '-p', pass].concat(sshRemoteOptsBase); + return ['sshpass', '-p', pass].concat(sshRemoteOptsBase, [cmd]); + } + if (cmd === '') { + return sshRemoteOptsBase; } - return sshRemoteOptsBase; + return sshRemoteOptsBase.concat([cmd]); } function loginOptions(command: string, remoteAddress: string): string[] {