Skip to content

Commit c06a533

Browse files
author
Matthew Bryant (mandatory)
committed
Update
1 parent 590dc30 commit c06a533

File tree

2 files changed

+47
-38
lines changed

2 files changed

+47
-38
lines changed

server.js

Lines changed: 37 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
const NodeCache = require("node-cache");
22
const AnyProxy = require('./anyproxy');
33
const cluster = require('cluster');
4-
const moment = require('moment');
54
const WebSocket = require('ws');
65
const https = require('https');
76
const redis = require("redis");
@@ -18,6 +17,7 @@ const Sequelize = require('sequelize');
1817
const Op = Sequelize.Op;
1918

2019
const get_secure_random_string = require('./utils.js').get_secure_random_string;
20+
const logit = require('./utils.js').logit;
2121

2222
const get_api_server = require('./api-server.js').get_api_server;
2323

@@ -184,7 +184,8 @@ async function get_authentication_status(inputRequestDetail) {
184184
);
185185

186186
if (!proxy_authentication || !(proxy_authentication.includes('Basic'))) {
187-
console.log(`No proxy credentials provided!`);
187+
logit(`No proxy credentials provided!`);
188+
console.log(proxy_authentication);
188189
return false;
189190
}
190191

@@ -226,7 +227,7 @@ async function get_authentication_status(inputRequestDetail) {
226227
});
227228

228229
if (!browserproxy_record) {
229-
console.log(`Invalid credentials for username '${username}'!`);
230+
logit(`Invalid credentials for username '${username}'!`);
230231
return false;
231232
}
232233

