Crates.io | db_ip |
lib.rs | db_ip |
version | 0.3.9 |
source | src |
created_at | 2022-02-10 20:29:33.604958 |
updated_at | 2024-08-17 19:01:54.588633 |
description | An (unofficial) Rust library for querying db-ip.com data |
homepage | |
repository | https://github.com/finnbear/db_ip/ |
max_upload_size | |
id | 530435 |
size | 19,360 |
An (unofficial) library for querying db-ip.com CSV databases in safe Rust.
This library is not affiliated with or endorsed by db-ip.com.
Be advised that, by using this library with lite databases (such as the one downloaded automatically in the build step), you are subject to license terms (requiring attribution).
You can use DbIpDatabase<CountryCode>
to get the actual two-letter country code. The country code database will be
embedded, in a compressed form, in your Rust binary.
use db_ip::{DbIpDatabase, CountryCode, include_country_code_database};
// Embed compressed database in binary:
let db = include_country_code_database!();
// Or, load it from the filesystem:
// let db = DbIpDatabase::<CountryCode>::from_csv_file("country_or_city_data.csv").unwrap();
assert_eq!(
db.get(&"192.99.174.0".parse().unwrap()),
Some(CountryCode::from_str("US").unwrap())
);
You can use DbIpDatabase<Region>
, enabled by the region
feature, to gain a broad understanding of an IP's location.
Since there are fewer possibilities, this takes less binary size and RAM.
use db_ip::{DbIpDatabase, Region, include_region_database};
// Embed compressed database in binary:
let db = include_region_database!();
// Or, load it from the filesystem:
// let db = DbIpDatabase::<Region>::from_csv_file("country_or_city_data.csv").unwrap();
assert_eq!(
db.get(&"192.99.174.0".parse().unwrap()),
Some(Region::NorthAmerica)
);
Finally, you can implement IpData
yourself, to store any other type of data that can be derived from Country or
City data records.
You can manually download the actual ip geolocation data (in CSV format) in one of the following ways.
download-country-lite
feature, which attempts to download the most recent available Country lite dataOnce you have downloaded a CSV file, use the csv
feature to load it.
The raw csv data takes a while to parse, even in release mode. You may use
the serde
feature to create and load a serialized version.
You can selectively disable the ipv4
and ipv6
features, depending on your needs. Both are
on by default.
Lookups are relatively speedy, taking less than 100ns in release mode.
If you want easier access to data other than CountryCode
and Region
, create an issue.
The db-ip.com API is not currently supported, so it is difficult to keep the database up to date.
Code licensed under either of
at your option.
Bundled/downloaded geolocation data licensed under LICENSE-DBIP.
Unless you explicitly state otherwise, any contribution intentionally submitted for inclusion in the work by you, as defined in the Apache-2.0 license, shall be dual licensed as above, without any additional terms or conditions.