forked from BrowserBox/BrowserBox
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
351 lines (322 loc) · 12.1 KB
/
index.js
File metadata and controls
351 lines (322 loc) · 12.1 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
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
import fs from 'fs';
import os from 'os';
import path from 'path';
import http from 'http';
import https from 'https';
import express from 'express';
import cookieParser from 'cookie-parser';
import WebSocket from 'ws';
//import {WebSocketServer} from 'ws';
import compression from 'compression';
import exitOnExpipe from 'exit-on-epipe';
import rateLimit from 'express-rate-limit';
import {
DEBUG,
app_port,
chrome_port,
version,
COOKIENAME,
CONFIG,
untilTrueOrTimeout,
} from '../../../common.js';
DEBUG.debugDevtoolsServer && console.log(process.argv);
const PORT = app_port + 1;
const COOKIE = process.argv[3];
const TOKEN = process.argv[4];
const sleep = ms => new Promise(res => setTimeout(res, ms));
const NO_AUTH = false; // true is insecure as anyone can connect
const CHROME_PORT = chrome_port;
const COOKIE_OPTS = {
secure: true,
httpOnly: true,
maxAge: 345600000,
sameSite: 'None'
};
if ( ! PORT || ! COOKIE || ! TOKEN ) {
throw new TypeError(`Must supply: <PORT> <COOKIE> <TOKEN>.
Receivedo only: ${JSON.stringify({PORT,COOKIE,TOKEN})}`
);
}
const SSL_OPTS = {};
let GO_SECURE = true;
try {
Object.assign(SSL_OPTS, {
key: fs.readFileSync(path.resolve(CONFIG.sslcerts(PORT), 'privkey.pem')),
cert: fs.readFileSync(path.resolve(CONFIG.sslcerts(PORT), 'fullchain.pem')),
ca: fs.existsSync(path.resolve(CONFIG.sslcerts(PORT), 'chain.pem')) ?
fs.readFileSync(path.resolve(CONFIG.sslcerts(PORT), 'chain.pem'))
:
undefined,
});
} catch(e) {
console.warn(`Error using SSL`, e);
GO_SECURE = false;
}
const SOCKETS = new Map();
//const internalEndpointRegex = /ws=localhost(:\d+)?([^ "'<>,;)}\]`]+)/g;
const internalEndpointRegex = /ws=localhost(:\d+)?([\/\w.-]+)/g;
//const internalEndpointRegex = /ws=localhost/g;
process.on('uncaughtException', err => {
console.error(`Uncaught exception`, err);
process.exit(1);
});
const limiter = rateLimit({
windowMs: 15 * 60 * 1000, // 15 minutes
max: 1000, // Limit each IP to 100 requests per `window` (here, per 15 minutes)
standardHeaders: true, // Return rate limit info in the `RateLimit-*` headers
legacyHeaders: false, // Disable the `X-RateLimit-*` headers
});
const app = express();
app.use(limiter);
app.use(compression());
app.use(express.urlencoded({extended:true}));
app.use(cookieParser());
app.use(function (req, res, next) {
res.setHeader('Cross-Origin-Resource-Policy', 'same-site');
res.setHeader('Cross-Origin-Embedder-Policy', 'require-corp');
next();
});
app.get('/login', (req, res) => {
const {token} = req.query;
let authorized;
// if we are bearing a valid token set the cookie
// so future requests will be authorized
if ( token == TOKEN ) {
DEBUG.debugDevtoolsServer && console.info(`Client logged in`, token);
res.cookie(COOKIENAME+PORT, COOKIE, COOKIE_OPTS);
authorized = true;
} else {
const cookie = req.cookies[COOKIENAME+PORT] || req.headers['x-browserbox-local-auth'];
authorized = (cookie === COOKIE) || NO_AUTH;
}
if ( authorized ) {
const uri = req.query.nextUri && ! process.env.TORBB ? decodeURIComponent(req.query.nextUri) : '/';
res.redirect(uri);
} else {
res.end(`
<!DOCTYPE html>
<style>:root { font-family: sans-serif; }</style>
<h1>Logging you into devtools...</h1>
<script src=devtools_login.js></script>
`);
}
});
app.post('/', (req, res) => {
const {token} = req.body;
let authorized;
// if we are bearing a valid token set the cookie
// so future requests will be authorized
if ( token == TOKEN ) {
res.cookie(COOKIENAME+PORT, COOKIE, COOKIE_OPTS);
authorized = true;
} else {
const cookie = req.cookies[COOKIENAME+PORT];
authorized = (cookie === COOKIE) || NO_AUTH;
}
if ( authorized ) {
res.redirect('/');
} else {
res.sendStatus(401);
}
});
app.get('/', (req, res) => {
res.sendFile(path.resolve('public', 'index.html'));
});
app.get('/devtools/LICENSE.txt', (req, res) => {
res.sendFile(path.resolve('public', 'devtools', 'LICENSE.txt'));
});
app.get('/devtools_login.js', (req, res) => {
res.sendFile(path.resolve('public', 'devtools_login.js'));
});
/**
// comment this out to ensure our proxy (below) uses the latest version
app.get('/devtools/inspector.html', (req, res) => {
res.sendFile(path.resolve('public', 'devtools', 'inspector.html'));
});
app.get('/devtools/inspector.js', (req, res) => {
res.sendFile(path.resolve('public', 'devtools', 'inspector.js'));
});
**/
app.get('/favicon.ico', (req, res) => {
res.sendFile(path.resolve('public', 'favicon.ico'));
});
app.get('/favicon.svg', (req, res) => {
res.sendFile(path.resolve('public', 'favicon.svg'));
});
app.get('/favicons/favicon.ico', (req, res) => {
res.sendFile(path.resolve('public', 'favicons', 'favicon.ico'));
});
app.get(/\/.*/, (req, res) => {
const cookie = req.cookies[COOKIENAME+PORT] || req.headers['x-browserbox-local-auth'];
const authorized = (cookie === COOKIE) || NO_AUTH;
if (authorized) {
const resource = {
hostname: '127.0.0.1',
port: CHROME_PORT,
path: req.url,
method: req.method,
headers: req.headers
};
DEBUG.debugDevtoolsServer && console.info(`Request authorized`, {resource});
const WSUrl_Raw = req.query.ws || req.query.wss || req.headers['host'].split(':')[0];
const Frame = req.protocol == 'https' ? 'wss:' : 'ws:';
const WSUrl = new URL(`${Frame}//${WSUrl_Raw}`);
//WSUrl.searchParams.set('token', TOKEN);
const ExternalEndpoint = `${Frame.slice(0,-1)}=${encodeURIComponent(WSUrl.href.slice(Frame.length+2).replace(/\/$/, ''))}`
//const ExternalEndpoint = req.query.ws || req.query.wss || `wss=${req.headers['host'].split(':')[0]}`;
DEBUG.debugDevtoolsServer && console.info({internalEndpointRegex, ExternalEndpoint});
// CRDP checks that host is localhost
req.headers['host'] = `${'localhost'}:${PORT}`;
const destination = http.request(resource, destinationResponse => {
const ct = destinationResponse.headers['content-type'];
if ( ct.includes('json') ) {
const onData = data => Data.body += data.toString();
const Data = {body: ''};
destinationResponse.on('data', onData);
destinationResponse.headers['cache-control'] = 'no-cache';
destinationResponse.on('end', () => {
//destinationResponse.removeListener('data', onData);
// save responses to inspect
/**
fs.writeFileSync(
path.resolve('save', `file${Math.random().toString(36)}.data`),
body
);
**/
if ( internalEndpointRegex.test(Data.body) ) {
const newVal = Data.body.replace(internalEndpointRegex, (match, port, capturedPart) => {
console.log('match', match);
// Construct the new URL using the captured part
//const result = `${ExternalEndpoint}`; //${capturedPart}/${encodeURIComponent(TOKEN)}`;
const result = `${ExternalEndpoint}${capturedPart}/${encodeURIComponent(TOKEN)}`;
console.log('result', result);
return result;
});
// update content length
destinationResponse.headers['content-length'] = newVal.length+'';
DEBUG.debugDevtoolServer && console.log(destinationResponse.headers, req.url, Data.body.length);
//res.writeHead(destinationResponse.statusCode, destinationResponse.headers);
res.write(newVal);
res.end();
} else {
res.writeHead(destinationResponse.statusCode, destinationResponse.headers);
res.end(Data.body);
}
});
} else if ( ct.includes('javascript') ) {
const onData = data => Data.body += data.toString();
const Data = {body: ''};
destinationResponse.on('data', onData);
destinationResponse.on('end', () => {
//destinationResponse.removeListener('data', onData);
// save responses to inspect
/**
fs.writeFileSync(
path.resolve('save', `file${Math.random().toString(36)}.data`),
body
);
**/
if ( Data.body.includes('chrome://new') ) {
let newVal = Data.body.replace(/chrome:\/\/newtab/g, 'data:text,about:blank');
newVal = newVal.replace(/chrome:\/\/new-tab-page/g, 'data:text,about:blank');
// update content length
destinationResponse.headers['content-length'] = newVal.length+'';
DEBUG.debugDevtoolServer && console.log(destinationResponse.headers, req.url, Data.body.length);
res.writeHead(destinationResponse.statusCode, destinationResponse.headers);
res.write(newVal);
res.end();
} else {
res.writeHead(destinationResponse.statusCode, destinationResponse.headers);
res.end(Data.body);
}
});
} else {
destinationResponse.headers['cache-control'] = 'max-age=86400';
res.writeHead(destinationResponse.statusCode, destinationResponse.headers);
destinationResponse.pipe(res, {end: true});
}
});
req.pipe(destination, {end: true});
} else {
console.log('Request not authorized to proxy through devtools server');
res.sendStatus(401);
}
});
const server = (GO_SECURE ? https : http).createServer(SSL_OPTS, app);
const wss = new WebSocket.Server({server});
// we should probably handle upgrade too to stop server crashing on a 404 websocket. weird
wss.on('connection', (ws, req) => {
try {
const cookie = req.headers.cookie || req.headers['x-browserbox-local-auth'];
const parts = req.url.split('/');
let token;
if ( parts.length == 5 ) {
token = parts.pop();
}
const path = parts.join('/');
const authorized = (cookie && cookie.includes(`${COOKIENAME+PORT}=${COOKIE}`)) || token == TOKEN || NO_AUTH;
DEBUG.debugDevtoolsServer && console.log('connect', {cookie, authorized, token, path, parts}, req.path, req.url);
if ( authorized ) {
const url = `ws://127.0.0.1:${CHROME_PORT}${path}`;
try {
let crdpSocket = new WebSocket(url);
SOCKETS.set(ws, crdpSocket);
ws.on('error', err => {
DEBUG.debugDevtoolServer && console.warn(`Front-end socket error`, err);
});
crdpSocket.on('error', err => {
DEBUG.debugDevtoolServer && console.warn(`CRDPSocket error`, err);
});
ws.on('open', () => {
DEBUG.debugDevtoolsServer && console.log('Front-end socket open');
});
crdpSocket.on('open', () => {
DEBUG.debugDevtoolsServer && console.log('CRDP Socket open');
});
crdpSocket.on('message', async msg => {
//console.log('Browser sends us message', msg);
if ( ws.readyState < WebSocket.OPEN ) {
await untilTrueOrTimeout(() => ws.readyState == WebSocket.OPEN, 150);
}
ws.send(msg);
});
ws.on('message', async msg => {
//console.log('We send browser message');
if ( crdpSocket.readyState < WebSocket.OPEN ) {
await untilTrueOrTimeout(() => crdpSocket.readyState == WebSocket.OPEN, 150);
}
crdpSocket.send(msg);
});
ws.on('close', (code, reason) => {
SOCKETS.delete(ws);
crdpSocket.close(1001, 'client disconnected');
});
crdpSocket.on('close', (code, reason) => {
SOCKETS.delete(ws);
ws.close(1011, 'browser disconnected');
});
} catch(e) {
console.warn('Error on websocket creation', e);
}
} else {
console.warn(`WS not authorized`);
if ( ws.readyState == WebSocket.OPEN ) {
ws.send(JSON.stringify({error:`Not authorized`}));
}
ws.close();
}
} catch(err) {
console.warn(`Error during ws connection`, err);
ws.close();
}
});
wss.on('error', err => {
console.warn(`DevTools server warning`, err, PORT);
});
server.listen(PORT, err => {
if ( err ) {
console.warn(`DevTools server warning`, err, PORT);
throw err;
}
DEBUG.debugDevtoolsServer && console.log({crdpSecureProxyServer: { up: new Date, port: PORT }});
});