{"id":17108460,"url":"https://github.com/g-andrade/locus","last_synced_at":"2025-05-15T22:11:05.188Z","repository":{"id":27736071,"uuid":"115066713","full_name":"g-andrade/locus","owner":"g-andrade","description":"MMDB reader for geolocation and ASN lookup of IP addresses","archived":false,"fork":false,"pushed_at":"2025-02-25T11:31:39.000Z","size":5421,"stargazers_count":116,"open_issues_count":5,"forks_count":17,"subscribers_count":6,"default_branch":"master","last_synced_at":"2025-04-08T09:09:28.991Z","etag":null,"topics":["asn-lookup","elixir","erlang","geoip","geoip2","geolite2","geolocation","maxmind-db","mmdb"],"latest_commit_sha":null,"homepage":"https://hexdocs.pm/locus/","language":"Erlang","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/g-andrade.png","metadata":{"files":{"readme":"README.md","changelog":"CHANGELOG.md","contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2017-12-22T02:16:49.000Z","updated_at":"2025-03-10T04:14:47.000Z","dependencies_parsed_at":"2023-12-16T21:17:53.803Z","dependency_job_id":"3fe340cf-3681-42d7-982d-3d9c5c667bbf","html_url":"https://github.com/g-andrade/locus","commit_stats":{"total_commits":733,"total_committers":9,"mean_commits":81.44444444444444,"dds":0.07639836289222379,"last_synced_commit":"3326feacef10f536253ed70e4426fc28b2040f2a"},"previous_names":[],"tags_count":58,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/g-andrade%2Flocus","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/g-andrade%2Flocus/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/g-andrade%2Flocus/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/g-andrade%2Flocus/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/g-andrade","download_url":"https://codeload.github.com/g-andrade/locus/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":254430331,"owners_count":22069909,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["asn-lookup","elixir","erlang","geoip","geoip2","geolite2","geolocation","maxmind-db","mmdb"],"created_at":"2024-10-14T16:05:26.000Z","updated_at":"2025-05-15T22:11:05.147Z","avatar_url":"https://github.com/g-andrade.png","language":"Erlang","funding_links":[],"categories":[],"sub_categories":[],"readme":"# locus\n\n[![Hex downloads](https://img.shields.io/hexpm/dt/locus.svg)](https://hex.pm/packages/locus)\n[![License](https://img.shields.io/hexpm/l/locus.svg)](https://github.com/g-andrade/locus/blob/master/LICENSE)\n[![Erlang Versions](https://img.shields.io/badge/Erlang%2FOTP-24%20to%2027-blue)](https://www.erlang.org)\n[![CI status](https://github.com/g-andrade/locus/actions/workflows/ci.yml/badge.svg)](https://github.com/g-andrade/locus/actions/workflows/ci.yml)\n[![Latest version](https://img.shields.io/hexpm/v/locus.svg?style=flat)](https://hex.pm/packages/locus)\n[![API reference](https://img.shields.io/badge/hex-docs-lightgreen.svg)](https://hexdocs.pm/locus/)\n[![Last commit](https://img.shields.io/github/last-commit/g-andrade/locus.svg)](https://github.com/g-andrade/locus/commits/master)\n\n`locus` is library for Erlang/OTP and Elixir that allows you to pinpoint\nthe country, city or ASN of IP addresses using [MaxMind\nGeoIP2](https://dev.maxmind.com/geoip/geoip2/geolite2/) and [other\nproviders](#alternative-providers).\n\nThe databases will be loaded on-demand and, when retrieved from the\nnetwork, cached on the filesystem and updated automatically.\n\n\u003e ⚠️ For instructions on how to upgrade to 2.x, check\n\u003e [`MIGRATION.md`](https://github.com/g-andrade/locus/blob/master/MIGRATION.md)\n\n## Usage\n\n#### 1\\. Configure your license key\n\n*Skip this step if you're not loading databases directly from\nMaxMind.*\n\nGet a free [license key](https://www.maxmind.com/en/geolite2/signup)\nfrom MaxMind if you haven't one already. Once logged in, you'll find the\npage to generate it on the left menu, under \"Manage License Keys\".\n\nThen clone the repository, run `make shell` and declare your key:\n\n``` erlang\napplication:set_env(locus, license_key, \"YOUR_LICENSE_KEY\").\n```\n\nIf you're using Elixir, add locus as a dependency to your mix project:\n\n```elixir\ndefp deps do\n    [\n      ...\n      {:locus, \"~\u003e 2.3\"}\n    ]\n  end\n```\n\nThen, configure your license key in `config.exs`:\n\n```elixir\nconfig :locus,\n  license_key: \u003cMAXMIND_API_KEY\u003e\n```\n\n#### 2\\. Start the database loader\n\n``` erlang\nok = locus:start_loader(country, {maxmind, \"GeoLite2-Country\"}).\n% You can also use:\n% * an HTTP(S) URL,\n% * or a local path, e.g. \"/usr/share/GeoIP/GeoLite2-City.mmdb\"\n% * or a {custom_fetcher, Module, Args} tuple, with Module\n%   implementing the locus_custom_fetcher behaviour.\n```\n\nOr, in Elixir, start the database loaders that you'll be using in `application.ex`:\n\n```elixir\n  def start(_type, _args) do\n    # :locus.start_loader(:asn, {:maxmind, \"GeoLite2-ASN\"})\n    # :locus.start_loader(:country, {:maxmind, \"GeoLite2-Country\"})\n    :locus.start_loader(:city, {:maxmind, \"GeoLite2-City\"})\n\n    ...\n```\n\n#### 3\\. Wait for the database to load (optional)\n\n``` erlang\n{ok, _DatabaseVersion} = locus:await_loader(country). % or `{error, Reason}'\n```\n\n#### 4\\. Look up IP addresses\n\n``` erlang\n\n% \u003e locus:lookup(country, \"93.184.216.34\").\n% \u003e locus:lookup(country, \"2606:2800:220:1:248:1893:25c8:1946\").\n\n% * '{ok, Entry}' in case of success;\n% * 'not_found' if no entry was found\n% * '{error, _}' if something bad happened\n\n{ok,#{\u003c\u003c\"continent\"\u003e\u003e =\u003e\n          #{\u003c\u003c\"code\"\u003e\u003e =\u003e \u003c\u003c\"NA\"\u003e\u003e,\n            \u003c\u003c\"geoname_id\"\u003e\u003e =\u003e 6255149,\n            \u003c\u003c\"names\"\u003e\u003e =\u003e\n                #{\u003c\u003c\"de\"\u003e\u003e =\u003e \u003c\u003c\"Nordamerika\"\u003e\u003e,\n                  \u003c\u003c\"en\"\u003e\u003e =\u003e \u003c\u003c\"North America\"\u003e\u003e,\n                  \u003c\u003c\"es\"\u003e\u003e =\u003e \u003c\u003c\"Norteamérica\"/utf8\u003e\u003e,\n                  \u003c\u003c\"fr\"\u003e\u003e =\u003e \u003c\u003c\"Amérique du Nord\"/utf8\u003e\u003e,\n                  \u003c\u003c\"ja\"\u003e\u003e =\u003e \u003c\u003c\"北アメリカ\"/utf8\u003e\u003e,\n                  \u003c\u003c\"pt-BR\"\u003e\u003e =\u003e \u003c\u003c\"América do Norte\"/utf8\u003e\u003e,\n                  \u003c\u003c\"ru\"\u003e\u003e =\u003e \u003c\u003c\"Северная Америка\"/utf8\u003e\u003e,\n                  \u003c\u003c\"zh-CN\"\u003e\u003e =\u003e \u003c\u003c\"北美洲\"/utf8\u003e\u003e}},\n      \u003c\u003c\"country\"\u003e\u003e =\u003e\n          #{\u003c\u003c\"geoname_id\"\u003e\u003e =\u003e 6252001,\n            \u003c\u003c\"iso_code\"\u003e\u003e =\u003e \u003c\u003c\"US\"\u003e\u003e,\n            \u003c\u003c\"names\"\u003e\u003e =\u003e\n                #{\u003c\u003c\"de\"\u003e\u003e =\u003e \u003c\u003c\"USA\"\u003e\u003e,\n                  \u003c\u003c\"en\"\u003e\u003e =\u003e \u003c\u003c\"United States\"\u003e\u003e,\n                  \u003c\u003c\"es\"\u003e\u003e =\u003e \u003c\u003c\"Estados Unidos\"\u003e\u003e,\n                  \u003c\u003c\"fr\"\u003e\u003e =\u003e \u003c\u003c\"États-Unis\"/utf8\u003e\u003e,\n                  \u003c\u003c\"ja\"\u003e\u003e =\u003e \u003c\u003c\"アメリカ合衆国\"/utf8\u003e\u003e,\n                  \u003c\u003c\"pt-BR\"\u003e\u003e =\u003e \u003c\u003c\"Estados Unidos\"\u003e\u003e,\n                  \u003c\u003c\"ru\"\u003e\u003e =\u003e \u003c\u003c\"США\"/utf8\u003e\u003e,\n                  \u003c\u003c\"zh-CN\"\u003e\u003e =\u003e \u003c\u003c\"美国\"/utf8\u003e\u003e}},\n      \u003c\u003c\"registered_country\"\u003e\u003e =\u003e\n          #{\u003c\u003c\"geoname_id\"\u003e\u003e =\u003e 6252001,\n            \u003c\u003c\"iso_code\"\u003e\u003e =\u003e \u003c\u003c\"US\"\u003e\u003e,\n            \u003c\u003c\"names\"\u003e\u003e =\u003e\n                #{\u003c\u003c\"de\"\u003e\u003e =\u003e \u003c\u003c\"USA\"\u003e\u003e,\n                  \u003c\u003c\"en\"\u003e\u003e =\u003e \u003c\u003c\"United States\"\u003e\u003e,\n                  \u003c\u003c\"es\"\u003e\u003e =\u003e \u003c\u003c\"Estados Unidos\"\u003e\u003e,\n                  \u003c\u003c\"fr\"\u003e\u003e =\u003e \u003c\u003c\"États-Unis\"/utf8\u003e\u003e,\n                  \u003c\u003c\"ja\"\u003e\u003e =\u003e \u003c\u003c\"アメリカ合衆国\"/utf8\u003e\u003e,\n                  \u003c\u003c\"pt-BR\"\u003e\u003e =\u003e \u003c\u003c\"Estados Unidos\"\u003e\u003e,\n                  \u003c\u003c\"ru\"\u003e\u003e =\u003e \u003c\u003c\"США\"/utf8\u003e\u003e,\n                  \u003c\u003c\"zh-CN\"\u003e\u003e =\u003e \u003c\u003c\"美国\"/utf8\u003e\u003e}}}}\n```\n\nOr, in Elixir, call the erlang library from your Elixir application:\n\n```elixir\niex\u003e :locus.lookup(:city, \"93.184.216.34\")\n{:ok,\n %{\n   \"city\" =\u003e %{\"geoname_id\" =\u003e 4945936, \"names\" =\u003e %{\"en\" =\u003e \"Norwell\"}},\n   ...\n }}\n```\n\n## Documentation\n\n1.  [Supported File Formats](#supported-file-formats)\n2.  [Database Types and Loading](#database-types-and-loading)\n3.  [Database Validation](#database-validation)\n4.  [Remote sources: Downloading and\n    Updating](#remote-sources-downloading-and-updating)\n5.  [Remote sources: Caching](#remote-sources-caching)\n6.  [Local sources: Loading and\n    Updating](#local-sources-loading-and-updating)\n7.  [Logging](#logging)\n8.  [Event Subscriptions](#event-subscriptions)\n9.  [API Reference](#api-reference)\n10. [Tested Setup](#tested-setup)\n11. [License](#license)\n12. [Alternative Providers](#alternative-providers)\n13. [Alternative Libraries (Erlang)](#alternative-libraries-erlang)\n14. [Alternative Libraries (Elixir)](#alternative-libraries-elixir)\n\n### Supported File Formats\n\n  - gzip-compressed tarballs (`.tar.gz`, `.tgz`)\n  - plain tarballs (`.tar`)\n  - MMDB files (`.mmdb`)\n  - gzip-compressed MMDB files (`.mmdb.gz`)\n\nFor tarball files, the first file to be found within it with an `.mmdb`\nextension is the one that's chosen for loading.\n\nThe implementation of [MaxMind DB\nformat](https://maxmind.github.io/MaxMind-DB/) is complete except for\nthe [`data cache\ncontainer`](https://maxmind.github.io/MaxMind-DB/#data-cache-container---12)\ndata type.\n\n### Database Types and Loading\n\n  - The free GeoLite2 [Country, City and ASN\n    databases](https://dev.maxmind.com/geoip/geoip2/geolite2/) were all\n    successfully tested; presumably `locus` can deal with [any MMDB\n    database](#alternative-providers) that maps IP address prefixes to\n    arbitrary data\n  - The databases are loaded into memory (mostly) as is; reference\n    counted binaries are shared with the application callers using\n    [`persistent_term`](https://erlang.org/doc/man/persistent_term.html),\n    and the original binary search tree is used to lookup addresses. The\n    data for each entry is decoded on the fly upon successful lookups.\n\n### Database Validation\n\nDatabases, local or remote, can have their compatibility validated\nthrough the `locus:check/1` function after they've been loaded (see\n[function reference](#api-reference).)\n\nAlternatively, they can also be checked from the command line by use of\nthe `locus` CLI utility:\n\n1.  Run `make cli` to build the script, named `locus`, which will be\n    deployed to the current directory.\n\n2.  Check the database:\n\n    ``` shell\n    ./locus check GeoLite2-City.mmdb\n    # Loading database from \"GeoLite2-City.mmdb\"...\n    # Database version {{2019,11,6},{11,58,0}} successfully loaded\n    # Checking database for flaws...\n    # Database is wholesome.\n    ```\n\nThe script will exit with code 1 in case of failure, and 0 otherwise.\n\nWarnings can produce failure through the `--warnings-as-errors` flag.\n\nRun `./locus check --help` for a description of supported options and\narguments.\n\n### Remote sources: Downloading and Updating\n\n  - The downloaded database files, when compressed, are inflated in\n    memory\n  - For MaxMind and HTTP downloads, the `last-modified` response header,\n    if present, is used to condition subsequent download attempts (using\n    `if-modified-since` request headers) in order to save bandwidth\n  - The downloaded databases are cached on the filesystem in order to\n    more quickly achieve readiness on future launches of the database\n    loader\n  - Database download attempts are retried upon error according to an\n    exponential backoff policy - quickly at first (every few seconds)\n    but gradually slowing down to every 15 minutes. Successful and\n    dismissed download attempts will be checked for update after 6\n    hours. Both of these behaviours can be tweaked through the\n    `error_retries` and `update_period` loader settings (see [function\n    reference](#api-reference).)\n  - When downloading from a MaxMind edition or an HTTP URL, the remote\n    certificate will be authenticated against [a list of known\n    Certification\n    Authorities](https://hexdocs.pm/tls_certificate_check/) and\n    connection negotiation will fail in case of an expired certificate,\n    mismatched hostname, self-signed certificate or unknown\n    certification authority. These checks can be disabled by specifying\n    the `insecure` loader option.\n\n### Remote sources: Caching\n\n  - Caching is a best effort; the system falls back to relying\n    exclusively on the network if needed\n  - By default a caching directory named `locus_erlang` is created under the\n    ['user\\_cache'\n    basedir](http://erlang.org/doc/man/filename.html#basedir-3)\n  - A cached database is named after either:\n      - the MaxMind database edition name (when explicitly downloading\n        from MaxMind), or\n      - the SHA256 hash of the HTTP(S) URL, or\n      - for `{custom_fetcher, Module, Args}` sources, a filesystem-safe\n        version of `Module`'s name concatenated with the 32-bit\n        [`erlang:phash2/2`](https://erlang.org/doc/man/erlang.html#phash2-2)\n        value of the opaque database source as returned by the\n        callbacks.\n  - Modification time of the databases is retrieved from either:\n      - the `last-modified` response header (when present, for MaxMind\n        and HTTP(S) sources)\n      - the `modified_on` metadata property for successful\n        `locus_custom_fetcher` `:fetch/1` and `:conditionally_fetch/2`\n        callbacks (for databases loaded with `locus_custom_fetcher`)\n  - Caching can be disabled by specifying the `no_cache` option when\n    running `:start_loader`\n  - The cache database location can be customised by providing\n    `{database_cache_file, FilePath}` option for `locus_loader`\n    (`FilePath` must have a \".mmdb.gz\" extension)\n\n### Local sources: Loading and Updating\n\n  - The loaded database files, when compressed, are inflated in memory\n  - The database modification timestamp is used to condition subsequent\n    load attempts in order to lower I/O activity\n  - Database load attempts are retried upon error according to an\n    exponential backoff policy - quickly at first (every few seconds)\n    but gradually slowing down to every 30 seconds. Successful and\n    dismissed load attempts will be checked for update after 30 seconds.\n    Both of these behaviours can be tweaked through the `error_retries`\n    and `update_period` loader settings (see [function\n    reference](#api-reference).)\n\n### Logging\n\n  - Five logging levels are supported: `debug`, `info`, `warning`,\n    `error` and `none`\n  - The chosen backend is\n    [logger](http://erlang.org/doc/man/logger.html) *if*\n    [lager](https://github.com/erlang-lager/lager/) is either missing or\n    it hasn't\n    [removed](https://github.com/erlang-lager/lager/issues/492)\n    `logger`'s default handler.\n  - The default log level is `error`; it can be changed in the\n    application's `env` config\n  - To tweak the log level in runtime, use `locus_logger:set_loglevel/1`\n\n### Event Subscriptions\n\n  - Any number of event subscribers can be attached to a database loader\n    by specifying the `{event_subscriber, Subscriber}` option when\n    starting the database\n  - A `Subscriber` can be either a module implementing the\n    `locus_event_subscriber` behaviour or an arbitrary `pid()`\n  - The format and content of reported events can be consulted in detail\n    on the `locus_event_subscriber` module documentation; most key steps\n    in the loader pipeline are reported (download started, download\n    succeeded, download failed, caching succeeded, loading failed, etc.)\n\n### API Reference\n\nThe API reference can be found on [HexDocs](https://hexdocs.pm/locus/).\n\n### Tested setup\n\n  - Erlang/OTP 24 or newer\n  - rebar3\n\n### License\n\nMIT License\n\nCopyright (c) 2017-2024 Guilherme Andrade\n\nPermission is hereby granted, free of charge, to any person obtaining a\ncopy of this software and associated documentation files (the\n\"Software\"), to deal in the Software without restriction, including\nwithout limitation the rights to use, copy, modify, merge, publish,\ndistribute, sublicense, and/or sell copies of the Software, and to\npermit persons to whom the Software is furnished to do so, subject to\nthe following conditions:\n\nThe above copyright notice and this permission notice shall be included\nin all copies or substantial portions of the Software.\n\nTHE SOFTWARE IS PROVIDED \"AS IS\", WITHOUT WARRANTY OF ANY KIND, EXPRESS\nOR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF\nMERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.\nIN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY\nCLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,\nTORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE\nSOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.\n\n`locus` is an independent project and has not been authorized,\nsponsored, or otherwise approved by MaxMind.\n\n### Alternative Providers\n\n  - [IPinfo](https://ipinfo.io/) MMDB databases are compatible with `locus`\n    **[since version 2.3.8](https://github.com/g-andrade/locus/issues/44)**\n  - [DB-IP.com](https://db-ip.com/db/): lite databases seem to work but\n    setting up auto-update for them is not practical, as there's no\n    \"latest\" official URL.\n\n### Alternative Libraries (Erlang)\n\n  - [egeoip](https://github.com/mochi/egeoip): IP Geolocation module,\n    currently supporting the MaxMind GeoLite City Database\n  - [geodata2](https://github.com/brigadier/geodata2): Application for\n    working with MaxMind geoip2 (.mmdb) databases\n  - [geoip](https://github.com/manifest/geoip): Returns the location of\n    an IP address; based on the ipinfodb.com web service\n  - [geolite2data](https://hex.pm/packages/geolite2data): Periodically\n    fetches the free MaxMind GeoLite2 databases\n  - [ip2location-erlang](https://github.com/ip2location/ip2location-erlang):\n    Uses IP2Location geolocation database\n\n### Alternative Libraries (Elixir)\n\n  - [asn](https://hex.pm/packages/asn): IP-to-AS-to-ASname lookup\n  - [freegeoip](https://hex.pm/packages/freegeoip): Simple wrapper for\n    freegeoip.net HTTP API\n  - [freegeoipx](https://hex.pm/packages/freegeoipx): API Client for\n    freegeoip.net\n  - [geoip](https://hex.pm/packages/geoip): Lookup the geo location for\n    a given IP address, hostname or Plug.Conn instance\n  - [geolix](https://hex.pm/packages/geolix): MaxMind GeoIP2 database\n    reader/decoder\n  - [plug\\_geoip2](https://hex.pm/packages/plug_geoip2): Adds geo\n    location to a Plug connection based upon the client IP address by\n    using MaxMind's GeoIP2 database\n  - [tz\\_world](https://hex.pm/packages/tz_world): Resolve timezones\n    rom a location efficiently using PostGIS and Ecto\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fg-andrade%2Flocus","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fg-andrade%2Flocus","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fg-andrade%2Flocus/lists"}