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

View file

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

View file

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