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
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
|
/*
* Copyright (c) 2019-2022 David Timber <dxdt@dev.snart.me>
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/
export class Resolver {
async resolve (type, rname) {} // abstract
};
function getRTypeNum (typestrd) {
switch (typestrd) {
case 'A': return 1;
case 'AAAA': return 28;
case 'MX': return 15;
}
throw new Error();
}
export class GooglePublicDNSResolver {
_mk_query_url (type, rname) {
return `https://dns.google/resolve?name=${rname}&type=${type}`;
}
async _resolve_inner (url) {
let rsp;
for (let i = 0; i < 3; i += 1) {
rsp = await fetch(url);
if (rsp.status / 100 === 5) {
await new Promise(r => setTimeout(r, 500));
continue;
}
break;
}
return rsp;
}
async resolve (type, rname) {
if (!rname.endsWith('.')) {
rname += '.';
}
const url = this._mk_query_url(type, rname);
const rsp = await this._resolve_inner(url);
const doc = await rsp.json();
const nrtype = getRTypeNum(type);
let ret = [];
if ('Answer' in doc) {
for (const rr of doc['Answer']) {
// ignore other extra answers like CNAME
if ('data' in rr &&
'name' in rr &&
'type' in rr &&
rr['name'] === rname &&
rr['type'] === nrtype)
{
ret.push(rr['data']);
}
}
}
return ret;
}
};
export function parseList (txt) {
let ret = [];
const lines = txt.split(/[\r\n]+/gm);
for (let l of lines) {
l = l.trim();
const d = l.search('#');
if (d >= 0) {
l = l.substring(0, d).trim();
}
if (l) {
ret.push(l);
}
}
return ret;
};
const defaultResolver = GooglePublicDNSResolver;
export class ResolveMXAAAA {
constructor (onresolve) {
this.onresolve = onresolve;
}
async doResolve (list, resolver = null) {
let ret = {
mxmap: {},
counts: {
list: list.length,
mx: 0,
has_a: 0,
has_aaaa: 0
}
};
if (!resolver) {
resolver = new defaultResolver();
}
// init the map
for (const domain of list) {
ret.mxmap[domain] = {
mx: [],
amap: {},
acnt: 0,
aaaamap: {},
aaaacnt: 0
};
}
// Get all MX
let i = 0;
for (const domain of list) {
const q_mx = await resolver.resolve('MX', domain);
let has_a = 0, has_aaaa = 0;
this.onresolve('start', { domain: domain }, ret);
for (let mx of q_mx) {
// don't need the number part
const d = mx.search(/\s+/);
if (d > 0) {
mx = mx.substring(d).trim();
}
ret.mxmap[domain].mx.push(mx);
ret.counts.mx += 1;
this.onresolve('mx', {
domain: domain,
mx: mx
}, ret);
// Resolve all A of MX
const q_a = await resolver.resolve('A', mx);
if (q_a.length) {
has_a = 1;
ret.mxmap[domain].acnt += 1;
}
ret.mxmap[domain].amap[mx] = q_a;
this.onresolve('a', {
domain: domain,
mx: mx,
a: q_a
}, ret);
// Resolve all AAAA of MX
const q_aaaa = await resolver.resolve('AAAA', mx);
if (q_aaaa.length) {
has_aaaa = 1;
ret.mxmap[domain].aaaacnt += 1;
}
ret.mxmap[domain].aaaamap[mx] = q_aaaa;
this.onresolve('aaaa', {
domain: domain,
mx: mx,
aaaa: q_aaaa
}, ret);
}
ret.counts.has_a += has_a;
ret.counts.has_aaaa += has_aaaa;
i += 1;
this.onresolve('end', {
domain: domain,
progress: {
cur: i,
cnt: list.length
}
}, ret);
}
return ret;
}
};
|