Merged via /review-pr -> /prepare-pr -> /merge-pr. Prepared head SHA: 71366e9532b6c67f0413b65a9ac8623eae000e9b Co-authored-by: Glucksberg <80581902+Glucksberg@users.noreply.github.com> Co-authored-by: obviyus <22031114+obviyus@users.noreply.github.com> Reviewed-by: @obviyus
72 lines
2.9 KiB
TypeScript
72 lines
2.9 KiB
TypeScript
import * as dns from "node:dns";
|
|
import * as net from "node:net";
|
|
import type { TelegramNetworkConfig } from "../config/types.telegram.js";
|
|
import { resolveFetch } from "../infra/fetch.js";
|
|
import { createSubsystemLogger } from "../logging/subsystem.js";
|
|
import {
|
|
resolveTelegramAutoSelectFamilyDecision,
|
|
resolveTelegramDnsResultOrderDecision,
|
|
} from "./network-config.js";
|
|
|
|
let appliedAutoSelectFamily: boolean | null = null;
|
|
let appliedDnsResultOrder: string | null = null;
|
|
const log = createSubsystemLogger("telegram/network");
|
|
|
|
// Node 22 workaround: enable autoSelectFamily to allow IPv4 fallback on broken IPv6 networks.
|
|
// Many networks have IPv6 configured but not routed, causing "Network is unreachable" errors.
|
|
// See: https://github.com/nodejs/node/issues/54359
|
|
function applyTelegramNetworkWorkarounds(network?: TelegramNetworkConfig): void {
|
|
// Apply autoSelectFamily workaround
|
|
const autoSelectDecision = resolveTelegramAutoSelectFamilyDecision({ network });
|
|
if (autoSelectDecision.value !== null && autoSelectDecision.value !== appliedAutoSelectFamily) {
|
|
if (typeof net.setDefaultAutoSelectFamily === "function") {
|
|
try {
|
|
net.setDefaultAutoSelectFamily(autoSelectDecision.value);
|
|
appliedAutoSelectFamily = autoSelectDecision.value;
|
|
const label = autoSelectDecision.source ? ` (${autoSelectDecision.source})` : "";
|
|
log.info(`autoSelectFamily=${autoSelectDecision.value}${label}`);
|
|
} catch {
|
|
// ignore if unsupported by the runtime
|
|
}
|
|
}
|
|
}
|
|
|
|
// Apply DNS result order workaround for IPv4/IPv6 issues.
|
|
// Some APIs (including Telegram) may fail with IPv6 on certain networks.
|
|
// See: https://github.com/openclaw/openclaw/issues/5311
|
|
const dnsDecision = resolveTelegramDnsResultOrderDecision({ network });
|
|
if (dnsDecision.value !== null && dnsDecision.value !== appliedDnsResultOrder) {
|
|
if (typeof dns.setDefaultResultOrder === "function") {
|
|
try {
|
|
dns.setDefaultResultOrder(dnsDecision.value as "ipv4first" | "verbatim");
|
|
appliedDnsResultOrder = dnsDecision.value;
|
|
const label = dnsDecision.source ? ` (${dnsDecision.source})` : "";
|
|
log.info(`dnsResultOrder=${dnsDecision.value}${label}`);
|
|
} catch {
|
|
// ignore if unsupported by the runtime
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
// Prefer wrapped fetch when available to normalize AbortSignal across runtimes.
|
|
export function resolveTelegramFetch(
|
|
proxyFetch?: typeof fetch,
|
|
options?: { network?: TelegramNetworkConfig },
|
|
): typeof fetch | undefined {
|
|
applyTelegramNetworkWorkarounds(options?.network);
|
|
if (proxyFetch) {
|
|
return resolveFetch(proxyFetch);
|
|
}
|
|
const fetchImpl = resolveFetch();
|
|
if (!fetchImpl) {
|
|
throw new Error("fetch is not available; set channels.telegram.proxy in config");
|
|
}
|
|
return fetchImpl;
|
|
}
|
|
|
|
export function resetTelegramFetchStateForTests(): void {
|
|
appliedAutoSelectFamily = null;
|
|
appliedDnsResultOrder = null;
|
|
}
|