Initial config file

Moved configuration variables from index.js to config.json using
read-config.
This commit is contained in:
billchurch 2016-05-20 10:39:58 -04:00
parent 472dd3f55b
commit 9a9cf5cdc0
4 changed files with 43 additions and 23 deletions

18
config.json Normal file
View file

@ -0,0 +1,18 @@
{
"listen": {
"ip": "127.0.0.1",
"port": 2222
},
"user": {
"name": null,
"password": null
},
"ssh": {
"host": null,
"port": 22
},
"header": {
"text": "My Header",
"background": "green"
}
}

View file

@ -6,13 +6,10 @@ var path = require('path');
var basicAuth = require('basic-auth');
var term = require('term.js');
var ssh = require('ssh2');
var readConfig = require('read-config'),
config = readConfig(__dirname + '/config.json');
var username = null;
var password = null;
var host = null;
var port = 22;
var header = 'Default Header';
var headerBackground = 'rgb (0,90,0)';
console.log(config);
function checkParams(arr) {
return function(req, res, next) {
@ -35,8 +32,8 @@ function checkParams(arr) {
}
server.listen({
host: '127.0.0.1',
port: 2222
host: config.listen.ip,
port: config.listen.port
});
app.use(express.static(__dirname + '/public')).use(term.middleware()).use(function(req, res, next) {
@ -46,16 +43,16 @@ app.use(express.static(__dirname + '/public')).use(term.middleware()).use(functi
res.setHeader('WWW-Authenticate', 'Basic realm="WebSSH"');
res.end('Username and password required for web SSH service.');
} else {
username = myAuth['name'];
password = myAuth['pass'];
config.user.name = myAuth['name'];
config.user.password = myAuth['pass'];
next();
}
}).get('/', checkParams(["host"]), function(req, res) {
res.sendFile(path.join(__dirname + '/public/client.htm'))
host = req.query.host
if (typeof req.query.port !== 'undefined' && req.query.port !== null){ port = req.query.port;}
if (typeof req.query.header !== 'undefined' && req.query.header !== null){ header = req.query.header;}
if (typeof req.query.headerBackground !== 'undefined' && req.query.headerBackground !== null){ headerBackground = req.query.headerBackground;}
config.ssh.host = req.query.host
if (typeof req.query.port !== 'undefined' && req.query.port !== null){ config.host.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;}
// debug // console.log('varibles passwd: ' + username + '/' + host + '/' + port);
});
@ -64,10 +61,10 @@ io.on('connection', function(socket) {
conn.on('banner', function(msg, lng) {
socket.emit('data', msg);
}).on('ready', function() {
socket.emit('title', 'ssh://' + host);
socket.emit('headerBackground', headerBackground);
socket.emit('header', header);
socket.emit('footer', 'ssh://' + username + '@' + host + ':' + port);
socket.emit('title', 'ssh://' + config.ssh.host);
socket.emit('headerBackground', config.header.background);
socket.emit('header', config.header.text);
socket.emit('footer', 'ssh://' + config.user.name + '@' + config.ssh.host + ':' + config.ssh.port);
socket.emit('status', 'SSH CONNECTION ESTABLISHED');
socket.emit('statusBackground', 'green');
conn.shell(function(err, stream) {
@ -91,10 +88,10 @@ io.on('connection', function(socket) {
socket.emit('status', 'SSH CONNECTION ERROR - ' + error)
socket.emit('statusBackground', 'red');
}).connect({
host: host,
port: port,
username: username,
password: password,
host: config.ssh.host,
port: config.ssh.port,
username: config.user.name,
password: config.user.password,
// some cisco routers need the these cipher strings
algorithms: {
'cipher': ['aes128-cbc', '3des-cbc', 'aes256-cbc'],

View file

@ -8,8 +8,9 @@
"dependencies": {
"basic-auth": "^1.0.3",
"express": "^4.13.4",
"read-config": "^1.6.0",
"socket.io": "^1.4.5",
"ssh2": "^0.5.0",
"term.js": "0.0.7"
}
}
}

View file

@ -25,8 +25,12 @@ client.run = function(options) {
socket.on('data', function(data) {
term.write(data);
}).on('disconnect', function() {
document.getElementById('status').style.backgroundColor = 'red';
document.getElementById('status').innerHTML = 'WEBSOCKET SERVER DISCONNECTED';
socket.io.reconnection(false);
}).on('error', function(err) {
document.getElementById('status').style.backgroundColor = 'red';
document.getElementById('status').innerHTML = 'ERROR ' + err;
});
});
}, false);