This repository has been archived by the owner on Jan 29, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathindex.js
62 lines (52 loc) · 1.53 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
const util = require('util');
const dns = require('dns');
const ipaddr = require('ipaddr.js');
const fetch = require('cross-fetch');
const CIDRS_URL =
'https://raw.githubusercontent.com/mongodb-js/mongodb-cloud-info/main/cidrs.json';
const dnsLookup = util.promisify(dns.lookup.bind(dns));
let unparsedCIDRsPromise;
function rangesContainsIP(ipRanges, ip) {
if (ip.kind() === 'ipv4') {
return !!ipRanges.v4.find((cidr) => ip.match(cidr));
}
return !!ipRanges.v6.find((cidr) => ip.match(cidr));
}
async function getCloudInfo(host) {
if (!host) {
return {
isAws: false,
isGcp: false,
isAzure: false
};
}
const address = await dnsLookup(host);
const ip = ipaddr.parse(address);
if (!unparsedCIDRsPromise) {
unparsedCIDRsPromise = fetch(CIDRS_URL, { timeout: 5000 }).then((res) => {
return res.json();
});
}
let unparsedCIDRs;
try {
unparsedCIDRs = await unparsedCIDRsPromise;
} catch (err) {
// If we failed to fetch, clean up the cached promise so that the next call
// can try again
unparsedCIDRsPromise = undefined;
throw err;
}
const cidrs = {};
for (const [name, { v4, v6 }] of Object.entries(unparsedCIDRs)) {
cidrs[name] = {
v4: v4.map((cidr) => [new ipaddr.IPv4(cidr[0]), cidr[1]]),
v6: v6.map((cidr) => [new ipaddr.IPv6(cidr[0]), cidr[1]])
};
}
return {
isAws: rangesContainsIP(cidrs.aws, ip),
isGcp: rangesContainsIP(cidrs.gcp, ip),
isAzure: rangesContainsIP(cidrs.azure, ip)
};
}
module.exports = { getCloudInfo };