Browse Source

allow path to connect to be passed from url

pull/153/head
butlerx 6 years ago
parent
commit
f978109972
No known key found for this signature in database GPG Key ID: B37CA765BAA89170
  1. 63
      wetty.mjs

63
wetty.mjs

@ -39,13 +39,41 @@ function createServer(port, sslopts) {
}); });
} }
const urlArgs = request => url.parse(request.headers.referer, true).query;
const getRemoteAddress = socket =>
socket.client.conn.remoteAddress.split(':')[3] === undefined
? 'localhost'
: socket.client.conn.remoteAddress.split(':')[3];
function sshOptions(path, address, port, auth, key, query) {
const sshRemoteOptsBase = [
path,
address,
'-t',
'-p',
port,
'-o',
`PreferredAuthentications=${auth}`,
query.command,
];
if (key) {
return sshRemoteOptsBase.concat(['-i', key]);
} else if (query.sshpass) {
return ['sshpass', '-p', query.sshpass].concat(sshRemoteOptsBase);
}
return sshRemoteOptsBase;
}
function getCommand(socket, sshuser, sshpass, sshhost, sshport, sshauth, sshkey, command) { function getCommand(socket, sshuser, sshpass, sshhost, sshport, sshauth, sshkey, command) {
const { request } = socket; const { request } = socket;
const match = request.headers.referer.match('.+/ssh/.+$'); const match = request.headers.referer.match('.+/ssh/.+$');
const sshAddress = sshuser ? `${sshuser}@${sshhost}` : sshhost; const sshAddress = sshuser ? `${sshuser}@${sshhost}` : sshhost;
const referer = url.parse(request.headers.referer, true); const query = urlArgs(request);
sshpass = referer.query.sshpass ? referer.query.sshpass : sshpass; query.sshpass = query.sshpass || sshpass;
const sshPath = sshuser || match ? 'ssh' : path.join(dirname, 'bin/ssh'); query.command =
query.path !== undefined
? `$SHELL -c "cd ${query.path};${command === '' ? '$SHELL' : command}"`
: command;
const ssh = match const ssh = match
? `${ ? `${
match[0] match[0]
@ -54,25 +82,19 @@ function getCommand(socket, sshuser, sshpass, sshhost, sshport, sshauth, sshkey,
.split('?')[0] .split('?')[0]
}@${sshhost}` }@${sshhost}`
: sshAddress; : sshAddress;
const sshRemoteOptsBase = [ const args = command === '' ? ['login', '-h', getRemoteAddress(socket)] : [command];
sshPath,
ssh,
'-t',
'-p',
sshport,
'-o',
`PreferredAuthentications=${sshauth}`,
`"${command}"`,
];
let sshRemoteOpts;
if (sshkey) sshRemoteOpts = sshRemoteOptsBase.concat(['-i', sshkey]);
else if (sshpass) sshRemoteOpts = ['sshpass', '-p', sshpass].concat(sshRemoteOptsBase);
else sshRemoteOpts = sshRemoteOptsBase;
return [ return [
process.getuid() === 0 && sshhost === 'localhost' process.getuid() === 0 && sshhost === 'localhost'
? ['login', '-h', socket.client.conn.remoteAddress.split(':')[3]] ? args
: sshRemoteOpts, : sshOptions(
sshuser || match ? 'ssh' : path.join(dirname, 'bin/ssh'),
ssh,
sshport,
sshauth,
sshkey,
query,
),
ssh, ssh,
]; ];
} }
@ -102,13 +124,14 @@ export default function start(
sshkey, sshkey,
command, command,
); );
console.debug({ args, ssh });
const term = pty.spawn('/usr/bin/env', args, { const term = pty.spawn('/usr/bin/env', args, {
name: 'xterm-256color', name: 'xterm-256color',
cols: 80, cols: 80,
rows: 30, rows: 30,
}); });
console.log(`${new Date()} PID=${term.pid} STARTED on behalf of user=${ssh}`); console.log(`${new Date()} PID=${term.pid} STARTED on behalf of remote=${ssh}`);
term.on('data', data => socket.emit('output', data)); term.on('data', data => socket.emit('output', data));
term.on('exit', code => { term.on('exit', code => {
console.log(`${new Date()} PID=${term.pid} ENDED`); console.log(`${new Date()} PID=${term.pid} ENDED`);

Loading…
Cancel
Save