|
|
@ -1,49 +1,51 @@ |
|
|
|
var express = require('express'); |
|
|
|
var http = require('http'); |
|
|
|
var https = require('https'); |
|
|
|
var path = require('path'); |
|
|
|
var server = require('socket.io'); |
|
|
|
var pty = require('pty.js'); |
|
|
|
var fs = require('fs'); |
|
|
|
const express = require('express'); |
|
|
|
const http = require('http'); |
|
|
|
const https = require('https'); |
|
|
|
const path = require('path'); |
|
|
|
const server = require('socket.io'); |
|
|
|
const pty = require('pty.js'); |
|
|
|
const fs = require('fs'); |
|
|
|
|
|
|
|
var opts = require('optimist') |
|
|
|
const opts = require('optimist') |
|
|
|
.options({ |
|
|
|
sslkey: { |
|
|
|
demand: false, |
|
|
|
description: 'path to SSL key' |
|
|
|
demand : false, |
|
|
|
description: 'path to SSL key', |
|
|
|
}, |
|
|
|
sslcert: { |
|
|
|
demand: false, |
|
|
|
description: 'path to SSL certificate' |
|
|
|
demand : false, |
|
|
|
description: 'path to SSL certificate', |
|
|
|
}, |
|
|
|
sshhost: { |
|
|
|
demand: false, |
|
|
|
description: 'ssh server host' |
|
|
|
demand : false, |
|
|
|
description: 'ssh server host', |
|
|
|
}, |
|
|
|
sshport: { |
|
|
|
demand: false, |
|
|
|
description: 'ssh server port' |
|
|
|
demand : false, |
|
|
|
description: 'ssh server port', |
|
|
|
}, |
|
|
|
sshuser: { |
|
|
|
demand: false, |
|
|
|
description: 'ssh user' |
|
|
|
demand : false, |
|
|
|
description: 'ssh user', |
|
|
|
}, |
|
|
|
sshauth: { |
|
|
|
demand: false, |
|
|
|
description: 'defaults to "password", you can use "publickey,password" instead' |
|
|
|
demand : false, |
|
|
|
description: 'defaults to "password", you can use "publickey,password" instead', |
|
|
|
}, |
|
|
|
port: { |
|
|
|
demand: true, |
|
|
|
alias: 'p', |
|
|
|
description: 'wetty listen port' |
|
|
|
demand : false, |
|
|
|
alias : 'p', |
|
|
|
description: 'wetty listen port', |
|
|
|
}, |
|
|
|
}).boolean('allow_discovery').argv; |
|
|
|
}) |
|
|
|
.boolean('allow_discovery').argv; |
|
|
|
|
|
|
|
var runhttps = false; |
|
|
|
var sshport = 22; |
|
|
|
var sshhost = 'localhost'; |
|
|
|
var sshauth = 'password'; |
|
|
|
var globalsshuser = ''; |
|
|
|
let runhttps = process.env.HTTPS || false; |
|
|
|
let globalsshuser = process.env.SSHUSER || ''; |
|
|
|
let sshhost = process.env.SSHHOST || 'localhost'; |
|
|
|
let sshauth = process.env.SSHAUTH || 'password'; |
|
|
|
let sshport = process.env.SSHPOST || 22; |
|
|
|
let port = process.env.PORT || 3000; |
|
|
|
|
|
|
|
if (opts.sshport) { |
|
|
|
sshport = opts.sshport; |
|
|
@ -54,13 +56,17 @@ if (opts.sshhost) { |
|
|
|
} |
|
|
|
|
|
|
|
if (opts.sshauth) { |
|
|
|
sshauth = opts.sshauth |
|
|
|
sshauth = opts.sshauth; |
|
|
|
} |
|
|
|
|
|
|
|
if (opts.sshuser) { |
|
|
|
globalsshuser = opts.sshuser; |
|
|
|
} |
|
|
|
|
|
|
|
if (opts.port) { |
|
|
|
port = opts.port; |
|
|
|
} |
|
|
|
|
|
|
|
if (opts.sslkey && opts.sslcert) { |
|
|
|
runhttps = true; |
|
|
|
opts['ssl'] = {}; |
|
|
@ -68,75 +74,76 @@ if (opts.sslkey && opts.sslcert) { |
|
|
|
opts.ssl['cert'] = fs.readFileSync(path.resolve(opts.sslcert)); |
|
|
|
} |
|
|
|
|
|
|
|
process.on('uncaughtException', function(e) { |
|
|
|
console.error('Error: ' + e); |
|
|
|
process.on('uncaughtException', e => { |
|
|
|
console.error(`Error: ${e}`); |
|
|
|
}); |
|
|
|
|
|
|
|
var httpserv; |
|
|
|
let httpserv; |
|
|
|
|
|
|
|
var app = express(); |
|
|
|
app.get('/wetty/ssh/:user', function(req, res) { |
|
|
|
res.sendfile(__dirname + '/public/wetty/index.html'); |
|
|
|
const app = express(); |
|
|
|
app.get('/wetty/ssh/:user', (req, res) => { |
|
|
|
res.sendfile(`${__dirname}/public/wetty/index.html`); |
|
|
|
}); |
|
|
|
app.use('/', express.static(path.join(__dirname, 'public'))); |
|
|
|
|
|
|
|
if (runhttps) { |
|
|
|
httpserv = https.createServer(opts.ssl, app).listen(opts.port, function() { |
|
|
|
console.log('https on port ' + opts.port); |
|
|
|
httpserv = https.createServer(opts.ssl, app).listen(port, () => { |
|
|
|
console.log(`https on port ${port}`); |
|
|
|
}); |
|
|
|
} else { |
|
|
|
httpserv = http.createServer(app).listen(opts.port, function() { |
|
|
|
console.log('http on port ' + opts.port); |
|
|
|
httpserv = http.createServer(app).listen(port, () => { |
|
|
|
console.log(`http on port ${port}`); |
|
|
|
}); |
|
|
|
} |
|
|
|
|
|
|
|
var io = server(httpserv,{path: '/wetty/socket.io'}); |
|
|
|
io.on('connection', function(socket){ |
|
|
|
var sshuser = ''; |
|
|
|
var request = socket.request; |
|
|
|
console.log((new Date()) + ' Connection accepted.'); |
|
|
|
if (match = request.headers.referer.match('/wetty/ssh/.+$')) { |
|
|
|
sshuser = match[0].replace('/wetty/ssh/', '') + '@'; |
|
|
|
const io = server(httpserv, { path: '/wetty/socket.io' }); |
|
|
|
io.on('connection', socket => { |
|
|
|
let sshuser = ''; |
|
|
|
const request = socket.request; |
|
|
|
console.log(`${new Date()} Connection accepted.`); |
|
|
|
const match = request.headers.referer.match('/wetty/ssh/.+$'); |
|
|
|
if (match) { |
|
|
|
sshuser = `${match[0].replace('/wetty/ssh/', '')}@`; |
|
|
|
} else if (globalsshuser) { |
|
|
|
sshuser = globalsshuser + '@'; |
|
|
|
sshuser = `${globalsshuser}@`; |
|
|
|
} |
|
|
|
|
|
|
|
var term; |
|
|
|
if (process.getuid() == 0 && sshhost == 'localhost') { |
|
|
|
let term; |
|
|
|
if (process.getuid() === 0 && sshhost === 'localhost') { |
|
|
|
term = pty.spawn('/bin/login', [], { |
|
|
|
name: 'xterm-256color', |
|
|
|
cols: 80, |
|
|
|
rows: 30 |
|
|
|
rows: 30, |
|
|
|
}); |
|
|
|
} else if (sshuser) { |
|
|
|
term = pty.spawn('ssh', [sshuser + sshhost, '-p', sshport, '-o', 'PreferredAuthentications=' + sshauth], { |
|
|
|
term = pty.spawn('ssh', [sshuser + sshhost, '-p', sshport, '-o', `PreferredAuthentications=${sshauth}`], { |
|
|
|
name: 'xterm-256color', |
|
|
|
cols: 80, |
|
|
|
rows: 30 |
|
|
|
rows: 30, |
|
|
|
}); |
|
|
|
} else { |
|
|
|
term = pty.spawn('./bin/ssh', [sshhost, '-p', sshport, '-o', 'PreferredAuthentications=' + sshauth], { |
|
|
|
term = pty.spawn('./bin/ssh', [sshhost, '-p', sshport, '-o', `PreferredAuthentications=${sshauth}`], { |
|
|
|
name: 'xterm-256color', |
|
|
|
cols: 80, |
|
|
|
rows: 30 |
|
|
|
rows: 30, |
|
|
|
}); |
|
|
|
} |
|
|
|
|
|
|
|
console.log((new Date()) + " PID=" + term.pid + " STARTED on behalf of user=" + sshuser) |
|
|
|
term.on('data', function(data) { |
|
|
|
console.log(`${new Date()} PID=${term.pid} STARTED on behalf of user=${sshuser}`); |
|
|
|
term.on('data', data => { |
|
|
|
socket.emit('output', data); |
|
|
|
}); |
|
|
|
term.on('exit', function(code) { |
|
|
|
console.log((new Date()) + " PID=" + term.pid + " ENDED"); |
|
|
|
term.on('exit', code => { |
|
|
|
console.log(`${new Date()} PID=${term.pid} ENDED`); |
|
|
|
socket.emit('logout'); |
|
|
|
}); |
|
|
|
socket.on('resize', function(data) { |
|
|
|
term.resize(data.col, data.row); |
|
|
|
socket.on('resize', ({ col, row }) => { |
|
|
|
term.resize(col, row); |
|
|
|
}); |
|
|
|
socket.on('input', function(data) { |
|
|
|
socket.on('input', data => { |
|
|
|
term.write(data); |
|
|
|
}); |
|
|
|
socket.on('disconnect', function() { |
|
|
|
socket.on('disconnect', () => { |
|
|
|
term.end(); |
|
|
|
}); |
|
|
|
}) |
|
|
|
}); |
|
|
|