mongodb-cloud-info 2.1.0 → 2.1.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/index.js DELETED
@@ -1,62 +0,0 @@
1
- const util = require('util');
2
- const dns = require('dns');
3
- const ipaddr = require('ipaddr.js');
4
- const fetch = require('cross-fetch');
5
-
6
- const CIDRS_URL =
7
- 'https://raw.githubusercontent.com/mongodb-js/mongodb-cloud-info/main/cidrs.json';
8
-
9
- const dnsLookup = util.promisify(dns.lookup.bind(dns));
10
-
11
- let unparsedCIDRsPromise;
12
-
13
- function rangesContainsIP(ipRanges, ip) {
14
- if (ip.kind() === 'ipv4') {
15
- return !!ipRanges.v4.find((cidr) => ip.match(cidr));
16
- }
17
-
18
- return !!ipRanges.v6.find((cidr) => ip.match(cidr));
19
- }
20
-
21
- async function getCloudInfo(host) {
22
- if (!host) {
23
- return {
24
- isAws: false,
25
- isGcp: false,
26
- isAzure: false
27
- };
28
- }
29
-
30
- const address = await dnsLookup(host);
31
- const ip = ipaddr.parse(address);
32
-
33
- if (!unparsedCIDRsPromise) {
34
- unparsedCIDRsPromise = fetch(CIDRS_URL, { timeout: 5000 }).then((res) => {
35
- return res.json();
36
- });
37
- }
38
- let unparsedCIDRs;
39
- try {
40
- unparsedCIDRs = await unparsedCIDRsPromise;
41
- } catch (err) {
42
- // If we failed to fetch, clean up the cached promise so that the next call
43
- // can try again
44
- unparsedCIDRsPromise = undefined;
45
- throw err;
46
- }
47
- const cidrs = {};
48
- for (const [name, { v4, v6 }] of Object.entries(unparsedCIDRs)) {
49
- cidrs[name] = {
50
- v4: v4.map((cidr) => [new ipaddr.IPv4(cidr[0]), cidr[1]]),
51
- v6: v6.map((cidr) => [new ipaddr.IPv6(cidr[0]), cidr[1]])
52
- };
53
- }
54
-
55
- return {
56
- isAws: rangesContainsIP(cidrs.aws, ip),
57
- isGcp: rangesContainsIP(cidrs.gcp, ip),
58
- isAzure: rangesContainsIP(cidrs.azure, ip)
59
- };
60
- }
61
-
62
- module.exports = { getCloudInfo };
@@ -1,111 +0,0 @@
1
- #!/usr/bin/env node
2
- const path = require('path');
3
- const util = require('util');
4
- const fs = require('fs');
5
- const ipaddr = require('ipaddr.js');
6
- const fetch = require('cross-fetch');
7
- const gceIps = require('gce-ips');
8
-
9
- const AWS_IP_RANGES_URL = 'https://ip-ranges.amazonaws.com/ip-ranges.json';
10
-
11
- /*
12
- Unfortunately we have to update this URL regularly. It looks like there isn't a
13
- stable URL where you can just get the latest set of IPs. When this gets updated
14
- the URL stops working and then there is a new URL. Browse to
15
- https://www.microsoft.com/en-us/download/details.aspx?id=56519, then click
16
- Download, then find the url out of your downloads list, then replace it here.
17
-
18
- At least the Github Action that runs this script fails when that becomes
19
- necessary.
20
- */
21
- const AZURE_IP_RANGES_URL = 'https://download.microsoft.com/download/7/1/D/71D86715-5596-4529-9B13-DA13A5DE5B63/ServiceTags_Public_20230724.json';
22
-
23
- const FETCH_TIMEOUT = 10000;
24
-
25
-
26
- function serializeV4CIDR(cidr) {
27
- // cidr is a two-element array. The first element is the address, the second
28
- // element is the part after /.
29
- // We can reconstruct the address with new ipaddr.IPv4(cidr[0].octets)
30
- return [cidr[0].octets, cidr[1]];
31
- }
32
-
33
- function serializeV6CIDR(cidr) {
34
- // cidr is a two-element array. The first element is the address, the second
35
- // element is the part after /.
36
- // We can reconstruct the address with new ipaddr.IPv6(cidr[0].parts).
37
- return [cidr[0].parts, cidr[1]];
38
- }
39
-
40
- async function getSplitGCPIpRanges() {
41
- const gceIpsInstance = gceIps();
42
- const lookup = util.promisify(gceIpsInstance.lookup.bind(gceIpsInstance));
43
-
44
- const prefixes = await lookup();
45
-
46
- const v4 = [];
47
- const v6 = [];
48
-
49
- for (const prefix of prefixes) {
50
- const cidr = ipaddr.parseCIDR(prefix);
51
- if (cidr[0].kind() === 'ipv4') {
52
- v4.push(serializeV4CIDR(cidr));
53
- } else {
54
- v6.push(serializeV6CIDR(cidr));
55
- }
56
- }
57
-
58
- return {
59
- v4,
60
- v6
61
- };
62
- }
63
-
64
- async function getSplitAWSIpRanges() {
65
- const result = await fetch(AWS_IP_RANGES_URL, { timeout: FETCH_TIMEOUT }).then(res => res.json());
66
-
67
- return {
68
- v4: result.prefixes.map((range) => serializeV4CIDR(ipaddr.parseCIDR(range.ip_prefix))),
69
- v6: result.ipv6_prefixes.map((range) => serializeV6CIDR(ipaddr.parseCIDR(range.ipv6_prefix)))
70
- };
71
- }
72
-
73
- async function getSplitAzureIpRanges() {
74
- const { values } = await fetch(AZURE_IP_RANGES_URL, { timeout: FETCH_TIMEOUT }).then(res => res.json());
75
-
76
- const v4 = [];
77
- const v6 = [];
78
-
79
- for (const value of values) {
80
- for (const addressPrefix of value.properties.addressPrefixes) {
81
- const cidr = ipaddr.parseCIDR(addressPrefix);
82
- if (cidr[0].kind() === 'ipv4') {
83
- v4.push(serializeV4CIDR(cidr));
84
- } else {
85
- v6.push(serializeV6CIDR(cidr));
86
- }
87
- }
88
- }
89
-
90
- return { v4, v6 };
91
- }
92
-
93
- async function writeAllIpRanges() {
94
- const [gcp, aws, azure] = (await Promise.all([
95
- getSplitGCPIpRanges(),
96
- getSplitAWSIpRanges(),
97
- getSplitAzureIpRanges()
98
- ]));
99
-
100
- const doc = {
101
- aws, azure, gcp
102
- };
103
-
104
- const filename = path.resolve(__dirname, '../', 'cidrs.json');
105
- await fs.promises.writeFile(filename, JSON.stringify(doc), 'utf8');
106
- }
107
-
108
- writeAllIpRanges()
109
- .catch((err) => {
110
- throw err;
111
- });