1
1
const NodeCache = require ( "node-cache" ) ;
2
2
const AnyProxy = require ( './anyproxy' ) ;
3
3
const cluster = require ( 'cluster' ) ;
4
- const moment = require ( 'moment' ) ;
5
4
const WebSocket = require ( 'ws' ) ;
6
5
const https = require ( 'https' ) ;
7
6
const redis = require ( "redis" ) ;
@@ -18,6 +17,7 @@ const Sequelize = require('sequelize');
18
17
const Op = Sequelize . Op ;
19
18
20
19
const get_secure_random_string = require ( './utils.js' ) . get_secure_random_string ;
20
+ const logit = require ( './utils.js' ) . logit ;
21
21
22
22
const get_api_server = require ( './api-server.js' ) . get_api_server ;
23
23
@@ -184,7 +184,8 @@ async function get_authentication_status(inputRequestDetail) {
184
184
) ;
185
185
186
186
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 ) ;
188
189
return false ;
189
190
}
190
191
@@ -226,7 +227,7 @@ async function get_authentication_status(inputRequestDetail) {
226
227
} ) ;
227
228
228
229
if ( ! browserproxy_record ) {
229
- console . log ( `Invalid credentials for username '${ username } '!` ) ;
230
+ logit ( `Invalid credentials for username '${ username } '!` ) ;
230
231
return false ;
231
232
}
232
233
@@ -250,12 +251,11 @@ const options = {
250
251
rule : {
251
252
async beforeSendRequest ( requestDetail ) {
252
253
const remote_address = requestDetail . _req . connection . remoteAddress ;
253
- const datetime = moment ( ) . format ( 'MMMM Do YYYY, h:mm:ss a' ) ;
254
254
255
255
const auth_details = await get_authentication_status ( requestDetail . requestOptions . headers ) ;
256
256
257
257
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!`) ;
259
259
return AUTHENTICATION_REQUIRED_PROXY_RESPONSE ;
260
260
}
261
261
@@ -265,7 +265,7 @@ const options = {
265
265
requestDetail . requestData . length > 0
266
266
) ? requestDetail . requestData . toString ( 'base64' ) : false ;
267
267
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 } `) ;
269
269
const response = await send_request_via_browser (
270
270
auth_details . browser_id ,
271
271
true ,
@@ -277,7 +277,7 @@ const options = {
277
277
278
278
// For connection errors
279
279
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 } `) ;
281
281
return {
282
282
response : {
283
283
statusCode : 503 ,
@@ -290,7 +290,7 @@ const options = {
290
290
} ;
291
291
}
292
292
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 } `) ;
294
294
295
295
let encoded_body_buffer = new Buffer ( response . body , 'base64' ) ;
296
296
let decoded_body = encoded_body_buffer . toString ( 'ascii' ) ;
@@ -319,7 +319,7 @@ const options = {
319
319
} ;
320
320
321
321
async function initialize_new_browser_connection ( ws ) {
322
- console . log ( `Authenticating newly-connected browser...` ) ;
322
+ logit ( `Authenticating newly-connected browser...` ) ;
323
323
324
324
// Authenticate the newly-connected client.
325
325
const auth_result = await authenticate_client ( ws ) ;
@@ -351,7 +351,7 @@ async function initialize_new_browser_connection(ws) {
351
351
This is to make the user's first use experience much easier so
352
352
they can easily try out the functionality.
353
353
*/
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...` ) ;
355
355
356
356
const new_username = `botuser${ get_secure_random_string ( 8 ) } ` ;
357
357
const new_password = get_secure_random_string ( 18 ) ;
@@ -393,7 +393,7 @@ async function initialize() {
393
393
"host" : process . env . REDIS_HOST ,
394
394
} ) ;
395
395
redis_client . on ( "error" , function ( error ) {
396
- console . error ( `Redis client encountered an error:` ) ;
396
+ logit ( `Redis client encountered an error:` ) ;
397
397
console . error ( error ) ;
398
398
} ) ;
399
399
subscriber = redis . createClient ( {
@@ -410,12 +410,12 @@ async function initialize() {
410
410
411
411
// Called when a new redis subscription is added
412
412
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}.`);
414
414
} ) ;
415
415
416
416
// Called when a new message is written to a channel
417
417
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}'`);
419
419
420
420
// For messages being sent to the browser from the proxy
421
421
if ( channel . startsWith ( 'TOBROWSER_' ) ) {
@@ -434,9 +434,9 @@ async function initialize() {
434
434
message
435
435
) ;
436
436
} 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 } ` ) ;
440
440
}
441
441
442
442
// Check if it's an action we recognize.
@@ -447,7 +447,7 @@ async function initialize() {
447
447
448
448
// Check if we're tracking this response
449
449
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}...`);
451
451
const resolve = REQUEST_TABLE . take ( inbound_message . id ) ;
452
452
resolve ( inbound_message . result ) ;
453
453
}
@@ -460,15 +460,15 @@ async function initialize() {
460
460
} ) ;
461
461
462
462
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!` ) ;
464
464
465
465
ws . isAlive = true ;
466
466
467
467
ws . on ( 'close' , async ( ) => {
468
468
// Only do this if there's a valid browser ID for
469
469
// the WebSocket which has died.
470
470
if ( ws . browser_id ) {
471
- console . log ( `WebSocket browser ${ ws . browser_id } has disconnected.` ) ;
471
+ logit ( `WebSocket browser ${ ws . browser_id } has disconnected.` ) ;
472
472
473
473
// Unsubscribe from the browser topic since we can no longer send
474
474
// any messages to the browser anymore
@@ -483,7 +483,7 @@ async function initialize() {
483
483
browserproxy_record . is_online = false ;
484
484
await browserproxy_record . save ( ) ;
485
485
} else {
486
- console . log ( `Unauthenticated WebSocket has disconnected from us.` ) ;
486
+ logit ( `Unauthenticated WebSocket has disconnected from us.` ) ;
487
487
}
488
488
} ) ;
489
489
@@ -495,17 +495,17 @@ async function initialize() {
495
495
message
496
496
) ;
497
497
} 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 } ` ) ;
501
501
}
502
502
503
503
// As a special case, if this is the result
504
504
// from an authentication request, we'll process it.
505
505
if ( inbound_message . origin_action === 'AUTH' ) {
506
506
// Check if we're tracking this response
507
507
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}...`)
509
509
const resolve = REQUEST_TABLE . take ( inbound_message . id ) ;
510
510
resolve ( inbound_message . result ) ;
511
511
}
@@ -517,51 +517,51 @@ async function initialize() {
517
517
// websocket connection.
518
518
publisher . publish ( `TOPROXY_${ ws . browser_id } ` , message ) ;
519
519
} 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 ) ;
522
522
}
523
523
} ) ;
524
524
525
525
await initialize_new_browser_connection ( ws ) ;
526
526
} ) ;
527
527
528
528
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 } .` )
530
530
} ) ;
531
531
532
532
proxyServer = new AnyProxy . ProxyServer ( options ) ;
533
533
534
534
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 } .` )
536
536
} ) ;
537
537
538
538
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:` ) ;
540
540
console . error ( e ) ;
541
541
} ) ;
542
542
543
- console . log ( `Starting the WebSocket server...` ) ;
543
+ logit ( `Starting the WebSocket server...` ) ;
544
544
545
- console . log ( `Starting the HTTP proxy server...` )
545
+ logit ( `Starting the HTTP proxy server...` )
546
546
proxyServer . start ( ) ;
547
547
548
- console . log ( `Starting API server...` ) ;
548
+ logit ( `Starting API server...` ) ;
549
549
550
550
// Start the API server
551
551
const api_server = await get_api_server ( ) ;
552
552
553
553
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 } ` ) ;
555
555
} ) ;
556
556
}
557
557
558
558
( async ( ) => {
559
559
// If we're the master process spin up workers
560
560
// If we're the worker processes, get to work!
561
561
if ( cluster . isMaster ) {
562
- console . log ( `Master ${ process . pid } is running` ) ;
562
+ logit ( `Master ${ process . pid } is running` ) ;
563
563
564
- console . log ( `Initializing the database connection...` ) ;
564
+ logit ( `Initializing the database connection...` ) ;
565
565
await database_init ( ) ;
566
566
567
567
// Fork workers.
@@ -570,12 +570,12 @@ async function initialize() {
570
570
}
571
571
572
572
cluster . on ( 'exit' , ( worker , code , signal ) => {
573
- console . log ( `worker ${ worker . process . pid } died` ) ;
573
+ logit ( `worker ${ worker . process . pid } died` ) ;
574
574
} ) ;
575
575
} else {
576
576
// Start worker
577
577
initialize ( ) ;
578
- console . log ( `Worker ${ process . pid } started` ) ;
578
+ logit ( `Worker ${ process . pid } started` ) ;
579
579
}
580
580
} ) ( ) ;
581
581
0 commit comments