Skip to content

Instantly share code, notes, and snippets.

@abdullahdevrel
Last active April 10, 2024 18:58
Show Gist options
  • Save abdullahdevrel/ace2c80bd53a7323a18bbf8c8ae6a4d2 to your computer and use it in GitHub Desktop.
Save abdullahdevrel/ace2c80bd53a7323a18bbf8c8ae6a4d2 to your computer and use it in GitHub Desktop.
IPinfo Country ASN mmdb database + Rust
use maxminddb::MaxMindDBError;
use std::net::IpAddr;
use serde::{Deserialize, Serialize};
// Declare the struct based on the IPinfo IP database schema
#[derive(Deserialize, Serialize, Clone, Debug)]
pub struct IpinfoCountryASN<'a> {
pub country: Option<&'a str>,
pub country_name: Option<&'a str>,
pub continent: Option<&'a str>,
pub continent_name: Option<&'a str>,
pub asn: Option<&'a str>,
pub as_name: Option<&'a str>,
pub as_domain: Option<&'a str>,
}
fn main() -> Result<(), MaxMindDBError> {
// Replace with the actual path to your IPinfo IP database MMDB path
let mmdb_path = "./country_asn.mmdb";
// Open the MMDB IP database
let reader = maxminddb::Reader::open_readfile(&mmdb_path)?;
// Replace with the IP address you want to look up
let ip_address: IpAddr = "0.0.0.0".parse().expect("Failed to parse IP address");
// Perform the IP address lookup
let record: IpinfoCountryASN = reader.lookup(ip_address).unwrap();
println!("Country: {}", record.country.unwrap());
println!("Country Name: {}", record.country_name.unwrap());
println!("Continent: {}", record.continent.unwrap());
println!("Continent Name: {}", record.continent_name.unwrap());
println!("ASN: {}", record.asn.unwrap());
println!("AS Organization Name: {}", record.as_name.unwrap());
println!("AS Domain/Website: {}", record.as_domain.unwrap());
println!("{:?}", record);
Ok(())
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment