Системный DNS Mac Studio отдаёт для sign.aitu.uz недоступные адреса Cloudflare; если relay не ответил, запрос повторяется через адреса от PEPPA_FALLBACK_DNS (по умолчанию 1.1.1.1) с тем же именем хоста. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
84 lines
3.3 KiB
TypeScript
84 lines
3.3 KiB
TypeScript
// Some Cloudflare addresses are unreachable from the Peppa host, while the system resolver keeps
|
|
// handing them out (sign.aitu.uz → 188.114.96.3/97.3, PEP-200). When a request gets no response at
|
|
// all, it is repeated through the addresses another resolver gives (PEPPA_FALLBACK_DNS, default
|
|
// 1.1.1.1), keeping the host name for TLS (SNI) and the Host header. No system settings are touched.
|
|
import { Resolver } from "node:dns/promises";
|
|
import { lookup as systemLookup } from "node:dns/promises";
|
|
import http from "node:http";
|
|
import https from "node:https";
|
|
|
|
export interface FallbackResponse {
|
|
status: number;
|
|
ok: boolean;
|
|
json(): Promise<unknown>;
|
|
}
|
|
|
|
type Resolve = (host: string) => Promise<string[]>;
|
|
|
|
let mockResolve: Resolve | null = null;
|
|
/** Tests: replace the fallback resolver (null restores the real one). */
|
|
export function mockFallbackResolver(fn: Resolve | null): void {
|
|
mockResolve = fn;
|
|
}
|
|
|
|
async function fallbackAddresses(host: string): Promise<string[]> {
|
|
if (mockResolve) return mockResolve(host);
|
|
const servers = (process.env.PEPPA_FALLBACK_DNS ?? "1.1.1.1").split(",").map((s) => s.trim()).filter(Boolean);
|
|
if (!servers.length || servers[0] === "off") return [];
|
|
const r = new Resolver({ timeout: 3_000, tries: 1 });
|
|
r.setServers(servers);
|
|
const ips = await r.resolve4(host).catch(() => [] as string[]);
|
|
const system = new Set((await systemLookup(host, { all: true, family: 4 }).catch(() => [])).map((a) => a.address));
|
|
// Addresses the system already gave were just tried; put them last.
|
|
return [...ips.filter((ip) => !system.has(ip)), ...ips.filter((ip) => system.has(ip))];
|
|
}
|
|
|
|
function requestVia(url: URL, ip: string, init: { method?: string; body?: string; headers?: Record<string, string> }, timeoutMs: number): Promise<FallbackResponse> {
|
|
const mod = url.protocol === "http:" ? http : https;
|
|
return new Promise((resolve, reject) => {
|
|
const req = mod.request(
|
|
url,
|
|
{
|
|
method: init.method ?? "GET",
|
|
headers: init.headers,
|
|
timeout: timeoutMs,
|
|
lookup: (_h, opts, cb) => {
|
|
if ((opts as { all?: boolean }).all) (cb as (e: null, a: { address: string; family: number }[]) => void)(null, [{ address: ip, family: 4 }]);
|
|
else cb(null, ip, 4);
|
|
},
|
|
},
|
|
(res) => {
|
|
const chunks: Buffer[] = [];
|
|
res.on("data", (c: Buffer) => chunks.push(c));
|
|
res.on("end", () => {
|
|
const text = Buffer.concat(chunks).toString("utf8");
|
|
const status = res.statusCode ?? 0;
|
|
resolve({ status, ok: status >= 200 && status < 300, json: async () => JSON.parse(text) });
|
|
});
|
|
res.on("error", reject);
|
|
},
|
|
);
|
|
req.on("timeout", () => req.destroy(new Error("timeout")));
|
|
req.on("error", reject);
|
|
if (init.body) req.write(init.body);
|
|
req.end();
|
|
});
|
|
}
|
|
|
|
/** The same request through the fallback resolver's addresses; null when none answered. */
|
|
export async function fetchViaFallbackDns(
|
|
target: string,
|
|
init: { method?: string; body?: string; headers?: Record<string, string> } = {},
|
|
timeoutMs = 8_000,
|
|
): Promise<FallbackResponse | null> {
|
|
const url = new URL(target);
|
|
for (const ip of await fallbackAddresses(url.hostname)) {
|
|
try {
|
|
return await requestVia(url, ip, init, timeoutMs);
|
|
} catch {
|
|
/* next address */
|
|
}
|
|
}
|
|
return null;
|
|
}
|