-
Notifications
You must be signed in to change notification settings - Fork 3
/
functions.js
375 lines (349 loc) · 12.8 KB
/
functions.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
/**
* Check if file exists
*/
function checkExistingFiles() {
checkExistingFile('./config/config.ini', false);
checkExistingFile('./data/amqp.ini', false);
checkExistingFile('./data/listeners.inc', false);
checkExistingFile('./data/senders.inc', false);
}
/**
*
*/
function checkExistingLogFiles() {
checkExistingFile(config.log.failedRequests, true);
if (config.log.logToFile) {
checkExistingFile(config.log.logFile, true);
}
}
/**
* Check if a file exists
*/
function checkExistingFile(file, create) {
console.log('[boot] checking file ' + file);
var fs = require('fs');
if (fs.existsSync(file)) {
//No problem.
}
else {
if (create) {
fs.createWriteStream(file, {flags: 'a'});
console.log(file + ' CREATED');
} else {
throw new Error('file ' + file + ' does not exist');
}
}
}
/**
* FailedRequest
*/
var failedRequests = function (content) {
var util = require('util');
var logfile = global.config.log.failedRequests;
//Exists
if (!fs.existsSync(logfile)) {
console.log('[FAILEDREQUESTS] created log file ' + logfile);
var fd = fs.openSync(logfile, 'w');
}
var failed_file = fs.createWriteStream(logfile, {flags: 'a'});
failed_file.write(util.format(content) + '\n');
failed_file.end();
};
/**
* connect to single Amqp Server
*/
var connectAmqpServer = function (amqpServer) {
var data = require('./data');
var amqp = require('amqp');
console.log('[' + amqpServer.name + '] AMQP connecting');
var connOptions = {
host: amqpServer.ip,
//heartbeat : config.amq.heartbeat,
port: amqpServer.port,
login: amqpServer.user,
password: amqpServer.password,
vhost: amqpServer.vhost
};
var amqpConnection;
amqpConnection = amqp.createConnection(connOptions, {reconnectBackoffStrategy: "exponential"});
global.amqpConnection = amqpConnection;
amqpConnection.on('error', function (err) {
console.log('[' + amqpServer.name + '] error ');
console.log(err);
});
amqpConnection.on('end', function () {
console.log('[' + amqpServer.name + '] ended');
});
amqpConnection.on('ready', function () {
console.log('[' + amqpServer.name + '] connection ready');
var subscribed = [];
data.getSenders(function (senders) {
data.getListeners(function (listeners) {
listeners.forEach(function (listener) {
if (listener.type.toUpperCase() == 'AMQP' && listener.server == amqpServer.name) {
console.log('received on correct listener.');
console.log(listener.server);
var options = {
autoDelete: false,
durable: false,
closeChannelOnUnsubscribe: true,
noDeclare: true
};
amqpConnection.queue(listener.queue, options, function (q) {
console.log('[' + amqpServer.name + '] queue connected ' + listener.queue);
//var subscribeOptions = {ack: true};//Ack manually
var subscribeOptions = {ack: true};
q.subscribe(subscribeOptions, function (message, headers, deliveryInfo, messageObject) {
console.log('=============AMQ MESG================');
console.log('[' + amqpServer.name + '] [' + listener.url + ']received on Queue: ');
console.log('[' + amqpServer.name + '] [headers]');
console.log(headers);
console.log('[' + amqpServer.name + '] [deliveryInfo]');
console.log(deliveryInfo);
if (typeof message.data != 'undefined') {
message = message.data.toString('utf8');
}
//console.log(message);
// if (typeof message == 'object') {
// var json = JSON.stringify(message)
// } else {
json = message;
//}
var trigger = {};
trigger.type = 'AMQP';
trigger.message = messageObject;
trigger.queue = q;
module.exports.loopListeners(listeners, senders, null, 'AMQP', listener.url, json, headers, trigger);
});
});
}
});
});
});
});
amqpConnection.on('close', function (msg) {
console.log("[" + amqpServer.name + "] connection closed: " + msg);
});
global.amqpConnections.push(amqpConnection);
};
/**
* Do sending
*/
var executeSender = function (req, sender, body, headers, trigger) {
module.exports.ackTrigger(trigger);
if (sender.type == 'POST') {
module.exports.executeSenderHTTP(req, sender, body, headers, trigger);
}
if (sender.type == 'GET') {
module.exports.executeSenderHTTP(req, sender, null, headers, trigger);
}
if (sender.type == 'SOCKET') {
module.exports.executeSenderSOCKET(req, sender, body, headers);
}
if (sender.type == 'AMQP') {
module.exports.executeSenderAMQP(req, sender, body, headers);
}
};
var ackTrigger = function (trigger) {
// Dead lettering is not supportet at the moment.
if (typeof trigger != 'undefined' && trigger.type == 'AMQP') {
module.exports.ackAmqpObject(trigger.message);
}
};
var rejectTrigger = function (trigger) {
// Dead lettering is not supportet at the moment.
// if (typeof trigger != 'undefined' && trigger.type == 'AMQP') {
// module.exports.rejectAmqpObject(trigger);
// }
};
var ackAmqpObject = function (amqpObject) {
if (typeof amqpObject != 'undefined') {
console.log('[AMQP] ack amqp message');
amqpObject.acknowledge(false);
}
};
var rejectAmqpObject = function (trigger) {
if (typeof trigger != 'undefined') {
console.log('[AMQP] reject amqp message');
trigger.queue.shift(true, false);
}
};
/**
* Do HTTP POST sending
*/
var executeSenderHTTP = function (req, sender, body, headers, trigger) {
var request = require('request');
var querystring = require('querystring');
var PostHeaders = {
'Content-Type': 'application/x-www-form-urlencoded',
};
console.log('[HTTP] sender HTTP');
console.log(sender);
for (var p in headers) {
if (headers.hasOwnProperty(p)) {
//result += p + " , " + obj[p] + "\n";
PostHeaders['Forwarded-header-' + p] = headers[p];
}
}
if (sender.type == 'POST') {
//POST
var postObject = {
headers: PostHeaders,
url: sender.url,
form: body
};
if (global.config.proxy.proxy_enabled) {
console.log('[HTTP] PROXY: ' + 'http://' + global.config.proxy.proxy_server + ':' + global.config.proxy.proxy_port);
var proxy = 'http://' + global.config.proxy.proxy_server + ':' + global.config.proxy.proxy_port;
var HttpProxyAgent = require('http-proxy-agent');
var agent = new HttpProxyAgent(proxy);
postObject.agent = agent;
postObject.proxy = proxy;
}
request.post(postObject, function (error, response, body) {
if (!error && response.statusCode == 200) {
console.log('[POSTREQUEST] request returned OK');
console.log(body);
} else {
console.log('[POSTREQUEST] request returned NOK: ');
var postResultObject = postObject;
postResultObject.error = error;
failedRequests(JSON.stringify(postResultObject));
module.exports.rejectTrigger(trigger);
}
});
} else {
//GET
var getObject = {
url: sender.url,
method: sender.type,
headers: headers,
};
if (global.config.proxy.proxy_enabled) {
console.log('[HTTP] PROXY: ' + 'http://' + global.config.proxy.proxy_server + ':' + global.config.proxy.proxy_port);
var proxy = 'http://' + global.config.proxy.proxy_server + ':' + global.config.proxy.proxy_port;
var HttpProxyAgent = require('http-proxy-agent');
var agent = new HttpProxyAgent(proxy);
getObject.agent = agent;
getObject.proxy = proxy;
}
// Start the request
request(getObject, function (error, response, body) {
if (!error && response.statusCode == 200) {
console.log('[GETREQUEST] request returned OK');
// Print out the response body
console.log(body);
module.exports.ackTrigger(trigger);
} else {
console.log('[GETREQUEST] request returned NOK: ', error);
var getResultObject = getObject;
getResultObject.error = error;
failedRequests(JSON.stringify(getResultObject));
module.exports.rejectTrigger(trigger);
}
});
}
};
/**
* Do AMQP sending
*/
var executeSenderAMQP = function (req, sender, body, headers) {
if (!global.config.amqp.useamq) {
console.log('[AMQP] AMQP is disabled');
return false;
}
console.log('[AMQP] send to queue ', sender, body, headers);
console.log(sender);
console.log(body);
console.log(headers);
//var senderParts =
var body = 'testbody';
if (typeof global.amqpConnection != 'undefined') {
//amqpConnection.
console.log('[AMQP] connected');
var queueOptions = {
autoDelete: false,
durable: false,
closeChannelOnUnsubscribe: true,
};
global.amqpConnection.queue(sender.queue, queueOptions, function (q) {
var exchangeOptions = {
type: 'topic', //'direct', 'fanout'
};
global.amqpConnection.exchange(sender.exchange, exchangeOptions, function (exchange) {
console.log('[AMQP] exchange created ', sender.exchange);
queue = q;
queue.bind(exchange, sender.key);
console.log('[AMQP] exchange added to queue ', sender.exchange, sender.queue);
var options = {};
options.headers = {};
exchange.publish(sender.key, body, options, function () {
console.log('[AMQP] Published to Exchange ' + exchange.name + ' message:', body);
})
});
});
}
};
/**
* Do socket sending
*/
var executeSenderSOCKET = function (req, sender, body, headers) {
if (!global.config.server.usesocketio) {
console.log('[SOCKET] SOCKET is disabled');
return false;
}
console.log('[SOCKET] execute socket ' + sender.url);
if (typeof global.io.sockets != 'undefined') {
global.io.sockets.emit(sender.url, body);
console.log('[SOCKET] pushed');
}
};
/**
* Loop listeners
*/
var loopListeners = function (listeners, senders, req, method, uri, body, headers, trigger) {
var qs = require('querystring');
var ret = false;
listeners.forEach(function (listener) {
if (listener.type == method && listener.url == uri) {
listener.senders.forEach(function (senderName) {
senders.forEach(function (sender) {
if (sender.name == senderName) {
module.exports.executeSender(req, sender, body, headers, trigger);
ret = true;
}
});
});
}
});
return ret;
};
/**
* Write contents to a file.
*/
var writeSettings = function (file, data, callback) {
var fs = require('fs');
fs.writeFile(file, data, function (err) {
if (err) {
console.log(err);
callback(err);
}
console.log("The file was saved!");
callback();
});
};
module.exports = {
connectAmqpServer: connectAmqpServer,
executeSenderAMQP: executeSenderAMQP,
executeSenderSOCKET: executeSenderSOCKET,
executeSenderHTTP: executeSenderHTTP,
loopListeners: loopListeners,
executeSender: executeSender,
writeSettings: writeSettings,
rejectAmqpObject: rejectAmqpObject,
ackAmqpObject: ackAmqpObject,
ackTrigger: ackTrigger,
rejectTrigger: rejectTrigger,
checkExistingFiles: checkExistingFiles,
checkExistingLogFiles: checkExistingLogFiles
};