Java: IP to Location
2021-11-20
Requirements
- The server needed to restrict access by IP
- Mainland China IPs: allow through
- Hong Kong IPs: require extra authentication
- US IPs: cannot authenticate
How the problem was solved
-
Option 1
- Handle it in Nginx by passing the address in a header
- Integration: http_geoip_module
- Handle it in Nginx by passing the address in a header
-
Option 2
- Look it up within the application itself
-
GeoIP2 / GeoLite2 free open-source database
- https://www.maxmind.com/en/accounts/467533/geoip/downloads
-
Via the Chunzhen (cz88) IP database
- https://www.cz88.net/ to get updated IP mappings
- √
-
Via APIs from IP lookup websites
- e.g. https://www.ip138.com/iplookup.asp?ip=122.196.203.216&action=2
- e.g. https://tool.lu/ip/
- e.g. https://ip.tool.chinaz.com/27.46.106.39
-
- Look it up within the application itself
Summary
- This database needs frequent updates to stay accurate
- because carriers’ IP ranges change over time