UNPKG

@lock-dev/geo-block

Version:

Geographic blocking module for lock.dev security framework

43 lines (42 loc) 1.48 kB
/** * IP-API.com provider - a free geo-ip lookup service * Free tier allows up to 45 requests per minute */ export class IpApiProvider { constructor(config) { this.apiKey = config.apiKey; } async init() { // No initialization needed } async lookup(ip) { try { // Construct URL based on whether we have an API key (for pro version) const url = this.apiKey ? `https://pro.ip-api.com/json/${ip}?key=${this.apiKey}&fields=status,message,countryCode,region,city,lat,lon` : `http://ip-api.com/json/${ip}?fields=status,message,countryCode,region,city,lat,lon`; const response = await fetch(url); if (!response.ok) { throw new Error(`IP-API request failed with status: ${response.status}`); } const data = await response.json(); if (data.status === 'success') { return { country: data.countryCode, region: data.region, city: data.city, latitude: data.lat, longitude: data.lon }; } else { console.warn(`IP-API lookup failed: ${data.message}`); return {}; } } catch (error) { console.error(`Error looking up IP ${ip} with IP-API:`, error); return {}; } } }