web-check/api/dnssec.js

58 lines
1.5 KiB
JavaScript
Raw Normal View History

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', () => {
2024-03-26 14:35:24 +01:00
try {
resolve(JSON.parse(data));
} catch (error) {
reject(new Error('Invalid JSON response'));
}
});
res.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;