forked from alex-myznikov/wetty
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathapp.js
139 lines (115 loc) · 3.79 KB
/
app.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
const express = require('express');
const http = require('http');
const https = require('https');
const path = require('path');
const server = require('socket.io');
const pty = require('node-pty');
const fs = require('fs');
const os = require('os');
const crypto = require('crypto');
let httpserv;
exports.start = function(opts) {
// Defaults for ssh terminal to be run with
let runhttps = false;
let globalsshport = 22;
let globalsshhost = 'localhost';
let globalsshauth = 'password,keyboard-interactive';
let globalsshuser = '';
// Change defaults if passed
if (opts.sshport) {
globalsshport = opts.sshport;
}
if (opts.sshhost) {
globalsshhost = opts.sshhost;
}
if (opts.sshauth) {
globalsshauth = opts.sshauth;
}
if (opts.sshuser) {
globalsshuser = opts.sshuser;
}
if (opts.sslkey && opts.sslcert) {
runhttps = true;
opts['ssl'] = {};
opts.ssl['key'] = fs.readFileSync(path.resolve(opts.sslkey));
opts.ssl['cert'] = fs.readFileSync(path.resolve(opts.sslcert));
}
process.on('uncaughtException', function(e) {
console.error('Error in Wetty terminal: ' + e);
});
const app = express();
app.get('/wetty/ssh', (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);
});
} else {
httpserv = http.createServer(app).listen(opts.port, function() {
console.log('http on port ' + opts.port);
});
}
const io = server(httpserv,{path: '/wetty/socket.io'});
io.on('connection', function(socket){
const request = socket.request;
let credentials = {};
console.log((new Date()) + ' Connection accepted.');
if (match = request.headers.referer.match(/\/wetty\/ssh\?(.*)#?/)) {
credentials = match[1].split('&').reduce((acc, cur) => {
const entry = cur.split('=');
return Object.assign(acc, { [entry[0].toLowerCase()]: entry[1] });
}, {});
}
let sshuser = credentials['sshuser'] || globalsshuser;
const sshhost = credentials['sshhost'] || globalsshhost;
const sshport = credentials['sshport'] || globalsshport;
const sshauth = globalsshauth;
if (sshuser) {
sshuser += '@';
}
if (process.env.DEBUG) {
console.log(
(new Date()) + " REFERER", request.headers.referer,
"PARSED INTO CREDENTIALS", sshuser, sshhost, sshport
);
}
let termPath = __dirname + '/terminal.js';
if (termPath.match(/^\/snapshot\//)) {
// Handle case for binary build with pkg: copy terminal.js from inside binary package to
// temporary directory and use it from there.
const fileData = fs.readFileSync(termPath);
termPath = path.join(os.tmpdir(), `${crypto.randomFillSync(Buffer.alloc(10)).toString('hex')}.js`);
fs.writeFileSync(termPath, fileData);
}
const term = pty.spawn('node', [
termPath,
sshuser + sshhost,
'-p',
sshport,
'-o',
'PreferredAuthentications=' + sshauth
], { name: 'xterm-256color', cols: 80, rows: 30 });
console.log((new Date()) + " PID=" + term.pid + " STARTED on behalf of user=" + sshuser);
term.on('data', function(data) {
socket.emit('output', data);
});
term.on('exit', function(code) {
console.log((new Date()) + " PID=" + term.pid + " ENDED");
});
socket.on('resize', function(data) {
term.resize(data.col, data.row);
});
socket.on('input', function(data) {
term.write(data);
});
socket.on('disconnect', function() {
console.log((new Date()) + " PID=" + term.pid + " DISCONNECTED");
term.end();
});
});
}
exports.stop = function() {
httpserv.close();
}