-
Notifications
You must be signed in to change notification settings - Fork 9
/
crawl-keys.js
executable file
·130 lines (95 loc) · 2.55 KB
/
crawl-keys.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
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
#!/usr/bin/env node
'use strict';
const Path = require('path');
const fs = require('bfile');
const StubResolver = require('bns/lib/resolver/stub');
const dnssec = require('bns/lib/dnssec');
const wire = require('bns/lib/wire');
const root = require('./build/root.json');
const {types, DSRecord, hashes} = wire;
const KEYS_PATH = Path.resolve(__dirname, 'build', 'keys.zone');
const names = Object.keys(root).sort();
const stub = new StubResolver({
rd: true,
cd: true,
edns: true,
ednsSize: 4096,
dnssec: true,
maxAttempts: 10,
maxTimeout: 3000,
hosts: [
['localhost.', '127.0.0.1'],
['localhost.', '::1']
],
// Note: safe to use a 3rd-party resolver,
// we verify the DS record hashes below.
servers: ['8.8.8.8', '8.8.4.4']
});
const records = [];
(async () => {
await stub.open();
for (const name of names) {
const zone = root[name];
if (zone.ds.length === 0)
continue;
console.log(`Crawling ${name}..`);
const ds = zone.ds.map(json => DSRecord.fromJSON(json));
const dsMap = new Map();
for (const rd of ds) {
if (!dsMap.get(rd.keyTag))
dsMap.set(rd.keyTag, new Map());
const map = dsMap.get(rd.keyTag);
map.set(rd.digestType, rd);
}
let res;
try {
res = await stub.lookup(name, types.DNSKEY);
} catch (e) {
console.log(`Could not lookup: ${name}`);
continue;
}
let found = 0;
for (const rr of res.answer) {
if (rr.type !== types.DNSKEY)
continue;
const rd = rr.data;
const map = dsMap.get(rd.keyTag());
if (!map)
continue;
for (const parent of map.values()) {
const ds = dnssec.createDS(rr, parent.digestType);
if (!ds)
continue;
if (parent.digestType === hashes.SHA1 && map.size > 1)
continue;
if (!ds.data.digest.equals(parent.digest))
continue;
if (ds.data.algorithm !== parent.algorithm)
continue;
console.log(rr.toString());
found += 1;
records.push(rr);
break;
}
}
if (found)
records.push(null);
if (found < dsMap.size)
console.log(`Missing DNS keys for: ${name} (${found} < ${dsMap.size}).`);
console.log('');
await new Promise(r => setTimeout(r, 500));
}
let text = '';
for (const rr of records) {
if (!rr) {
text += '\n';
continue;
}
text += rr.toString() + '\n';
}
fs.writeFileSync(KEYS_PATH, text);
await stub.close();
})().catch((err) => {
console.error(err.stack);
process.exit(1);
});