mirror of
https://github.com/sub-store-org/Sub-Store.git
synced 2026-12-02 09:23:54 +00:00
feat: 域名解析支持多个自定义 DNS, 支持设置 DNS 超时和单独的 DNS 缓存时长
This commit is contained in:
3 files changed
+654
-59
No files matched your search
@@ -1,6 +1,6 @@
|
||||
{
|
||||
"name": "sub-store",
|
||||
"version": "2.32.0",
|
||||
"version": "2.32.1",
|
||||
"description": "Advanced Subscription Manager for QX, Loon, Surge, Stash and Shadowrocket.",
|
||||
"main": "src/main.js",
|
||||
"packageManager": "pnpm@11.0.9",
|
||||
|
||||
@@ -601,6 +601,7 @@ function parseIP4P(IP4P) {
|
||||
}
|
||||
|
||||
const DEFAULT_RESOLVE_DOMAIN_CONCURRENCY = 10;
|
||||
const DEFAULT_RESOLVE_DOMAIN_CUSTOM_DNS_CONCURRENCY = 2;
|
||||
const RESOLVE_DOMAIN_CONCURRENCY_WARN_THRESHOLD = 20;
|
||||
|
||||
function normalizeResolveDomainConcurrency(concurrency) {
|
||||
@@ -620,6 +621,85 @@ function normalizeResolveDomainConcurrency(concurrency) {
|
||||
return parsed;
|
||||
}
|
||||
|
||||
function normalizeResolveDomainCustomDnsConcurrency(concurrency) {
|
||||
if (
|
||||
typeof concurrency === 'undefined' ||
|
||||
concurrency === null ||
|
||||
(typeof concurrency === 'string' && concurrency.trim() === '')
|
||||
) {
|
||||
return DEFAULT_RESOLVE_DOMAIN_CUSTOM_DNS_CONCURRENCY;
|
||||
}
|
||||
|
||||
const parsed = Number(concurrency);
|
||||
if (!Number.isInteger(parsed) || parsed < 1) {
|
||||
throw new Error('多 DNS 并发数应为大于 0 的整数');
|
||||
}
|
||||
|
||||
return parsed;
|
||||
}
|
||||
|
||||
function normalizeResolveDomainTimeout(timeout, defaultTimeout) {
|
||||
const hasExplicitTimeout = !(
|
||||
typeof timeout === 'undefined' ||
|
||||
timeout === null ||
|
||||
(typeof timeout === 'string' && timeout.trim() === '')
|
||||
);
|
||||
|
||||
if (hasExplicitTimeout) {
|
||||
const parsed = Number(timeout);
|
||||
if (!Number.isInteger(parsed) || parsed < 1) {
|
||||
throw new Error('DNS 超时应为大于 0 的整数');
|
||||
}
|
||||
return parsed;
|
||||
}
|
||||
|
||||
if (
|
||||
typeof defaultTimeout === 'undefined' ||
|
||||
defaultTimeout === null ||
|
||||
(typeof defaultTimeout === 'string' &&
|
||||
defaultTimeout.trim() === '')
|
||||
) {
|
||||
return 8000;
|
||||
}
|
||||
|
||||
const parsedDefaultTimeout = Number(defaultTimeout);
|
||||
if (!isFinite(parsedDefaultTimeout) || parsedDefaultTimeout <= 0) {
|
||||
return 8000;
|
||||
}
|
||||
|
||||
return parsedDefaultTimeout;
|
||||
}
|
||||
|
||||
function normalizeResolveDomainCacheTtl(cacheTtl) {
|
||||
if (
|
||||
typeof cacheTtl === 'undefined' ||
|
||||
cacheTtl === null ||
|
||||
(typeof cacheTtl === 'string' && cacheTtl.trim() === '')
|
||||
) {
|
||||
return undefined;
|
||||
}
|
||||
|
||||
const parsed = Number(cacheTtl);
|
||||
if (!Number.isInteger(parsed) || parsed < 1) {
|
||||
throw new Error('域名解析缓存时长应为大于 0 的整数');
|
||||
}
|
||||
|
||||
return parsed * 1000;
|
||||
}
|
||||
|
||||
function parseCustomDnsUrls(url) {
|
||||
const urls = `${url || ''}`
|
||||
.split(/\r?\n/)
|
||||
.map((item) => item.trim())
|
||||
.filter((item) => item);
|
||||
if (urls.length === 0) throw new Error('自定义 DNS 不能为空');
|
||||
return urls;
|
||||
}
|
||||
|
||||
function normalizeCustomDnsUrlList(url) {
|
||||
return parseCustomDnsUrls(url).join('\n');
|
||||
}
|
||||
|
||||
async function resolveDomainsWithConcurrency(
|
||||
domains,
|
||||
concurrency,
|
||||
@@ -638,6 +718,55 @@ async function resolveDomainsWithConcurrency(
|
||||
await Promise.all(workers);
|
||||
}
|
||||
|
||||
async function resolveWithCustomDnsConcurrency(urls, concurrency, resolveUrl) {
|
||||
return new Promise((resolve, reject) => {
|
||||
let nextIndex = 0;
|
||||
let activeCount = 0;
|
||||
let finishedCount = 0;
|
||||
let settled = false;
|
||||
const errors = [];
|
||||
const workerCount = Math.min(concurrency, urls.length);
|
||||
|
||||
function maybeReject() {
|
||||
if (!settled && finishedCount === urls.length) {
|
||||
reject(
|
||||
new Error(
|
||||
errors.length > 0 ? errors.join('; ') : 'No answers',
|
||||
),
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
function startNext() {
|
||||
if (settled) return;
|
||||
while (activeCount < workerCount && nextIndex < urls.length) {
|
||||
const resolverUrl = urls[nextIndex];
|
||||
nextIndex += 1;
|
||||
activeCount += 1;
|
||||
Promise.resolve()
|
||||
.then(() => resolveUrl(resolverUrl))
|
||||
.then((result) => {
|
||||
activeCount -= 1;
|
||||
finishedCount += 1;
|
||||
if (settled) return;
|
||||
settled = true;
|
||||
resolve({ result, resolverUrl });
|
||||
})
|
||||
.catch((err) => {
|
||||
activeCount -= 1;
|
||||
finishedCount += 1;
|
||||
errors.push(`${resolverUrl}: ${err}`);
|
||||
startNext();
|
||||
maybeReject();
|
||||
});
|
||||
}
|
||||
maybeReject();
|
||||
}
|
||||
|
||||
startNext();
|
||||
});
|
||||
}
|
||||
|
||||
function getDomainResolverCacheId(
|
||||
provider,
|
||||
domain,
|
||||
@@ -681,12 +810,64 @@ function getCachedDomainResolverResult(
|
||||
url,
|
||||
tlsSkipCertVerify,
|
||||
);
|
||||
return id ? resourceCache.get(id) : null;
|
||||
const cached = id ? resourceCache.get(id) : null;
|
||||
return provider === 'Custom'
|
||||
? unpackCustomDnsCachedResult(cached)
|
||||
: cached;
|
||||
}
|
||||
|
||||
function formatResolverUrlLog(provider, resolverUrl) {
|
||||
if (provider !== 'Custom' || !resolverUrl) return '';
|
||||
return ` via ${resolverUrl}`;
|
||||
const urls = `${resolverUrl}`
|
||||
.split(/\r?\n/)
|
||||
.map((item) => item.trim())
|
||||
.filter((item) => item);
|
||||
if (urls.length <= 1) return ` via ${resolverUrl}`;
|
||||
return ` via ${urls.length} custom DNS`;
|
||||
}
|
||||
|
||||
function formatResolverUrlInfo(provider, resolverUrl) {
|
||||
if (provider !== 'Custom' || !resolverUrl) return resolverUrl || '';
|
||||
const urls = `${resolverUrl}`
|
||||
.split(/\r?\n/)
|
||||
.map((item) => item.trim())
|
||||
.filter((item) => item);
|
||||
if (urls.length <= 1) return resolverUrl;
|
||||
return `${urls.length} custom DNS`;
|
||||
}
|
||||
|
||||
function annotateCustomDnsResult(result, resolverUrl) {
|
||||
if (result && typeof result === 'object') {
|
||||
Object.defineProperty(result, '_resolverUrl', {
|
||||
value: resolverUrl,
|
||||
enumerable: false,
|
||||
configurable: true,
|
||||
});
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
function packCustomDnsCachedResult(result, resolverUrl) {
|
||||
return {
|
||||
result,
|
||||
resolverUrl,
|
||||
};
|
||||
}
|
||||
|
||||
function unpackCustomDnsCachedResult(cached) {
|
||||
if (
|
||||
cached &&
|
||||
typeof cached === 'object' &&
|
||||
!Array.isArray(cached) &&
|
||||
Array.isArray(cached.result)
|
||||
) {
|
||||
return annotateCustomDnsResult(cached.result, cached.resolverUrl);
|
||||
}
|
||||
return cached;
|
||||
}
|
||||
|
||||
function getCustomDnsResultResolverUrl(result) {
|
||||
return result && typeof result === 'object' ? result._resolverUrl : null;
|
||||
}
|
||||
|
||||
const DOMAIN_RESOLVERS = {
|
||||
@@ -698,39 +879,70 @@ const DOMAIN_RESOLVERS = {
|
||||
edns,
|
||||
url,
|
||||
tlsSkipCertVerify,
|
||||
dnsConcurrency,
|
||||
cacheTtl,
|
||||
) {
|
||||
const urls = parseCustomDnsUrls(url);
|
||||
const normalizedUrl = urls.join('\n');
|
||||
const customDnsConcurrency =
|
||||
normalizeResolveDomainCustomDnsConcurrency(dnsConcurrency);
|
||||
const id = hex_md5(
|
||||
`CUSTOM:${
|
||||
tlsSkipCertVerify ? 'INSECURE:' : ''
|
||||
}${url}:${domain}:${type}`,
|
||||
}${normalizedUrl}:${domain}:${type}`,
|
||||
);
|
||||
const cached = resourceCache.get(id);
|
||||
const cached = unpackCustomDnsCachedResult(resourceCache.get(id));
|
||||
if (!noCache && cached) return cached;
|
||||
const answerType = type === 'IPv6' ? 'AAAA' : 'A';
|
||||
const res = await resolveDns({
|
||||
url,
|
||||
domain,
|
||||
type: answerType,
|
||||
timeout,
|
||||
edns,
|
||||
skipCertVerify: tlsSkipCertVerify,
|
||||
});
|
||||
const resolveUrl = async (resolverUrl) => {
|
||||
const res = await resolveDns({
|
||||
url: resolverUrl,
|
||||
domain,
|
||||
type: answerType,
|
||||
timeout,
|
||||
edns,
|
||||
skipCertVerify: tlsSkipCertVerify,
|
||||
});
|
||||
|
||||
const { answers } = res;
|
||||
if (!Array.isArray(answers) || answers.length === 0) {
|
||||
throw new Error('No answers');
|
||||
}
|
||||
const result = answers
|
||||
.filter((i) => i?.type === answerType)
|
||||
.map((i) => i?.data)
|
||||
.filter((i) => i);
|
||||
if (result.length === 0) {
|
||||
throw new Error('No answers');
|
||||
}
|
||||
resourceCache.set(id, result);
|
||||
return result;
|
||||
const { answers } = res;
|
||||
if (!Array.isArray(answers) || answers.length === 0) {
|
||||
throw new Error('No answers');
|
||||
}
|
||||
const result = answers
|
||||
.filter((i) => i?.type === answerType)
|
||||
.map((i) => i?.data)
|
||||
.filter((i) => i);
|
||||
if (result.length === 0) {
|
||||
throw new Error('No answers');
|
||||
}
|
||||
return result;
|
||||
};
|
||||
const { result, resolverUrl } =
|
||||
urls.length === 1
|
||||
? { result: await resolveUrl(urls[0]), resolverUrl: urls[0] }
|
||||
: await resolveWithCustomDnsConcurrency(
|
||||
urls,
|
||||
customDnsConcurrency,
|
||||
resolveUrl,
|
||||
);
|
||||
resourceCache.set(
|
||||
id,
|
||||
packCustomDnsCachedResult(result, resolverUrl),
|
||||
cacheTtl,
|
||||
);
|
||||
return annotateCustomDnsResult(result, resolverUrl);
|
||||
},
|
||||
Google: async function (domain, type, noCache, timeout, edns) {
|
||||
Google: async function (
|
||||
domain,
|
||||
type,
|
||||
noCache,
|
||||
timeout,
|
||||
edns,
|
||||
url,
|
||||
tlsSkipCertVerify,
|
||||
dnsConcurrency,
|
||||
cacheTtl,
|
||||
) {
|
||||
const id = hex_md5(`GOOGLE:${domain}:${type}`);
|
||||
const cached = resourceCache.get(id);
|
||||
if (!noCache && cached) return cached;
|
||||
@@ -754,10 +966,20 @@ const DOMAIN_RESOLVERS = {
|
||||
if (result.length === 0) {
|
||||
throw new Error('No answers');
|
||||
}
|
||||
resourceCache.set(id, result);
|
||||
resourceCache.set(id, result, cacheTtl);
|
||||
return result;
|
||||
},
|
||||
'IP-API': async function (domain, type, noCache, timeout) {
|
||||
'IP-API': async function (
|
||||
domain,
|
||||
type,
|
||||
noCache,
|
||||
timeout,
|
||||
edns,
|
||||
url,
|
||||
tlsSkipCertVerify,
|
||||
dnsConcurrency,
|
||||
cacheTtl,
|
||||
) {
|
||||
if (['IPv6'].includes(type)) {
|
||||
throw new Error(`域名解析服务提供方 IP-API 不支持 ${type}`);
|
||||
}
|
||||
@@ -781,10 +1003,20 @@ const DOMAIN_RESOLVERS = {
|
||||
if (result.length === 0) {
|
||||
throw new Error('No answers');
|
||||
}
|
||||
resourceCache.set(id, result);
|
||||
resourceCache.set(id, result, cacheTtl);
|
||||
return result;
|
||||
},
|
||||
Cloudflare: async function (domain, type, noCache, timeout, edns) {
|
||||
Cloudflare: async function (
|
||||
domain,
|
||||
type,
|
||||
noCache,
|
||||
timeout,
|
||||
edns,
|
||||
url,
|
||||
tlsSkipCertVerify,
|
||||
dnsConcurrency,
|
||||
cacheTtl,
|
||||
) {
|
||||
const id = hex_md5(`CLOUDFLARE:${domain}:${type}`);
|
||||
const cached = resourceCache.get(id);
|
||||
if (!noCache && cached) return cached;
|
||||
@@ -808,10 +1040,20 @@ const DOMAIN_RESOLVERS = {
|
||||
if (result.length === 0) {
|
||||
throw new Error('No answers');
|
||||
}
|
||||
resourceCache.set(id, result);
|
||||
resourceCache.set(id, result, cacheTtl);
|
||||
return result;
|
||||
},
|
||||
Ali: async function (domain, type, noCache, timeout, edns) {
|
||||
Ali: async function (
|
||||
domain,
|
||||
type,
|
||||
noCache,
|
||||
timeout,
|
||||
edns,
|
||||
url,
|
||||
tlsSkipCertVerify,
|
||||
dnsConcurrency,
|
||||
cacheTtl,
|
||||
) {
|
||||
const id = hex_md5(`ALI:${domain}:${type}`);
|
||||
const cached = resourceCache.get(id);
|
||||
if (!noCache && cached) return cached;
|
||||
@@ -834,10 +1076,20 @@ const DOMAIN_RESOLVERS = {
|
||||
if (result.length === 0) {
|
||||
throw new Error('No answers');
|
||||
}
|
||||
resourceCache.set(id, result);
|
||||
resourceCache.set(id, result, cacheTtl);
|
||||
return result;
|
||||
},
|
||||
Tencent: async function (domain, type, noCache, timeout, edns) {
|
||||
Tencent: async function (
|
||||
domain,
|
||||
type,
|
||||
noCache,
|
||||
timeout,
|
||||
edns,
|
||||
url,
|
||||
tlsSkipCertVerify,
|
||||
dnsConcurrency,
|
||||
cacheTtl,
|
||||
) {
|
||||
const id = hex_md5(`TENCENT:${domain}:${type}`);
|
||||
const cached = resourceCache.get(id);
|
||||
if (!noCache && cached) return cached;
|
||||
@@ -858,7 +1110,7 @@ const DOMAIN_RESOLVERS = {
|
||||
if (result.length === 0) {
|
||||
throw new Error('No answers');
|
||||
}
|
||||
resourceCache.set(id, result);
|
||||
resourceCache.set(id, result, cacheTtl);
|
||||
return result;
|
||||
},
|
||||
};
|
||||
@@ -871,14 +1123,20 @@ function ResolveDomainOperator({
|
||||
url,
|
||||
tlsSkipCertVerify,
|
||||
timeout,
|
||||
cacheTtl,
|
||||
edns: _edns,
|
||||
concurrency: _concurrency,
|
||||
dnsConcurrency: _dnsConcurrency,
|
||||
}) {
|
||||
if (['IPv6', 'IP4P'].includes(_type) && ['IP-API'].includes(provider)) {
|
||||
throw new Error(`域名解析服务提供方 ${provider} 不支持 ${_type}`);
|
||||
}
|
||||
const { defaultTimeout } = $.read(SETTINGS_KEY) || {};
|
||||
const requestTimeout = timeout || defaultTimeout || 8000;
|
||||
const requestTimeout = normalizeResolveDomainTimeout(
|
||||
timeout,
|
||||
defaultTimeout,
|
||||
);
|
||||
const domainResolverCacheTtl = normalizeResolveDomainCacheTtl(cacheTtl);
|
||||
let type = ['IPv6', 'IP4P'].includes(_type) ? 'IPv6' : 'IPv4';
|
||||
|
||||
const resolver = DOMAIN_RESOLVERS[provider];
|
||||
@@ -888,18 +1146,34 @@ function ResolveDomainOperator({
|
||||
let edns = _edns || '223.6.6.6';
|
||||
if (!isIP(edns)) throw new Error(`域名解析 EDNS 应为 IP`);
|
||||
const concurrency = normalizeResolveDomainConcurrency(_concurrency);
|
||||
const customDnsUrl =
|
||||
provider === 'Custom' ? normalizeCustomDnsUrlList(url) : url;
|
||||
const customDnsCount =
|
||||
provider === 'Custom' ? parseCustomDnsUrls(customDnsUrl).length : 1;
|
||||
const customDnsConcurrency =
|
||||
provider === 'Custom'
|
||||
? normalizeResolveDomainCustomDnsConcurrency(_dnsConcurrency)
|
||||
: 1;
|
||||
const totalConcurrency =
|
||||
concurrency * Math.min(customDnsConcurrency, customDnsCount);
|
||||
const customDnsTlsSkipCertVerify =
|
||||
provider === 'Custom' &&
|
||||
(tlsSkipCertVerify === true || tlsSkipCertVerify === 'enabled');
|
||||
if (concurrency > RESOLVE_DOMAIN_CONCURRENCY_WARN_THRESHOLD) {
|
||||
if (totalConcurrency > RESOLVE_DOMAIN_CONCURRENCY_WARN_THRESHOLD) {
|
||||
$.warn(
|
||||
`域名解析并发数 ${concurrency} 超过建议值 ${RESOLVE_DOMAIN_CONCURRENCY_WARN_THRESHOLD}, 可能导致代理 App TCP 连接数激增`,
|
||||
`域名解析总并发数上限 ${totalConcurrency} 超过建议值 ${RESOLVE_DOMAIN_CONCURRENCY_WARN_THRESHOLD}, 可能导致代理 App TCP 连接数激增`,
|
||||
);
|
||||
}
|
||||
$.info(
|
||||
`Domain Resolver: [${_type}] ${provider} ${edns || ''} ${url || ''}${
|
||||
`Domain Resolver: [${_type}] ${provider} ${edns || ''} ${
|
||||
formatResolverUrlInfo(provider, customDnsUrl)
|
||||
}${
|
||||
customDnsTlsSkipCertVerify ? ' tlsSkipCertVerify=enabled' : ''
|
||||
} concurrency=${concurrency}`,
|
||||
} concurrency=${concurrency}${
|
||||
provider === 'Custom'
|
||||
? ` dnsConcurrency=${customDnsConcurrency}`
|
||||
: ''
|
||||
}`,
|
||||
);
|
||||
return {
|
||||
name: 'Resolve Domain Operator',
|
||||
@@ -919,7 +1193,7 @@ function ResolveDomainOperator({
|
||||
provider,
|
||||
p.server,
|
||||
type,
|
||||
url,
|
||||
customDnsUrl,
|
||||
customDnsTlsSkipCertVerify,
|
||||
);
|
||||
return [id, { id, domain: p.server }];
|
||||
@@ -933,15 +1207,20 @@ function ResolveDomainOperator({
|
||||
domain,
|
||||
type,
|
||||
cache,
|
||||
url,
|
||||
customDnsUrl,
|
||||
customDnsTlsSkipCertVerify,
|
||||
);
|
||||
if (cached) {
|
||||
results[id] = cached;
|
||||
const resolverUrl =
|
||||
provider === 'Custom'
|
||||
? getCustomDnsResultResolverUrl(cached) ||
|
||||
customDnsUrl
|
||||
: customDnsUrl;
|
||||
$.info(
|
||||
`Using cached resolved domain: ${domain}${formatResolverUrlLog(
|
||||
provider,
|
||||
url,
|
||||
resolverUrl,
|
||||
)} ➟ ${cached}`,
|
||||
);
|
||||
} else {
|
||||
@@ -959,21 +1238,28 @@ function ResolveDomainOperator({
|
||||
cache === 'disabled',
|
||||
requestTimeout,
|
||||
edns,
|
||||
url,
|
||||
customDnsUrl,
|
||||
customDnsTlsSkipCertVerify,
|
||||
customDnsConcurrency,
|
||||
domainResolverCacheTtl,
|
||||
);
|
||||
results[id] = ip;
|
||||
const resolverUrl =
|
||||
provider === 'Custom'
|
||||
? getCustomDnsResultResolverUrl(ip) ||
|
||||
customDnsUrl
|
||||
: customDnsUrl;
|
||||
$.info(
|
||||
`Successfully resolved domain: ${domain}${formatResolverUrlLog(
|
||||
provider,
|
||||
url,
|
||||
resolverUrl,
|
||||
)} ➟ ${ip}`,
|
||||
);
|
||||
} catch (err) {
|
||||
$.error(
|
||||
`Failed to resolve domain: ${domain}${formatResolverUrlLog(
|
||||
provider,
|
||||
url,
|
||||
customDnsUrl,
|
||||
)} with resolver [${provider}]: ${err}`,
|
||||
);
|
||||
}
|
||||
@@ -985,7 +1271,7 @@ function ResolveDomainOperator({
|
||||
provider,
|
||||
p.server,
|
||||
type,
|
||||
url,
|
||||
customDnsUrl,
|
||||
customDnsTlsSkipCertVerify,
|
||||
);
|
||||
if (id && results[id]) {
|
||||
|
||||
@@ -26,15 +26,18 @@ function createDnsResponse(query, data = '192.0.2.55') {
|
||||
id: query.id,
|
||||
flags: dnsPacket.RECURSION_DESIRED,
|
||||
questions: query.questions,
|
||||
answers: [
|
||||
{
|
||||
type: question.type,
|
||||
class: 'IN',
|
||||
name: question.name,
|
||||
ttl: 60,
|
||||
data,
|
||||
},
|
||||
],
|
||||
answers:
|
||||
data == null
|
||||
? []
|
||||
: [
|
||||
{
|
||||
type: question.type,
|
||||
class: 'IN',
|
||||
name: question.name,
|
||||
ttl: 60,
|
||||
data,
|
||||
},
|
||||
],
|
||||
});
|
||||
}
|
||||
|
||||
@@ -46,14 +49,20 @@ function getEdnsClientSubnet(query) {
|
||||
)?.ip;
|
||||
}
|
||||
|
||||
function startUdpDnsServer(onQuery) {
|
||||
function startUdpDnsServer(onQuery, { data = '192.0.2.55', delay = 0 } = {}) {
|
||||
return new Promise((resolve, reject) => {
|
||||
const server = dgram.createSocket('udp4');
|
||||
server.once('error', reject);
|
||||
server.on('message', (message, rinfo) => {
|
||||
const query = dnsPacket.decode(message);
|
||||
onQuery(query);
|
||||
server.send(createDnsResponse(query), rinfo.port, rinfo.address);
|
||||
setTimeout(() => {
|
||||
server.send(
|
||||
createDnsResponse(query, data),
|
||||
rinfo.port,
|
||||
rinfo.address,
|
||||
);
|
||||
}, delay);
|
||||
});
|
||||
server.bind(0, '127.0.0.1', () => {
|
||||
server.removeListener('error', reject);
|
||||
@@ -307,6 +316,177 @@ describe('Resolve Domain Operator', function () {
|
||||
expect(maxActiveRequests).to.equal(2);
|
||||
});
|
||||
|
||||
it('uses the global default timeout when DNS timeout is omitted', async function () {
|
||||
const timeouts = [];
|
||||
$.read = (key) => {
|
||||
if (key === SETTINGS_KEY) return { defaultTimeout: 1234 };
|
||||
return originalRead(key);
|
||||
};
|
||||
ResolveDomainOperator.resolver.Google = async (
|
||||
domain,
|
||||
type,
|
||||
noCache,
|
||||
timeout,
|
||||
) => {
|
||||
timeouts.push(timeout);
|
||||
return ['192.0.2.41'];
|
||||
};
|
||||
|
||||
const processor = ResolveDomainOperator({
|
||||
provider: 'Google',
|
||||
type: 'IPv4',
|
||||
});
|
||||
const output = await ApplyProcessor(processor, [
|
||||
{ name: 'Global Timeout', server: 'global-timeout.example.com' },
|
||||
]);
|
||||
|
||||
expect(timeouts).to.deep.equal([1234]);
|
||||
expect(output[0].server).to.equal('192.0.2.41');
|
||||
});
|
||||
|
||||
it('falls back to 8000 when the global default timeout is invalid', async function () {
|
||||
const timeouts = [];
|
||||
$.read = (key) => {
|
||||
if (key === SETTINGS_KEY) return { defaultTimeout: 0 };
|
||||
return originalRead(key);
|
||||
};
|
||||
ResolveDomainOperator.resolver.Google = async (
|
||||
domain,
|
||||
type,
|
||||
noCache,
|
||||
timeout,
|
||||
) => {
|
||||
timeouts.push(timeout);
|
||||
return ['192.0.2.45'];
|
||||
};
|
||||
|
||||
const processor = ResolveDomainOperator({
|
||||
provider: 'Google',
|
||||
type: 'IPv4',
|
||||
});
|
||||
const output = await ApplyProcessor(processor, [
|
||||
{
|
||||
name: 'Fallback Timeout',
|
||||
server: 'fallback-timeout.example.com',
|
||||
},
|
||||
]);
|
||||
|
||||
expect(timeouts).to.deep.equal([8000]);
|
||||
expect(output[0].server).to.equal('192.0.2.45');
|
||||
});
|
||||
|
||||
it('prefers explicit DNS timeout over the global default timeout', async function () {
|
||||
const timeouts = [];
|
||||
$.read = (key) => {
|
||||
if (key === SETTINGS_KEY) return { defaultTimeout: 1234 };
|
||||
return originalRead(key);
|
||||
};
|
||||
ResolveDomainOperator.resolver.Google = async (
|
||||
domain,
|
||||
type,
|
||||
noCache,
|
||||
timeout,
|
||||
) => {
|
||||
timeouts.push(timeout);
|
||||
return ['192.0.2.42'];
|
||||
};
|
||||
|
||||
const processor = ResolveDomainOperator({
|
||||
provider: 'Google',
|
||||
type: 'IPv4',
|
||||
timeout: '2345',
|
||||
});
|
||||
const output = await ApplyProcessor(processor, [
|
||||
{ name: 'Explicit Timeout', server: 'explicit-timeout.example.com' },
|
||||
]);
|
||||
|
||||
expect(timeouts).to.deep.equal([2345]);
|
||||
expect(output[0].server).to.equal('192.0.2.42');
|
||||
});
|
||||
|
||||
it('uses the global resource cache ttl when DNS cache ttl is omitted', async function () {
|
||||
const domain = 'global-cache-ttl.example.com';
|
||||
const cacheKey = hex_md5(`IP-API:${domain}`);
|
||||
cacheKeys.push(cacheKey);
|
||||
$.read = (key) => {
|
||||
if (key === SETTINGS_KEY) return { resourceCacheTtl: 7 };
|
||||
return originalRead(key);
|
||||
};
|
||||
$.http.get = async () => ({
|
||||
body: JSON.stringify({ status: 'success', query: '192.0.2.43' }),
|
||||
});
|
||||
|
||||
const before = Date.now();
|
||||
const processor = ResolveDomainOperator({
|
||||
provider: 'IP-API',
|
||||
type: 'IPv4',
|
||||
});
|
||||
const output = await ApplyProcessor(processor, [
|
||||
{ name: 'Global Cache TTL', server: domain },
|
||||
]);
|
||||
const after = Date.now();
|
||||
|
||||
expect(output[0].server).to.equal('192.0.2.43');
|
||||
expect(resourceCache.resourceCache[cacheKey].time).to.be.at.least(
|
||||
before + 7000,
|
||||
);
|
||||
expect(resourceCache.resourceCache[cacheKey].time).to.be.at.most(
|
||||
after + 7000,
|
||||
);
|
||||
});
|
||||
|
||||
it('prefers explicit DNS cache ttl over the global resource cache ttl', async function () {
|
||||
const domain = 'explicit-cache-ttl.example.com';
|
||||
const cacheKey = hex_md5(`IP-API:${domain}`);
|
||||
cacheKeys.push(cacheKey);
|
||||
$.read = (key) => {
|
||||
if (key === SETTINGS_KEY) return { resourceCacheTtl: 60 };
|
||||
return originalRead(key);
|
||||
};
|
||||
$.http.get = async () => ({
|
||||
body: JSON.stringify({ status: 'success', query: '192.0.2.44' }),
|
||||
});
|
||||
|
||||
const before = Date.now();
|
||||
const processor = ResolveDomainOperator({
|
||||
provider: 'IP-API',
|
||||
type: 'IPv4',
|
||||
cacheTtl: '5',
|
||||
});
|
||||
const output = await ApplyProcessor(processor, [
|
||||
{ name: 'Explicit Cache TTL', server: domain },
|
||||
]);
|
||||
const after = Date.now();
|
||||
|
||||
expect(output[0].server).to.equal('192.0.2.44');
|
||||
expect(resourceCache.resourceCache[cacheKey].time).to.be.at.least(
|
||||
before + 5000,
|
||||
);
|
||||
expect(resourceCache.resourceCache[cacheKey].time).to.be.at.most(
|
||||
after + 5000,
|
||||
);
|
||||
});
|
||||
|
||||
it('rejects invalid DNS timeout values', function () {
|
||||
expect(() =>
|
||||
ResolveDomainOperator({
|
||||
provider: 'Google',
|
||||
type: 'IPv4',
|
||||
timeout: 0,
|
||||
}),
|
||||
).to.throw('DNS 超时应为大于 0 的整数');
|
||||
});
|
||||
|
||||
it('rejects invalid DNS cache ttl values', function () {
|
||||
expect(() =>
|
||||
ResolveDomainOperator({
|
||||
provider: 'Google',
|
||||
type: 'IPv4',
|
||||
cacheTtl: 0,
|
||||
}),
|
||||
).to.throw('域名解析缓存时长应为大于 0 的整数');
|
||||
});
|
||||
|
||||
it('rejects invalid concurrency values', function () {
|
||||
expect(() =>
|
||||
ResolveDomainOperator({
|
||||
@@ -349,6 +529,17 @@ describe('Resolve Domain Operator', function () {
|
||||
}
|
||||
});
|
||||
|
||||
it('rejects invalid Custom DNS concurrency values', function () {
|
||||
expect(() =>
|
||||
ResolveDomainOperator({
|
||||
provider: 'Custom',
|
||||
type: 'IPv4',
|
||||
url: 'https://1.1.1.1/dns-query',
|
||||
dnsConcurrency: 0,
|
||||
}),
|
||||
).to.throw('多 DNS 并发数应为大于 0 的整数');
|
||||
});
|
||||
|
||||
it('parses Custom DNS resolver formats', function () {
|
||||
expect(parseDnsResolver('https://1.1.1.1/dns-query')).to.deep.equal({
|
||||
protocol: 'doh',
|
||||
@@ -433,6 +624,124 @@ describe('Resolve Domain Operator', function () {
|
||||
expect(requests[0].insecure).to.equal(true);
|
||||
});
|
||||
|
||||
it('uses the first Custom DNS resolver that returns valid answers', async function () {
|
||||
const originalInfo = $.info;
|
||||
const logs = [];
|
||||
const slowQueries = [];
|
||||
const fastQueries = [];
|
||||
const slowServer = await startUdpDnsServer(
|
||||
(query) => slowQueries.push(query),
|
||||
{ data: '192.0.2.59', delay: 20 },
|
||||
);
|
||||
const fastServer = await startUdpDnsServer(
|
||||
(query) => fastQueries.push(query),
|
||||
{ data: '192.0.2.60', delay: 1 },
|
||||
);
|
||||
const url = `${slowServer.host}:${slowServer.port}\n${fastServer.host}:${fastServer.port}`;
|
||||
cacheKeys.push(
|
||||
hex_md5(`CUSTOM:${url}:multi-valid.example.com:IPv4`),
|
||||
);
|
||||
|
||||
try {
|
||||
$.info = (message) => logs.push(message);
|
||||
const processor = ResolveDomainOperator({
|
||||
provider: 'Custom',
|
||||
type: 'IPv4',
|
||||
url,
|
||||
dnsConcurrency: 2,
|
||||
});
|
||||
const output = await ApplyProcessor(processor, [
|
||||
{
|
||||
name: 'Multi Custom',
|
||||
server: 'multi-valid.example.com',
|
||||
port: 443,
|
||||
},
|
||||
]);
|
||||
|
||||
expect(output[0].server).to.equal('192.0.2.60');
|
||||
expect(slowQueries).to.have.length(1);
|
||||
expect(fastQueries).to.have.length(1);
|
||||
expect(
|
||||
logs.some(
|
||||
(message) =>
|
||||
message.includes('Successfully resolved domain') &&
|
||||
message.includes(
|
||||
`${fastServer.host}:${fastServer.port}`,
|
||||
),
|
||||
),
|
||||
).to.equal(true);
|
||||
logs.length = 0;
|
||||
const cachedOutput = await ApplyProcessor(processor, [
|
||||
{
|
||||
name: 'Multi Custom Cached',
|
||||
server: 'multi-valid.example.com',
|
||||
port: 443,
|
||||
},
|
||||
]);
|
||||
|
||||
expect(cachedOutput[0].server).to.equal('192.0.2.60');
|
||||
expect(
|
||||
logs.some(
|
||||
(message) =>
|
||||
message.includes('Using cached resolved domain') &&
|
||||
message.includes(
|
||||
`${fastServer.host}:${fastServer.port}`,
|
||||
),
|
||||
),
|
||||
).to.equal(true);
|
||||
await sleep(25);
|
||||
} finally {
|
||||
$.info = originalInfo;
|
||||
await slowServer.close();
|
||||
await fastServer.close();
|
||||
}
|
||||
});
|
||||
|
||||
it('keeps the default Custom DNS concurrency at 2', async function () {
|
||||
const queries = [];
|
||||
let activeQueries = 0;
|
||||
let maxActiveQueries = 0;
|
||||
const onQuery = (query) => {
|
||||
queries.push(query);
|
||||
activeQueries += 1;
|
||||
maxActiveQueries = Math.max(maxActiveQueries, activeQueries);
|
||||
setTimeout(() => {
|
||||
activeQueries -= 1;
|
||||
}, 15);
|
||||
};
|
||||
const servers = await Promise.all(
|
||||
Array.from({ length: 3 }, () =>
|
||||
startUdpDnsServer(onQuery, { data: null, delay: 15 }),
|
||||
),
|
||||
);
|
||||
const url = servers
|
||||
.map((server) => `${server.host}:${server.port}`)
|
||||
.join('\n');
|
||||
|
||||
try {
|
||||
const processor = ResolveDomainOperator({
|
||||
provider: 'Custom',
|
||||
type: 'IPv4',
|
||||
url,
|
||||
cache: 'disabled',
|
||||
});
|
||||
const output = await ApplyProcessor(processor, [
|
||||
{
|
||||
name: 'Default Multi Custom',
|
||||
server: 'default-multi.example.com',
|
||||
port: 443,
|
||||
},
|
||||
]);
|
||||
|
||||
expect(maxActiveQueries).to.equal(2);
|
||||
expect(queries).to.have.length(3);
|
||||
expect(output[0].server).to.equal('default-multi.example.com');
|
||||
expect(output[0].resolved).to.equal(false);
|
||||
} finally {
|
||||
await Promise.all(servers.map((server) => server.close()));
|
||||
}
|
||||
});
|
||||
|
||||
it('resolves Custom UDP DNS and sends EDNS', async function () {
|
||||
const queries = [];
|
||||
const server = await startUdpDnsServer((query) => queries.push(query));
|
||||
|
||||
Reference in new issue
Block a user