export interface GeoInfo { country: string; city: string; } const LOCAL_IPS = new Set(["127.0.0.1", "::1", "localhost"]); function isLocalIp(ip: string): boolean { if (LOCAL_IPS.has(ip)) return true; if (ip.startsWith("192.168.") || ip.startsWith("10.") || ip.startsWith("172.")) return true; return false; } export async function getGeoFromIp(ip: string): Promise { // Strip port if present (e.g. "::ffff:1.2.3.4") const cleanIp = ip.replace(/^::ffff:/, "").split(",")[0].trim(); if (isLocalIp(cleanIp)) { return { country: "Réseau local", city: "" }; } try { const res = await fetch( `http://ip-api.com/json/${encodeURIComponent(cleanIp)}?fields=status,country,city&lang=fr`, { signal: AbortSignal.timeout(3000) } ); if (!res.ok) throw new Error("HTTP error"); const data = await res.json(); if (data.status === "success") { return { country: data.country || "", city: data.city || "" }; } } catch { // Geo lookup failed — non-blocking } return { country: "", city: "" }; }