feat: 内置的 Google/Cloudflare DNS 更换为 DoH

This commit is contained in:
xream 2024-09-09 14:56:47 +08:00
parent 5cf0c98f5f
commit c9158ceb1d
No known key found for this signature in database
GPG Key ID: 1D2C5225471789F9
2 changed files with 26 additions and 30 deletions

View File

@ -1,6 +1,6 @@
{ {
"name": "sub-store", "name": "sub-store",
"version": "2.14.380", "version": "2.14.381",
"description": "Advanced Subscription Manager for QX, Loon, Surge, Stash and ShadowRocket.", "description": "Advanced Subscription Manager for QX, Loon, Surge, Stash and ShadowRocket.",
"main": "src/main.js", "main": "src/main.js",
"scripts": { "scripts": {

View File

@ -421,26 +421,23 @@ const DOMAIN_RESOLVERS = {
const id = hex_md5(`GOOGLE:${domain}:${type}`); const id = hex_md5(`GOOGLE:${domain}:${type}`);
const cached = resourceCache.get(id); const cached = resourceCache.get(id);
if (!noCache && cached) return cached; if (!noCache && cached) return cached;
const resp = await $.http.get({ const answerType = type === 'IPv6' ? 'AAAA' : 'A';
url: `https://8.8.4.4/resolve?name=${encodeURIComponent( const res = await doh({
domain, url: 'https://8.8.4.4/dns-query',
)}&type=${ domain,
type === 'IPv6' ? 'AAAA' : 'A' type: answerType,
}&edns_client_subnet=${edns}`,
headers: {
accept: 'application/dns-json',
},
timeout, timeout,
edns,
}); });
const body = JSON.parse(resp.body);
if (body['Status'] !== 0) { const { answers } = res;
throw new Error(`Status is ${body['Status']}`);
}
const answers = body['Answer'];
if (!Array.isArray(answers) || answers.length === 0) { if (!Array.isArray(answers) || answers.length === 0) {
throw new Error('No answers'); throw new Error('No answers');
} }
const result = answers.map((i) => i?.data).filter((i) => i); const result = answers
.filter((i) => i?.type === answerType)
.map((i) => i?.data)
.filter((i) => i);
if (result.length === 0) { if (result.length === 0) {
throw new Error('No answers'); throw new Error('No answers');
} }
@ -474,28 +471,27 @@ const DOMAIN_RESOLVERS = {
resourceCache.set(id, result); resourceCache.set(id, result);
return result; return result;
}, },
Cloudflare: async function (domain, type, noCache, timeout) { Cloudflare: async function (domain, type, noCache, timeout, edns) {
const id = hex_md5(`CLOUDFLARE:${domain}:${type}`); const id = hex_md5(`CLOUDFLARE:${domain}:${type}`);
const cached = resourceCache.get(id); const cached = resourceCache.get(id);
if (!noCache && cached) return cached; if (!noCache && cached) return cached;
const resp = await $.http.get({ const answerType = type === 'IPv6' ? 'AAAA' : 'A';
url: `https://1.0.0.1/dns-query?name=${encodeURIComponent( const res = await doh({
domain, url: 'https://1.0.0.1/dns-query',
)}&type=${type === 'IPv6' ? 'AAAA' : 'A'}`, domain,
headers: { type: answerType,
accept: 'application/dns-json',
},
timeout, timeout,
edns,
}); });
const body = JSON.parse(resp.body);
if (body['Status'] !== 0) { const { answers } = res;
throw new Error(`Status is ${body['Status']}`);
}
const answers = body['Answer'];
if (!Array.isArray(answers) || answers.length === 0) { if (!Array.isArray(answers) || answers.length === 0) {
throw new Error('No answers'); throw new Error('No answers');
} }
const result = answers.map((i) => i?.data).filter((i) => i); const result = answers
.filter((i) => i?.type === answerType)
.map((i) => i?.data)
.filter((i) => i);
if (result.length === 0) { if (result.length === 0) {
throw new Error('No answers'); throw new Error('No answers');
} }