java.lang.Object
net.i2p.router.transport.GeoIP

public class GeoIPextends Object
Manage geoip lookup in a file with the Tor geoip format.

The lookup is expensive, so a lookup is queued with add(). The actual lookup of multiple IPs is fired with lookup(). To get a country for an IP, use get() which returns a lower-case, generally two-letter country code or null.

Everything here uses longs, since Java is signed-only, the file is sorted by unsigned, and we don't store the table in memory (unlike in Blocklist.java, where it's in-memory so we want to be space-efficient).

  • Field Details

    • PROP_GEOIP_ENABLED

      static final String PROP_GEOIP_ENABLED
      Property: whether GeoIP lookups are enabled.
      See Also:
    • PROP_NORMALIZE_ISP

      static final String PROP_NORMALIZE_ISP
      Normalize ISP/org names from ASN database (strip suffixes, abbreviate, title-case).
      See Also:
    • PROP_GEOIP_DIR

      public static final String PROP_GEOIP_DIR
      Property: the directory the GeoIP databases are read from.
      See Also:
    • GEOIP_DIR_DEFAULT

      public static final String GEOIP_DIR_DEFAULT
      Default GeoIP directory, under the router's base directory.
      See Also:
    • GEOIP_FILE_DEFAULT

      static final String GEOIP_FILE_DEFAULT
      Default geoip file name.
      See Also:
    • GEOIP2_FILE_DEFAULT

      public static final String GEOIP2_FILE_DEFAULT
      GEOIP2_FILE_DEFAULT.
      See Also:
    • ASN_FILE_DEFAULT

      public static final String ASN_FILE_DEFAULT
      Default MaxMind ASN database file name.
      See Also:
    • COUNTRY_FILE_DEFAULT

      static final String COUNTRY_FILE_DEFAULT
      Default country names file name.
      See Also:
    • PROP_IP_COUNTRY

      public static final String PROP_IP_COUNTRY
      Property: the last country an IP was seen in, set for the console.
      See Also:
    • PROP_DEBIAN_GEOIP

      public static final String PROP_DEBIAN_GEOIP
      Property: the Debian-packaged IPv4 GeoIP database path.
      See Also:
    • PROP_DEBIAN_GEOIPV6

      public static final String PROP_DEBIAN_GEOIPV6
      PROP_DEBIAN_GEOIPV6.
      See Also:
    • PROP_BLOCK_MY_COUNTRY

      public static final String PROP_BLOCK_MY_COUNTRY
      Property: whether to refuse tunnels to destinations in our own country.
      See Also:
  • Constructor Details

    • GeoIP

      public GeoIP(I2PAppContext context)
      GeoIP lookup service for IP to country mapping.
      Parameters:
      context - RouterContext in production, I2PAppContext for testing only
  • Method Details

    • shutdown

      public void shutdown()
      Shutdown and release resources.
    • blockingLookup

      public void blockingLookup()
      Blocking lookup of all pending IPs. Results will be added to the table and available via get() after completion.

      Public for BundleRouterInfos.

    • getOrgName

      public String getOrgName(String ipAddress)
      The organization name for an IP from the local ASN database. Returns the AS organization (e.g. "Google LLC") or null if not found.
      Parameters:
      ipAddress - IPv4 or IPv6 address string
      Returns:
      org name or null
    • normalizeOrgName

      public static String normalizeOrgName(String raw)
      Normalize an ISP/org name from the ASN database. Strips suffixes, abbreviates verbose words, title-cases with acronym preservation.
      Parameters:
      raw - original org name from MMDB
      Returns:
      the normalized organisation name, or the input if it was null or empty
    • getGeoIPBuildInfo

      public String getGeoIPBuildInfo()
      Return the country database's build date, size and path, for the console.
      Returns:
      the HTML build info, or a not-found or error message
    • getASNBuildInfo

      public String getASNBuildInfo()
      Return the current ASN database version.
      Returns:
      the HTML build info, or a not-found or error message
    • notifyVersion

      static void notifyVersion(I2PAppContext ctx, String subtype, long version)
      Tell the update manager.
      Parameters:
      ctx - the application context whose client app manager holds the registered update manager
      subtype - the updater subtype recorded against the GEOIP update type, e.g. "GeoIP"
      version - the version number being reported; anything zero or below is ignored
    • notifyVersion

      static void notifyVersion(I2PAppContext ctx, String subtype, String version)
      Tell the update manager, with a version string as-is, e.g. the product version embedded in a database's metadata.
      Parameters:
      ctx - the application context whose client app manager holds the registered update manager
      subtype - the updater subtype recorded against the GEOIP update type, e.g. "ASN"
      version - the version being reported; null or empty is ignored
    • banCountry

      public static void banCountry(RouterContext ctx, String country)
      Bans routers located in a specified country.

      This method iterates through all known routers in the network database and adds those located in the specified country to the banlist. Routers are banned indefinitely with a reason message that reflects whether the application is in 'block my country' mode or 'hidden' mode.

      Parameters:
      ctx - the RouterContext instance providing access to network database and banlist
      country - the country code (ISO 3166-1 alpha-2 format) of the country whose routers should be banned
    • add

      public void add(String ip)
      Add to the list needing lookup Public for BundleRouterInfos.
      Parameters:
      ip - IPv4 or IPv6
    • add

      public void add(byte[] ip)
      Add to the list needing lookup Public for BundleRouterInfos.
      Parameters:
      ip - IPv4 or IPv6
    • get

      public String get(String ip)
      The country for an IP from the cache. Public for BundleRouterInfos.
      Parameters:
      ip - IPv4 or IPv6
      Returns:
      lower-case code, generally two letters, or null.
    • get

      String get(byte[] ip)
      The country for an IP from the cache.
      Parameters:
      ip - IPv4 or IPv6
      Returns:
      lower-case code, generally two letters, or null.
    • fullName

      public String fullName(String code)
      The untranslated country name for a country code. Public for BundleRouterInfos.
      Parameters:
      code - two-letter lower case code
      Returns:
      untranslated name or null
    • getCountries

      public Map<String,String> getCountries()
      The country code map.
      Returns:
      Map of two-letter lower case code to untranslated country name, unmodifiable