forked from skupperproject/skupper-proxy
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmulticast.js
433 lines (388 loc) · 14.5 KB
/
multicast.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
/*
* 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.
*/
'use strict';
var EventEmitter = require('events');
var http = require('http');
var util = require('util');
var rhea = require('rhea');
var url_parse = require('url').parse;
var log = require('./log.js').logger();
var metrics = require('./metrics.js');
var rclient = require('./request.js');
var client_ip = require('./utils.js').client_ip;
function elapsed(start) {
return Date.now() - start;
}
function AmqpToHttpBridge(address, host, port) {
log.info('Created AMQP to HTTP bridge %s => %s:%s', address, host, port);
this.host = host;
this.port = port;
this.address = address;
this.delivery_mapping = {};
var container = rhea.create_container({enable_sasl_external:true});
container.id = process.env.HOSTNAME + '_amqp:' + this.address + '=>http://' + this.host + ':' + this.port;
this.connection = container.connect();
this.connection.on('message', this.incoming.bind(this));
this.connection.on('settled', this.on_response_settled.bind(this));
this.connection.open_receiver({source:address, autoaccept:false,credit_window:10/*TODO: make configurable*/});
}
AmqpToHttpBridge.prototype.incoming = function (context) {
var start = Date.now();
var url = url_parse('http://' + context.message.to);
var headers = {};
for (var key in context.message.application_properties) {
headers[key] = context.message.application_properties[key];
}
if (context.message.content_type) {
headers['content-type'] = context.message.content_type;
}
var options = {
host: this.host,
port: this.port,
path: url.path,
method: context.message.subject,
headers: headers
};
var self = this;
var request = http.request(options, function (response) {
log.info('%s: %s', url, response.statusCode);
var message_out = {
to: context.message.reply_to,
correlation_id: context.message.correlation_id,
subject: '' + response.statusCode,
application_properties: {},
body: ''
};
for (var key in response.headers) {
if (key === 'content-type') {
message_out.content_type = response.headers['content-type'];
} else {
message_out.application_properties[key] = response.headers[key]
}
}
response.on('data', function (chunk) { message_out.body += chunk; });
response.on('end', function () {
log.info('server sending reply: %j', message_out);
var response_delivery = context.connection.send(message_out);
self.delivery_mapping[response_delivery.id] = context.delivery;
metrics.http_egress_request(self.metrics, self.host, options.method, response.statusCode, context.message.body.length, message_out.body.length, elapsed(start));
});
});
request.on('error', function (error) {
console.error(error);
context.delivery.modified();
});
request.write(context.message.body);
request.end();
};
AmqpToHttpBridge.prototype.on_response_settled = function (context) {
var response_delivery = context.delivery;
var request_delivery = this.delivery_mapping[response_delivery.id];
request_delivery.accept();
}
AmqpToHttpBridge.prototype.stop = function () {
this.connection.close();
};
function Request (client, id, request) {
EventEmitter.call(this);
this.client = client;
this.id = id;
this.request = request;
this.request.correlation_id = this.id;
this.resends = 0;
}
util.inherits(Request, EventEmitter);
Request.prototype.handle_response = function (response) {
this.emit('data', response);
log.info('Request %s got response %j', this.id, response);
};
Request.prototype.send = function () {
this.request.reply_to = this.client.address;
this.client.sender.send(this.request, this.id);
};
Request.prototype.released = function () {
log.info('Request %s released', this.id);
if (this.resends === 0) {
this.resends++;
this.send(sender);
} else if (this.resends < this.client.max_resends) {
var self = this;
setTimeout(function () {
self.send();
}, Math.min(50 * this.resends, this.client.max_resend_delay));
this.resends++;
} else {
this.failed('Could not deliver request, max resends exceeded');
}
};
Request.prototype.failed = function (error) {
log.info('Request %s failed: %s', this.id, error);
this.emit('error', error);
this.completed();
};
Request.prototype.completed = function () {
log.info('Request %s completed', this.id);
this.emit('end');
this.client.discard(this.id);
};
Request.prototype.as_promise = function () {
var self = this;
var responses = [];
var error = undefined;
return new Promise(function (resolve, reject) {
self.on('data', function (response) {
responses.push(response);
});
self.on('error', function (e) {
error = e;
});
self.on('end', function () {
if (error) {
reject(error);
} else if (responses.length === 1) {
resolve(responses[0]);
} else if (responses.length > 1) {
resolve(responses);
}
});
});
};
function bind(client, event) {
client.connection.on(event, client['on_' + event].bind(client));
}
function RequestClient (connection, target) {
this.requests = {};
this.pending = [];
this.connection = connection;
this.sender = this.connection.open_sender(target);
this.receiver = this.connection.open_receiver({source:{dynamic:true}});
this.counter = 1;
this.max_resends = 10;
this.max_resend_delay = 250;
bind(this, 'message');
bind(this, 'receiver_open');
bind(this, 'sendable');
bind(this, 'disconnected');
bind(this, 'released');
bind(this, 'rejected');
bind(this, 'modified');
bind(this, 'accepted');
};
RequestClient.prototype.discard = function (request_id) {
delete this.requests[request_id];
};
RequestClient.prototype.on_message = function (context) {
var handler = this.requests[context.message.correlation_id];
if (handler) {
handler.handle_response(context.message);
} else {
log.info('Unrecognised response: %s', context.message.correlation_id);
}
};
RequestClient.prototype.on_receiver_open = function (context) {
this.address = context.receiver.source.address;
this.send_pending_requests();
};
RequestClient.prototype.on_sendable = function (context) {
this.send_pending_requests();
};
RequestClient.prototype.on_released = function (context) {
var request = this.requests[context.delivery.tag];
if (request) {
request.released();
}
};
RequestClient.prototype.on_rejected = function (context) {
this.delivery_failure(context.delivery.tag, context.delivery.remote_state.error);
};
RequestClient.prototype.on_modified = function (context) {
//TODO: retry?
this.delivery_failure(context.delivery.tag, 'Request failed');
};
RequestClient.prototype.on_accepted = function (context) {
var request = this.requests[context.delivery.tag];
if (request) {
request.completed();
}
};
RequestClient.prototype.delivery_failure = function (id, error) {
var request = this.requests[id];
if (request) {
request.failed(error);
}
};
RequestClient.prototype.send_pending_requests = function (context) {
while (this.ready() && this.pending.length > 0) {
this.pending.shift().send();
}
return this.pending.length === 0;
};
RequestClient.prototype.send_or_queue = function (request) {
if (this.ready() && this.send_pending_requests()) {
request.send();
} else {
this.pending.push(request);
}
};
RequestClient.prototype.next_id = function () {
return 'req-' + this.counter++;
};
RequestClient.prototype.request = function (message) {
return this.submit(message).as_promise();
};
RequestClient.prototype.submit = function (message) {
var id = this.next_id();
var request = new Request(this, id, message);
this.requests[id] = request;
this.send_or_queue(request);
return request;
};
RequestClient.prototype.ready = function (details) {
return this.address !== undefined && this.sender.sendable();
};
RequestClient.prototype.close = function () {
this.connection.close();
};
RequestClient.prototype.on_disconnected = function (context) {
this.address = undefined;
for (var id in this.requests) {
//TODO: could try and resend on reconnecting...
this.delivery_failure(id, 'aborted');
}
};
function HttpToAmqpBridge(port, address, aggregator) {
log.info('Created HTTP to AMQP bridge %s => %s', port, address);
this.port = port;
this.address = address;
var container = rhea.create_container({enable_sasl_external:true});
container.id = process.env.HOSTNAME + '_http:' + this.port + '=>amqp:' + address;
this.client = new RequestClient(container.connect(), address);
this.aggregator = aggregator;
//listen for http requests
this.server = http.createServer(this.request.bind(this));
this.server.listen(this.port, '0.0.0.0');
log.info('listening for http on %s', this.port);
}
const skipped_headers = ['CONNECTION', 'KEEP-ALIVE'];
function write_header(response, key, value) {
response.write(key + ': ' + value + '\r\n');
}
const boundary_marker = 'skupper-boundary';
function write_multipart_aggregated_response(response, messages_in) {
var response_bytes = 0;
for (var i = 0; i < messages_in.length; i++) {
var message_in = messages_in[i];
if (i == 0) {
response.writeHead(200/* Is this right? */, {'content-type': 'multipart/mixed; boundary='+ boundary_marker});
}
log.info('got reply for outbound request: %s %s', message_in.subject, message_in.to);
if (message_in.content_type) {
write_header(response, 'content-type', message_in.content_type);
}
for (var key in message_in.application_properties) {
if (skipped_headers.indexOf(key.toUpperCase()) < 0) {
write_header(response, key, message_in.application_properties[key]);
}
}
response.write('\r\n');
response.write(message_in.body);
if (i == (messages_in.length - 1)) response.write('\r\n--' + boundary_marker + '--\r\n');
else response.write('\r\n--' + boundary_marker + '\r\n');
response_bytes += message_in.body.length;
}
response.end();
return response_bytes;
}
function write_json_aggregated_response(response, messages_in) {
var aggregated = [];
var headers = {};
for (var i = 0; i < messages_in.length; i++) {
var message_in = messages_in[i];
var part = {};
if (message_in.content_type === 'application/json') {
try {
part.content = JSON.parse(message_in.body);
} catch (e) {
part.error = e;
}
} else {
part.error = 'Wrong content-type. Expected application/json, got ' + message_in.content_type;
}
aggregated.push(part);
for (var key in message_in.application_properties) {
if (skipped_headers.indexOf(key.toUpperCase()) < 0 && key !== 'content-type' && key !== 'content-length') {
headers[key] = message_in.application_properties[key];
}
}
}
response.statusCode = 200;
response.setHeader('content-type', 'application/json');
for (var key in headers) {
response.setHeader(key, headers[key]);
}
var content = JSON.stringify(aggregated);
response.end(content);
return content.length;
}
HttpToAmqpBridge.prototype.request = function (request, response) {
var start = Date.now();
var self = this;
var url = url_parse(request.url);
var path = url.path;
var address = request.headers.host ? request.headers.host.split(':')[0] + path : url_parse(request.url);
log.info('outgoing request %s (%s)', request.headers.host, address);
var body = '';
request.on('data', function (data) { body += data; });
request.on('end', function () {
var message_out = {
to: address,
subject: request.method,
application_properties: {},
message_annotations: {},
body: body
};
for (var key in request.headers) {
if (key === 'content-type') {
message_out.content_type = request.headers[key];
} else {
message_out.application_properties[key] = request.headers[key];
}
}
log.info('client sending message: %s %s', message_out.subject, message_out.to);
self.client.request(message_out).then(function (messages_in) {
if (messages_in.length === 0) {
response.writeHead(204/** Is this right**/, {});
response.end();
} else {
var response_bytes = self.aggregator(response, messages_in);
metrics.http_ingress_request(self.metrics, client_ip(request.socket), request.method, response.statusCode, message_out.body.length, response_bytes, elapsed(start));
}
}).catch(function (error) {
console.error(error);
response.statusCode = 500;
response.end(error.toString());
metrics.http_ingress_request(self.metrics, client_ip(request.socket), request.method, response.statusCode, message_out.body.length, error.toString().length, elapsed(start));
});
});
};
module.exports.amqp_to_http = function (address, host, port) {
return new AmqpToHttpBridge('mc/'+address, host, port);
};
module.exports.http_to_amqp = function (port, address, aggregator) {
if (aggregator === 'json') {
return new HttpToAmqpBridge(port, 'mc/'+address, write_json_aggregated_response);
} else { //aggregator === 'multipart'
return new HttpToAmqpBridge(port, 'mc/'+address, write_multipart_aggregated_response);
}
};