forked from cdklabs/construct-hub
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathupdate-github-ip-allowlist.js
60 lines (56 loc) · 1.63 KB
/
update-github-ip-allowlist.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
const fs = require('fs');
const https = require('https');
const path = require('path');
function savePrefixLists(list, name) {
// IPv4 CIDR ranges do NOT include ":"
fs.writeFileSync(
path.join(
process.cwd(),
'resources',
'vpc-allow-lists',
`github.${name}-IPv4.txt`
),
list.filter((cidr) => !cidr.includes(':')).join('\n'),
'utf-8',
);
// We do not emit IPv6 rules, as our VPCs don't have IPv6 support.
}
void https
.get(
'https://api.github.com/meta',
{
headers: {
Accept: 'application/json',
'Accept-Encoding': 'identity',
'User-Agent': `node ${process.versions.node}`,
},
},
(res) => {
const chunks = [];
res.on('data', (chunk) => chunks.push(chunk));
res.once('error', (cause) => {
console.error('An error occurred: ', cause);
return process.exit(-1);
});
res.once('close', () => {
const data = JSON.parse(Buffer.concat(chunks).toString('utf-8'));
if (res.statusCode !== 200) {
console.error(
`GET https://api.github.com/meta - HTTP ${res.statusCode} (${res.statusMessage})`
);
for (const [name, values] of Object.entries(res.headers)) {
for (const value of typeof values === 'string' ? [values] : values) {
console.error(`${name}: ${value}`);
}
}
console.error();
console.error(data);
return process.exit(-1);
}
savePrefixLists(data.git, 'git');
savePrefixLists(data.web, 'web');
savePrefixLists(data.api, 'api');
});
}
)
.end();