Browse Source

allow path to connect to be passed from url

pull/155/head
butlerx 6 years ago
committed by Cian Butler
parent
commit
97f13c46e2
  1. 28
      cli.mjs
  2. 71
      wetty.mjs

28
cli.mjs

@ -6,51 +6,51 @@ import wetty from './wetty';
const opts = optimist const opts = optimist
.options({ .options({
sslkey: { sslkey: {
demand : false, demand: false,
description: 'path to SSL key', description: 'path to SSL key',
}, },
sslcert: { sslcert: {
demand : false, demand: false,
description: 'path to SSL certificate', description: 'path to SSL certificate',
}, },
sshhost: { sshhost: {
demand : false, demand: false,
description: 'ssh server host', description: 'ssh server host',
}, },
sshport: { sshport: {
demand : false, demand: false,
description: 'ssh server port', description: 'ssh server port',
}, },
sshuser: { sshuser: {
demand : false, demand: false,
description: 'ssh user', description: 'ssh user',
}, },
sshpass: { sshpass: {
demand : false, demand: false,
description: 'ssh password', description: 'ssh password',
}, },
sshauth: { sshauth: {
demand : false, demand: false,
description: 'defaults to "password", you can use "publickey,password" instead', description: 'defaults to "password", you can use "publickey,password" instead',
}, },
sshkey: { sshkey: {
demand : false, demand: false,
description: description:
'path to an optional client private key (connection will be password-less and insecure!)', 'path to an optional client private key (connection will be password-less and insecure!)',
}, },
port: { port: {
demand : false, demand: false,
alias : 'p', alias: 'p',
description: 'wetty listen port', description: 'wetty listen port',
}, },
command: { command: {
demand : false, demand: false,
alias : 'c', alias: 'c',
description: 'command to run in shell, defaults to /bin/login', description: 'command to run in shell, defaults to /bin/login',
}, },
help: { help: {
demand : false, demand: false,
alias : 'h', alias: 'h',
description: 'Print help message', description: 'Print help message',
}, },
}) })

71
wetty.mjs

@ -32,20 +32,48 @@ app.use('/', express.static(path.join(dirname, 'public')));
function createServer(port, sslopts) { function createServer(port, sslopts) {
return sslopts && sslopts.key && sslopts.cert return sslopts && sslopts.key && sslopts.cert
? https.createServer(sslopts, app).listen(port, () => { ? https.createServer(sslopts, app).listen(port, () => {
console.log(`https on port ${port}`); console.log(`https on port ${port}`);
}) })
: http.createServer(app).listen(port, () => { : http.createServer(app).listen(port, () => {
console.log(`http on port ${port}`); console.log(`http on port ${port}`);
}); });
}
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