export async function getGeoInfo(ip: string, headers: Headers) { // 1. Try platform-specific headers first (Vercel, Cloudflare, etc.) const headerCountry = headers.get('x-vercel-ip-country') || headers.get('cf-ipcountry') || headers.get('x-country-code'); if (headerCountry && headerCountry !== 'XX') { // If we have a country code, we might still want to get the name // For now, let's keep it simple or use a lookup map return { country: headerCountry, city: headers.get('x-vercel-ip-city') || 'Unknown' }; } // 2. Handle localhost if (ip === '127.0.0.1' || ip === '::1' || ip.includes('127.0.0.1')) { return { country: 'Local / Dev', city: 'Localhost' }; } // 3. Fallback to external API try { const controller = new AbortController(); const timeoutId = setTimeout(() => controller.abort(), 2000); // 2s timeout const response = await fetch(`http://ip-api.com/json/${ip}`, { signal: controller.signal }); clearTimeout(timeoutId); if (response.ok) { const data = await response.json(); if (data.status === 'success') { return { country: data.country || 'Unknown', city: data.city || 'Unknown' }; } } } catch (error) { console.error('GeoIP lookup error:', error); } return { country: 'Unknown', city: 'Unknown' }; }