-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathzone.js
executable file
·107 lines (83 loc) · 2.16 KB
/
zone.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
#!/usr/bin/env node
'use strict';
const assert = require('assert');
const Path = require('path');
const fs = require('bfile');
const util = require('bns/lib/util');
const wire = require('bns/lib/wire');
const {types} = wire;
const ZONE_FILE = Path.resolve(__dirname, 'data', 'root.zone');
const ZONE_JSON = Path.resolve(__dirname, 'build', 'root.json');
const text = fs.readFileSync(ZONE_FILE, 'utf8');
const records = wire.fromZone(text);
const glue = new Map();
for (const rr of records) {
if (rr.type === types.A || rr.type === types.AAAA) {
const name = rr.name.toLowerCase();
if (!glue.has(name))
glue.set(name, { name, inet4: [], inet6: [] });
const item = glue.get(name);
switch (rr.type) {
case types.A:
item.inet4.push(rr.data.address);
break;
case types.AAAA:
item.inet6.push(rr.data.address);
break;
}
}
}
const domains = new Map();
for (const rr of records) {
if (util.countLabels(rr.name) !== 1)
continue;
if (rr.type !== types.NS && rr.type !== types.DS)
continue;
const name = rr.name.toLowerCase();
if (!domains.has(name))
domains.set(name, []);
const records = domains.get(name);
switch (rr.type) {
case types.NS: {
const ns = rr.data.ns.toLowerCase();
const auth = glue.get(ns);
assert(auth);
records.push({
type: 'NS',
ns
});
for (const address of auth.inet4) {
records.push({
type: 'GLUE4',
ns,
address
});
}
for (const address of auth.inet6) {
records.push({
type: 'GLUE6',
ns,
address
});
}
break;
}
case types.DS: {
records.push({
type: 'DS',
keyTag: rr.data.keyTag,
algorithm: rr.data.algorithm,
digestType: rr.data.digestType,
digest: rr.data.digest.toString('hex')
});
break;
}
}
}
function cmp(a, b) {
return a.type.localeCompare(b.type);
}
const out = Object.create(null);
for (const [key, value] of domains)
out[key] = { records: value.sort(cmp) };
fs.writeFileSync(ZONE_JSON, JSON.stringify(out, null, 2) + '\n');