Browse Source

ensure command is passed correctly

pull/126/head
butlerx 6 years ago
parent
commit
0378c63617
No known key found for this signature in database GPG Key ID: B37CA765BAA89170
  1. 2
      .babelrc
  2. 2
      index.js
  3. 26
      src/server/command.ts

2
.babelrc

@ -3,7 +3,7 @@
"@babel/preset-typescript", "@babel/preset-typescript",
["@babel/env"] ["@babel/env"]
], ],
"compact": true "compact": true,
"plugins": [ "plugins": [
"lodash" "lodash"
] ]

2
index.js

@ -79,7 +79,7 @@ if (require.main === module) {
alias: 'c', alias: 'c',
description: 'command to run in shell', description: 'command to run in shell',
type: 'string', type: 'string',
default: process.env.COMMAND || 'command', default: process.env.COMMAND || 'login',
}, },
help: { help: {
demand: false, demand: false,

26
src/server/command.ts

@ -33,7 +33,7 @@ export default (
? loginOptions(command, remoteAddress) ? loginOptions(command, remoteAddress)
: sshOptions( : sshOptions(
urlArgs(referer, { urlArgs(referer, {
ssh: address(referer, user, host), host: address(referer, user, host),
port: `${port}`, port: `${port}`,
pass, pass,
command, command,
@ -48,29 +48,37 @@ export default (
address(referer, user, host).includes('@'), 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( function sshOptions(
{ pass, path, command, ssh, port, auth }: { [s: string]: string }, { pass, path, command, host, port, auth }: { [s: string]: string },
key?: string key?: string
): string[] { ): string[] {
const cmd = parseCommand(command, path);
const sshRemoteOptsBase = [ const sshRemoteOptsBase = [
'ssh', 'ssh',
ssh, host,
'-t', '-t',
'-p', '-p',
port, port,
'-o', '-o',
`PreferredAuthentications=${auth}`, `PreferredAuthentications=${auth}`,
path !== undefined
? `$SHELL -c "cd ${path};${command === 'login' ? '$SHELL' : command}"`
: command,
]; ];
if (key) { if (key) {
return sshRemoteOptsBase.concat(['-i', key]); return sshRemoteOptsBase.concat(['-i', key, cmd]);
} }
if (pass) { 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[] { function loginOptions(command: string, remoteAddress: string): string[] {

Loading…
Cancel
Save