Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: extend regex for valid urls extraction logic and remove dns dependency #852

Merged
merged 3 commits into from
Dec 28, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 17 additions & 7 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 All @@ -25,7 +24,11 @@ export const handler: PluginHandler = async (
}

// Find all URLs in the content, they may or may not start with http(s)
const urls = content.match(/https?:\/\/[^\s]*/g) || [];
// Regex explanation: https?:\/\/[^\s,"'{}\[\]]+
// https?:\/\/ - matches http or https
// [^\s,"'{}\[\]]+ - matches any characters that are not whitespace, comma, single quote, curly brace, or square bracket
const urls = content.match(/https?:\/\/[^\s,"'{}\[\]]+/g) || [];

const onlyDNS = parameters.onlyDNS || false;

if (urls.length === 0) {
Expand Down Expand Up @@ -144,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
Loading