From 08332034dbf72a4fd5e8247249f67f985b863a99 Mon Sep 17 00:00:00 2001 From: James Turnbull Date: Sat, 26 Apr 2014 17:47:03 -0400 Subject: [PATCH] Added support for remote SSH host and user name --- README.md | 50 +++++++++++++++++++++++++++++++++++--------------- app.js | 29 ++++++++++++++++++++++++----- 2 files changed, 59 insertions(+), 20 deletions(-) diff --git a/README.md b/README.md index e8004ec..acb5310 100644 --- a/README.md +++ b/README.md @@ -1,7 +1,11 @@ Wetty = Web + tty ----------------- -Terminal over http. Wetty is an alternative to ajaxterm/anyterm but much better than them because wetty uses ChromeOS' terminal emulator (hterm) which is a full fledged implementation of terminal emulation written entirely in Javascript. Also it uses websockets instead of Ajax and hence better response time. +Terminal over HTTP and HTTPS. Wetty is an alternative to +ajaxterm/anyterm but much better than them because wetty uses ChromeOS' +terminal emulator (hterm) which is a full fledged implementation of +terminal emulation written entirely in Javascript. Also it uses +websockets instead of Ajax and hence better response time. hterm source - https://chromium.googlesource.com/apps/libapps/+/master/hterm/ @@ -11,35 +15,47 @@ Install ------- * `git clone https://github.com/krishnasrinivas/wetty` - + * `cd wetty` * `npm install` -Run on http: +Run on HTTP: ----------- - `node app.js -p 3000` -If you run it as root it will launch /bin/login (where you can specify the username), else it will launch ssh to localhost and you can specify the sshport using --sshport option and specify username in address bar like this: + node app.js -p 3000 + +If you run it as root it will launch `/bin/login` (where you can specify +the user name), else it will launch `ssh` and connect by default to +`localhost`. + +If instead you wish to connect to a remote host you can specify the +`--sshhost` option, the SSH port using the `--sshport` option and the +SSH user using the `--sshuser` option. + +You can also specify the SSH user name in the address bar like this: `http://yourserver:3000/wetty/ssh/` -Run on https: +Run on HTTPS: ------------ -Always use https! If you don't have ssl certificates from CA you can create a self signed certificate using this command: + +Always use HTTPS! If you don't have SSL certificates from a CA you can +create a self signed certificate using this command: `openssl req -x509 -newkey rsa:2048 -keyout key.pem -out cert.pem -days 30000 -nodes` And then run: - `node app.js --sslkey key.pem --sslcert cert.pem -p 3000` + node app.js --sslkey key.pem --sslcert cert.pem -p 3000 -Again, if you run it as root it will launch /bin/login, else it will launch ssh to localhost as explained above. +Again, if you run it as root it will launch `/bin/login`, else it will +launch SSH to `localhost` or a specified host as explained above. Run wetty behind nginx: ---------------------- -Put the following config in nginx's conf: +Put the following configuration in nginx's conf: location /wetty { proxy_pass http://127.0.0.1:3000/wetty; @@ -54,13 +70,17 @@ Put the following config in nginx's conf: proxy_set_header X-NginX-Proxy true; } -If you are running app.js as root and have nginx proxy you have to use: - `http://yourserver.com/wetty` -Else if you are running app.js as a regular user you have to use: - `http://yourserver.com/wetty/ssh/` +If you are running `app.js` as `root` and have an Nginx proxy you have to use: -Note that if your nginx is configured for https you should run wetty without ssl. + http://yourserver.com/wetty + +Else if you are running `app.js` as a regular user you have to use: + + http://yourserver.com/wetty/ssh/ + +**Note that if your Nginx is configured for HTTPS you should run wetty without SSL.** Issues ------ + Does not work on Firefox as hterm was written for ChromeOS. So works well on Chrome. diff --git a/app.js b/app.js index dcfbe95..a7200c6 100644 --- a/app.js +++ b/app.js @@ -6,7 +6,6 @@ var ws = require('websocket').server; var pty = require('pty.js'); var fs = require('fs'); - var opts = require('optimist') .options({ sslkey: { @@ -17,10 +16,18 @@ var opts = require('optimist') 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' + }, port: { demand: true, alias: 'p', @@ -30,11 +37,21 @@ var opts = require('optimist') var runhttps = false; var sshport = 22; +var sshhost = 'localhost'; +var globalsshuser = ''; if (opts.sshport) { sshport = opts.sshport; } +if (opts.sshhost) { + sshhost = opts.sshhost; +} + +if (opts.sshuser) { + globalsshuser = opts.sshuser; +} + if (opts.sslkey && opts.sslcert) { runhttps = true; opts['ssl'] = {}; @@ -42,7 +59,6 @@ if (opts.sslkey && opts.sslcert) { opts.ssl['cert'] = fs.readFileSync(path.resolve(opts.sslcert)); } - process.on('uncaughtException', function(e) { console.error('Error: ' + e); }); @@ -78,8 +94,11 @@ wss.on('request', function(request) { sshuser = request.resource; sshuser = sshuser.replace('/wetty/ssh/', ''); } - if (sshuser) + if (sshuser) { sshuser = sshuser + '@'; + } else if (globalsshuser) { + sshuser = globalsshuser + '@'; + } conn.on('message', function(msg) { var data = JSON.parse(msg.utf8Data); if (!term) { @@ -90,11 +109,11 @@ wss.on('request', function(request) { rows: 30 }); } else { - term = pty.spawn('ssh', [sshuser + 'localhost', '-p', sshport], { + term = pty.spawn('ssh', [sshuser + sshhost, '-p', sshport], { name: 'xterm-256color', cols: 80, rows: 30 - }); + }); } term.on('data', function(data) { conn.send(JSON.stringify({