You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

96 lines
2.2 KiB

7 years ago
import fs from 'fs-extra';
import path from 'path';
import optimist from 'optimist';
import wetty from './wetty';
const opts = optimist
.options({
sslkey: {
demand : false,
description: 'path to SSL key',
},
sslcert: {
demand : false,
description: 'path to SSL certificate',
},
sshhost: {
demand : false,
description: 'ssh server host',
},
sshport: {
demand : false,
description: 'ssh server port',
},
sshuser: {
demand : false,
description: 'ssh user',
},
sshauth: {
demand : false,
description: 'defaults to "password", you can use "publickey,password" instead',
},
port: {
demand : false,
alias : 'p',
description: 'wetty listen port',
},
help: {
demand : false,
alias : 'h',
description: 'Print help message',
},
})
.boolean('allow_discovery').argv;
if (opts.help) {
optimist.showHelp();
process.exit(0);
}
const sshuser = opts.sshuser || process.env.SSHUSER || '';
const sshhost = opts.sshhost || process.env.SSHHOST || 'localhost';
const sshauth = opts.sshauth || process.env.SSHAUTH || 'password,keyboard-interactive';
const sshport = opts.sshport || process.env.SSHPORT || 22;
7 years ago
const port = opts.port || process.env.PORT || 3000;
7 years ago
loadSSL(opts)
.then(ssl => {
opts.ssl = ssl;
})
.catch(err => {
console.error(`Error: ${err}`);
process.exit(1);
});
7 years ago
process.on('uncaughtException', err => {
console.error(`Error: ${err}`);
});
const tty = wetty(port, sshuser, sshhost, sshport, sshauth, opts.ssl);
tty.on('exit', code => {
console.log(`exit with code: ${code}`);
});
tty.on('disconnect', () => {
console.log('disconnect');
});
function loadSSL({ sslkey, sslcert }) {
return new Promise((resolve, reject) => {
const ssl = {};
7 years ago
if (sslkey && sslcert) {
fs
.readFile(path.resolve(sslkey))
.then(key => {
ssl.key = key;
})
.then(fs.readFile(path.resolve(sslcert)))
.then(cert => {
ssl.cert = cert;
})
.then(resolve(ssl))
.catch(reject);
7 years ago
}
resolve(ssl);
});
7 years ago
}