minor formatting and name cleanup

This commit is contained in:
billchurch 2017-03-24 11:23:45 -04:00
parent 35009f91f4
commit 6b05e5e34f
4 changed files with 59 additions and 45 deletions

View file

@ -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']
}
});
});
});

View file

@ -3,9 +3,9 @@
<title>Web SSH</title>
<link rel="stylesheet" href="/src/xterm.css" />
<link rel="stylesheet" href="/style/style.css" />
<script src="/socket.io/socket.io.js"></script>
<script src="/src/xterm.js"></script>
<script src="/addons/fit/fit.js"></script>
<script src="/socket.io/socket.io.js" defer></script>
<script src="/src/xterm.js" defer></script>
<script src="/addons/fit/fit.js" defer></script>
<script src="/client.js" defer></script>
</head>
<body>
@ -17,7 +17,7 @@
<div id="status"></div>
<div id="credentials"><a class="credentials" href="javascript:void(0);" onclick="replayCredentials()">CREDENTIALS</a></div>
<div id="downloadLog"><a class="downloadLog" href="javascript:void(0);" onclick="downloadLog()">Download Log</a></div>
<div id="startLog"><a class="startLog" href="javascript:void(0);" onclick="startLog();">Start Log</a></div>
<div id="toggleLog"><a class="toggleLog" href="javascript:void(0);" onclick="toggleLog();">Start Log</a></div>
</div>
</div>
</body>

View file

@ -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 = '<a class="startLog" href="javascript:void(0);" onclick="startLog();">Start Log</a>';
document.getElementById('toggleLog').innerHTML = '<a class="toggleLog" href="javascript:void(0);" onclick="toggleLog();">Start Log</a>';
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 = '<a class="startLog" href="javascript:void(0)" onclick="startLog();">Logging - STOP LOG</a>';
document.getElementById('toggleLog').innerHTML = '<a class="toggleLog" href="javascript:void(0)" onclick="toggleLog();">Logging - STOP LOG</a>';
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);

View file

@ -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;
}