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.
⚠️ Starting on December 31st, 2019, a license key is now required to download MaxMind GeoLite2 databases.
The existing URLs have been discontinued; you should upgradelocus
to version1.9.0
or higher if you used them.
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").
ok = locus:start_loader(country, 'GeoLite2-Country').
% You can also use a HTTP URL or a local path, e.g. "/usr/share/GeoIP/GeoLite2-City.mmdb"
{ok, _DatabaseVersion} = locus:await_loader(country). % or `{error, Reason}'
% > locus:lookup(country, "93.184.216.34").
% > locus:lookup(country, "2606:2800:220:1:248:1893:25c8:1946").
{ok,#{prefix => {{93,184,216,0},31},
<<"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>>}}}}
.tar.gz
, .tgz
).tar
).mmdb
).mmdb.gz
)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 mostly complete.
locus
can deal with any MaxMind DB 2.x database that maps
IP address prefixes to arbitrary data, but no
commercial databases have yet been tested
Databases, local or remote, can have their compatibility validated through the locus:analyze/1
function after they've been loaded (see function reference.)
locus
CLI utility:
make cli
to build the script, named locus
, which will be deployed to the current directory.
./locus analyze GeoLite2-City.mmdb
# Loading database from "GeoLite2-City.mmdb"...
# Database version {{2019,11,6},{11,58,0}} 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.
last-modified
response header, if present, is used to condition subsequent download
attempts (using if-modified-since
request headers) in order to save bandwidth
pre_readiness_update_period
and post_readiness_update_period
loader settings (in milliseconds.)
insecure
loader option.
locus_erlang
is created under the 'user_cache' basedirlast-modified
response header (when present)
and used to condition downloads on subsequent boots and save bandwidth
no_cache
option when running :start_loader
debug
, info
, warning
, error
and none
logger
's
default handler; for all other scenarios,
error_logger is picked instead
error
; it can be changed in the application's env
configlocus_logger:set_loglevel/1
{event_subscriber, Subscriber}
option when starting the database
Subscriber
can be either a module implementing the locus_event_subscriber
behaviour or an arbitrary pid()
locus_event_subscriber
module documentation;
most key steps in the loader pipeline are reported (download started, download succeeded, download failed,
caching succeeded, loading failed, etc.)
The API reference can be found on HexDocs.
MIT License
Copyright (c) 2017-2020 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.
Generated by EDoc