Java: IP to Location

2021-11-20

[Code]

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
  • Option 2

    • Look it up within the application itself
      1. GeoIP2 / GeoLite2 free open-source database

        • https://www.maxmind.com/en/accounts/467533/geoip/downloads
      2. Via the Chunzhen (cz88) IP database

        • https://www.cz88.net/ to get updated IP mappings
        • √
      3. 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

Summary

  1. This database needs frequent updates to stay accurate
    • because carriers’ IP ranges change over time