forked from butlerx/wetty
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathapp.js
More file actions
237 lines (210 loc) · 7.3 KB
/
app.js
File metadata and controls
237 lines (210 loc) · 7.3 KB
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
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
var bunyan = require('bunyan');
var Docker = require('dockerode');
var express = require('express');
var http = require('http');
var https = require('https');
var Loader = require('yaml-config-loader');
var path = require('path');
var server = require('socket.io');
var pty = require('node-pty');
var fs = require('fs');
var jwt = require('jsonwebtoken');
var loader = new Loader();
'use strict';
var ms = require('ms');
var config = require('./lib/config');
var log = bunyan.createLogger({name: 'probo-shell', level: 'debug',
streams: [{
stream: process.stdout
}]});
process.title = 'probo-shell';
config.load(function(error, config) {
var app = express();
var io;
if (error) {
throw error;
}
process.on('uncaughtException', function(e) {
log.error(e);
});
app.get('/wetty/ssh/:user', function(req, res) {
res.sendfile(__dirname + '/public/wetty/index.html');
});
app.use('/', express.static(path.join(__dirname, 'public')));
httpserv = http.createServer(app).listen(config['server.port'], function() {
log.info('http on port ' + config['server.port']);
});
io = server(httpserv,{path: '/wetty/socket.io'});
function getQuery(query, request) {
// Get the token from the current request _GET.
queryRegex = new RegExp(query + '=\(\[\\w-\\d\\.\]*\)', '');
if (matches = request.headers.referer.match(queryRegex)) {
return matches[1];
}
return false;
}
function getOperation(request, dockerCommandArray) {
var operationName = getQuery('op', request);
var operationCommandArray;
operationName = operationName || 'bash';
if (config.operations.hasOwnProperty(operationName)) {
// Need to use slice() to copy the config, as it will be changed.
operationCommandArray = config.operations[operationName].slice();
}
else {
throw new Exception('Unknown command ' . operationName);
}
operationCommandArray = operationAddTailLength(operationCommandArray, request);
operationCommandArray = operationAddWatchdogLength(operationCommandArray, request);
// Need to use slice() to copy the docker command, as it will be changed.
operationCommandArray = dockerCommandArray.slice().concat(operationCommandArray);
return operationCommandArray;
}
function operationAddTailLength(operationCommandArray, request) {
var logLength;
if (operationCommandArray[0] == 'tail' && (logLength = getQuery('log-length', request))) {
operationCommandArray.splice(1, 0, '-n');
if (logLength == 'all') {
operationCommandArray.splice(2, 0, '+1');
}
else {
operationCommandArray.splice(2, 0, logLength);
}
}
return operationCommandArray;
}
function operationAddWatchdogLength(operationCommandArray, request) {
var logLength;
if (operationCommandArray[2] == 'watchdog-show' && (logLength = getQuery('log-length', request))) {
operationCommandArray.splice(3, 0, '--count');
operationCommandArray.splice(4, 0, logLength);
}
log.info(operationCommandArray);
return operationCommandArray;
}
function sanitizeData(data) {
for (var n in config.sanitizeStrings) {
var replacement = config.sanitizeStrings[n].replacement || '<*****>';
var regex = new RegExp(config.sanitizeStrings[n].pattern, 'gi');;
data = data.replace(regex, replacement);
}
return data;
}
io.on('connection', function(socket){
var request = socket.request;
var token;
var term;
var containerName;
try {
var loginData;
var containerName;
var docker = Docker(config.dockerConfig);
var matches;
var shellAuthQueryRegex;
var keylogger = '';
// Get the token from the current request _GET.
if (token = getQuery(config.shellAuthQuery, request)) {
loginData = jwt.verify(token, config.shellSecret).data;
loginData = JSON.parse(loginData);
containerName = loginData.containerName;
}
else {
throw new Error('Authentication token missing.')
}
/**
* Kick off a shell instance.
*/
var next = function(err, container) {
// TODO: Create a yaml file configuration for what to log.
var logString = '';
try {
if (err) {
throw new Error('Error while connecting: ' . err);
}
dockerCommandArray = getOperation(request, ['docker', 'exec', '-it', container.Id]);
term = pty.spawn('/usr/bin/env', dockerCommandArray, {
name: 'xterm-256color',
cols: 80,
rows: 30,
});
log.info('Connected to docker', loginData, 'PID ' + term.pid);
// Sept 20, 2021 - Remove logging of keystrokes and output
term.on('data', function(data) {
//log.info(data);
data = sanitizeData(data, config.sanitizeStrings);
//log.info(data);
//logString = logString.concat(data);
socket.emit('output', data);
});
term.on('exit', function(code) {
log.info('Probo-Shell Log:', 'Date: ' + (new Date()), loginData, logString);
log.info('exit', container.Id);
});
socket.on('resize', function(data) {
term.resize(data.col, data.row);
});
socket.on('input', function(data) {
term.write(data);
});
socket.on('disconnect', function() {
log.info('Probo-Shell Log:', 'Date: ' + (new Date()), loginData, logString);
log.info('disconnect', container.Id);
term.end();
});
}
catch (e) {
throw new Error('Problem connecting to Probo.ci shell: ' + (e.message));
}
};
/**
* Get the container referenced in the JWT.
*/
docker.listContainers({all: true, size: false}, function(err, containers) {
var container;
try {
if (err) throw Error(err);
function proboFilter(containerInfo) {
// .Names is an array, and first name in array is the main name
// container names in docker start with a /, so look for our prefix
// starting with second character (index 1)
return containerInfo.Names[0].indexOf(containerName) === 1;
}
containers = containers.filter(proboFilter);
if (containers.length == 0) {
throw new Error('Container not found');
}
container = containers[0];
}
catch (e) {
throw new Error('Could not connect to Docker instance: ' + e.message);
}
if (container.State != 'running') {
try {
var containerObject = docker.getContainer(container.Id);
containerObject.start(function(err, data) {
next(null, container);
});
}
catch (e) {
throw new Error('Could not start Docker instance.');
}
}
else {
next(null, container);
}
});
} catch (e) {
var err;
if (e.name === 'JsonWebTokenError') {
err = ('Authentication token invalid.');
}
if (e.name === 'TokenExpiredError') {
err = ('Authentication token expired.');
}
err = err || e.message;
socket.emit('output', err);
socket.write('output', err);
return false;
}
});
});