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 and other providers.

The databases will be loaded on-demand and, when retrieved from the network, cached on the filesystem and updated automatically.

⚠️ For instructions on how to upgrade to 2.x, check MIGRATION.md

Usage

1. Configure your license key

Skip this step if you're not loading databases directly from MaxMind.

Get a free license key from MaxMind if you haven't one already. Once logged in, you'll find the page to generate it on the left menu, under "My License Key".

Then clone the repository, run make shell and declare your key:

application:set_env(locus, license_key, "YOUR_LICENSE_KEY").
2. Start the database loader
ok = locus:start_loader(country, {maxmind, "GeoLite2-Country"}).
% You can also use:
% * an HTTP(S) URL,
% * or a local path, e.g. "/usr/share/GeoIP/GeoLite2-City.mmdb"
% * or a {custom, Module, Args} tuple, with Module
%   implementing the locus_custom_fetcher behaviour.
3. Wait for the database to load (optional)
{ok, _DatabaseVersion} = locus:await_loader(country). % or `{error, Reason}'
4. Look up IP addresses

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

% * '{ok, Entry}' in case of success;
% * 'not_found' if no entry was found
% * '{error, _}' if something bad happened

{ok,#{<<"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. Supported File Formats
  2. Database Types and Loading
  3. Database Validation
  4. Remote sources: Downloading and Updating
  5. Remote sources: Caching
  6. Local sources: Loading and Updating
  7. Logging
  8. Event Subscriptions
  9. API Reference
  10. Tested Setup
  11. License
  12. Alternative Providers
  13. Alternative Libraries (Erlang)
  14. Alternative Libraries (Elixir)
Supported File Formats

For tarball files, the first file to be found within it with an .mmdb extension is the one that's chosen for loading.

The implementation of MaxMind DB format is complete except for the data cache container data type.

Database Types and Loading
Database Validation

Databases, local or remote, can have their compatibility validated through the locus:check/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. Check the database:
    ./locus check GeoLite2-City.mmdb
    # Loading database from "GeoLite2-City.mmdb"...
    # Database version {{2019,11,6},{11,58,0}} successfully loaded
    # Checking database for flaws...
    # Database is wholesome.
    

The script will exit with code 1 in case of failure, and 0 otherwise.

Warnings can produce failure through the --warnings-as-errors flag.

Run ./locus check --help for a description of supported options and arguments.

Remote sources: Downloading and Updating
Remote sources: Caching
Local sources: Loading and Updating
Logging
Event Subscriptions
API Reference

The API reference can be found on HexDocs.

Tested setup
License

MIT License

Copyright (c) 2017-2021 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.

Alternative Providers
Alternative Libraries (Erlang)
Alternative Libraries (Elixir)

Generated by EDoc