Skip to content

Commit

Permalink
chore: replace node dns package with dns over http approach
Browse files Browse the repository at this point in the history
  • Loading branch information
VisargD committed Dec 28, 2024
1 parent 6d2f40d commit f502fd8
Showing 1 changed file with 12 additions and 6 deletions.
18 changes: 12 additions & 6 deletions plugins/default/validUrls.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import {
PluginHandler,
PluginParameters,
} from '../types';
import dns from 'dns';
import { getText } from '../utils';

export const handler: PluginHandler = async (
Expand Down Expand Up @@ -148,11 +147,18 @@ async function checkUrl(target: string): Promise<boolean> {
async function checkDNS(target: string): Promise<boolean> {
try {
const parsedUrl = new URL(target);
return new Promise((resolve) => {
dns.lookup(parsedUrl.hostname, (err) => {
resolve(err === null);
});
});
const response = await fetch(
// Using DNS over HTTPS (DoH) for cross-runtime compatibility (works in both Edge and Node.js)
// https://developers.cloudflare.com/1.1.1.1/encryption/dns-over-https/make-api-requests/
`https://1.1.1.1/dns-query?name=${parsedUrl.hostname}`,
{
headers: {
accept: 'application/dns-json',
},
}
);
const data: Record<string, any> = await response.json();
return data.Status === 0 && data.Answer && data.Answer.length > 0;
} catch (error) {
return false;
}
Expand Down

0 comments on commit f502fd8

Please sign in to comment.