|
|
@ -6,6 +6,7 @@ import server from 'socket.io'; |
|
|
|
import { spawn } from 'node-pty'; |
|
|
|
import EventEmitter from 'events'; |
|
|
|
import favicon from 'serve-favicon'; |
|
|
|
import url from 'url'; |
|
|
|
|
|
|
|
const app = express(); |
|
|
|
app.use(favicon(`${__dirname}/public/favicon.ico`)); |
|
|
@ -36,12 +37,24 @@ function createServer(port, sslopts) { |
|
|
|
}); |
|
|
|
} |
|
|
|
|
|
|
|
function getCommand(socket, sshuser, sshhost, sshport, sshauth, sshkey) { |
|
|
|
function getCommand(socket, sshuser, sshpass, sshhost, sshport, sshauth, sshkey) { |
|
|
|
const { request } = socket; |
|
|
|
const match = request.headers.referer.match('.+/ssh/.+$'); |
|
|
|
console.log("Match ", match); |
|
|
|
console.log("user ", sshuser); |
|
|
|
const sshAddress = sshuser ? `${sshuser}@${sshhost}` : sshhost; |
|
|
|
const sshPath = sshuser || match ? 'ssh' : path.join(__dirname, 'bin/ssh'); |
|
|
|
const ssh = match ? `${match[0].split('/ssh/').pop()}@${sshhost}` : sshAddress; |
|
|
|
console.log("Address ", sshAddress); |
|
|
|
const referer = url.parse(request.headers.referer, true); |
|
|
|
sshpass = referer.query.sshpass ? referer.query.sshpass : sshpass; |
|
|
|
console.log("PASS ", sshpass); |
|
|
|
let sshPath = '' |
|
|
|
if (!sshpass) |
|
|
|
sshPath = sshuser || match ? 'ssh' : path.join(__dirname, 'bin/ssh'); |
|
|
|
else |
|
|
|
sshPath = ['sshpass', '-p', sshpass].join(' '); |
|
|
|
console.log("PATH ", sshPath); |
|
|
|
const ssh = match ? `${match[0].split('/ssh/').pop().split('?')[0]}@${sshhost}` : sshAddress; |
|
|
|
console.log("SSH ", ssh); |
|
|
|
const sshRemoteOptsBase = [ |
|
|
|
sshPath, |
|
|
|
ssh, |
|
|
@ -52,7 +65,7 @@ function getCommand(socket, sshuser, sshhost, sshport, sshauth, sshkey) { |
|
|
|
] |
|
|
|
const sshRemoteOpts = sshkey ? sshRemoteOptsBase.concat(['-i', sshkey]) |
|
|
|
: sshRemoteOptsBase |
|
|
|
|
|
|
|
console.log(sshRemoteOpts); |
|
|
|
return [ |
|
|
|
process.getuid() === 0 && sshhost === 'localhost' |
|
|
|
? ['login', '-h', socket.client.conn.remoteAddress.split(':')[3]] |
|
|
@ -62,12 +75,14 @@ function getCommand(socket, sshuser, sshhost, sshport, sshauth, sshkey) { |
|
|
|
]; |
|
|
|
} |
|
|
|
|
|
|
|
export default function start(port, sshuser, sshhost, sshport, sshauth, sshkey, sslopts) { |
|
|
|
export default function start(port, sshuser, sshpass, sshhost, sshport, sshauth, sshkey, sslopts) { |
|
|
|
console.log("START", port, sshuser, sshpass, sshhost, sshport, sshauth, sshkey, sslopts); |
|
|
|
const events = new EventEmitter(); |
|
|
|
const io = server(createServer(port, sslopts), { path: '/wetty/socket.io' }); |
|
|
|
io.on('connection', socket => { |
|
|
|
console.log(`${new Date()} Connection accepted.`); |
|
|
|
const [args, ssh] = getCommand(socket, sshuser, sshhost, sshport, sshauth, sshkey); |
|
|
|
const [args, ssh] = getCommand(socket, sshuser, sshpass, sshhost, sshport, sshauth, sshkey); |
|
|
|
console.log("PIKA PIKA", args, ssh); |
|
|
|
const term = spawn('/usr/bin/env', args, { |
|
|
|
name: 'xterm-256color', |
|
|
|
cols: 80, |
|
|
|