locus

locus is library for Erlang/OTP and Elixir that allows you to pinpoint the country, city or ASN of IP addresses using MaxMind GeoIP2.

The free MaxMind databases you choose are loaded on-demand and, if using HTTP, cached on the filesystem and updated automatically.

You're encouraged to host your own private copies of the databases when using this library in production, both for reliability and netiquette towards MaxMind.

Usage

Clone the repository and run make console to bring up a shell.

1. Start the database loader
URL = "https://geolite.maxmind.com/download/geoip/database/GeoLite2-Country.tar.gz",
ok = locus:start_loader(country, URL).

% URL can also be a local path, e.g. "/opt/MaxMind/GeoLite2-Country.tar.gz"
2. Wait for the database to load (optional)
% Give up after 30 seconds
{ok, _DatabaseVersion} = locus:wait_for_loader(country, 30000). % {error,timeout}
3. Lookup IP addresses

% > locus:lookup(country, "93.184.216.34").
% > locus:lookup(country, "2606:2800:220:1:248:1893:25c8:1946").

{ok,#{prefix => {{93,184,216,0},24},
      <<"continent">> =>
          #{<<"code">> => <<"NA">>,
            <<"geoname_id">> => 6255149,
            <<"names">> =>
                #{<<"de">> => <<"Nordamerika">>,
                  <<"en">> => <<"North America">>,
                  <<"es">> => <<"Norteamérica"/utf8>>,
                  <<"fr">> => <<"Amérique du Nord"/utf8>>,
                  <<"ja">> => <<"北アメリカ"/utf8>>,
                  <<"pt-BR">> => <<"América do Norte"/utf8>>,
                  <<"ru">> => <<"Северная Америка"/utf8>>,
                  <<"zh-CN">> => <<"北美洲"/utf8>>}},
      <<"country">> =>
          #{<<"geoname_id">> => 6252001,
            <<"iso_code">> => <<"US">>,
            <<"names">> =>
                #{<<"de">> => <<"USA">>,
                  <<"en">> => <<"United States">>,
                  <<"es">> => <<"Estados Unidos">>,
                  <<"fr">> => <<"États-Unis"/utf8>>,
                  <<"ja">> => <<"アメリカ合衆国"/utf8>>,
                  <<"pt-BR">> => <<"Estados Unidos">>,
                  <<"ru">> => <<"США"/utf8>>,
                  <<"zh-CN">> => <<"美国"/utf8>>}},
      <<"registered_country">> =>
          #{<<"geoname_id">> => 6252001,
            <<"iso_code">> => <<"US">>,
            <<"names">> =>
                #{<<"de">> => <<"USA">>,
                  <<"en">> => <<"United States">>,
                  <<"es">> => <<"Estados Unidos">>,
                  <<"fr">> => <<"États-Unis"/utf8>>,
                  <<"ja">> => <<"アメリカ合衆国"/utf8>>,
                  <<"pt-BR">> => <<"Estados Unidos">>,
                  <<"ru">> => <<"США"/utf8>>,
                  <<"zh-CN">> => <<"美国"/utf8>>}}}}

Documentation

  1. File Formats
  2. Database Types and Loading
  3. Database Validation
  4. HTTP URLs: Downloading and Updating
  5. HTTP URLs: Caching
  6. Filesystem URLs: Loading and Updating
  7. Logging
  8. Event Subscriptions
  9. API Reference
  10. Tested Setup
  11. License
  12. Alternative Libraries (Erlang)
  13. Alternative Libraries (Elixir)
File Formats
Database Types and Loading
Database Validation

Databases, local or remote, can have their compatibility validated through the locus:analyze/1 function after they've been loaded (see function reference.)

Alternatively, they can also be checked from the command line by use of the locus CLI utility:
  1. Run make cli to build the script, named locus, which will be deployed to the current directory.
  2. Run analysis:
    ./locus analyze https://geolite.maxmind.com/download/geoip/database/GeoLite2-ASN.tar.gz
    # Loading database from "https://geolite.maxmind.com/download/geoip/database/GeoLite2-ASN.tar.gz"...
    # Database version {{2019,1,22},{11,42,28}} successfully loaded
    # Analyzing database for flaws...
    # Database is wholesome.
    

The script will exit with code 1 in case of failure, and 0 otherwise. Run ./locus analyze --help for a description of supported options and arguments.

HTTP URLs: Downloading and Updating
HTTP URLs: Caching
Filesystem URLs: Loading and Updating
Logging
Event Subscriptions
API Reference

The API reference can be found on HexDocs.

Tested setup
License

MIT License

Copyright (c) 2017-2019 Guilherme Andrade

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

locus is an independent project and has not been authorized, sponsored, or otherwise approved by MaxMind.

locus includes code extracted from OTP source code, by Ericsson AB, released under the Apache License 2.0.

Alternative Libraries (Erlang)
Alternative Libraries (Elixir)

Generated by EDoc