common: Check status code & fix

This commit is contained in:
LittleChest 2026-02-12 17:12:15 +08:00
parent 61e691df70
commit 66f9b52254

View File

@ -9,10 +9,11 @@ export default async function handler(
) { ) {
if (!dns || dns.length === 0) { if (!dns || dns.length === 0) {
dns = [ dns = [
"https://dns.google/dns-query",
"https://8.8.8.8/dns-query", "https://8.8.8.8/dns-query",
"https://8.8.4.4/dns-query", "https://8.8.4.4/dns-query",
"https://[2001:4860:4860::8888]/dns-query", "https://[2001:4860:4860::8888]/dns-query",
"https://[2001:4860:4860::8888]/dns-query", "https://[2001:4860:4860::8844]/dns-query",
]; ];
} else { } else {
try { try {
@ -26,10 +27,11 @@ export default async function handler(
} }
if (!api || api.length === 0) { if (!api || api.length === 0) {
api = [ api = [
"https://dns.google/resolve",
"https://8.8.8.8/resolve", "https://8.8.8.8/resolve",
"https://8.8.4.4/resolve", "https://8.8.4.4/resolve",
"https://[2001:4860:4860::8888]/resolve", "https://[2001:4860:4860::8888]/resolve",
"https://[2001:4860:4860::8888]/resolve", "https://[2001:4860:4860::8844]/resolve",
]; ];
} else { } else {
try { try {
@ -73,6 +75,13 @@ export default async function handler(
"User-Agent": "User-Agent":
"Dohna-NS (https://github.com/LittleChest/Dohna-NS)", "Dohna-NS (https://github.com/LittleChest/Dohna-NS)",
}, },
}).then((res) => {
if (res.status !== 200) {
throw new Error(
`Failed to connect to ${server}: ${res.status} ${res.statusText}`,
);
}
return res;
}), }),
), ),
); );
@ -89,7 +98,7 @@ export default async function handler(
"Dohna-NS (https://github.com/LittleChest/Dohna-NS)", "Dohna-NS (https://github.com/LittleChest/Dohna-NS)",
}, },
}); });
break; if (res.status === 200) break;
} catch (e) { } catch (e) {
console.warn(`Failed to connect to ${server}: ${e.message}`); console.warn(`Failed to connect to ${server}: ${e.message}`);
continue; continue;
@ -189,7 +198,16 @@ async function queryDns(
if (concurrent) { if (concurrent) {
try { try {
res = await Promise.any( res = await Promise.any(
dns.map((server) => fetchUpstream(server, ip, newQueryData)), dns.map((server) =>
fetchUpstream(server, ip, newQueryData).then((res) => {
if (res.status !== 200) {
throw new Error(
`Failed to connect to ${server}: ${res.status} ${res.statusText}`,
);
}
return res;
}),
),
); );
} catch (e) { } catch (e) {
console.error("All upstream DNS over HTTPS servers failed: " + e.message); console.error("All upstream DNS over HTTPS servers failed: " + e.message);
@ -201,7 +219,7 @@ async function queryDns(
const server = servers.splice(index, 1)[0]; const server = servers.splice(index, 1)[0];
try { try {
res = await fetchUpstream(server, ip, newQueryData); res = await fetchUpstream(server, ip, newQueryData);
break; if (res.status === 200) break;
} catch (e) { } catch (e) {
console.warn(`Failed to connect to ${server}: ${e.message}`); console.warn(`Failed to connect to ${server}: ${e.message}`);
continue; continue;