-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
73 lines (67 loc) · 2.27 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
const fs = require('fs');
const Promise = require('bluebird');
const Slack = require('slack-node');
const lookup = Promise.promisify(require('dns').lookup);
const request = Promise.promisify(require('request'), { multiArgs: true });
const ipRangeCheck = require("ip-range-check");
const config = require('./config');
const ipRanges = {};
slack = new Slack();
slack.setWebhook(config.slack.webhookUri);
const sendMessage = (text, channel) => {
slack.webhook({
channel: channel || config.slack.stdChannel,
username: config.slack.username,
text
}, function(err, response) {
if (err) {
console.log(new Date(), 'Could not connect to slack', err.message);
} else if (response.status === 'fail') {
console.log(new Date(), 'Could not send to slack', response);
}
});
}
const sendErrorMessage = (text) => {
sendMessage(text, config.slack.errorChannel);
}
const isIpValid = (ipRanges, ip, services, regions) => {
return ipRanges.some(range =>
services.indexOf(range.service) !== -1
&& regions.indexOf(range.region) !== -1
&& ipRangeCheck(ip, range.ip_prefix)
);
};
const resolveDns = () => {
console.log(new Date(), "Running dns-checker");
request({ url: config.ipRangesUrl, json: true })
.then((response) => {
const ipRanges = response[1].prefixes;
return ipRanges;
})
.then((ipRanges) => Promise.map(config.domains, domain => lookup(domain.name, 4)
.then((resolvedIp) => ({
domain,
resolvedIp,
valid: isIpValid(ipRanges, resolvedIp, [domain.service], ['GLOBAL', domain.region])
}))
)
.then(function(results) {
// send error for invalid IPs
const invalids = results.filter(result => !result.valid);
if (invalids.length > 0) {
const message = `*INVALID IPS*: \`\`\`${JSON.stringify(invalids, null, 2)}\`\`\``;
sendErrorMessage(message);
}
}).catch((error) => {
const message = (error && error.message) || 'Unknown';
sendMessage(`*DNS LOOKUP ERROR*:\n${message}`);
})
).catch((error) => {
const message = (error && error.message) || 'Unknown';
sendMessage(`*IP RANGES ERROR*:\n${message}`);
}).finally(() => {
console.log(new Date(), "Stopping dns-checker");
setTimeout(resolveDns, config.interval);
});
};
resolveDns();