-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
97 lines (87 loc) · 3.25 KB
/
index.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
const SMTP = require("./smtp");
const IMAP = require("./imap");
const {sendToInsights} = require("./insights");
const POP3 = require('./pop3');
const Data = require("./load-data");
const {v4: uuidv4} = require('uuid');
const sp = require('synchronized-promise');
const sleep = require('atomic-sleep');
const cron = require("node-cron");
const retrieveMessage = (scenario, subject) => {
let method;
switch (scenario.protocol) {
case "imap":
method = IMAP.getEmails(scenario, subject)
break;
case "pop3":
method = POP3.getEmails(scenario, subject);
break;
default:
throw Error(`${scenario.protocol} not defined as a protocol, it must be imap or pop3`)
}
return (method);
};
const configuration = Data.getYaml();
global.debug = configuration.debug;
global.proxy = configuration.proxy
cron.schedule("* * * * *", function () {
for (scenario of configuration.scenarios) {
const subject = `${configuration.subject} - {${uuidv4()}} - ${scenario.protocol}`;
const sentTimeDate = new Date();
const sentTime = sentTimeDate.getTime();
try {
let SMTPResponse;
try {
SMTPResponse = sp(SMTP.send)(scenario, subject);
} catch (e) {
if (global.debug) console.log(e);
throw new Error(`SMTP: ${e.message}`);
}
//Extract time that the message was sent
const {messageTime, messageId} = SMTPResponse;
sleep(1000);
//Get the message that was sent previously
//We will be passing the different scenario which contains the configuration details
let protocolResponse = sp(retrieveMessage)(scenario, subject);
const {startProtocolTime} = protocolResponse;
const endProtocolTime = new Date().getTime();
const protocolResponseTime = (endProtocolTime - startProtocolTime);
const totalResponseTime = messageTime + protocolResponseTime;
const payload = {
subject: subject,
messageId: messageId,
sent_at: sentTime,
total_response_time: totalResponseTime,
smtp_response_time: messageTime,
retrieval_response_time: protocolResponseTime,
smtp_server: scenario.smtp_host,
retrieval_server: scenario.protocol_host,
protocol: scenario.protocol,
smtp_error: SMTPResponse.rejected.length > 0,
protocol_error: !!protocolResponse.rejected
};
if (global.debug) console.log(payload);
sendToInsights(payload, configuration.newrelic)
.catch((e => {
console.log(`Not able to send data to New Relic: ${e.message}`)
}));
} catch (e) {
if (global.debug) console.log(e);
let errorObject = removeSensitiveKeys(scenario);
errorObject["error"] = true;
errorObject["message"] = e.message;
sendToInsights(errorObject, configuration.newrelic)
.catch((e => {
console.log(`Not able to send data to New Relic: ${e}`);
}));
}
}
});
function removeSensitiveKeys(object) {
Object.keys(object).forEach(key => {
if (key.includes("password")) {
delete object[key];
}
});
return object;
}