-
-
Notifications
You must be signed in to change notification settings - Fork 2.6k
Expand file tree
/
Copy pathdnssec.js
More file actions
61 lines (50 loc) · 1.55 KB
/
dnssec.js
File metadata and controls
61 lines (50 loc) · 1.55 KB
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
import https from 'https';
import middleware from './_common/middleware.js';
const dnsSecHandler = async (domain) => {
const dnsTypes = ['DNSKEY', 'DS', 'RRSIG'];
const records = {};
for (const type of dnsTypes) {
const options = {
hostname: 'dns.google',
path: `/resolve?name=${encodeURIComponent(domain)}&type=${type}`,
method: 'GET',
headers: {
'Accept': 'application/dns-json'
}
};
try {
const dnsResponse = await new Promise((resolve, reject) => {
const req = https.request(options, res => {
let data = '';
res.on('data', chunk => {
data += chunk;
});
res.on('end', () => {
try {
resolve(JSON.parse(data));
} catch (error) {
reject(new Error('Invalid JSON response'));
}
});
res.on('error', error => {
reject(error);
});
});
req.on('error', error => {
reject(error);
});
req.end();
});
if (dnsResponse.Answer) {
records[type] = { isFound: true, answer: dnsResponse.Answer, response: dnsResponse.Answer };
} else {
records[type] = { isFound: false, answer: null, response: dnsResponse };
}
} catch (error) {
throw new Error(`Error fetching ${type} record: ${error.message}`); // This will be caught and handled by the commonMiddleware
}
}
return records;
};
export const handler = middleware(dnsSecHandler);
export default handler;