From 6b05e5e34fc0ad300c3f532be9576691e64d332e Mon Sep 17 00:00:00 2001 From: billchurch Date: Fri, 24 Mar 2017 11:23:45 -0400 Subject: [PATCH] minor formatting and name cleanup --- index.js | 79 ++++++++++++++++++++++++++--------------------- public/client.htm | 8 ++--- public/client.js | 13 +++++--- public/style.css | 4 +-- 4 files changed, 59 insertions(+), 45 deletions(-) diff --git a/index.js b/index.js index 3adea28..9264974 100644 --- a/index.js +++ b/index.js @@ -2,36 +2,34 @@ * WebSSH2 - Web to SSH2 gateway * Bill Church - https://github.com/billchurch - April 2016 * - */ - -var express = require('express'); -var app = express(); -var cookieParser = require('cookie-parser'); -var server = require('http').Server(app); -var io = require('socket.io')(server); -var path = require('path'); - -var basicAuth = require('basic-auth'); -var ssh = require('ssh2').Client; -var readConfig = require('read-config'), - config = readConfig(__dirname + '/config.json'); -var myError = " - "; -var termCols; -var termRows; + */ +var express = require('express'), + app = express(), + cookieParser = require('cookie-parser'), + server = require('http').Server(app), + io = require('socket.io')(server), + path = require('path'), + basicAuth = require('basic-auth'), + ssh = require('ssh2').Client, + readConfig = require('read-config'), + config = readConfig(__dirname + '/config.json'), + myError = " - ", + termCols, + termRows; function logErrors(err, req, res, next) { - console.error(err.stack); - next(err); + console.error(err.stack); + next(err); } server.listen({ host: config.listen.ip, port: config.listen.port -}).on('error', function (err) { +}).on('error', function(err) { if (err.code === 'EADDRINUSE') { config.listen.port++; console.log('Address in use, retrying on port ' + config.listen.port); - setTimeout(function () { + setTimeout(function() { server.listen(config.listen.port); }, 250); } @@ -55,21 +53,27 @@ app.use(express.static(__dirname + '/public')).use(function(req, res, next) { }).use(cookieParser()).get('/ssh/host/:host?', function(req, res) { res.sendFile(path.join(__dirname + '/public/client.htm')); config.ssh.host = req.params.host; - if (typeof req.query.port !== 'undefined' && req.query.port !== null){ config.ssh.port = req.query.port;} - if (typeof req.query.header !== 'undefined' && req.query.header !== null){ config.header.text = req.query.header;} - if (typeof req.query.headerBackground !== 'undefined' && req.query.headerBackground !== null){ config.header.background = req.query.headerBackground;} - console.log ('webssh2 Login: user=' + config.user.name + ' from=' + req.ip + ' host=' + config.ssh.host + ' port=' + config.ssh.port + ' sessionID=' + req.headers.sessionid + ' allowreplay=' + req.headers.allowreplay); - console.log ('Headers: ' + JSON.stringify(req.headers)); + if (typeof req.query.port !== 'undefined' && req.query.port !== null) { + config.ssh.port = req.query.port; + } + if (typeof req.query.header !== 'undefined' && req.query.header !== null) { + config.header.text = req.query.header; + } + if (typeof req.query.headerBackground !== 'undefined' && req.query.headerBackground !== null) { + config.header.background = req.query.headerBackground; + } + console.log('webssh2 Login: user=' + config.user.name + ' from=' + req.ip + ' host=' + config.ssh.host + ' port=' + config.ssh.port + ' sessionID=' + req.headers.sessionid + ' allowreplay=' + req.headers.allowreplay); + console.log('Headers: ' + JSON.stringify(req.headers)); config.options.allowreplay = req.headers.allowreplay; - -}).use('/style',express.static(__dirname + '/public')).use('/src',express.static(__dirname + '/node_modules/xterm/dist')).use('/addons',express.static(__dirname + '/node_modules/xterm/dist/addons')); +}).use('/style', express.static(__dirname + '/public')).use('/src', express.static(__dirname + '/node_modules/xterm/dist')).use('/addons', express.static(__dirname + '/node_modules/xterm/dist/addons')); io.on('connection', function(socket) { var conn = new ssh(); - socket.on('geometry', function (cols, rows) { + socket.on('geometry', function(cols, rows) { termCols = cols; termRows = rows; }); + conn.on('banner', function(d) { //need to convert to cr/lf for proper formatting d = d.replace(/\r?\n/g, "\r\n"); @@ -83,9 +87,13 @@ io.on('connection', function(socket) { socket.emit('statusBackground', 'green'); socket.emit('allowreplay', config.options.allowreplay); - conn.shell( { term: config.ssh.term, cols: termCols, rows: termRows }, function(err, stream) { - if (err) { - console.log (err.message); + conn.shell({ + term: config.ssh.term, + cols: termCols, + rows: termRows + }, function(err, stream) { + if (err) { + console.log(err.message); myError = myError + err.message; return socket.emit('status', 'SSH EXEC ERROR: ' + err.message).emit('statusBackground', 'red'); } @@ -93,14 +101,15 @@ io.on('connection', function(socket) { stream.write(data); }); socket.on('control', function(controlData) { - switch(controlData) { + switch (controlData) { case 'replayCredentials': stream.write(config.user.password + '\n'); /* falls through */ default: - console.log ('controlData: '+ controlData); + console.log('controlData: ' + controlData); } }); + stream.on('data', function(d) { socket.emit('data', d.toString('binary')); }).on('close', function(code, signal) { @@ -133,10 +142,10 @@ io.on('connection', function(socket) { username: config.user.name, password: config.user.password, tryKeyboard: true, -// some cisco routers need the these cipher strings + // some cisco routers need the these cipher strings algorithms: { 'cipher': ['aes128-cbc', '3des-cbc', 'aes256-cbc'], 'hmac': ['hmac-sha1', 'hmac-sha1-96', 'hmac-md5-96'] } }); -}); +}); \ No newline at end of file diff --git a/public/client.htm b/public/client.htm index bbd4b84..06babe3 100644 --- a/public/client.htm +++ b/public/client.htm @@ -3,9 +3,9 @@ Web SSH - - - + + + @@ -17,7 +17,7 @@
CREDENTIALS
Download Log
-
Start Log
+
Start Log
diff --git a/public/client.js b/public/client.js index 91e3944..129bb87 100644 --- a/public/client.js +++ b/public/client.js @@ -32,10 +32,12 @@ function replayCredentials() { return false; } -function startLog() { +// Set variable to toggle log data from client/server to a varialble +// for later download +function toggleLog() { if (sessionLogEnable == true) { sessionLogEnable = false; - document.getElementById('startLog').innerHTML = 'Start Log'; + document.getElementById('toggleLog').innerHTML = 'Start Log'; console.log("stopping log, " + sessionLogEnable); currentDate = new Date(); sessionLog = sessionLog + "\r\n\r\nLog End for " + sessionFooter + ": " + currentDate.getFullYear() + "/" + (currentDate.getMonth() + 1) + "/" + currentDate.getDate() + " @ " + currentDate.getHours() + ":" + currentDate.getMinutes() + ":" + currentDate.getSeconds() + "\r\n"; @@ -43,7 +45,7 @@ function startLog() { return false; } else { sessionLogEnable = true; - document.getElementById('startLog').innerHTML = 'Logging - STOP LOG'; + document.getElementById('toggleLog').innerHTML = 'Logging - STOP LOG'; document.getElementById('downloadLog').style.display = 'inline'; console.log("starting log, " + sessionLogEnable); currentDate = new Date(); @@ -53,6 +55,8 @@ function startLog() { } } +// cross browser method to "download" an element to the local system +// used for our client-side logging feature function downloadLog() { myFile = "WebSSH2-" + logDate.getFullYear() + (logDate.getMonth() + 1) + logDate.getDate() + "_" + logDate.getHours() + logDate.getMinutes() + logDate.getSeconds() + ".log"; var blob = new Blob([sessionLog], { @@ -69,6 +73,7 @@ function downloadLog() { document.body.removeChild(elem); } } + socket.on('connect', function() { socket.emit('geometry', term.cols, term.rows); term.on('data', function(data) { @@ -99,7 +104,7 @@ socket.on('connect', function() { if (sessionLogEnable) { sessionLog = sessionLog + data; } - }).on('disconnect', function() { + }).on('disconnect', function(err) { document.getElementById('status').style.backgroundColor = 'red'; document.getElementById('status').innerHTML = 'WEBSOCKET SERVER DISCONNECTED' + err; socket.io.reconnection(false); diff --git a/public/style.css b/public/style.css index a422a27..55a76e2 100644 --- a/public/style.css +++ b/public/style.css @@ -106,7 +106,7 @@ a.downloadLog { color: rgb(240, 240, 240); text-decoration: none; } -#startLog { +#toggleLog { display: inline-block; color: rgb(240, 240, 240); background-color: rgb(0, 127, 0); @@ -118,7 +118,7 @@ a.downloadLog { text-align: left; z-index: 100; } -a.startLog { +a.toggleLog { color: rgb(240, 240, 240); text-decoration: none; }