feat(proxy-fitness): multi-endpoint egress geo probe chain
Try ipwho.is -> ip-api.com -> ipapi.co -> ipinfo.io in order so a
rate-limited/broken provider falls through to the next; ipinfo (most
quota-bound) is last. Normalize each payload to {ip,country,region,city,org},
keep 15s timeout per endpoint, and support GEO_PROBE_URL to replace the
chain with a single custom endpoint.
This commit is contained in:
@@ -2,14 +2,13 @@
|
||||
// Proxy Pools egress column) and any provider region policy that wants to
|
||||
// pre-mark pools unfit by egress region.
|
||||
//
|
||||
// The probe transport is provider-agnostic: it fetches ipinfo.io THROUGH the
|
||||
// pool itself (relay headers for vercel/cloudflare/deno, proxy URL for
|
||||
// socks/http), so it works for every pool type and every provider.
|
||||
|
||||
// The probe transport is provider-agnostic: it fetches ipinfo.io THROUGH the
|
||||
// pool itself (relay headers for vercel/cloudflare/deno, proxy URL for
|
||||
// The probe transport is provider-agnostic: it fetches a geo service THROUGH
|
||||
// the pool itself (relay headers for vercel/cloudflare/deno, proxy URL for
|
||||
// socks/http), so it works for every pool type and every provider.
|
||||
//
|
||||
// A chain of free endpoints is tried in order so a rate-limited or broken
|
||||
// provider drops to the next; ipinfo (the most quota-bound) is last.
|
||||
//
|
||||
// State lives on globalThis (same reason as proxyPoolFitness): the background
|
||||
// probe and the /api/proxy-pools reader must share ONE cache across Next dev
|
||||
// bundles.
|
||||
@@ -82,49 +81,96 @@ export function pruneStaleGeo(now = Date.now()) {
|
||||
return removed;
|
||||
}
|
||||
|
||||
// Probe the egress geo of one pool via ipinfo through the pool. Fail-open:
|
||||
// Normalize a single provider's geo payload to { ip, country, region, city, org }.
|
||||
const GEO_PARSE = {
|
||||
"ipwho.is": (d) => ({ ip: d?.ip, country: d?.country, region: d?.region, city: d?.city, org: d?.org || d?.connection?.org }),
|
||||
"ip-api": (d) => ({ ip: d?.query, country: d?.country, region: d?.regionName, city: d?.city, org: d?.org }),
|
||||
"ipapi.co": (d) => ({ ip: d?.ip, country: d?.country_name, region: d?.region, city: d?.city, org: d?.org }),
|
||||
ipinfo: (d) => ({ ip: d?.ip, country: d?.country, region: d?.region, city: d?.city, org: d?.org }),
|
||||
};
|
||||
|
||||
// Tried in order — rate-friendly first, quota-bound ipinfo last.
|
||||
const GEO_PROBES = [
|
||||
{ name: "ipwho.is", url: "https://ipwho.is/" },
|
||||
{ name: "ip-api", url: "https://ip-api.com/json/?fields=status,message,query,country,regionName,city,org" },
|
||||
{ name: "ipapi.co", url: "https://ipapi.co/json/" },
|
||||
{ name: "ipinfo", url: "https://ipinfo.io/json" },
|
||||
];
|
||||
|
||||
// Custom single endpoint via env (replaces the chain). Keys mapped generically.
|
||||
function customGeoParse(d) {
|
||||
if (!d || typeof d !== "object") return null;
|
||||
const pick = (...keys) => keys.map((k) => d[k]).find((v) => v != null && v !== "");
|
||||
return {
|
||||
ip: pick("query", "ip"),
|
||||
country: pick("country", "country_name", "countryName"),
|
||||
region: pick("regionName", "region", "state", "regionCode"),
|
||||
city: pick("city"),
|
||||
org: pick("org", "organization", "isp", "orgName", "connection", "asn"),
|
||||
};
|
||||
}
|
||||
|
||||
function probeSource({ proxyAwareFetch, url, proxyOptions, timeoutMs, name }) {
|
||||
const ctrl = new AbortController();
|
||||
const timer = setTimeout(() => ctrl.abort(new Error("geo probe timeout")), timeoutMs);
|
||||
return (async () => {
|
||||
try {
|
||||
const res = await proxyAwareFetch(url, { signal: ctrl.signal }, proxyOptions);
|
||||
if (!res.ok) {
|
||||
const txt = await res.text().catch(() => "");
|
||||
const error = res.status === 429 || res.status === 403 ? "rate-limit"
|
||||
: res.status >= 500 ? "server"
|
||||
: "network";
|
||||
return { ok: false, error, detail: `${res.status} ${txt.slice(0, 80)}` };
|
||||
}
|
||||
const data = await res.json().catch(() => null);
|
||||
const parsed = name ? GEO_PARSE[name]?.(data) : customGeoParse(data);
|
||||
if (!parsed?.ip) return { ok: false, error: "no-ip", detail: `${name || "custom"}` };
|
||||
return {
|
||||
ok: true,
|
||||
geo: {
|
||||
ip: String(parsed.ip || "").trim(),
|
||||
country: String(parsed.country || "").trim(),
|
||||
region: String(parsed.region || "").trim(),
|
||||
city: String(parsed.city || "").trim(),
|
||||
org: String(parsed.org || "").trim(),
|
||||
isDatacenter: /(cloudflare|vercel|amazon|aws|google|microsoft|azure|digitalocean|hetzner|ovh|contabo|leaseweb)/i.test(
|
||||
String(parsed.org || ""),
|
||||
),
|
||||
},
|
||||
};
|
||||
} catch (error) {
|
||||
const timedOut = ctrl.signal?.aborted && error?.name === "AbortError";
|
||||
return { ok: false, error: timedOut ? "timeout" : "network", detail: `${error?.name}: ${error?.message}` };
|
||||
} finally {
|
||||
clearTimeout(timer);
|
||||
}
|
||||
})();
|
||||
}
|
||||
|
||||
// Probe the egress geo of one pool through a chain of geo providers. Fail-open:
|
||||
// returns { ok:true, geo } on success, { ok:false, error } otherwise with
|
||||
// `error` one of "rate-limit" | "server" | "no-ip" | "network" | "timeout".
|
||||
// `pool` shape: { proxyUrl, type }.
|
||||
// `pool` shape: { proxyUrl, type }. 15s timeout per endpoint by default.
|
||||
export async function probePoolGeo(pool, timeoutMs = 15000) {
|
||||
const proxyUrl = pool?.proxyUrl;
|
||||
if (!proxyUrl) return { ok: false, error: "network" };
|
||||
if (!proxyUrl) return { ok: false, error: "network", detail: "no proxy url" };
|
||||
const { proxyAwareFetch } = await import("../utils/proxyFetch.js");
|
||||
const isRelay = ["vercel", "cloudflare", "deno"].includes(pool?.type);
|
||||
const proxyOptions = isRelay
|
||||
? { vercelRelayUrl: proxyUrl }
|
||||
: { connectionProxyEnabled: true, connectionProxyUrl: proxyUrl };
|
||||
|
||||
const ctrl = new AbortController();
|
||||
const timer = setTimeout(() => ctrl.abort(new Error("geo probe timeout")), timeoutMs);
|
||||
try {
|
||||
const res = await proxyAwareFetch("https://ipinfo.io/json", { signal: ctrl.signal }, proxyOptions);
|
||||
if (!res.ok) {
|
||||
const txt = await res.text().catch(() => "");
|
||||
const error = res.status === 429 || res.status === 403 ? "rate-limit"
|
||||
: res.status >= 500 ? "server"
|
||||
: "network";
|
||||
return { ok: false, error, detail: `${res.status} ${txt.slice(0, 60)}` };
|
||||
}
|
||||
const data = await res.json().catch(() => null);
|
||||
if (!data?.ip) {
|
||||
return { ok: false, error: "no-ip" };
|
||||
}
|
||||
return {
|
||||
ok: true,
|
||||
geo: {
|
||||
ip: data.ip || "",
|
||||
country: data.country || "",
|
||||
region: data.region || "",
|
||||
city: data.city || "",
|
||||
org: data.org || "",
|
||||
isDatacenter: /(cloudflare|vercel|amazon|aws|google|microsoft|azure|digitalocean|hetzner|ovh|contabo|leaseweb)/i.test(String(data.org || "")),
|
||||
},
|
||||
};
|
||||
} catch (error) {
|
||||
const timedOut = ctrl.signal?.aborted && error?.name === "AbortError";
|
||||
return { ok: false, error: timedOut ? "timeout" : "network", detail: `${error?.name}: ${error?.message}` };
|
||||
} finally {
|
||||
clearTimeout(timer);
|
||||
const custom = process.env.GEO_PROBE_URL ? [{ name: null, url: process.env.GEO_PROBE_URL }] : [];
|
||||
const probes = custom.length ? custom : GEO_PROBES;
|
||||
|
||||
let last = { ok: false, error: "network", detail: "no provider responded" };
|
||||
for (const p of probes) {
|
||||
const res = await probeSource({ proxyAwareFetch, url: p.url, proxyOptions, timeoutMs, name: p.name });
|
||||
if (res.ok) return res;
|
||||
// Prefer the most specific failure: rate-limit/server over generic network.
|
||||
if (res.error === "rate-limit" || res.error === "server") last = res;
|
||||
else if (last.error === "network") last = res;
|
||||
}
|
||||
return last;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user