IP geolocation maps IP addresses to physical locations using databases compiled from various sources including WHOIS records, BGP routing data, user submissions, and commercial partnerships.
How IP Geolocation Works:
- Database Compilation: Vendors aggregate data from multiple sources
- Regional Internet Registries (ARIN, RIPE, APNIC, LACNIC, AFRINIC)
- WHOIS records showing registrant location
- BGP routing tables and AS path information
- Latency measurements (trilateration from known points)
- User-submitted location data (mobile apps, web services)
- Commercial partnerships with ISPs and network operators
- Lookup Process:
- Parse and validate IP address
- Query geolocation database (usually binary search or tree structure)
- Match IP to CIDR block with location metadata
- Return geographic coordinates, country, region, city, ISP, ASN
- Data Structure: IP ranges stored as CIDR blocks with associated metadata
- Example: 8.8.8.0/24 to Mountain View, CA, US (Google)
- Hierarchical storage for efficient lookups
Accuracy and Limitations:
- Country-level: ~95-99% accurate
- Most reliable due to RIR allocation data
- Regulatory requirements ensure accuracy
- City-level: ~50-80% accurate within 25 miles
- Varies by region and ISP
- More accurate in urban areas with dense infrastructure
- Less accurate in rural areas or when using VPN/proxy
- Coordinate-level: Approximate, not GPS-precise
- Often represents city center or ISP location
- NOT accurate enough for physical address
- Legal privacy concern if misused for precise tracking
- Mobile IPs: Less accurate due to carrier-grade NAT (CGNAT)
- Mobile carriers use centralized NAT gateways
- IP location may show carrier hub, not user location
- VPN/Proxy/Tor: Shows VPN server location, not user location
- Trivially bypassed for geo-fencing
- Requires additional signals (browser fingerprint, payment method)
Geolocation Database Providers:
- MaxMind GeoIP2: Most popular commercial solution
- City, Country, ISP, Anonymous IP databases
- Monthly updates, downloadable databases or API
- Free GeoLite2 version with lower accuracy
- IP2Location: Alternative commercial provider
- 25+ data fields including weather station, ZIP code
- More granular data in some regions
- ipinfo.io: Modern API-first service
- JSON API with generous free tier
- Privacy detection (VPN, proxy, Tor, hosting)
- Company data for business IPs
- IPGeolocation.io: API service with timezone and currency data
- Abstract API: Combined IP geolocation and enrichment
Autonomous System Number (ASN):
- Definition: Unique identifier for networks participating in BGP routing
- Format: AS followed by number (e.g., AS15169 for Google)
- Allocation: Assigned by Regional Internet Registries (RIRs)
- Uses:
- Identify ISP or hosting provider
- Detect cloud services (AS16509 = Amazon, AS8075 = Microsoft)
- Reputation scoring (some ASNs associated with spam/abuse)
- DDoS mitigation (block/rate-limit by ASN)
- Example ASNs:
- AS15169: Google LLC
- AS13335: Cloudflare
- AS16509: Amazon AWS
- AS8075: Microsoft Azure
- AS32934: Facebook/Meta
Privacy and Legal Considerations:
- Not Personal Data (usually): IP geolocation is not precise enough to identify individuals
- GDPR: IP addresses are personal data, but geolocation may not be
- Depends on precision and additional context
- Legitimate Interest: Common uses (localization, fraud prevention) often qualify
- User Consent: May be required depending on jurisdiction and use case
- Data Retention: Consider privacy policies and retention limits
- Transparency: Inform users if making geo-based decisions (blocking, pricing)
Use Cases in Detail:
- Fraud Detection:
- Compare IP location to billing address and shipping address
- Flag transactions from high-risk countries
- Detect impossible travel (e.g., login from US then China in 5 minutes)
- Identify VPN/proxy use during high-value transactions
- Content Localization:
- Auto-select language based on country
- Display regional pricing and currency
- Show relevant products/services for market
- Redirect to country-specific subdomain
- Geo-fencing (Regional Restrictions):
- Enforce content licensing agreements (streaming services)
- Comply with export control regulations
- Regional rollouts and A/B testing
- Limit access to specific markets
- Security:
- Block traffic from known bad actor countries
- Alert on logins from unusual locations
- Investigate attack sources during incidents
- Honeypot analysis and threat intelligence
API Integration Example (MaxMind GeoIP2):
// Install: npm install @maxmind/geoip2-node
import { Reader } from '@maxmind/geoip2-node'
const reader = await Reader.open('/path/to/GeoLite2-City.mmdb')
const response = reader.city('8.8.8.8')
console.log(response.country.isoCode) // "US"
console.log(response.city.names.en) // "Mountain View"
console.log(response.location.latitude) // 37.4056
console.log(response.location.longitude) // -122.0775
Alternative: IP API (Free Public Service):
// Free API (rate-limited, not for production)
const response = await fetch('http://ip-api.com/json/8.8.8.8')
const data = await response.json()
console.log(data.country) // "United States"
console.log(data.city) // "Mountain View"
console.log(data.isp) // "Google LLC"
console.log(data.as) // "AS15169 Google LLC"
Best Practices:
- Use geolocation as one signal among many, not sole decision factor
- Provide override mechanism (let users manually select country/language)
- Cache results to reduce API calls and latency
- Update databases monthly or use API for real-time accuracy
- Be transparent with users about geo-based decisions
- Don't assume precise location from IP (privacy and accuracy concerns)
- Test with VPN/proxy to understand evasion scenarios