forked from BrowserBox/BrowserBox
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
287 lines (263 loc) · 9.17 KB
/
index.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
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
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 {
DEBUG,
app_port,
chrome_port,
version,
COOKIENAME,
CONFIG,
} 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(os.homedir(), CONFIG.sslcerts, 'privkey.pem')),
cert: fs.readFileSync(path.resolve(os.homedir(), CONFIG.sslcerts, 'fullchain.pem')),
ca: fs.existsSync(path.resolve(os.homedir(), CONFIG.sslcerts, 'chain.pem')) ?
fs.readFileSync(path.resolve(os.homedir(), CONFIG.sslcerts, 'chain.pem'))
:
undefined,
});
} catch(e) {
console.warn(`Error using SSL`, e);
GO_SECURE = false;
}
const SOCKETS = new Map();
const app = express();
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];
authorized = (cookie === COOKIE) || NO_AUTH;
}
if ( authorized ) {
res.redirect('/');
} 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];
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 InternalEndpoint = /ws=localhost/g;
const ExternalEndpoint = req.query.ws || req.query.wss || `wss=${req.headers['host'].split(':')[0]}`;
DEBUG.debugDevtoolsServer && console.info({InternalEndpoint, 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 ( InternalEndpoint.test(Data.body) ) {
const newVal = Data.body.replace(InternalEndpoint, ExternalEndpoint);
// 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 {
res.sendStatus(401);
}
});
const server = (GO_SECURE ? https : http).createServer(SSL_OPTS, app);
const wss = new WebSocket.Server({server});
wss.on('connection', (ws, req) => {
const cookie = req.headers.cookie;
const authorized = (cookie && cookie.includes(`${COOKIENAME+PORT}=${COOKIE}`)) || NO_AUTH;
DEBUG.debugDevtoolServer && console.log('connect', {cookie, authorized}, req.path, req.url);
if ( authorized ) {
const url = `ws://127.0.0.1:${CHROME_PORT}${req.url}`;
try {
const crdpSocket = new WebSocket(url);
SOCKETS.set(ws, crdpSocket);
crdpSocket.on('open', () => {
DEBUG.debugDevtoolServer && console.log('CRDP Socket open');
});
crdpSocket.on('message', msg => {
//console.log('Browser sends us message', msg);
ws.send(msg);
});
ws.on('message', msg => {
//console.log('We send browser message');
crdpSocket.send(msg);
});
ws.on('close', (code, reason) => {
SOCKETS.delete(ws);
crdpSocket.close(1001, 'client disconnected');
});
crdpSocket.on('close', (code, reason) => {
SOCKETS.delete(ws);
crdpSocket.close(1011, 'browser disconnected');
});
} catch(e) {
console.warn('Error on websocket creation', e);
}
} else {
ws.send(JSON.stringify({error:`Not authorized`}));
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 }});
});