-
Notifications
You must be signed in to change notification settings - Fork 8
Expand file tree
/
Copy pathcommon.js
More file actions
662 lines (547 loc) · 17.9 KB
/
common.js
File metadata and controls
662 lines (547 loc) · 17.9 KB
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
/*
Copyright NetFoundry, Inc.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
https://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
var common = exports,
env = require('../env'),
url = require('url'),
required = require('requires-port'),
fs = require('fs'),
requestIp = require('request-ip'),
find = require('lodash.find'),
{ isEqual, isUndefined } = require('lodash'),
swpjson = require('@openziti/ziti-browzer-sw/package.json'),
getAccessToken = require('../../lib/oidc/utils').getAccessToken,
ZITI_CONSTANTS = require('../../lib/edge/constants'),
ZitiContext = require('../../lib/edge/context'),
Mutex = require('async-mutex').Mutex,
withTimeout = require('async-mutex').withTimeout,
pjson = require('../../package.json');
var zitiContextMutex = withTimeout(new Mutex(), 3 * 1000, new Error('timeout on zitiContextMutex'));
var upgradeHeader = /(^|,)\s*upgrade\s*($|,)/i,
isSSL = /^https/;
/**
* Simple Regex for testing if protocol is https
*/
common.isSSL = isSSL;
common.getConfigValue = function( ...args ) {
return env( args );
}
/**
*
*/
var logLevelMap = new Map();
var logLevel = env('ZITI_BROWZER_RUNTIME_LOGLEVEL');
logLevelMap.set('*', logLevel);
function mapEntriesToString(entries) {
return Array
.from(entries, ([k, v]) => `${k}:${v}, `)
.join("") + "";
}
common.logLevelSet = function (key, val) {
logLevelMap.set(key, val);
};
common.logLevelGet = function () {
return mapEntriesToString(logLevelMap.entries());
};
common.logLevelGetForClient = function (client) {
let level = logLevelMap.get(client);
if (typeof level === 'undefined') {
level = logLevel ? logLevel : 'error';
}
return level;
};
common.removeValue = function(list, value, separator) {
if (typeof list === 'undefined') return list;
separator = separator || ",";
var values = list.split(separator);
for(var i = 0 ; i < values.length ; i++) {
if(values[i].trim() == value) {
values.splice(i, 1);
return values.join(separator);
}
}
return list;
}
/**
* Copies the right headers from `options` and `req` to
* `outgoing` which is then used to fire the proxied
* request.
*
* Examples:
*
* common.setupOutgoing(outgoing, options, req)
* // => { host: ..., hostname: ...}
*
* @param {Object} Outgoing Base object to be filled with required properties
* @param {Object} Options Config object passed to the proxy
* @param {ClientRequest} Req Request Object
* @param {String} Forward String to select forward or target
*
* @return {Object} Outgoing Object with all required properties set
*
* @api private
*/
common.setupOutgoing = function(outgoing, options, req, forward) {
outgoing.port = options[forward || 'target'].port ||
(isSSL.test(options[forward || 'target'].protocol) ? 443 : 80);
[
'host',
'hostname',
'socketPath',
'pfx',
'key',
'passphrase',
'cert',
'ca',
'ciphers',
'secureProtocol',
'protocol'
].forEach(
function(e) { outgoing[e] = options[forward || 'target'][e]; }
);
outgoing.method = options.method || req.method;
outgoing.headers = Object.assign({}, req.headers);
if (options.headers){
Object.assign(outgoing.headers, options.headers);
}
// Prevent this from being sent (results in 500 errors on initial TSPlus requests)
delete outgoing.headers['if-modified-since'];
// Prevent 'br' from being sent as a viable 'Accept-Encoding' (we do not support the Brotli algorithm here)
let val = common.removeValue(outgoing.headers['accept-encoding'], 'br');
if (val === "") {
delete outgoing.headers['accept-encoding'];
} else {
outgoing.headers['accept-encoding'] = val;
}
if (options.auth) {
outgoing.auth = options.auth;
}
if (options.ca) {
outgoing.ca = options.ca;
}
if (isSSL.test(options[forward || 'target'].protocol)) {
outgoing.rejectUnauthorized = (typeof options.secure === "undefined") ? true : options.secure;
}
outgoing.agent = options.agent || false;
outgoing.localAddress = options.localAddress;
//
// Remark: If we are false and not upgrading, set the connection: close. This is the right thing to do
// as node core doesn't handle this COMPLETELY properly yet.
//
if (!outgoing.agent) {
outgoing.headers = outgoing.headers || {};
if (typeof outgoing.headers.connection !== 'string'
|| !upgradeHeader.test(outgoing.headers.connection)
) { outgoing.headers.connection = 'close'; }
}
// the final path is target path + relative path requested by user:
var target = options[forward || 'target'];
var targetPath = target && options.prependPath !== false
? (target.path || '')
: '';
var outgoingPath = !options.toProxy
? (url.parse(req.url).path || '')
: req.url;
//
// Remark: ignorePath will just straight up ignore whatever the request's
// path is. This can be labeled as FOOT-GUN material if you do not know what
// you are doing and are using conflicting options.
//
outgoingPath = !options.ignorePath ? outgoingPath : '';
outgoing.path = common.urlJoin(targetPath, outgoingPath);
if (options.changeOrigin) {
outgoing.headers.host =
required(outgoing.port, options[forward || 'target'].protocol) && !hasPort(outgoing.host)
? outgoing.host + ':' + outgoing.port
: outgoing.host;
}
return outgoing;
};
/**
* Generates the config for the Ziti browZer Runtime
*
*
* @return {Object} config Object
*
* @api private
*/
common.generateZitiConfig = function(url, client) {
var zc = common.generateZitiConfigObject(url, client);
var ziti_config = `var zitiConfig = ` + JSON.stringify(zc);
return ziti_config;
}
common.getZBRname = function() {
try {
let pathToZitiBrowzerRuntimeModule = require.resolve('@openziti/ziti-browzer-runtime');
pathToZitiBrowzerRuntimeModule = pathToZitiBrowzerRuntimeModule.substring(0, pathToZitiBrowzerRuntimeModule.lastIndexOf('/'));
let zbrName;
fs.readdirSync(pathToZitiBrowzerRuntimeModule).forEach(file => {
if (file.startsWith('ziti-browzer-runtime')) {
zbrName = file;
}
});
return zbrName;
}
catch (e) {
console.error(e);
}
}
common.getZBRCSSname = function() {
try {
let pathToZitiBrowzerRuntimeModule = require.resolve('@openziti/ziti-browzer-runtime');
pathToZitiBrowzerRuntimeModule = pathToZitiBrowzerRuntimeModule.substring(0, pathToZitiBrowzerRuntimeModule.lastIndexOf('/'));
let zbrCSSName;
fs.readdirSync(pathToZitiBrowzerRuntimeModule).forEach(file => {
if (file.startsWith('ziti-browzer-css-')) {
zbrCSSName = file;
}
});
return zbrCSSName;
}
catch (e) {
console.error(e);
}
}
common.generateZitiConfigObject = function(url, req, options) {
var client = requestIp.getClientIp(req);
var zitiClient = client || '*';
var browzer_bootstrapper_host = req.get('host');
var u = new URL(`https://${browzer_bootstrapper_host}`);
var target;
if (env('ZITI_BROWZER_BOOTSTRAPPER_WILDCARD_VHOSTS')) {
target = find(options.targetArray, {
vhost: `*`
});
target_service = req.ziti_vhost;
target_path = target.path;
target_scheme = target.scheme;
} else {
target = find(options.targetArray, {
vhost: u.hostname
});
if (typeof target === 'undefined') {
options.logger.error({message: 'Host header has no match in targets array', host: browzer_bootstrapper_host});
target_service = 'UNKNOWN';
target_path = '/';
target_scheme = 'https';
} else {
target_service = target.service;
target_path = target.path;
target_scheme = target.scheme;
}
}
var ziti_controller_host = env('ZITI_CONTROLLER_HOST');
var ziti_controller_port = env('ZITI_CONTROLLER_PORT');
var browzer_bootstrapper_scheme = env('ZITI_BROWZER_BOOTSTRAPPER_SCHEME');
var browzer_bootstrapper_listen_port = env('ZITI_BROWZER_BOOTSTRAPPER_LISTEN_PORT');
var idp_issuer_url = req.ziti_idp_issuer_base_url;
var browzer_load_balancer = env('ZITI_BROWZER_LOAD_BALANCER_HOST');
var browzer_load_balancer_port = env('ZITI_BROWZER_LOAD_BALANCER_PORT');
if (!browzer_load_balancer_port) {
browzer_load_balancer_port = 443;
}
let selfHost;
if (env('ZITI_BROWZER_BOOTSTRAPPER_WILDCARD_VHOSTS')) {
selfHost = `${req.ziti_vhost}.${ common.trimFirstSection( env('ZITI_BROWZER_BOOTSTRAPPER_HOST') )}`
} else {
selfHost = `${target.vhost}`;
}
let whitelabel = JSON.parse( env('ZITI_BROWZER_WHITELABEL'));
let svgurl = new URL(whitelabel.branding.browZerButtonIconSvgUrl);
if (isEqual(browzer_load_balancer, svgurl.hostname)) {
svgurl.hostname = selfHost;
whitelabel.branding.browZerButtonIconSvgUrl = svgurl.toString();
}
let cssurl = new URL(whitelabel.branding.browZerCSS);
if (isEqual(browzer_load_balancer, cssurl.hostname)) {
cssurl.hostname = selfHost;
whitelabel.branding.browZerCSS = cssurl.toString();
}
var ziti_config =
{
controller: {
api: `https://${ziti_controller_host}:${ziti_controller_port}/edge/client/v1`
},
browzer: {
bootstrapper: {
self: {
scheme: `${browzer_bootstrapper_scheme}`,
host: `${selfHost}`,
port: `${browzer_bootstrapper_listen_port}`,
version: `${pjson.version}`,
latestReleaseVersion: options.getLatestBrowZerReleaseVersion(),
},
target: {
service: `${target_service}`,
path: `${target_path}`,
scheme: `${target_scheme}`
},
},
sw: {
location: `ziti-browzer-sw.js`,
version: `${swpjson.version}`,
logLevel: `${common.logLevelGetForClient(zitiClient)}`,
},
runtime: {
src: `${common.getZBRname()}`,
css: `${common.getZBRCSSname()}`,
logLevel: `${common.logLevelGetForClient(zitiClient)}`,
skipDeprecationWarnings: env('ZITI_BROWZER_BOOTSTRAPPER_SKIP_DEPRECATION_WARNINGS'),
},
loadbalancer: {
host: browzer_load_balancer ? `${browzer_load_balancer}` : undefined,
port: browzer_load_balancer ? `${browzer_load_balancer_port}` : undefined
},
whitelabel: whitelabel,
},
idp: {
host: `${idp_issuer_url}`,
clientId: `${req.ziti_idp_client_id}`,
authorization_endpoint_parms: req.ziti_idp_authorization_endpoint_parms ? `${req.ziti_idp_authorization_endpoint_parms}` : undefined,
authorization_scope: req.ziti_idp_authorization_scope ? `${req.ziti_idp_authorization_scope}` : undefined,
}
};
return ziti_config;
}
/**
* Set the proper configuration for sockets,
* set no delay and set keep alive, also set
* the timeout to 0.
*
* Examples:
*
* common.setupSocket(socket)
* // => Socket
*
* @param {Socket} Socket instance to setup
*
* @return {Socket} Return the configured socket.
*
* @api private
*/
common.setupSocket = function(socket) {
socket.setTimeout(0);
socket.setNoDelay(true);
socket.setKeepAlive(true, 0);
return socket;
};
/**
* Get the port number from the host. Or guess it based on the connection type.
*
* @param {Request} req Incoming HTTP request.
*
* @return {String} The port number.
*
* @api private
*/
common.getPort = function(req) {
var res = req.headers.host ? req.headers.host.match(/:(\d+)/) : '';
return res ?
res[1] :
common.hasEncryptedConnection(req) ? '443' : '80';
};
/**
* Check if the request has an encrypted connection.
*
* @param {Request} req Incoming HTTP request.
*
* @return {Boolean} Whether the connection is encrypted or not.
*
* @api private
*/
common.hasEncryptedConnection = function(req) {
return Boolean(req.connection.encrypted || req.connection.pair);
};
/**
* OS-agnostic join (doesn't break on URLs like path.join does on Windows)>
*
* @return {String} The generated path.
*
* @api private
*/
common.urlJoin = function() {
//
// We do not want to mess with the query string. All we want to touch is the path.
//
var args = Array.prototype.slice.call(arguments),
lastIndex = args.length - 1,
last = args[lastIndex],
lastSegs = last.split('?'),
retSegs;
args[lastIndex] = lastSegs.shift();
//
// Join all strings, but remove empty strings so we don't get extra slashes from
// joining e.g. ['', 'am']
//
retSegs = [
args.filter(Boolean).join('/')
.replace(/\/+/g, '/')
.replace('http:/', 'http://')
.replace('https:/', 'https://')
];
// Only join the query string if it exists so we don't have trailing a '?'
// on every request
// Handle case where there could be multiple ? in the URL.
retSegs.push.apply(retSegs, lastSegs);
return retSegs.join('?')
};
/**
* Rewrites or removes the domain of a cookie header
*
* @param {String|Array} Header
* @param {Object} Config, mapping of domain to rewritten domain.
* '*' key to match any domain, null value to remove the domain.
*
* @api private
*/
common.rewriteCookieProperty = function rewriteCookieProperty(header, config, property) {
if (Array.isArray(header)) {
return header.map(function (headerElement) {
return rewriteCookieProperty(headerElement, config, property);
});
}
return header.replace(new RegExp("(;\\s*" + property + "=)([^;]+)", 'i'), function(match, prefix, previousValue) {
var newValue;
if (previousValue in config) {
newValue = config[previousValue];
} else if ('*' in config) {
newValue = config['*'];
} else {
//no match, return previous value
return match;
}
if (newValue) {
//replace value
return prefix + newValue;
} else {
//remove value
return '';
}
});
};
/**
* Check the host and see if it potentially has a port in it (keep it simple)
*
* @returns {Boolean} Whether we have one or not
*
* @api private
*/
function hasPort(host) {
return !!~host.indexOf(':');
};
/**
* Determine of a value is a boolean or not
*/
common.toBool = function (item) {
switch(typeof item) {
case "boolean":
return item;
case "function":
return true;
case "number":
return item > 0 || item < 0;
case "object":
return !!item;
case "string":
item = item.toLowerCase();
return ["true", "1"].indexOf(item) >= 0;
case "symbol":
return true;
case "undefined":
return false;
default:
throw new TypeError("Unrecognised type: unable to convert to boolean");
}
};
/**
* Generates the config for the Ziti browZer Runtime
*
*/
common.generateAccessControlAllowOrigin = function(req) {
return `https://${req.ziti_vhost}`;
}
/**
* Determine if 'path' is one on the 'target path'
*/
common.isRequestOnTargetPath = function( req, options, path ) {
let result = false;
if (req.ziti_target_path) {
let pathNoQuery = path.replace(/\?.*$/,"");
let regex = new RegExp( pathNoQuery + '$', 'g' );
let hit = (req.ziti_target_path.match(regex) || []).length;
if ((hit > 0)) {
options.logger.silly({message: 'common.isRequestOnTargetPath: HIT on path', path: path, clientIp: requestIp.getClientIp(req), method: req.method, url: req.url});
result = true;
}
}
return result;
}
common.addServerHeader = function(headerObj) {
return Object.assign(headerObj, {
'Server': `ziti-browzer-bootstrapper/${pjson.version}`
});
}
/** --------------------------------------------------------------------------------------------------
* Spin up a fresh zitiContext
*/
var zitiContext;
common.newZitiContext = async ( logger ) => {
await zitiContextMutex.runExclusive(async () => {
// If we have an active context, release any associated resources
if (!isUndefined(zitiContext)) {
logger.silly({message: `now destroying zitiContext[${zitiContext._uuid}] - apiSessionHeartbeat has been cleared`});
clearTimeout(zitiContext.apiSessionHeartbeatId);
zitiContext.deleted = true;
zitiContext = undefined;
}
// Instantiate/initialize the zitiContext we will use to obtain Service info from the Controller
zitiContext = new ZitiContext(Object.assign({
logger: logger,
controllerApi: `https://${env('ZITI_CONTROLLER_HOST')}:${env('ZITI_CONTROLLER_PORT')}/edge/client/v1`,
token_type: `Bearer`,
access_token: await getAccessToken( logger ),
}));
await zitiContext.initialize( {} );
// Monitor M2M JWT expiration events
zitiContext.on(ZITI_CONSTANTS.ZITI_EVENT_IDP_AUTH_HEALTH, common.idpAuthHealthEventHandler);
await zitiContext.ensureAPISession();
});
};
/** --------------------------------------------------------------------------------------------------
* Refresh the M2M IdP access token if it has expired
*/
common.idpAuthHealthEventHandler = async ( idpAuthHealthEvent ) => {
if (!idpAuthHealthEvent.zitiContext.deleted) {
if (idpAuthHealthEvent.expired) {
common.newZitiContext( idpAuthHealthEvent.zitiContext.logger );
}
}
};
common.setZitiContext = function(context) {
zitiContext = context;
}
common.getZitiContext = async function() {
let ctx;
await zitiContextMutex.runExclusive( () => {
ctx = zitiContext;
});
return ctx;
}
common.trimFirstSection = function(hostname) {
const sections = hostname.split('.');
sections.shift();
const trimmedHostname = sections.join('.');
return trimmedHostname;
}
common.delay = function(time) {
return new Promise(resolve => setTimeout(resolve, time));
}