This repository has been archived by the owner on Sep 7, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
proxy.js
728 lines (602 loc) · 21.4 KB
/
proxy.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
// Copyright 2018 Google LLC
//
// 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
//
// http://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 http = require('http');
var url = require('url');
var util = require('util');
var path = require('path');
var EventEmitter = require('events').EventEmitter;
var Rels = require('zetta-rels');
var WsQueryHandler = require('./query_ws_handler.js');
var HttpQueryHandler = require('./query_http_handler.js');
var PeerManagementHandler = require('./peer_management_handler');
var TargetAllocation = require('./target_allocation');
var RouterStateHandler = require('./proxy_state_handler');
var WsEventStreamHandler = require('./event_stream_ws_handler');
var parseUri = require('./parse_uri');
var joinUri = require('./join_uri');
var getBody = require('./get_body');
var getTenantId = require('./get_tenant_id');
var confirmWs = require('./confirm_ws');
var statusCode = require('./status_code');
var sirenResponse = require('./siren_response');
var RouterCache = require('./router_cache');
var ws = require('ws');
function parseSubscription(hash) {
var arr = hash.split(':');
return {
tenantId: decodeURIComponent(arr[0]),
targetName: decodeURIComponent(arr[1]),
targetHost: decodeURIComponent(arr[2]),
url: decodeURIComponent(arr[3])
};
}
var Proxy = module.exports = function(serviceRegistryClient, routerClient, versionClient, statsClient, targetMonitor) {
EventEmitter.call(this);
var self = this;
this._serviceRegistryClient = serviceRegistryClient;
this._routerClient = routerClient;
this._versionClient = versionClient;
this._statsClient = statsClient;
this._currentVersion = null;
this._server = http.createServer();
this._routerCache = new RouterCache();
this._cache = {};
this._subscriptions = {};
this._servers = {};
this._peerSockets = [];
this._targetMonitor = targetMonitor;
this._targetAllocation = new TargetAllocation(this);
this.peerActivityTimeout = 60000;
this._setup();
};
util.inherits(Proxy, EventEmitter);
Proxy.prototype._setup = function() {
var self = this;
var wsQueryHandler = new WsQueryHandler(this);
var httpQueryHandler = new HttpQueryHandler(this);
var routerStateHandler= new RouterStateHandler(this);
var peerManagementHandler = new PeerManagementHandler(this);
var wsEventStreamHandler = new WsEventStreamHandler(this);
this._server.on('upgrade', function(request, socket) {
socket.allowHalfOpen = false;
if (/^\/peers\//.test(request.url)) {
self._proxyPeerConnection(request, socket);
} else {
var receiver = new ws.Receiver();
socket.on('data', function(buf) {
receiver.add(buf);
});
// request from client to close websocket
receiver.onclose = function(code, data, flags) {
socket.end();
};
// handle ping requests
receiver.onping = function(data, flags) {
var sender = new ws.Sender(socket);
sender.pong(data, { binary: flags.binary === true }, true);
};
socket.once('close', function() {
receiver.cleanup();
});
if (/^\/events/.test(request.url)) {
var parsed = url.parse(request.url, true);
if(parsed.query.hasOwnProperty('topic')) {
wsQueryHandler.wsQuery(request, socket, receiver);
} else {
wsEventStreamHandler.connection(request, socket, receiver);
}
} else if (/^\/peer-management/.test(request.url)) {
peerManagementHandler.routeWs(request, socket, receiver);
} else {
self._proxyEventSubscription(request, socket, receiver);
}
}
});
this._server.on('request', function(request, response) {
var parsed = url.parse(request.url, true);
if (parsed.pathname === '/') {
if (parsed.query.ql) {
httpQueryHandler.serverQuery(request, response, parsed);
} else {
self._serveRoot(request, response);
}
} else if (parsed.pathname === '/state') {
routerStateHandler.request(request, response);
} else if (/^\/peer-management/.test(request.url)) {
peerManagementHandler.routeHttp(request, response, parsed);
} else {
self._proxyRequest(request, response);
}
});
this._versionClient.on('change', function(versionObject) {
self._currentVersion = versionObject.version;
});
this._versionClient.get(function(err, versionObject) {
if(err) {
return;
}
self._currentVersion = versionObject.version;
self.emit('version-update', self._currentVersion);
});
self._routerClient.on('change', function(results) {
// Clear cache b/c we have full list of router from results
self._routerCache.reset();
results.forEach(function(obj) {
self._routerCache.set(obj.tenantId, obj.name, obj.url);
});
self._disconnectStaleWsClients();
self.emit('router-update', self._routerCache);
});
self._serviceRegistryClient.on('change', function(results) {
self._processServerList(results);
self.emit('services-update');
});
this._loadServers(function() {
self.emit('services-update');
});
setInterval(function() {
var counts = {};
self._peerSockets.forEach(function(peerObj) {
if (!counts[peerObj.tenantId]) {
counts[peerObj.tenantId] = 0;
}
counts[peerObj.tenantId]++;
});
Object.keys(counts).forEach(function(tenant) {
self._statsClient.gauge('ws.peers', counts[tenant], { tenant: tenant });
});
var wsCounts = {};
Object.keys(self._subscriptions).forEach(function(topicHash) {
var parsed = parseSubscription(topicHash);
if (!wsCounts[parsed.tenantId]) {
wsCounts[parsed.tenantId] = 0;
}
wsCounts[parsed.tenantId]++;
});
Object.keys(wsCounts).forEach(function(tenant) {
self._statsClient.gauge('ws.event', wsCounts[tenant], { tenant: tenant });
});
}, 5000);
};
Proxy.prototype._processServerList = function(servers) {
var tempServers = {};
servers.forEach(function(server) {
if (!server.tenantId) {
return;
}
if (!tempServers.hasOwnProperty(server.tenantId)) {
tempServers[server.tenantId] = [];
}
tempServers[server.tenantId].push(server);
});
this._servers = tempServers;
};
Proxy.prototype._loadServers = function(cb) {
var self = this;
this._serviceRegistryClient.find('cloud-target', function(err, results) {
// TODO: Add some resiliency here.
if (err) {
if (cb) {
cb(err);
}
return;
}
if (!results) {
if (cb) {
cb();
}
return;
}
self._processServerList(results);
//self._shuffleServers();
if (cb) {
cb();
}
});
};
Proxy.prototype._next = function(tenantId, cb) {
var self = this;
return this._targetAllocation.lookup(tenantId, cb);
}
Proxy.prototype._proxyPeerConnection = function(request, socket) {
var self = this;
var parsed = url.parse(request.url, true);
var targetName;
var tenantId = getTenantId(request);
var match = /^\/peers\/(.+)$/.exec(request.url);
if (match) {
targetName = decodeURIComponent(/^\/peers\/(.+)$/.exec(parsed.pathname)[1]);
}
socket.on('error', function(err) {
console.error('Peer Socket Error:', tenantId, targetName, err);
});
self._statsClient.increment('http.req.peer', { tenant: tenantId });
this._routerClient.get(tenantId, targetName, function(err, peer) {
if (err && (!err.error || err.error.errorCode !== 100) ) {
socket.end('HTTP/1.1 500 Server Error\r\n\r\n\r\n');
self._statsClient.increment('http.req.peer.status.5xx', { tenant: tenantId });
return;
}
if (peer) {
socket.end('HTTP/1.1 409 Peer Conflict\r\n\r\n\r\n');
self._statsClient.increment('http.req.peer.status.4xx', { tenant: tenantId });
return;
}
self._next(tenantId, function(err, serverUrl) {
if(err) {
console.error('Peer Socket Failed to allocated target:', err);
socket.end('HTTP/1.1 503 Service Unavailable\r\n\r\n\r\n');
self._statsClient.increment('http.req.peer.status.5xx', { tenant: tenantId });
return;
}
if (!serverUrl) {
socket.end('HTTP/1.1 503 Service Unavailable\r\n\r\n\r\n');
self._statsClient.increment('http.req.peer.status.5xx', { tenant: tenantId });
return;
}
var server = url.parse(serverUrl);
var options = {
method: request.method,
headers: request.headers,
hostname: server.hostname,
port: server.port,
path: parsed.path
};
var target = http.request(options);
// Handle non 101 responses from target
target.on('response', function(targetResponse) {
var code = targetResponse.statusCode;
var responseLine = 'HTTP/1.1 ' + code + ' ' + http.STATUS_CODES[code];
var headers = Object.keys(targetResponse.headers).map(function(header) {
return header + ': ' + targetResponse.headers[header];
});
socket.write(responseLine + '\r\n' + headers.join('\r\n') + '\r\n\r\n');
targetResponse.pipe(socket);
});
target.on('upgrade', function(targetResponse, upgradeSocket, upgradeHead) {
var timer = null;
var code = targetResponse.statusCode;
var peerObj = { tenantId: tenantId,
targetName: targetName,
upgradeSocket: upgradeSocket,
socket: socket
};
var cleanup = function() {
clearInterval(timer);
var idx = self._peerSockets.indexOf(peerObj);
if (idx >= 0) {
self._peerSockets.splice(idx, 1);
}
self._routerClient.remove(tenantId, targetName, function(err) {});
};
self._statsClient.increment('http.req.peer.status.1xx', { tenant: tenantId });
var responseLine = 'HTTP/1.1 ' + code + ' ' + http.STATUS_CODES[code];
var headers = Object.keys(targetResponse.headers).map(function(header) {
return header + ': ' + targetResponse.headers[header];
});
upgradeSocket.on('error', function(err) {
console.error('Target Socket Error:', tenantId, targetName, err);
cleanup();
});
upgradeSocket.on('timeout', function() {
console.error('Target Socket Timeout:', tenantId, targetName);
upgradeSocket.destroy();
socket.end();
});
upgradeSocket.setTimeout(self.peerActivityTimeout);
socket.write(responseLine + '\r\n' + headers.join('\r\n') + '\r\n\r\n');
upgradeSocket.pipe(socket).pipe(upgradeSocket);
socket.on('close', cleanup);
upgradeSocket.on('close', cleanup);
if (code === 101) {
self._peerSockets.push(peerObj);
self._routerClient.add(tenantId, targetName, serverUrl, function(err) {});
timer = setInterval(function() {
self._routerClient.add(tenantId, targetName, serverUrl, function(err) {});
}, 60000);
} else {
socket.end();
}
});
target.on('error', function() {
var responseLine = 'HTTP/1.1 500 Internal Server Error\r\n\r\n\r\n';
socket.end(responseLine);
self._statsClient.increment('http.req.peer.status.5xx', { tenant: tenantId });
});
request.pipe(target);
});
});
};
Proxy.prototype.listen = function() {
this._server.listen.apply(this._server, Array.prototype.slice.call(arguments));
};
// disconnects client ws where the hub no longer exists or
// exists on a different target server
Proxy.prototype._disconnectStaleWsClients = function() {
var self = this;
// make sure target exists in router and is the same as what is subscribed to
Object.keys(this._cache).forEach(function(hash) {
var obj = parseSubscription(hash);
var serverUrl = self._routerCache.get(obj.tenantId, obj.targetName);
if (serverUrl === undefined || serverUrl !== obj.targetHost) {
// end subscription to zetta target, will close all clients
self._cache[hash].end();
}
});
};
Proxy.prototype._proxyEventSubscription = function(request, socket) {
var self = this;
var parsed = url.parse(request.url, true);
var targetName;
var tenantId = getTenantId(request);
var match = /^\/servers\/(.+)$/.exec(request.url);
if (match) {
targetName = decodeURIComponent(/^\/servers\/(.+)$/.exec(parsed.pathname)[1].split('/')[0]);
} else {
self._statsClient.increment('http.req.event.status.4xx', { tenant: tenantId });
var responseLine = 'HTTP/1.1 404 Server Not Found\r\n\r\n\r\n';
socket.end(responseLine);
return;
}
this.lookupPeersTarget(tenantId, targetName, function(err, serverUrl) {
if (err) {
self._statsClient.increment('http.req.event.status.5xx', { tenant: tenantId });
var responseLine = 'HTTP/1.1 500 Internal Server Error\r\n\r\n\r\n';
socket.end(responseLine);
return;
}
if (!serverUrl) {
self._statsClient.increment('http.req.event.status.4xx', { tenant: tenantId });
var responseLine = 'HTTP/1.1 404 Server Not Found\r\n\r\n\r\n';
socket.end(responseLine);
return;
}
var urlHash = [tenantId, targetName, serverUrl, request.url].map(encodeURIComponent).join(':');
if (!self._subscriptions.hasOwnProperty(urlHash)) {
self._subscriptions[urlHash] = [];
}
socket.on('error', function(err) {
console.error('Client Socket Error:', tenantId, targetName, err);
});
function removeSocketFromCache() {
var idx = self._subscriptions[urlHash].indexOf(socket);
if (idx >= 0) {
self._subscriptions[urlHash].splice(idx, 1);
}
if (self._subscriptions[urlHash].length === 0) {
if (self._cache[urlHash]) {
self._cache[urlHash].end();
}
delete self._subscriptions[urlHash];
delete self._cache[urlHash];
}
}
var cleanup = function() {};
socket.on('close', function() {
cleanup();
});
if (self._cache.hasOwnProperty(urlHash)) {
confirmWs(request, socket);
self._subscriptions[urlHash].push(socket);
self._statsClient.increment('http.req.event.status.1xx', { tenant: tenantId });
cleanup = function() {
removeSocketFromCache();
};
return;
}
var server = url.parse(serverUrl);
var options = {
method: request.method,
headers: request.headers,
hostname: server.hostname,
port: server.port,
path: parsed.path
};
var target = http.request(options);
cleanup = function () {
target.abort();
};
target.on('upgrade', function(targetResponse, upgradeSocket, upgradeHead) {
var code = targetResponse.statusCode;
var responseLine = 'HTTP/1.1 ' + code + ' ' + http.STATUS_CODES[code];
var headers = Object.keys(targetResponse.headers).map(function(header) {
return header + ': ' + targetResponse.headers[header];
});
self._statsClient.increment('http.req.event.status.1xx', { tenant: tenantId });
socket.write(responseLine + '\r\n' + headers.join('\r\n') + '\r\n\r\n');
self._cache[urlHash] = upgradeSocket;
self._subscriptions[urlHash].push(socket);
cleanup = function() {
removeSocketFromCache();
};
upgradeSocket.on('data', function(data) {
if (self._subscriptions[urlHash]) {
self._subscriptions[urlHash].forEach(function(socket) {
socket.write(data);
});
}
});
upgradeSocket.on('close', function() {
delete self._cache[urlHash];
if (self._subscriptions[urlHash]) {
self._subscriptions[urlHash].forEach(function(socket) {
socket.end();
});
}
});
upgradeSocket.on('error', function(err) {
console.error('Target Ws Socket Error:', tenantId, targetName, err);
});
});
target.on('error', function() {
var responseLine = 'HTTP/1.1 500 Internal Server Error\r\n\r\n\r\n';
socket.end(responseLine);
});
request.pipe(target);
});
};
Proxy.prototype._proxyRequest = function(request, response) {
var self = this;
var parsed = url.parse(request.url, true);
var targetName;
var tenantId = getTenantId(request);
var startTime = new Date().getTime();
var match = /^\/servers\/(.+)$/.exec(request.url);
if (match) {
targetName = decodeURIComponent(/^\/servers\/(.+)$/.exec(parsed.pathname)[1].split('/')[0]);
} else {
self._statsClient.increment('http.req.proxy.status.4xx', { tenant: tenantId });
response.statusCode = 404;
response.end();
return;
}
this.lookupPeersTarget(tenantId, targetName, function(err, serverUrl) {
if (err) {
self._statsClient.increment('http.req.proxy.status.4xx', { tenant: tenantId });
response.statusCode = 404;
response.end();
return;
}
var server = url.parse(serverUrl);
var options = {
method: request.method,
headers: request.headers,
hostname: server.hostname,
port: server.port,
path: parsed.path
};
var target = http.request(options);
// close target req if client is closed before target finishes
response.on('close', function() {
target.abort();
});
target.on('response', function(targetResponse) {
response.statusCode = targetResponse.statusCode;
Object.keys(targetResponse.headers).forEach(function(header) {
response.setHeader(header, targetResponse.headers[header]);
});
var duration = new Date().getTime() - startTime;
self._statsClient.timing('http.req.proxy', duration, { tenant: tenantId, targetName: targetName });
self._statsClient.increment('http.req.proxy.status.' + statusCode(response.statusCode), { tenant: tenantId, targetName: targetName });
targetResponse.pipe(response);
});
target.on('error', function() {
self._statsClient.increment('http.req.proxy.status.5xx', { tenant: tenantId, targetName: targetName });
response.statusCode = 500;
response.end();
});
request.pipe(target);
});
};
Proxy.prototype._serveRoot = function(request, response) {
var self = this;
var tenantId = getTenantId(request);
var startTime = new Date().getTime();
var body = {
class: ['root'],
links: [
{
rel: ['self'],
href: parseUri(request)
},
{
rel: [ Rels.peerManagement ],
href: joinUri(request, '/peer-management')
},
{
rel: [ Rels.events ],
href: joinUri(request, '/events').replace(/^http/,'ws')
}
]
};
var clientAborted = false;
request.on('close', function() {
clientAborted = true;
});
var entities = this._routerClient.findAll(tenantId, function(err, results) {
if (clientAborted) {
return;
}
if (results) {
results.forEach(function(peer) {
body.links.push({
title: peer.name,
rel: [Rels.peer, Rels.server],
href: joinUri(request, '/servers/' + peer.name)
});
});
}
body.actions = [
{
name: 'query-devices',
method: 'GET',
href: parseUri(request),
type: 'application/x-www-form-urlencoded',
fields: [
{ name: 'server', type: 'text' },
{ name: 'ql', type: 'text' }
]
}
];
var duration = new Date().getTime()-startTime;
self._statsClient.timing('http.req.root', duration, { tenant: tenantId });
self._statsClient.increment('http.req.root.status.2xx', { tenant: tenantId });
sirenResponse(response, 200, body);
});
};
// Return all targets for a tenantId with the current active version and any targets that have
// peers currently connected.
Proxy.prototype.activeTargets = function(tenantId) {
var self = this;
var activeServers = [];
// Get all target servers from routerCache
this._routerCache.keys(tenantId).forEach(function(obj) {
activeServers.push(self._routerCache.get(obj.tenantId, obj.targetName));
});
if (!this._servers.hasOwnProperty(tenantId)) {
return activeServers;
}
return this._servers[tenantId].filter(function(server) {
if (server.version === self._currentVersion || activeServers.indexOf(server.url) >= 0) {
// Only return online servers
return self._targetMonitor.status(server.url);
}
});
};
// Return all targets for a tenantId with the current version
Proxy.prototype.targets = function(tenantId) {
var self = this;
if (!this._servers.hasOwnProperty(tenantId)) {
return [];
}
return this._servers[tenantId].filter(function(server) {
return server.version === self._currentVersion && self._targetMonitor.status(server.url);
});
};
Proxy.prototype.lookupPeersTarget = function(tenantId, targetName, cb) {
var self = this;
var serverUrl = this._routerCache.get(tenantId, targetName);
if (serverUrl === undefined) {
this._routerClient.get(tenantId, targetName, function(err, serverUrl) {
if (serverUrl) {
self._routerCache.set(tenantId, targetName, serverUrl);
cb(null, serverUrl);
} else {
cb(new Error('No server found.'));
}
});
} else {
cb(null, serverUrl);
}
}