Changing over from term.js to xterm.js
Changing over from term.js to https://github.com/sourcelair/xterm.js Added cookieParser for some additional to do some URI translation
This commit is contained in:
parent
181907ce07
commit
ca4e059c39
1 changed files with 37 additions and 28 deletions
65
index.js
65
index.js
|
@ -1,32 +1,24 @@
|
||||||
|
/*
|
||||||
|
* WebSSH2 - Web to SSH2 gateway
|
||||||
|
* Bill Church - https://github.com/billchurch - April 2016
|
||||||
|
*
|
||||||
|
*/
|
||||||
|
|
||||||
var express = require('express');
|
var express = require('express');
|
||||||
var app = express();
|
var app = express();
|
||||||
|
var cookieParser = require('cookie-parser')
|
||||||
var server = require('http').Server(app);
|
var server = require('http').Server(app);
|
||||||
var io = require('socket.io')(server);
|
var io = require('socket.io')(server);
|
||||||
var path = require('path');
|
var path = require('path');
|
||||||
|
|
||||||
var basicAuth = require('basic-auth');
|
var basicAuth = require('basic-auth');
|
||||||
var term = require('term.js');
|
|
||||||
var ssh = require('ssh2');
|
var ssh = require('ssh2');
|
||||||
var readConfig = require('read-config'),
|
var readConfig = require('read-config'),
|
||||||
config = readConfig(__dirname + '/config.json');
|
config = readConfig(__dirname + '/config.json');
|
||||||
|
|
||||||
function checkParams(arr) {
|
function logErrors(err, req, res, next) {
|
||||||
return function(req, res, next) {
|
console.error(err.stack);
|
||||||
// Make sure each param listed in arr is present in req.query
|
next(err);
|
||||||
var missing_params = [];
|
|
||||||
for (var i = 0; i < arr.length; i++) {
|
|
||||||
if (!eval("req.query." + arr[i])) {
|
|
||||||
missing_params.push(arr[i]);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
if (missing_params.length == 0) {
|
|
||||||
next();
|
|
||||||
} else {
|
|
||||||
next(JSON.stringify({
|
|
||||||
"error": "query error",
|
|
||||||
"message": "Parameter(s) missing: " + missing_params.join(",")
|
|
||||||
}));
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
server.listen({
|
server.listen({
|
||||||
|
@ -42,25 +34,28 @@ server.listen({
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
app.use(express.static(__dirname + '/public')).use(term.middleware()).use(function(req, res, next) {
|
app.use(express.static(__dirname + '/public')).use(function(req, res, next) {
|
||||||
var myAuth = basicAuth(req);
|
var myAuth = basicAuth(req);
|
||||||
if (myAuth === undefined) {
|
if (myAuth === undefined) {
|
||||||
res.statusCode = 401;
|
res.statusCode = 401;
|
||||||
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 {
|
||||||
config.user.name = myAuth['name'];
|
config.user.name = myAuth.name;
|
||||||
config.user.password = myAuth['pass'];
|
config.user.password = myAuth.pass;
|
||||||
next();
|
next();
|
||||||
}
|
}
|
||||||
}).get('/', checkParams(["host"]), function(req, res) {
|
}).use(cookieParser()).get('/ssh/host/:host?', function(req, res) {
|
||||||
res.sendFile(path.join(__dirname + '/public/client.htm'))
|
res.sendFile(path.join(__dirname + '/public/client.htm'));
|
||||||
config.ssh.host = req.query.host
|
config.ssh.host = req.params.host;
|
||||||
if (typeof req.query.port !== 'undefined' && req.query.port !== null){ config.host.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){ config.header.text = 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){ config.header.background = 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);
|
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'));
|
||||||
|
|
||||||
io.on('connection', function(socket) {
|
io.on('connection', function(socket) {
|
||||||
var conn = new ssh();
|
var conn = new ssh();
|
||||||
|
@ -73,13 +68,23 @@ io.on('connection', function(socket) {
|
||||||
socket.emit('footer', 'ssh://' + config.user.name + '@' + config.ssh.host + ':' + config.ssh.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');
|
||||||
|
socket.emit('allowreplay', config.options.allowreplay)
|
||||||
conn.shell(function(err, stream) {
|
conn.shell(function(err, stream) {
|
||||||
if (err) return socket.emit('status', 'SSH EXEC ERROR: ' + err.message).emit('statusBackground', 'red');
|
if (err) return socket.emit('status', 'SSH EXEC ERROR: ' + err.message).emit('statusBackground', 'red');
|
||||||
socket.on('data', function(data) {
|
socket.on('data', function(data) {
|
||||||
stream.write(data);
|
stream.write(data);
|
||||||
});
|
});
|
||||||
|
socket.on('control', function(controlData) {
|
||||||
|
switch(controlData) {
|
||||||
|
case 'replayCredentials':
|
||||||
|
stream.write(config.user.password + '\n');
|
||||||
|
default:
|
||||||
|
console.log ('controlData: '+ controlData);
|
||||||
|
};
|
||||||
|
});
|
||||||
stream.on('data', function(d) {
|
stream.on('data', function(d) {
|
||||||
socket.emit('data', d.toString('binary'));
|
socket.emit('data', d.toString('binary'));
|
||||||
|
// console.log('data: ' + d);
|
||||||
}).on('close', function() {
|
}).on('close', function() {
|
||||||
conn.end();
|
conn.end();
|
||||||
});
|
});
|
||||||
|
@ -91,13 +96,17 @@ io.on('connection', function(socket) {
|
||||||
socket.emit('status', 'SSH CONNECTION CLOSED');
|
socket.emit('status', 'SSH CONNECTION CLOSED');
|
||||||
socket.emit('statusBackground', 'red');
|
socket.emit('statusBackground', 'red');
|
||||||
}).on('error', function(error) {
|
}).on('error', function(error) {
|
||||||
socket.emit('status', 'SSH CONNECTION ERROR - ' + error)
|
socket.emit('status', 'SSH CONNECTION ERROR - ' + error);
|
||||||
socket.emit('statusBackground', 'red');
|
socket.emit('statusBackground', 'red');
|
||||||
|
}).on('keyboard-interactive', function(name, instructions, instructionsLang, prompts, finish) {
|
||||||
|
console.log('Connection :: keyboard-interactive');
|
||||||
|
finish([config.user.password]);
|
||||||
}).connect({
|
}).connect({
|
||||||
host: config.ssh.host,
|
host: config.ssh.host,
|
||||||
port: config.ssh.port,
|
port: config.ssh.port,
|
||||||
username: config.user.name,
|
username: config.user.name,
|
||||||
password: config.user.password,
|
password: config.user.password,
|
||||||
|
tryKeyboard: true,
|
||||||
// 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'],
|
||||||
|
|
Loading…
Reference in a new issue