-
Notifications
You must be signed in to change notification settings - Fork 7
/
router.js
704 lines (570 loc) · 19.1 KB
/
router.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
"use strict";
var sslConfig = require('ssl-config')('modern'),
http = require('http'),
https = require('https'),
httpProxy = require('http-proxy'),
async = require('async'),
crypto = require('crypto'),
tls = require('tls'),
//express = require('express'),
spawn = require('child_process').spawn,
spawnSync = require('spawn-sync'),
fs = require('fs'),
path = require('path'),
fsAccess = require('fs-access'),
mkdirp = require('mkdirp'),
colors = require('colors'),
urlParser = require('url'),
proxy,
configFilePath,
Router,
router;
configFilePath = __dirname + '/config.json';
// If certs fail after updating this version, run rm -rf /etc/letsencrypt to remove all cache and then start again
// TODO: Need to create a timer here to run certbot-auto renew every so often
/*
./certbot-auto renew --webroot --webroot-path ./ssl/
May also need these for an automatic script to stop upgrading etc: --quiet --no-self-upgrade
*/
Router = function () {
};
Router.prototype.loadConfigData = function (callback) {
var self = this,
waterfallArr = [];
// Get config file
waterfallArr.push(function (finished) {
console.log('Loading configuration data from "' + configFilePath + '"...');
fs.readFile(configFilePath, 'utf8', function (err, data) {
if (!err) {
finished(false, data);
} else {
finished('Error reading router config file: ' + err, data);
}
});
});
// Parse config file JSON
waterfallArr.push(function (data, finished) {
var configData;
console.log('Parsing configuration data...');
try {
configData = JSON.parse(data);
finished(false, configData);
} catch (e) {
finished('Configuration data is not valid JSON!');
}
});
// Check config file sections
waterfallArr.push(function (configData, finished) {
console.log('Checking configuration data...');
if (!configData.server) {
finished('Missing config "server" section!');
} else if (!configData.routerTable) {
finished('Missing config "routerTable" section!');
} else {
finished(false, configData);
}
});
// Get router table
waterfallArr.push(function (configData, finished) {
var routerTable;
console.log('Getting router table data...');
self.configData = configData;
routerTable = configData.routerTable;
finished(false, configData, routerTable);
});
// Make SSL folder
waterfallArr.push(function (configData, routerTable, finished) {
console.log('Generating SSL folder...');
// Generate the ssl folder
mkdirp('./ssl', function (err) {
// We don't care if there was an error since it usually means the folder
// already exists. We might want to actually check the err to ensure this
finished(false, configData, routerTable);
});
});
async.waterfall(waterfallArr, function (err) {
if (!err) {
callback(false);
} else {
console.log(colors.red('ERROR: ') + err);
}
});
};
Router.prototype.checkForCerts = function (callback) {
var self = this,
waterfallArr = [];
// Push a first function that just passes the configData and routerTable
// to the subsequent waterfall functions
waterfallArr.push(function (finished) {
finished(false, self.configData, self.configData.routerTable);
});
// Scan router table and check if SSL cert needs generating
waterfallArr.push(function (configData, routerTable, finished) {
var domain,
route,
certProcessArr = [],
generateGetCertFunc;
console.log('Scanning router table for SSL requirements...');
generateGetCertFunc = function (domain, generate) {
return function (finished) {
console.log(' - Checking ' + domain + ' for SSL cert...');
self.getCert(domain, generate, finished);
};
};
// Add new routes
for (domain in routerTable) {
if (routerTable.hasOwnProperty(domain)) {
route = routerTable[domain];
if (route.enabled !== false) {
if (route.target) {
console.log(colors.yellow.bold('Routing: ') + colors.green.bold(domain) + colors.yellow.bold(' => ') + colors.green.bold(route.target));
// Check if the route is secured via TLS
if (route.ssl && route.ssl.enable) {
certProcessArr.push(generateGetCertFunc(domain, route.ssl.generate));
}
} else {
return finished('Route "' + domain + '" missing "target" property!');
}
} else {
console.log('Ignoring ' + domain + ' because it is DISABLED');
}
}
}
finished(false, configData, routerTable, certProcessArr);
});
// Process the certificates for all domains
waterfallArr.push(function (configData, routerTable, certProcessArr, finished) {
console.log('Processing cert checks...');
if (self.configData.letsencrypt.test) {
console.log("+++ USING TESTING (STAGING) LETSENCRYPT SERVER +++");
}
async.series(certProcessArr, function (err) {
if (!err) {
console.log('Cert checks complete');
console.log(colors.yellow.bold('Router table config data updated successfully.'));
finished(false);
} else {
finished(err);
}
});
});
async.waterfall(waterfallArr, function (err) {
if (!err) {
callback(false);
} else {
console.log(colors.red('ERROR: ') + err);
}
});
};
Router.prototype.getCert = function (domain, generate, finished) {
var self = this;
self.checkSecureContext(domain, function (err, domain) {
if (!err) {
console.log(colors.green.bold(' - Cert for ' + domain + ' already exists, using existing cert'));
self.secureContext[domain] = self.getSecureContext(domain);
finished(false);
} else {
console.log(colors.yellow.bold(' - Cert for ' + domain + ' does not yet exist'));
// The cert doesn't exist, are we set to generate?
if (generate) {
console.log(' - Attempting to generate cert for ' + domain + '...');
self.generateCert(domain, finished);
} else {
finished(' - Unable to load ssl cert for domain: ' + domain + ' and auto-generate is switched off!');
}
}
});
};
Router.prototype.generateCert = function (domain, finished) {
var self = this,
child,
childErr = '',
childHadError = false,
calledCallback = false,
args;
// We need to generate the certificates
if (self.configData.letsencrypt && self.configData.letsencrypt.email) {
console.log(' - Certificates for ' + domain + ' do not exist, moving to create (' + self.configData.letsencrypt.email + ')...');
args = [
'certonly',
'--agree-tos',
'--email', self.configData.letsencrypt.email,
'--webroot',
'--webroot-path', './ssl/',
'--domains', domain,
'--keep',
'--quiet'
];
if (self.configData.letsencrypt.test) {
args.push('--server', 'https://acme-staging.api.letsencrypt.org/directory');
}
// Execute letsencrypt to generate certificates
//console.log('Executing: ' + 'letsencrypt certonly --agree-tos --email ' + self.configData.letsencrypt.email + ' --standalone --domains ' + domain + ' --cert-path ./ssl/:hostname.cert.pem --fullchain-path ./ssl/:hostname.fullchain.pem --chain-path ./ssl/:hostname.chain.pem');
child = spawn('./certbot-auto', args);
var finishFunc = function (code, type) {
console.log(' - Process ' + type + ' with code ' + code);
if (!childHadError) {
if (!calledCallback) {
calledCallback = true;
/*spawnSync('cp', [
'/etc/letsencrypt/live/' + domain + '/cert.pem',
'./ssl/' + domain + '.cert.pem'
]);*/
/*spawnSync('cp', [
'/etc/letsencrypt/live/' + domain + '/chain.pem',
'./ssl/' + domain + '.chain.pem'
]);*/
/*spawnSync('cp', [
'/etc/letsencrypt/live/' + domain + '/fullchain.pem',
'./ssl/' + domain + '.fullchain.pem'
]);*/
/*spawnSync('cp', [
'/etc/letsencrypt/live/' + domain + '/privkey.pem',
'./ssl/' + domain + '.key.pem'
]);*/
self.secureContext[domain] = self.getSecureContext(domain);
console.log(colors.green.bold(' - Cert generated successfully'));
finished(false);
}
} else {
if (!calledCallback) {
calledCallback = true;
finished(childErr);
}
}
};
child.stderr.on("data", function (data) {
console.log(' - Process -> ' + data);
childErr += data + "\n";
childHadError = true;
});
child.on("exit", function (code) {
console.log(' - Child process exit');
finishFunc(code, 'exit');
});
child.on("close", function (code) {
console.log(' - Child process close');
finishFunc(code, 'close');
});
child.on("error", function (e) {
console.log(' - Process error: ' + e);
child.kill();
if (!calledCallback) {
finished(false);
}
});
} else {
console.log(' - Certificates for ' + domain + ' do not exist but could not auto-create!', 'Config file is missing letsencrypt.email parameter!');
finished(' - Config file is missing letsencrypt.email parameter!');
}
};
Router.prototype.configFileEvent = function (curr, prev) {
if (curr.mtime !== prev.mtime) {
// The file has been modified so update the router table
console.log(colors.cyan.bold('Router table config data has changed, updating...'));
this.restart();
//this.loadConfigData();
}
};
Router.prototype.checkSecureContext = function (domain, callback) {
console.log(' - Checking for existing file: /etc/letsencrypt/live/' + domain + '/privkey.pem');
fsAccess('/etc/letsencrypt/live/' + domain + '/privkey.pem', function (err) {
callback(err, domain);
});
};
Router.prototype.getSecureContext = function (domain) {
var cryptoData;
var credentials = {
key: fs.readFileSync('/etc/letsencrypt/live/' + domain + '/privkey.pem'),
cert: fs.readFileSync('/etc/letsencrypt/live/' + domain + '/fullchain.pem'),
ca: fs.readFileSync('/etc/letsencrypt/live/' + domain + '/chain.pem'),
ciphers: sslConfig.ciphers,
honorCipherOrder: true,
secureOptions: sslConfig.minimumTLSVersion
};
if (tls.createSecureContext) {
cryptoData = tls.createSecureContext(credentials);
} else {
cryptoData = crypto.createCredentials(credentials).context;
}
//console.log('Got crypto', cryptoData);
return cryptoData;
};
Router.prototype.startServer = function (callback) {
var self = this;
console.log(colors.green.bold('Starting server...'));
// TODO: We need to generate default server certs here
self.httpsServer = https.createServer({
SNICallback: function (domain, callback) {
//console.log('Getting secure context for domain: ' + domain);
if (callback) {
//console.log('Calling back with:', self.secureContext[domain]);
callback(null, self.secureContext[domain]);
} else {
//console.log('Returning with:', self.secureContext[domain]);
return self.secureContext[domain];
}
},
ciphers: sslConfig.ciphers,
honorCipherOrder: true,
secureOptions: sslConfig.minimumTLSVersion
}, function (req, res) {
self.handleRequest.call(self, true, req, res);
});
self.httpServer = http.createServer(function (req, res) {
self.handleRequest.call(self, false, req, res);
});
self.httpServer.on('upgrade', function () {
self.handleUpgrade.apply(self, arguments);
});
try {
proxy = httpProxy.createProxyServer({});
} catch (e) {
console.log(colors.red('ERROR: ') + 'Proxy threw error: ' + e);
}
proxy.on('proxyError', function (err, req, res) {
var route;
if (self.configData && self.configData.routerTable) {
if (self.configData.routerTable[req.headers.host] !== null) {
route = self.configData.routerTable[req.headers.host];
if (route.errorRedirect) {
res.writeHead(302, {'Location': route.errorRedirect});
res.end();
} else {
self.doErrorResponse(404, res);
}
} else {
self.doErrorResponse(404, res);
}
} else {
self.doErrorResponse(404, res);
}
return true;
});
proxy.on('error', function (err, req, res) {
// Don't console log connection resets, they are very common
if (err.code !== 'ECONNRESET') {
console.log(colors.red('ERROR routing ' + colors.bold(req.headers.host) + ': ') + 'Proxy said: ' + err);
}
self.doErrorResponse(503, res);
});
// Config data was loaded so... start the server
self.httpServer.listen(self.configData.server.httpPort);
self.httpsServer.listen(self.configData.server.httpsPort);
console.log(colors.cyan.bold('Started HTTP server, listening on port ') + colors.yellow.bold(self.configData.server.httpPort));
console.log(colors.cyan.bold('Started HTTPS server, listening on port ') + colors.yellow.bold(self.configData.server.httpsPort));
callback(false);
};
Router.prototype.stopServer = function (callback) {
var self = this;
console.log(colors.red.bold('Stopping server...'));
self.httpsServer.close();
self.httpServer.close();
console.log(colors.cyan.bold('Stopped HTTP server'));
console.log(colors.cyan.bold('Stopped HTTPS server'));
callback(false);
};
Router.prototype.handleRequest = function (secure, req, res) {
var self = this,
route,
clientIp,
parsedRoute,
filePath,
stat,
readStream;
if (!req || !req.connection) {
res.statusCode(500).send('No');
}
clientIp = req.headers['x-forwarded-for'] ||
(req.connection && req.connection.remoteAddress ? req.connection.remoteAddress : undefined) ||
req.socket.remoteAddress ||
(req.connection && req.connection.socket && req.connection.socket.remoteAddress ? req.connection.socket.remoteAddress : undefined);
parsedRoute = urlParser.parse(req.url);
console.log(colors.yellow.bold(req.headers.host), 'from', colors.yellow(clientIp));
console.log("-->", colors.cyan(parsedRoute.path));
// Check for an ssl cert request
if (parsedRoute.path.indexOf('/.well-known/acme-challenge') === 0) {
// Requesting ssl cert data, serve it
filePath = path.join(__dirname, '/ssl', parsedRoute.path);
console.log("-->", colors.cyan('Attempting to serve: ' + filePath));
fs.exists(filePath, function (exists) {
if (exists) {
stat = fs.statSync(filePath);
res.writeHead(200, {
'Content-Type': 'application/octet-stream',
'Content-Length': stat.size
});
readStream = fs.createReadStream(filePath);
res.on('error', function(err) {
readStream.end();
});
readStream.pipe(res);
} else {
console.log("-->", colors.red('Unable to load: ' + filePath));
self.doErrorResponse(404, res);
}
});
return;
}
// Check for an entry in the router table
if (self.configData && self.configData.routerTable) {
if (self.configData.routerTable[req.headers.host] != null) { // Use != here!!
route = self.configData.routerTable[req.headers.host];
if (route.enabled !== false) {
// Ensure we pass through the host from the header
route.headers = route.headers || {};
route.headers.host = route.headers.host || req.headers.host;
// Check if we only allow secure connections to this host
if (route.ssl) {
if (route.ssl.onlySecure) {
if (!secure) {
// Check for secure redirect
if (route.ssl.insecureRedirect) {
if (typeof route.ssl.insecureRedirect === 'string') {
// Redirect to URL string
res.writeHead(302, {'Location': route.ssl.insecureRedirect});
res.end();
return;
}
// Redirect is a boolean
res.writeHead(302, {'Location': 'https://' + route.headers.host + req.url});
res.end();
return;
}
// We only allow secure connections but this is not a secure connection
return self.doErrorResponse(404, res, 'Service not available on insecure connection!');
}
}
if (secure) {
// Add headers that internal services might need (like gitlab)
req.headers['x-forwarded-ssl'] = "on";
req.headers['x-forwarded-port'] = self.configData.server.httpsPort;
}
}
if (route.target) {
try {
proxy.web(req, res, route);
console.log(colors.green('-->'), 'routed to', route.target);
} catch (e) {
console.log(colors.red('ERROR: ') + 'Routing ' + req.headers.host + ' caused error: ' + e);
}
} else {
console.log(colors.red('ERROR: ') + 'Cannot route ' + req.headers.host + ' because config entry is missing "target" property.');
return self.doErrorResponse(404, res);
}
} else {
console.log(colors.red('ERROR: ') + 'Cannot route ' + req.headers.host + ' because config entry is disabled.');
return self.doErrorResponse(404, res);
}
} else {
return self.doErrorResponse(404, res);
}
} else {
return self.doErrorResponse(404, res);
}
};
Router.prototype.handleUpgrade = function(req, socket, head) {
var self = this,
route;
// Check for an entry in the router table
if (self.configData && self.configData.routerTable) {
if (self.configData.routerTable[req.headers.host] !== null) {
route = self.configData.routerTable[req.headers.host];
if (route.enabled !== false) {
if (route.target) {
try {
proxy.ws(req, socket, head, route);
} catch (e) {
console.log(colors.red('ERROR: ') + 'Routing websocket ' + req.headers.host + ' caused error: ' + e);
}
} else {
console.log(colors.red('ERROR: ') + 'Cannot route ' + req.headers.host + ' because config entry is missing "target" property.');
}
} else {
console.log(colors.red('ERROR: ') + 'Cannot route ' + req.headers.host + ' because config entry is disabled.');
}
} else {
console.log(colors.red('ERROR: ') + 'Cannot upgrade socket for websockets because the header host does not exist in the routing table!', req.headers);
}
}
};
Router.prototype.start = function (done) {
var self = this;
self.secureContext = {};
async.series([
// Load config data
self.loadConfigData.bind(self),
// Setup servers
self.startServer.bind(self),
// Check for certificates
self.checkForCerts.bind(self),
// Setup file watchers
function (callback) {
console.log('Watching config file for changes...');
// Watch the config file for changes
fs.watchFile(configFilePath, function (curr, prev) {
self.configFileEvent(curr, prev);
});
callback(false);
}
], function (err, data) {
if (err) {
return console.log('Error starting up!', err);
}
// Startup complete
console.log(colors.cyan('Startup complete'));
if (done) { done(); }
});
};
Router.prototype.stop = function (done) {
var self = this;
async.series([
// Setup servers
self.stopServer.bind(self)
], function (err, data) {
if (err) {
return console.log('Error stopping!', err);
}
console.log(colors.cyan('Stop complete'));
if (done) { done(); }
});
};
Router.prototype.restart = function (done) {
var self = this;
async.series([
// Stop server
self.stopServer.bind(self),
// Load config data
self.loadConfigData.bind(self),
// Setup servers
self.startServer.bind(self)
], function (err) {
});
};
Router.prototype.doErrorResponse = function (code, res, errMsg) {
var self = this,
msg;
if (!errMsg) {
if (self.configData && self.configData.errors && self.configData.errors[code]) {
errMsg = self.configData.errors[code];
}
}
if (!errMsg) {
switch (code) {
case 404:
msg = 'Not found';
break;
case 503:
msg = 'Service unavailable';
break;
default:
msg = 'An error occurred';
break;
}
errMsg = code + ' ' + msg;
}
res.writeHead(code, {'Content-Type': 'text/plain; charset=utf-8'});
res.end(errMsg);
};
router = new Router();
router.start();