This library reads the IP2Location DB format for both IP2Location and IP2Proxy and returns geo information for the given IP.
Rust 1.60.0
and above (edition 2021)bash
cargo b
bash
cargo b --release
bash
cargo t -v
toml
[dependencies]
ip2location = "0.3.2"
```rust use crate::{error, Record, DB};
const IPV4BIN: &str = "data/IP2LOCATION-LITE-DB1.BIN"; const IPV6BIN: &str = "data/IP2LOCATION-LITE-DB1.IPV6.BIN"; const IP2PROXYBIN: &str = "data/IP2PROXY-IP-COUNTRY.BIN";
// Lookup an IP v4 in the IP2Location V6 BIN Database fn iplookupinipv6bin() -> Result<(), error::Error> { let mut db = DB::fromfile(IPV6BIN)?; let record = db.iplookup("43.224.159.155".parse().unwrap())?; let record = if let Record::LocationDb(rec) = record { Some(rec) } else { None }; assert!(record.issome()); let record = record.unwrap(); assert!(!record.country.isnone()); asserteq!(record.country.clone().unwrap().shortname, "IN"); asserteq!(record.country.unwrap().long_name, "India"); Ok(()) }
// Lookup an IP v4 in the IP2Location V4 BIN Database fn iplookupinipv4bin() -> Result<(), error::Error> { let mut db = DB::fromfile(IPV4BIN)?; let record = db.iplookup("43.224.159.155".parse().unwrap())?; let record = if let Record::LocationDb(rec) = record { Some(rec) } else { None }; assert!(record.issome()); let record = record.unwrap(); assert!(!record.country.isnone()); asserteq!(record.country.clone().unwrap().shortname, "IN"); asserteq!(record.country.unwrap().long_name, "India"); Ok(()) }
// Lookup an IP in the Proxy Database fn iplookupinproxybin() -> Result<(), error::Error> { let mut db = DB::fromfile(IP2PROXYBIN)?; let record = db.iplookup("1.1.1.1".parse().unwrap())?; let record = if let Record::ProxyDb(rec) = record { Some(rec) } else { None }; assert!(record.issome()); let record = record.unwrap(); assert!(!record.country.isnone()); Ok(()) } ```
```bash cargo b --example
./target/debug/examples/lookup data/IP2LOCATION-LITE-DB1.IPV6.BIN 2a01:cb08:8d14:: Db Path: data/IP2LOCATION-LITE-DB1.IPV6.BIN |- Db Type: 1 |- Db Column: 2 |- Db Date (YY/MM/DD): 20/12/28
Ok( Record { ip: "2a01:cb08:8d14::", latitude: None, longitude: None, country: Some( Country { shortname: "FR", longname: "France", }, ), region: None, city: None, isp: None, domain: None, zipcode: None, timezone: None, netspeed: None, iddcode: None, areacode: None, weatherstationcode: None, weatherstationname: None, mcc: None, mnc: None, mobilebrand: None, elevation: None, usagetype: None, addresstype: None, category: None, }, )
./target/debug/examples/lookup data/sample.bin.px11/IP2PROXY-IP-PROXYTYPE-COUNTRY-REGION-CITY-ISP-DOMAIN-USAGETYPE-ASN-LASTSEEN-THREAT-RESIDENTIAL-PROVIDER.BIN 194.59.249.19 Db Path: data/sample.bin.px11/IP2PROXY-IP-PROXYTYPE-COUNTRY-REGION-CITY-ISP-DOMAIN-USAGETYPE-ASN-LASTSEEN-THREAT-RESIDENTIAL-PROVIDER.BIN |- Db Type: 11 |- Db Column: 13 |- Db Date (YY/MM/DD): 21/5/28
ProxyDb( ProxyRecord { ip: 1.1.1.1, country: Some( Country { shortname: "US", longname: "United States of America", }, ), region: Some( "California", ), city: Some( "Los Angeles", ), isp: Some( "APNIC and CloudFlare DNS Resolver Project", ), domain: Some( "cloudflare.com", ), isproxy: Some( IsAProxy, ), proxytype: Some( "US", ), asn: Some( "13335", ), as: Some( "CloudFlare Inc", ), lastseen: Some( "27", ), threat: Some( "-", ), provider: Some( "-", ), usage_type: Some( "CDN", ), }, ) ```
This is free software, licensed under the MIT license.
Sriram