From 23cdb026e5076b16e8f669c73e0bf4d7d057ded6 Mon Sep 17 00:00:00 2001 From: Tim Small Date: Wed, 31 Dec 2025 12:20:10 +0000 Subject: [PATCH 1/2] Update to maxminddb 0.27 --- Cargo.lock | 4 ++-- Cargo.toml | 2 +- src/ip/mmdb/mmdb_reader.rs | 29 ++++++++++++++++++----------- src/ip/mmdb/mmdb_record.rs | 34 ++++++++-------------------------- 4 files changed, 29 insertions(+), 40 deletions(-) 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..859d418 100644 --- a/src/ip/mmdb/mmdb_reader.rs +++ b/src/ip/mmdb/mmdb_reader.rs @@ -1,7 +1,6 @@ -use std::net::IpAddr; -use maxminddb::{MaxMindDbError, Reader}; -use serde::Deserialize; -use crate::ip::mmdb::mmdb_record::{MMDBRecord, MMDBResult}; +use log::warn; +use maxminddb::Reader; +use crate::ip::mmdb::mmdb_record::MMDBResult; pub struct MMDBReader { reader : Reader> @@ -15,13 +14,21 @@ impl MMDBReader { 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()) + 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 From 0bd67b50d2436ee2ac77f3671d88b2351049fa59 Mon Sep 17 00:00:00 2001 From: Tim Small Date: Wed, 31 Dec 2025 16:08:40 +0000 Subject: [PATCH 2/2] Apply rustfmt to mmdb (whitespace changes). --- src/ip/mmdb/mmdb_reader.rs | 12 +++++++----- 1 file changed, 7 insertions(+), 5 deletions(-) diff --git a/src/ip/mmdb/mmdb_reader.rs b/src/ip/mmdb/mmdb_reader.rs index 859d418..fc7d41a 100644 --- a/src/ip/mmdb/mmdb_reader.rs +++ b/src/ip/mmdb/mmdb_reader.rs @@ -1,21 +1,23 @@ +use crate::ip::mmdb::mmdb_record::MMDBResult; use log::warn; use maxminddb::Reader; -use crate::ip::mmdb::mmdb_record::MMDBResult; -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 } } - pub fn lookup(&mut self,address: &str) -> Option { + pub fn lookup(&mut self, address: &str) -> Option { match self.reader.lookup(address.parse().unwrap()) { Err(e) => { warn!("Geo IP database error: {}", e);