mirror of
https://github.com/SukkaW/Surge.git
synced 2025-12-12 17:20:35 +08:00
Fix Domain Alive Check
This commit is contained in:
parent
42e0cb5b9f
commit
6dc63fa667
4
Build/mod.d.ts
vendored
4
Build/mod.d.ts
vendored
@ -8,7 +8,9 @@ declare module 'dns2' {
|
|||||||
/** @example dns.google.com */
|
/** @example dns.google.com */
|
||||||
dns: string,
|
dns: string,
|
||||||
/** @description whether to use HTTP or HTTPS */
|
/** @description whether to use HTTP or HTTPS */
|
||||||
http: boolean
|
http: boolean,
|
||||||
|
|
||||||
|
get?: (url: string) => any
|
||||||
}
|
}
|
||||||
|
|
||||||
export type DnsResolver<T = DnsResponse> = (name: string, type: PacketQuestion) => Promise<T>;
|
export type DnsResolver<T = DnsResponse> = (name: string, type: PacketQuestion) => Promise<T>;
|
||||||
|
|||||||
@ -11,6 +11,7 @@ import asyncRetry from 'async-retry';
|
|||||||
import * as whoiser from 'whoiser';
|
import * as whoiser from 'whoiser';
|
||||||
import picocolors from 'picocolors';
|
import picocolors from 'picocolors';
|
||||||
import createKeywordFilter from './lib/aho-corasick';
|
import createKeywordFilter from './lib/aho-corasick';
|
||||||
|
import './lib/fetch-retry';
|
||||||
|
|
||||||
const dohServers: Array<[string, DNS2.DnsResolver]> = ([
|
const dohServers: Array<[string, DNS2.DnsResolver]> = ([
|
||||||
'8.8.8.8',
|
'8.8.8.8',
|
||||||
@ -31,12 +32,12 @@ const dohServers: Array<[string, DNS2.DnsResolver]> = ([
|
|||||||
// 'dns.bebasid.com', // BebasID, path not /dns-query but /unfiltered
|
// 'dns.bebasid.com', // BebasID, path not /dns-query but /unfiltered
|
||||||
// '193.110.81.0', // dns0.eu
|
// '193.110.81.0', // dns0.eu
|
||||||
// '185.253.5.0', // dns0.eu
|
// '185.253.5.0', // dns0.eu
|
||||||
|
// 'zero.dns0.eu',
|
||||||
'dns.nextdns.io',
|
'dns.nextdns.io',
|
||||||
'anycast.dns.nextdns.io',
|
'anycast.dns.nextdns.io',
|
||||||
'wikimedia-dns.org',
|
'wikimedia-dns.org',
|
||||||
// 'ordns.he.net',
|
// 'ordns.he.net',
|
||||||
'dns.mullvad.net',
|
// 'dns.mullvad.net',
|
||||||
// 'zero.dns0.eu',
|
|
||||||
'basic.rethinkdns.com'
|
'basic.rethinkdns.com'
|
||||||
// 'ada.openbld.net',
|
// 'ada.openbld.net',
|
||||||
// 'dns.rabbitdns.org'
|
// 'dns.rabbitdns.org'
|
||||||
@ -45,16 +46,17 @@ const dohServers: Array<[string, DNS2.DnsResolver]> = ([
|
|||||||
DNS2.DOHClient({
|
DNS2.DOHClient({
|
||||||
dns: server,
|
dns: server,
|
||||||
http: false
|
http: false
|
||||||
|
// get: (url: string) => undici.request(url).then(r => r.body)
|
||||||
})
|
})
|
||||||
] as const);
|
] as const);
|
||||||
|
|
||||||
const queue = newQueue(18);
|
const queue = newQueue(32);
|
||||||
const mutex = new Map<string, Promise<unknown>>();
|
const mutex = new Map<string, Promise<unknown>>();
|
||||||
function keyedAsyncMutexWithQueue<T>(key: string, fn: () => Promise<T>) {
|
function keyedAsyncMutexWithQueue<T>(key: string, fn: () => Promise<T>) {
|
||||||
if (mutex.has(key)) {
|
if (mutex.has(key)) {
|
||||||
return mutex.get(key) as Promise<T>;
|
return mutex.get(key) as Promise<T>;
|
||||||
}
|
}
|
||||||
const promise = queue.add(() => fn()).finally(() => mutex.delete(key));
|
const promise = queue.add(() => fn());
|
||||||
mutex.set(key, promise);
|
mutex.set(key, promise);
|
||||||
return promise;
|
return promise;
|
||||||
}
|
}
|
||||||
@ -76,12 +78,12 @@ const resolve: DNS2.DnsResolver<DnsResponse> = async (...args) => {
|
|||||||
const [dohServer, dohClient] = dohServers[Math.floor(Math.random() * dohServers.length)];
|
const [dohServer, dohClient] = dohServers[Math.floor(Math.random() * dohServers.length)];
|
||||||
|
|
||||||
try {
|
try {
|
||||||
const resp = await dohClient(...args);
|
|
||||||
return {
|
return {
|
||||||
...resp,
|
...await dohClient(...args),
|
||||||
dns: dohServer
|
dns: dohServer
|
||||||
} satisfies DnsResponse;
|
} satisfies DnsResponse;
|
||||||
} catch (e) {
|
} catch (e) {
|
||||||
|
console.error(e);
|
||||||
throw new DnsError((e as Error).message, dohServer);
|
throw new DnsError((e as Error).message, dohServer);
|
||||||
}
|
}
|
||||||
}, { retries: 5 });
|
}, { retries: 5 });
|
||||||
@ -172,7 +174,7 @@ export async function isDomainAlive(domain: string, isSuffix: boolean): Promise<
|
|||||||
return [domain, true] as const;
|
return [domain, true] as const;
|
||||||
}
|
}
|
||||||
|
|
||||||
const apexDomainAlive = await keyedAsyncMutexWithQueue(apexDomain, () => isApexDomainAlive(apexDomain));
|
const apexDomainAlive = await isApexDomainAlive(apexDomain);
|
||||||
|
|
||||||
if (!apexDomainAlive[1]) {
|
if (!apexDomainAlive[1]) {
|
||||||
domainAliveMap.set(domain, false);
|
domainAliveMap.set(domain, false);
|
||||||
@ -214,14 +216,8 @@ export async function runAgainstRuleset(filepath: string) {
|
|||||||
case 'DOMAIN-SUFFIX':
|
case 'DOMAIN-SUFFIX':
|
||||||
case 'DOMAIN': {
|
case 'DOMAIN': {
|
||||||
promises.push(keyedAsyncMutexWithQueue(domain, () => isDomainAlive(domain, type === 'DOMAIN-SUFFIX')));
|
promises.push(keyedAsyncMutexWithQueue(domain, () => isDomainAlive(domain, type === 'DOMAIN-SUFFIX')));
|
||||||
continue;
|
break;
|
||||||
}
|
}
|
||||||
default:
|
|
||||||
continue;
|
|
||||||
// no default
|
|
||||||
// case 'DOMAIN-KEYWORD': {
|
|
||||||
// break;
|
|
||||||
// }
|
|
||||||
// no default
|
// no default
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user