Lazy-MMDB is a zero-maintenance, self-updating GeoIP lookup server that provides a fast query API over a Unix Domain Socket. It leverages the MaxMind GeoIP2 databases to provide ASN, city, and country lookup capabilities for IP addresses, with automatic periodic updates to ensure data freshness.
- Fast API: Exposes endpoints for ASN, city, and country lookups over a Unix Domain Socket.
- Self-Updating: Automatically downloads and updates MaxMind GeoLite2 databases at configurable intervals.
- Lightweight: Built with Rust for performance and reliability.
- Configurable: Uses environment variables for easy customization of paths, update intervals, and database URLs.
- Concurrent Safety: Utilizes
parking_lot::RwLockfor thread-safe database access.
- Rust: Ensure you have Rust installed (version 1.56 or later recommended). Install via rustup.
- Cargo: Comes with Rust installation.
- Dependencies: The project uses external libraries like
maxminddb,tokio, andreqwest. These are managed viaCargo.toml.
-
Clone the Repository:
git clone https://github.com/canmi21/lazy-mmdb.git cd lazy-mmdb -
Build the Project:
cargo build --release
-
Configure Environment (optional): Copy
.env.exampleto.envand modify as needed:cp .env.example .env
Edit
.envto set custom paths, log levels, or database URLs. Example:LOG_LEVEL=debug SOCKET_PATH=/tmp/lazy-mmdb.sock DB_PATH=/path/to/db UPDATE_INTERVAL_HOURS=12
-
Run the Server:
cargo run --release
The server will start, download the GeoLite2 databases (if not already present), and listen on the configured Unix Domain Socket (default:
/tmp/lazy-mmdb.sock).
The server exposes three API endpoints over a Unix Domain Socket:
/lookup/asn?ip=<IP_ADDRESS>: Returns ASN information for the given IP./lookup/city?ip=<IP_ADDRESS>: Returns city-level geolocation data./lookup/country?ip=<IP_ADDRESS>: Returns country-level geolocation data.
Use curl or a similar tool to query the API over the Unix Domain Socket:
curl --unix-socket /tmp/lazy-mmdb.sock "http://localhost/lookup/country?ip=208.67.222.222"Response (example):
{"continent":{"code":"NA","geoname_id":6255149,"names":{"de":"Nordamerika","en":"North America","es":"Norteamérica","fr":"Amérique du Nord","ja":"北アメリカ","pt-BR":"América do Norte","ru":"Северная Америка","zh-CN":"北美洲"}},"country":{"geoname_id":6252001,"iso_code":"US","names":{"de":"USA","en":"United States","es":"Estados Unidos","fr":"États Unis","ja":"アメリカ","pt-BR":"EUA","ru":"США","zh-CN":"美国"}},"registered_country":{"geoname_id":6252001,"iso_code":"US","names":{"de":"USA","en":"United States","es":"Estados Unidos","fr":"États Unis","ja":"アメリカ","pt-BR":"EUA","ru":"США","zh-CN":"美国"}}}The server is configured via environment variables, with defaults provided in src/config.rs. Key settings include:
LOG_LEVEL: Set todebug,info,warn, orerror(default:info).SOCKET_PATH: Path to the Unix Domain Socket (default:/tmp/lazy-mmdb.sock).DB_PATH: Directory for storing MMDB files (default:~/lazy-mmdb).UPDATE_INTERVAL_HOURS: Database update interval in hours (default:24).ASN_DB_URL,CITY_DB_URL,COUNTRY_DB_URL: URLs for downloading GeoLite2 databases.
See .env.example for a full list of configurable options.
lazy-mmdb/
├── src/
│ ├── api.rs # API server logic and route handlers
│ ├── config.rs # Configuration loading from environment variables
│ ├── db_updater.rs # Background task for downloading and updating MMDB files
│ └── main.rs # Application entry point
├── .env.example # Example environment variable configuration
├── Cargo.toml # Rust project configuration and dependencies
└── README.md # This file
Key dependencies include:
maxminddb: For reading GeoLite2 MMDB files.tokio: For asynchronous runtime and tasks.reqwest: For downloading database files.parking_lot: For efficient concurrent state management.serdeandserde_json: For JSON serialization.lazy-sock: For Unix Domain Socket server functionality.fancy-logandlazy-motd: For logging and startup messaging.
See Cargo.toml for a complete list.
This project is licensed under the MIT License. See the LICENSE file for details.
Contributions are welcome! Please open an issue or submit a pull request on GitHub.
- MaxMind GeoLite2 for providing free geolocation databases.
- P3TERX for hosting the GeoLite2 database files.