Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
39 changes: 24 additions & 15 deletions src/ip/mmdb/mmdb_reader.rs
Original file line number Diff line number Diff line change
@@ -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<Vec<u8>>
pub struct MMDBReader {
reader: Reader<Vec<u8>>,
}

impl MMDBReader {
pub fn from(path: &str) -> Option<Self> {
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<Option<T>, MaxMindDbError> {
self.reader.lookup(ip)
}
pub fn lookup(&mut self,address: &str) -> Option<MMDBResult> {
if let Ok(Some(result)) = self.raw_lookup::<MMDBRecord>(address.parse().unwrap()) {
return Some(result.get_result())

pub fn lookup(&mut self, address: &str) -> Option<MMDBResult> {
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
}
}
}
34 changes: 8 additions & 26 deletions src/ip/mmdb/mmdb_record.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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(),
}
}
}