@@ -250,12 +251,11 @@ const options = {
250251
rule: {
251252
async beforeSendRequest(requestDetail) {
252253
const remote_address = requestDetail._req.connection.remoteAddress;
253-
const datetime = moment().format('MMMM Do YYYY, h:mm:ss a');
254254

255255
const auth_details = await get_authentication_status(requestDetail.requestOptions.headers);
256256

257257
if (!auth_details) {
258-
console.error(`[${datetime}][${remote_address}] Request denied for URL ${requestDetail.url}, no authentication information provided in proxy HTTP request!`);
258+
logit(`[${remote_address}] Request denied for URL ${requestDetail.url}, no authentication information provided in proxy HTTP request!`);
259259
return AUTHENTICATION_REQUIRED_PROXY_RESPONSE;
260260
}
261261

@@ -265,7 +265,7 @@ const options = {
265265
requestDetail.requestData.length > 0
266266
) ? requestDetail.requestData.toString('base64') : false;
267267

268-
console.log(`[${datetime}][${auth_details.id}][${auth_details.name}] Proxying request ${requestDetail._req.method} ${requestDetail.url}`);
268+
logit(`[${auth_details.id}][${auth_details.name}] Proxying request ${requestDetail._req.method} ${requestDetail.url}`);
269269
const response = await send_request_via_browser(
270270
auth_details.browser_id,
271271
true,
@@ -277,7 +277,7 @@ const options = {
277277

278278
// For connection errors
279279
if (!response) {
280-
console.error(`[${datetime}][${auth_details.id}][${auth_details.name}] A connection error occurred while requesting ${requestDetail._req.method} ${requestDetail.url}`);
280+
logit(`[${auth_details.id}][${auth_details.name}] A connection error occurred while requesting ${requestDetail._req.method} ${requestDetail.url}`);
281281
return {
282282
response: {
283283
statusCode: 503,
@@ -290,7 +290,7 @@ const options = {
290290
};
291291
}
292292

293-
console.log(`[${datetime}][${auth_details.id}][${auth_details.name}] Got response ${response.status} ${requestDetail.url}`);
293+
logit(`[${auth_details.id}][${auth_details.name}] Got response ${response.status} ${requestDetail.url}`);
294294

295295
let encoded_body_buffer = new Buffer(response.body, 'base64');
296296
let decoded_body = encoded_body_buffer.toString('ascii');
@@ -319,7 +319,7 @@ const options = {
319319
};
320320

321321
async function initialize_new_browser_connection(ws) {
322-
console.log(`Authenticating newly-connected browser...`);
322+
logit(`Authenticating newly-connected browser...`);
323323

324324
// Authenticate the newly-connected client.
325325
const auth_result = await authenticate_client(ws);
@@ -351,7 +351,7 @@ async function initialize_new_browser_connection(ws) {
351351
This is to make the user's first use experience much easier so
352352
they can easily try out the functionality.
353353
*/
354-
console.log(`Browser ID ${browser_id} is not already registered. Creating new credentials for it...`);
354+
logit(`Browser ID ${browser_id} is not already registered. Creating new credentials for it...`);
355355

356356
const new_username = `botuser${get_secure_random_string(8)}`;
357357
const new_password = get_secure_random_string(18);
@@ -393,7 +393,7 @@ async function initialize() {
393393
"host": process.env.REDIS_HOST,
394394
});
395395
redis_client.on("error", function(error) {
396-
console.error(`Redis client encountered an error:`);
396+
logit(`Redis client encountered an error:`);
397397
console.error(error);
398398
});
399399
subscriber = redis.createClient({
@@ -410,12 +410,12 @@ async function initialize() {
410410

411411
// Called when a new redis subscription is added
412412
subscriber.on("subscribe", function(channel, count) {
413-
//console.log(`New subscription created for channel ${channel}, bring total to ${count}.`);
413+
//logit(`New subscription created for channel ${channel}, bring total to ${count}.`);
414414
});
415415

416416
// Called when a new message is written to a channel
417417
subscriber.on("message", function(channel, message) {
418-
//console.log(`Received a new message at channel '${channel}', message is '${message}'`);
418+
//logit(`Received a new message at channel '${channel}', message is '${message}'`);
419419

420420
// For messages being sent to the browser from the proxy
421421
if (channel.startsWith('TOBROWSER_')) {
@@ -434,9 +434,9 @@ async function initialize() {
434434
message
435435
);
436436
} catch (e) {
437-
console.error(`Error parsing message received from browser:`);
438-
console.error(`Message: ${message}`);
439-
console.error(`Exception: ${e}`);
437+
logit(`Error parsing message received from browser:`);
438+
logit(`Message: ${message}`);
439+
logit(`Exception: ${e}`);
440440
}
441441

442442
// Check if it's an action we recognize.
@@ -447,7 +447,7 @@ async function initialize() {
447447

448448
// Check if we're tracking this response
449449
if (REQUEST_TABLE.has(inbound_message.id)) {
450-
//console.log(`Resolving function for message ID ${inbound_message.id}...`);
450+
//logit(`Resolving function for message ID ${inbound_message.id}...`);
451451
const resolve = REQUEST_TABLE.take(inbound_message.id);
452452
resolve(inbound_message.result);
453453
}
@@ -460,15 +460,15 @@ async function initialize() {
460460
});
461461

462462
wss.on('connection', async function connection(ws) {
463-
console.log(`A new browser has connected to us via WebSocket!`);
463+
logit(`A new browser has connected to us via WebSocket!`);
464464

465465
ws.isAlive = true;
466466

467467
ws.on('close', async () => {
468468
// Only do this if there's a valid browser ID for
469469
// the WebSocket which has died.
470470
if (ws.browser_id) {
471-
console.log(`WebSocket browser ${ws.browser_id} has disconnected.`);
471+
logit(`WebSocket browser ${ws.browser_id} has disconnected.`);
472472

473473
// Unsubscribe from the browser topic since we can no longer send
474474
// any messages to the browser anymore
@@ -483,7 +483,7 @@ async function initialize() {
483483
browserproxy_record.is_online = false;
484484
await browserproxy_record.save();
485485
} else {
486-
console.log(`Unauthenticated WebSocket has disconnected from us.`);
486+
logit(`Unauthenticated WebSocket has disconnected from us.`);
487487
}
488488
});
489489

@@ -495,17 +495,17 @@ async function initialize() {
495495
message
496496
);
497497
} catch (e) {
498-
console.error(`Error parsing message received from browser:`);
499-
console.error(`Message: ${message}`);
500-
console.error(`Exception: ${e}`);
498+
logit(`Error parsing message received from browser:`);
499+
logit(`Message: ${message}`);
500+
logit(`Exception: ${e}`);
501501
}
502502

503503
// As a special case, if this is the result
504504
// from an authentication request, we'll process it.
505505
if (inbound_message.origin_action === 'AUTH') {
506506
// Check if we're tracking this response
507507
if (REQUEST_TABLE.has(inbound_message.id)) {
508-
//console.log(`Resolving function for message ID ${inbound_message.id}...`)
508+
//logit(`Resolving function for message ID ${inbound_message.id}...`)
509509
const resolve = REQUEST_TABLE.take(inbound_message.id);
510510
resolve(inbound_message.result);
511511
}
@@ -517,51 +517,51 @@ async function initialize() {
517517
// websocket connection.
518518
publisher.publish(`TOPROXY_${ws.browser_id}`, message);
519519
} else {
520-
console.error(`Wat, this shouldn't happen? Orphaned message:`);
521-
console.error(message);
520+
logit(`Wat, this shouldn't happen? Orphaned message (somebody might be probing you!):`);
521+
logit(message);
522522
}
523523
});
524524

525525
await initialize_new_browser_connection(ws);
526526
});
527527

528528
wss.on('ready', () => {
529-
console.log(`CursedChrome WebSocket server is now running on port ${WS_PORT}.`)
529+
logit(`CursedChrome WebSocket server is now running on port ${WS_PORT}.`)
530530
});
531531

532532
proxyServer = new AnyProxy.ProxyServer(options);
533533

534534
proxyServer.on('ready', () => {
535-
console.log(`CursedChrome HTTP Proxy server is now running on port ${PROXY_PORT}.`)
535+
logit(`CursedChrome HTTP Proxy server is now running on port ${PROXY_PORT}.`)
536536
});
537537

538538
proxyServer.on('error', (e) => {
539-
console.error(`CursedChrome HTTP Proxy server encountered an unexpected error:`);
539+
logit(`CursedChrome HTTP Proxy server encountered an unexpected error:`);
540540
console.error(e);
541541
});
542542

543-
console.log(`Starting the WebSocket server...`);
543+
logit(`Starting the WebSocket server...`);
544544

545-
console.log(`Starting the HTTP proxy server...`)
545+
logit(`Starting the HTTP proxy server...`)
546546
proxyServer.start();
547547

548-
console.log(`Starting API server...`);
548+
logit(`Starting API server...`);
549549

550550
// Start the API server
551551
const api_server = await get_api_server();
552552

553553
api_server.listen(API_SERVER_PORT, () => {
554-
console.log(`CursedChrome API server is now listening on port ${API_SERVER_PORT}`);
554+
logit(`CursedChrome API server is now listening on port ${API_SERVER_PORT}`);
555555
});
556556
}
557557

558558
(async () => {
559559
// If we're the master process spin up workers
560560
// If we're the worker processes, get to work!
561561
if (cluster.isMaster) {
562-
console.log(`Master ${process.pid} is running`);
562+
logit(`Master ${process.pid} is running`);
563563

564-
console.log(`Initializing the database connection...`);
564+
logit(`Initializing the database connection...`);
565565
await database_init();
566566

567567
// Fork workers.
@@ -570,12 +570,12 @@ async function initialize() {
570570
}
571571

572572
cluster.on('exit', (worker, code, signal) => {
573-
console.log(`worker ${worker.process.pid} died`);
573+
logit(`worker ${worker.process.pid} died`);
574574
});
575575
} else {
576576
// Start worker
577577
initialize();
578-
console.log(`Worker ${process.pid} started`);
578+
logit(`Worker ${process.pid} started`);
579579
}
580580
})();
581581

utils.js

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
const crypto = require('crypto');
22
const bcrypt = require('bcrypt');
3+
const moment = require('moment');
34

45
function copy(input_data) {
56
return JSON.parse(JSON.stringify(input_data));
@@ -24,8 +25,16 @@ async function get_hashed_password(password) {
2425
);
2526
}
2627

28+
function logit(input_string) {
29+
const datetime = moment().format('MMMM Do YYYY, h:mm:ss a');
30+
// Add spacer unless it starts with a `[`
31+
const spacer = input_string.startsWith('[') ? '' : ' ';
32+
console.log(`[${datetime}]${spacer}${input_string.trim()}`);
33+
}
34+
2735
module.exports = {
2836
copy: copy,
2937
get_secure_random_string: get_secure_random_string,
30-
get_hashed_password: get_hashed_password
38+
get_hashed_password: get_hashed_password,
39+
logit: logit
3140
};

0 commit comments

Comments
 (0)