This repository has been archived by the owner on Dec 13, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 32
/
Copy pathcbt_tunnels.js
executable file
·757 lines (696 loc) · 28.5 KB
/
cbt_tunnels.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
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
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
var net = require('net'),
util = require('util'),
tls = require('tls'),
fs = require('fs'),
connection_list = {},
_ = require('lodash'),
gfx = require('./gfx.js'),
warn = gfx.warn,
utils = require('./utils.js'),
WebSocket = require('ws'),
proxyAgent = require('https-proxy-agent'),
os = require('os'),
crypto = require('crypto'),
version = require('./package.json').version;
function pad(n, width, z) {
z = z || '0';
n = n + '';
return n.length >= width ? n : new Array(width - n.length + 1).join(z) + n;
}
function padHeaderLength(lengthNumber) {
while (lengthNumber.length < 4) {
lengthNumber = '0' + lengthNumber;
}
return lengthNumber;
}
// takes an header object and packs it alongside binary blob
function packData(obj, dataReceived) {
var binaryObject = Buffer.from(JSON.stringify(obj));
var paddedLength = padHeaderLength(String(binaryObject.byteLength));
var objectLength = Buffer.from(paddedLength);
if (!dataReceived) {
return Buffer.concat([objectLength, binaryObject]);
}
return Buffer.concat([objectLength, binaryObject, dataReceived]);
}
function unpackData(binaryData) {
// look at first three bytes to get the length of the header
var length = binaryData.slice(0,4);
length = parseInt(length);
var headerData = binaryData.slice(4, length+4);
headerData = JSON.parse(headerData);
if (length + 4 == binaryData.byteLength){
headerData.data = null;
} else {
headerData.data = binaryData.slice(length+4, binaryData.byteLength);
}
return headerData;
}
function cbtSocket(api, params) {
var inbound;
var outbound;
var self = this;
var killLever = utils.killLever(self);
params.context = self;
self.tunnelId = params.tid;
self.api = api;
function getInbound(){
return inbound;
}
function getOutbound(){
return outbound;
}
self.startStaticServer = function(attempt){
if (!attempt){ attempt = 0 };
self.localServe = require('express')();
self.serveDir = require('serve-index');
self.serveStatic = require('serve-static');
self.directory = params.directory;
var sPort = self.sPort = params.port || 8080+attempt;
self.localServe.use('/', self.serveDir(self.directory, {'icons': true, 'hidden': true, 'view':'details'}));
self.localServe.use('/', self.serveStatic(self.directory));
self.server = self.localServe.listen(sPort);
self.server.on('error',function(err){
if(err.code == 'EADDRINUSE' && attempt<9 && (!params.port)){
warn('Port '+(8080+attempt)+' in use');
self.startStaticServer(attempt+1);
}else if(attempt>=9){
warn('Tried serving local directory on ports 8080-8089--all appear to be in use. Please specify a custom port using the --port flag.');
self.endWrap();
}else if(params.port){
warn(params.port+' in use. Please choose a different port.');
self.endWrap();
}else{
warn('Error starting webserver.');
self.endWrap();
}
});
self.server.on('listening',function(){
global.logger.info('Server listening on port '+sPort+', serving '+self.directory+'.');
})
}
var tType = self.tType = params.tType;
self.auth_header = (Buffer.from(params.username+':'+params.authkey)).toString('base64');
// not used elsewhere
self.t = params.t;
self.userId = params.userId;
self.authkey = params.authkey;
self.qPort = (params.bytecode ? pad((params.tcpPort-11000),3) : pad((params.tcpPort-11000), 3));
self.wsPort = params.tcpPort+1000;
self.cbtServer = 'https://'+params.cbtServer;
self.cbtApp = 'https://'+params.urls.node;
self.path = '/wsstunnel' + self.qPort + '/socket.io';
self.query = 'userid=' + self.userId + '&authkey=' + self.authkey;
self.wsPath = self.cbtServer+self.path+'?'+self.query;
if (global.isLocal) {
self.wsPath = params.wssUrl;
global.logger.info(`change wsPath to ${self.path}`);
}
self.tunnelapi = params.urls.node+'/api/v3/tunnels/'+params.tid;
var proxyAuthString = self.proxyAuthString = '';
self.nokill = params.nokill;
if(!!params.proxyUser && !!params.proxyPass){
proxyAuthString = self.proxyAuthString = 'Proxy-Authorization: Basic ' + (Buffer.from(params.proxyUser + ':' + params.proxyPass)).toString('base64');
}
self.ready = params.ready;
switch(tType){
case 'simple':
break;
case 'webserver':
self.startStaticServer();
break;
case 'tunnel':
var tType = self.tType = 'tunnel';
var port = self.proxyPort = params.proxyPort;
var host = self.proxyHost = params.proxyIp;
break;
default:
}
var conn = self.conn = null;
if (process.env.http_proxy || process.env.https_proxy){
var agent = makeProxyAgent();
conn = self.conn = new WebSocket(self.wsPath,{agent: agent});
}else{
conn = self.conn = new WebSocket(self.wsPath,{ perMessageDeflate: false });
}
self.conn.bufferType = "arraybuffer";
if(!params.rejectUnauthorized){
conn.rejectUnauthorized = false;
}
var sendLog = self.sendLog = function(log){
var dataToServer = {
event:'clientLog',
client_verbose_log: log
}
var payload = packData(dataToServer, Buffer.from([]))
conn.send(payload);
}
self.start = function(cb){
if(proxyAuthString !== ''){
global.logger.debug('Using basic authentication for proxy server mode.');
// sendLog('Using basic authentication for proxy server mode.');
}
var reconnecting = false;
var reconnectAttempts = 0;
var ping = setInterval(function(){
conn.ping();
},10000);
global.logger.debug('Started connection attempt!');
conn.on('message',function(message){
try{
/*
the first hello we get will be a string,
we'll respond with a buffer, and from that
point forward, we'll be talking buffers.
*/
if (_.isString(message)) {
global.logger.debug("Incoming message is string");
msg = JSON.parse(message);
} else {
msg = unpackData(message)
}
self.handleMessage(msg);
}catch(e){
warn(e.message);
warn('Could not parse websocket message:');
global.logger.error(util.inspect(message));
}
})
conn.on('close',function(data){
reconnecting = true;
clearInterval(ping);
if(!params.verbose&&!params.quiet && params.cmd){
clearInterval(self.drawTimeout);
self.spin(null,'Disconnected from CBT server.\n');
}else{
warn('Disconnected from CBT server.\n');
}
connection_list = {};
});
conn.on('error',function(e){
global.logger.error('WebSocket error!');
cb(e);
});
conn.on('open',function(){
if(params.pac){
global.logger.info('Connecting using PAC file.');
}else{
global.logger.info('Connecting as '+self.tType+'.');
}
if(!reconnecting){
cb(null,self);
sendLog('node tunnel client connected.');
} else {
reconnecting = false;
clearInterval(self.drawTimeout);
}
if(!!self.ready){
global.logger.info('Setting ready file: '+self.ready);
fs.open(self.ready,'wx',function(err,fd){
if(err){
warn('The path specified for the "ready" file already exists or cannot be created (likely for permissions issues).');
self.endWrap();
}else{
fs.close(fd, function(err){
if(err){
global.logger.error(err);
self.endWrap();
}
sendLog('ready file written: '+self.ready);
});
}
});
}
});
}
self.handleMessage = function(msg){
var data = null,
id = null;
if(!!msg.data){
data = msg.data;
id = data.id;
}
var wsid = msg.wsid;
switch(msg.event){
case 'hello':
var dataToServer = {
event: 'established',
wsid: wsid,
}
// versions of cbt_tunnels > 1.0.0 send their version to server.js on hello. - CC
var payload = packData(dataToServer, Buffer.from(version));
conn.send(payload);
break;
case 'versions':
var checkResult = utils.checkVersion(msg,params);
sendLog(checkResult);
if(checkResult.includes('dead')){
self.endWrap();
}
break;
case 'check':
global.logger.debug('Received check request!');
sendLog('node client received check request.');
self.api.checkTunnelIp((err, resp) => {
if(err){
warn('IP check error!');
global.logger.error(util.inspect(response));
warn(err);
var data = err;
var dataToServer = {
event: 'checkrecv',
wsid: wsid
}
var payload = packData(dataToServer, data)
conn.send(payload);
} else {
try{
global.logger.debug('IP appears to CBT as: '+resp.ip);
var dataToServer = {
event: 'checkrecv',
ip: resp.ip,
wsid: wsid
}
var payload = packData(dataToServer, Buffer.from([]))
conn.send(payload);
}catch(e){
warn('Parsing response failed: '+e);
var dataToServer = {
event: 'checkrecv',
error: e,
wsid: wsid
}
var payload = packData(dataToServer, Buffer.from([]))
conn.send(payload);
}
}
});
break;
case 'legitdead':
warn('User requested ending this tunnel.');
sendLog('User requested ending this tunnel via UI. Theoretically. Server.js got a SIGINT.');
self.endWrap();
break;
case 'data':
self.handleData(msg);
break;
default:
warn('Unknown message type:');
global.logger.error(util.inspect(msg));
throw new Error('Unknown message type: '+msg);
}
}
self.handleData = function(msg){
var data = msg;
var id = msg.id;
var wsid = msg.wsid;
if (!connection_list[id]) {
connection_list[id] = { id : data.id , client : null };
connection_list[id].established = false;
}
if(socketExists(id) && data._type === 'end'){
if(connection_list[data.id].client){
global.logger.debug(id+" client ended by CBT server.");
sendLog(''+id+' tcp client ended by CBT server.');
connection_list[id].established=false;
connection_list[id].client.end();
connection_list[id].client.destroy();
connection_list[id].ended=true;
}
return;
}
if( (data._type != 'end') && (!connection_list[id].established) && (!connection_list[id].ended) ){
inbound += 1;
utils.determineHost({host:data.host,port:data.port,proxyHost:self.proxyHost,proxyPort:self.proxyPort,tType:self.tType},params,function(err,hostInfo){
if(err){
sendLog(err);
warn(err);
}
var host = self.host = hostInfo.host;
var port = self.port = hostInfo.port;
if(host === 'local' && self.tType === 'webserver'){
host = 'localhost';
port = self.sPort;
}else if(host === 'local'){
host = 'localhost';
}
global.logger.debug('Creating TCP socket on: \n'+data._type+' '+host+' '+port+' '+id);
sendLog('Creating TCP socket on: '+data._type+' '+host+' '+port+' '+id);
connection_list[id].manipulateHeaders = hostInfo.manipulateHeaders;
connection_list[id].host = data.host;
connection_list[id].port = data.port;
connection_list[id].connected = false;
var client = self.client = connection_list[id].client = net.createConnection({allowHalfOpen:true, port: port, host: host},function(err){
if(err){
global.logger.error(err);
}
connection_list[id].established = true;
connection_list[id].ended = false;
var dataToServer = {
event: 'ack ack ack',
id : id,
finished : false,
wsid: wsid
}
var payload = packData(dataToServer, Buffer.from([]))
conn.send(payload);
global.logger.debug('Created TCP socket: '+data._type+' '+host+' '+port+' '+id);
sendLog('Created TCP socket: '+data._type+' '+host+' '+port+' '+id);
});
client.on('error',function(error){
global.logger.debug('Error on '+id+'!');
global.logger.debug(error.stack);
sendLog('Error on TCP socket '+id+'\n'+error.stack);
var dataToServer = {
event: 'htmlrecv',
id : id,
finished : true,
wsid: wsid
}
var payload = packData(dataToServer, Buffer.from([]))
conn.send(payload);
connection_list[id].established=false;
client.end();
connection_list[id].ended=true;
});
client.on('data', function(dataRcvd){
if(socketExists(id)){
global.logger.debug('TCP socket '+id+' received data: Port:'+port+' Host:'+host);
var dataToServer = {
event: 'htmlrecv',
id : id,
finished : false,
wsid: wsid
}
var payload = packData(dataToServer, dataRcvd)
self.isConnected(dataRcvd,id,function(err,connected){
if(err){
throw err;
}else if(!err&&!connected){
conn.send(payload);
global.logger.debug('TCP socket '+id+' internet data emitted to server.js!');
}else if(connected){
connection_list[id].connected = true;
}
});
}
});
client.setTimeout(1000000);
client.on('timeout',function(data){
global.logger.debug('TCP socket '+id+' session timed out.');
sendLog('TCP socket '+id+' session timed out.');
var dataToServer = {
event: 'htmlrecv',
id : id,
finished : true,
wsid: wsid
}
var payload = packData(dataToServer, Buffer.from([]))
conn.send(payload);
client.write('end');
client.end();
client.destroy();
if(connection_list[id]){
connection_list[id].established=false;
connection_list[id].ended=true;
}
});
client.on('end', function(data,err){
if(socketExists(id)){
global.logger.debug(err);
global.logger.debug('TCP socket '+id+' ended by external site.');
sendLog('TCP socket '+id+' ended by external site.');
var dataToServer = {
event: 'htmlrecv',
id : id,
finished : true,
wsid: wsid
}
var payload = packData(dataToServer, Buffer.from([]))
conn.send(payload);
connection_list[id].established=false;
client.write('end');
client.end();
connection_list[id].ended=true;
}
});
client.on('close', function(err){
if(socketExists(id)){
global.logger.debug('TCP socket '+id+' closed by external site.');
sendLog('TCP socket '+id+' closed by external site.');
if(err&¶ms.verbose){
global.logger.debug('Error on close of TCP socket: '+id);
sendLog('Error on close of TCP socket: '+id);
}
var dataToServer = {
event: 'htmlrecv',
id : id,
finished : true,
wsid: wsid
}
var payload = packData(dataToServer, Buffer.from([]))
conn.send(payload);
connection_list[id].established=false;
client.write('end');
client.end();
connection_list[id].ended=true;
}
});
});
}
if((socketExists(id)&&data.data)||(data._type==='bytesonly')){
var client = connection_list[id].client;
var receivedDataString = Buffer.from(data.data).toString();
if( (data._type === 'bytesonly') && (proxyAuthString !== '') && (receivedDataString.includes('Host')) ){
data = self.addProxyAuth(data, receivedDataString);
}
if(connection_list[id].manipulateHeaders){
data = self.manipulateHeaders(data);
}
self.isTLSHello(connection_list[id],data.data,id,function(err){
if(!err){
var bufferToSend = Buffer.from(data.data.toJSON());
client.write(bufferToSend, function(err){
if(err){
sendLog('Error writing data to: '+util.inspect(client)+' '+util.inspect(err));
var dataToServer = {
event: 'htmlrecv',
id : id,
data : null,
finished : true,
wsid: wsid
}
connection_list[id].established=false;
client.end();
client.destroy();
connection_list[id].ended=true;
}
outbound+=1;
global.logger.debug('Wrote to TCP socket '+id);
});
}else{
global.logger.debug('TLS error:');
global.logger.debug(util.inspect(err));
throw err;
}
});
}
}
self.spin = function(old,msg){
inbound = 0;
outbound = 0;
gfx.draw(getInbound(), getOutbound(), old, msg, self.tType);
self.drawTimeout = setInterval(function(){
gfx.draw(getInbound(), getOutbound(), old, msg, self.tType);
inbound = 0;
outbound = 0;
}, 1000);
process.stdout.on('resize', function() {
clearInterval(self.drawTimeout);
gfx.draw(getInbound(), getOutbound(), old, msg, self.tType);
self.drawTimeout = setInterval(function(){
gfx.draw(getInbound(), getOutbound(), old, msg, self.tType);
inbound = 0;
outbound = 0;
}, 1000);
});
}
self.addProxyAuth = function(data, dataStr){
dataArr = dataStr.split('\r\n');
dataArr = _.filter(dataArr, function(col){
if(!col==''){
return col
}
});
dataArr.push(proxyAuthString);
dataStr = dataArr.join('\r\n');
dataStr+='\r\n\r\n';
data.data = Buffer.from(dataStr,'ascii');
return data;
}
self.manipulateHeaders = function(data){
var dataArr = [];
data.data.map((char)=>{
dataArr.push(String.fromCharCode(char));
})
dataStr = dataArr.join('');
dataArr = dataStr.split('\r\n');
dataArr = _.filter(dataArr, function(col){
if(!col==''){
return col
}
});
var method = dataArr[0].includes('GET') ? 'GET' : (dataArr[0].includes('POST') ? 'POST' : (dataArr[0].includes('PUT') ? 'PUT' : (dataArr[0].includes('DELETE') ? 'DELETE' : (dataArr[0].includes('OPTIONS') ? 'OPTIONS' : null))));
if(method){
var host = dataArr.find((element)=>{
return (element.includes('host:')||element.includes('Host:')||element.includes('HOST:'))
});
host = host.split('Host:')[1].replace(' ','');
host = !(host.includes('http://')&&host.includes('https://')) ? 'http://'+host : host;
dataArr[0] = dataArr[0].replace(method+' ','GET '+host);
dataStr = dataArr.join('\r\n');
dataStr+='\r\n\r\n';
data.data = Buffer.from(dataStr,'ascii');
return data;
}
return data
}
self.buildConnect = function(destination){
var connect = "CONNECT "+destination+" HTTP/1.1\r\nhost: "+destination+"\r\nConnection: close\r\n\r\n";
return connect;
}
self.isConnected = function(packet,id,cb){
if(connection_list[id].manipulateHeaders){
var dataArr = [];
packet.map((char)=>{
dataArr.push(String.fromCharCode(char));
});
dataStr = dataArr.join('');
if(dataStr.includes('Connection established')){
cb(null,true);
}else{
cb(null,false);
}
}else{
cb(null,false);
}
}
self.isTLSHello = function(connection,packet,id,cb){
//&&(packet[4]===0x7C||packet[4]===0x7C)
if((packet[0]===22&&packet[5]===191 && connection_list[id].manipulateHeaders)){
var client = connection.client;
global.logger.debug(id+' This is a TLS HELLO! Sending CONNECT...');
sendLog('Client found TLS hello on: '+id);
var bufferToSend = Buffer.from(self.buildConnect(connection.host+':'+connection.port));
client.write(bufferToSend, function(err){
if(err&¶ms.verbose){
global.logger.debug('Error writing data to: ');
global.logger.debug(util.inspect(client));
global.logger.debug(util.inspect(err));
sendLog('Error writing data to: '+util.inspect(client)+' '+util.inspect(err));
var dataToServer = {
event: 'htmlrecv',
id : id,
data : null,
finished : true,
wsid: wsid
}
connection_list[id].established=false;
client.end();
client.destroy();
connection_list[id].ended=true;
cb(err);
}
outbound+=1;
global.logger.debug('Wrote to TCP socket '+id);
var connectedInterval = setInterval(function(){
if(connection_list[id].connected){
if(params.verbose){
global.logger.debug(id+' Received connection established!');
}
clearInterval(connectedInterval);
connection_list[id].connected = false;
cb(null);
}
},1);
});
}else{
cb(null);
}
}
self.end = function(cb){
clearInterval(self.drawTimeout);
clearInterval(self.ping);
self.api.deleteTunnel(self.tunnelId, (err, deleteResp) => {
if (!!self.server){
self.server.close();
};
for(connection in connection_list){
if(socketExists(connection.id)){
connection.client.end();
}
};
if (err){
global.logger.error(err);
return cb(err);
} else {
return cb(null, 'killit');
}
});
if(self.ready){
fs.unlink(self.ready, function(err){
if(err){
global.logger.error(err);
if (!self.nokill){
setTimeout(function(){
process.exit(1);
},10000);
}
}
})
}
}
self.endWrap = function(cb){
self.end(function(err, killit){
if(!err && killit === 'killit'){
global.logger.info('Local connection disconnected.');
if(cb) cb(null,true)
if(!self.nokill){
global.logger.info('Bye!');
process.exit(0);
}
if(cb) cb(null,true)
}else if(err){
global.logger.error(err);
if(cb) cb(err,false)
if(!self.nokill){
setTimeout(function(){
process.exit(1);
}, 10000);
}
}
});
}
function socketExists(id){
return (
(!!connection_list[id]) // connection list has property `id`
&& (!!connection_list[id].client) // id has property client
&& (!connection_list[id].ended) // id property ended is false
&& (connection_list[id].established) // id property established is true
&& (Object.getOwnPropertyNames(connection_list[id].client.address()).length > 0)
)
}
function makeProxyAgent(){
var pString = process.env.http_proxy || process.env.https_proxy;
pString = pString.replace('http://','');
var agentAuth;
if(pString.includes('@')){
agentAuth = pString.slice(0,pString.indexOf('@'));
}
var agentHost = pString.slice(pString.indexOf('@')+1,pString.lastIndexOf(':'));
var agentPort = pString.slice(pString.lastIndexOf(':')+1);
var agent = new proxyAgent({host:agentHost,port:agentPort,auth:agentAuth,secureProxy:true});
return agent;
}
}
module.exports = cbtSocket;