diff --git a/Cargo.lock b/Cargo.lock index 346b9fc..d5e6747 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -1332,9 +1332,9 @@ dependencies = [ [[package]] name = "maxminddb" -version = "0.26.0" +version = "0.27.1" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "2a197e44322788858682406c74b0b59bf8d9b4954fe1f224d9a25147f1880bba" +checksum = "99681a80368084e68fff1a4ec657b09ae6a04f1107762ec6346a82b8cc19d8eb" dependencies = [ "ipnetwork", "log", diff --git a/Cargo.toml b/Cargo.toml index 6bf0efc..9747738 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -22,7 +22,7 @@ socket2 = "0.6.1" listenfd = "1.0.2" futures = "0.3.31" #ip -maxminddb = "0.26.0" +maxminddb = "0.27" #image processing imageproc = "0.25.0" ab_glyph = "0.2.32" diff --git a/src/ip/mmdb/mmdb_reader.rs b/src/ip/mmdb/mmdb_reader.rs index e0762e7..fc7d41a 100644 --- a/src/ip/mmdb/mmdb_reader.rs +++ b/src/ip/mmdb/mmdb_reader.rs @@ -1,27 +1,36 @@ -use std::net::IpAddr; -use maxminddb::{MaxMindDbError, Reader}; -use serde::Deserialize; -use crate::ip::mmdb::mmdb_record::{MMDBRecord, MMDBResult}; +use crate::ip::mmdb::mmdb_record::MMDBResult; +use log::warn; +use maxminddb::Reader; -pub struct MMDBReader { - reader : Reader> +pub struct MMDBReader { + reader: Reader>, } impl MMDBReader { pub fn from(path: &str) -> Option { if let Ok(custom_reader) = maxminddb::Reader::open_readfile(path) { - Some(MMDBReader {reader : custom_reader}) + Some(MMDBReader { + reader: custom_reader, + }) } else { None } } - fn raw_lookup<'a, T: Deserialize<'a>>(&'a self, ip: IpAddr) -> Result, MaxMindDbError> { - self.reader.lookup(ip) - } - pub fn lookup(&mut self,address: &str) -> Option { - if let Ok(Some(result)) = self.raw_lookup::(address.parse().unwrap()) { - return Some(result.get_result()) + + pub fn lookup(&mut self, address: &str) -> Option { + match self.reader.lookup(address.parse().unwrap()) { + Err(e) => { + warn!("Geo IP database error: {}", e); + None + } + Ok(o) => { + if let Ok(Some(result)) = o.decode() { + Some(result) + } else { + warn!("Failed to deserialise Geo IP data {:?}", o); + None + } + } } - None } -} \ No newline at end of file +} diff --git a/src/ip/mmdb/mmdb_record.rs b/src/ip/mmdb/mmdb_record.rs index 2a96780..eb5a6f6 100644 --- a/src/ip/mmdb/mmdb_record.rs +++ b/src/ip/mmdb/mmdb_record.rs @@ -2,38 +2,20 @@ use serde::Deserialize; /*ip info country asn database model*/ -#[derive(Clone, PartialEq, Eq, Hash, Debug)] +#[derive(Clone, PartialEq, Eq, Hash, Debug, Deserialize)] pub struct MMDBResult { + #[serde(default)] pub asn: String, + #[serde(default)] pub as_name: String, + #[serde(default)] pub as_domain: String, + #[serde(default)] pub continent: String, + #[serde(default)] pub continent_name: String, + #[serde(default)] pub country: String, + #[serde(default)] pub country_name: String, } - -#[derive(Deserialize)] -pub struct MMDBRecord<'a> { - asn: Option<&'a str>, - as_name: Option<&'a str>, - as_domain: Option<&'a str>, - continent: Option<&'a str>, - continent_name: Option<&'a str>, - country: Option<&'a str>, - country_name: Option<&'a str>, -} - -impl MMDBRecord<'_> { - pub fn get_result(&self) -> MMDBResult { - MMDBResult { - asn: self.asn.unwrap_or_default().to_string(), - as_name: self.as_name.unwrap_or_default().to_string(), - as_domain: self.as_domain.unwrap_or_default().to_string(), - continent: self.continent.unwrap_or_default().to_string(), - continent_name: self.continent_name.unwrap_or_default().to_string(), - country: self.country.unwrap_or_default().to_string(), - country_name: self.country_name.unwrap_or_default().to_string(), - } - } -} \ No newline at end of file