feat: 域名解析支持类型和过滤

This commit is contained in:
xream 2024-01-19 21:43:54 +08:00
parent ca0d800bbb
commit c7d00ac512
No known key found for this signature in database
GPG Key ID: 1D2C5225471789F9
2 changed files with 34 additions and 19 deletions

View File

@ -1,6 +1,6 @@
{ {
"name": "sub-store", "name": "sub-store",
"version": "2.14.184", "version": "2.14.185",
"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

@ -348,14 +348,14 @@ function ScriptOperator(script, targetPlatform, $arguments, source) {
} }
const DOMAIN_RESOLVERS = { const DOMAIN_RESOLVERS = {
Google: async function (domain) { Google: async function (domain, type) {
const id = hex_md5(`GOOGLE:${domain}`); const id = hex_md5(`GOOGLE:${domain}:${type}`);
const cached = resourceCache.get(id); const cached = resourceCache.get(id);
if (cached) return cached; if (cached) return cached;
const resp = await $.http.get({ const resp = await $.http.get({
url: `https://8.8.4.4/resolve?name=${encodeURIComponent( url: `https://8.8.4.4/resolve?name=${encodeURIComponent(
domain, domain,
)}&type=A`, )}&type=${type === 'IPv6' ? 'AAAA' : 'A'}`,
headers: { headers: {
accept: 'application/dns-json', accept: 'application/dns-json',
}, },
@ -389,14 +389,14 @@ const DOMAIN_RESOLVERS = {
resourceCache.set(id, result); resourceCache.set(id, result);
return result; return result;
}, },
Cloudflare: async function (domain) { Cloudflare: async function (domain, type) {
const id = hex_md5(`CLOUDFLARE:${domain}`); const id = hex_md5(`CLOUDFLARE:${domain}:${type}`);
const cached = resourceCache.get(id); const cached = resourceCache.get(id);
if (cached) return cached; if (cached) return cached;
const resp = await $.http.get({ const resp = await $.http.get({
url: `https://1.0.0.1/dns-query?name=${encodeURIComponent( url: `https://1.0.0.1/dns-query?name=${encodeURIComponent(
domain, domain,
)}&type=A`, )}&type=${type === 'IPv6' ? 'AAAA' : 'A'}`,
headers: { headers: {
accept: 'application/dns-json', accept: 'application/dns-json',
}, },
@ -413,14 +413,14 @@ const DOMAIN_RESOLVERS = {
resourceCache.set(id, result); resourceCache.set(id, result);
return result; return result;
}, },
Ali: async function (domain) { Ali: async function (domain, type) {
const id = hex_md5(`ALI:${domain}`); const id = hex_md5(`ALI:${domain}:${type}`);
const cached = resourceCache.get(id); const cached = resourceCache.get(id);
if (cached) return cached; if (cached) return cached;
const resp = await $.http.get({ const resp = await $.http.get({
url: `http://223.6.6.6/resolve?name=${encodeURIComponent( url: `http://223.6.6.6/resolve?name=${encodeURIComponent(
domain, domain,
)}&type=A&short=1`, )}&type=${type === 'IPv6' ? 'AAAA' : 'A'}&short=1`,
headers: { headers: {
accept: 'application/dns-json', accept: 'application/dns-json',
}, },
@ -433,14 +433,14 @@ const DOMAIN_RESOLVERS = {
resourceCache.set(id, result); resourceCache.set(id, result);
return result; return result;
}, },
Tencent: async function (domain) { Tencent: async function (domain, type) {
const id = hex_md5(`ALI:${domain}`); const id = hex_md5(`ALI:${domain}:${type}`);
const cached = resourceCache.get(id); const cached = resourceCache.get(id);
if (cached) return cached; if (cached) return cached;
const resp = await $.http.get({ const resp = await $.http.get({
url: `http://119.28.28.28/d?type=A&dn=${encodeURIComponent( url: `http://119.28.28.28/d?type=${
domain, type === 'IPv6' ? 'AAAA' : 'A'
)}`, }&dn=${encodeURIComponent(domain)}`,
headers: { headers: {
accept: 'application/dns-json', accept: 'application/dns-json',
}, },
@ -455,10 +455,13 @@ const DOMAIN_RESOLVERS = {
}, },
}; };
function ResolveDomainOperator({ provider }) { function ResolveDomainOperator({ provider, type, filter }) {
if (type === 'IPv6' && ['IP-API'].includes(provider)) {
throw new Error(`域名解析服务提供方 ${provider} 不支持 IPv6`);
}
const resolver = DOMAIN_RESOLVERS[provider]; const resolver = DOMAIN_RESOLVERS[provider];
if (!resolver) { if (!resolver) {
throw new Error(`Cannot find resolver: ${provider}`); throw new Error(`找不到域名解析服务提供方: ${provider}`);
} }
return { return {
name: 'Resolve Domain Operator', name: 'Resolve Domain Operator',
@ -477,7 +480,7 @@ function ResolveDomainOperator({ provider }) {
const currentBatch = []; const currentBatch = [];
for (let domain of totalDomain.splice(0, limit)) { for (let domain of totalDomain.splice(0, limit)) {
currentBatch.push( currentBatch.push(
resolver(domain) resolver(domain, type)
.then((ip) => { .then((ip) => {
results[domain] = ip; results[domain] = ip;
$.info( $.info(
@ -504,7 +507,19 @@ function ResolveDomainOperator({ provider }) {
} }
}); });
return proxies; return proxies.filter((p) => {
if (filter === 'removeFailed') {
return p['no-resolve'] || p.resolved;
} else if (filter === 'IPOnly') {
return isIP(p.server);
} else if (filter === 'IPv4Only') {
return isIPv4(p.server);
} else if (filter === 'IPv6Only') {
return isIPv6(p.server);
} else {
return true;
}
});
}, },
}; };
} }