Skip to content

Commit

Permalink
add generic MmdbAsnEntry to also support IPinfo database entries
Browse files Browse the repository at this point in the history
  • Loading branch information
GyulyVGC committed Jul 7, 2024
1 parent 3a8540a commit 8dfc5da
Show file tree
Hide file tree
Showing 5 changed files with 46 additions and 24 deletions.
7 changes: 4 additions & 3 deletions src/gui/pages/connection_details_page.rs
Original file line number Diff line number Diff line change
Expand Up @@ -270,7 +270,8 @@ fn get_host_info_col(
language: Language,
) -> Column<'static, Message, StyleType> {
let mut host_info_col = Column::new().spacing(4);
if r_dns.parse::<IpAddr>().is_err() || (!host.asn.name.is_empty() && host.asn.number > 0) {
if r_dns.parse::<IpAddr>().is_err() || (!host.asn.name.is_empty() && !host.asn.code.is_empty())
{
host_info_col = host_info_col.push(Rule::horizontal(10.0));
}
if r_dns.parse::<IpAddr>().is_err() {
Expand All @@ -280,10 +281,10 @@ fn get_host_info_col(
font,
));
}
if !host.asn.name.is_empty() && host.asn.number > 0 {
if !host.asn.name.is_empty() && !host.asn.code.is_empty() {
host_info_col = host_info_col.push(TextType::highlighted_subtitle_with_desc(
administrative_entity_translation(language),
&format!("{} (ASN {})", host.asn.name, host.asn.number),
&format!("{} (ASN {})", host.asn.name, host.asn.code),
font,
));
}
Expand Down
2 changes: 1 addition & 1 deletion src/gui/pages/thumbnail_page.rs
Original file line number Diff line number Diff line change
Expand Up @@ -166,7 +166,7 @@ mod tests {
domain: domain.to_string(),
asn: Asn {
name: asn.to_string(),
number: 512,
code: "512".to_string(),
},
country: Default::default(),
}
Expand Down
12 changes: 4 additions & 8 deletions src/mmdb/asn.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
use maxminddb::{geoip2, MaxMindDBError};
use crate::mmdb::types::mmdb_asn_entry::MmdbAsnEntry;
use maxminddb::MaxMindDBError;

use crate::mmdb::types::mmdb_reader::MmdbReader;
use crate::networking::types::asn::Asn;
Expand All @@ -7,17 +8,12 @@ pub const ASN_MMDB: &[u8] = include_bytes!("../../resources/DB/GeoLite2-ASN.mmdb

#[allow(clippy::module_name_repetitions)]
pub fn get_asn(address_to_lookup: &str, asn_db_reader: &MmdbReader) -> Asn {
let asn_result: Result<geoip2::Asn, MaxMindDBError> = match asn_db_reader {
let asn_result: Result<MmdbAsnEntry, MaxMindDBError> = match asn_db_reader {
MmdbReader::Default(reader) => reader.lookup(address_to_lookup.parse().unwrap()),
MmdbReader::Custom(reader) => reader.lookup(address_to_lookup.parse().unwrap()),
};
if let Ok(res) = asn_result {
if res.autonomous_system_number.is_some() && res.autonomous_system_organization.is_some() {
return Asn {
number: res.autonomous_system_number.unwrap(),
name: res.autonomous_system_organization.unwrap().to_string(),
};
}
return res.get_asn();
}
Asn::default()
}
47 changes: 36 additions & 11 deletions src/mmdb/types/mmdb_asn_entry.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,36 @@
// use maxminddb::geoip2;
//
// pub enum MmdbAsnEntry<'a> {
// Standard(geoip2::Asn<'a>),
// Ipinfo(IpinfoAsnEntry<'a>),
// }
//
// pub struct IpinfoAsnEntry<'a> {
// pub asn: Option<&'a str>,
// pub asn_name: Option<&'a str>,
// }
use crate::networking::types::asn::Asn;
use serde::Deserialize;

#[derive(Deserialize)]
pub struct MmdbAsnEntry<'a> {
#[serde(alias = "autonomous_system_number", alias = "asn")]
code: MmdbAsnCode<'a>,
#[serde(alias = "autonomous_system_organization", alias = "as_name")]
name: Option<&'a str>,
}

impl MmdbAsnEntry<'_> {
pub fn get_asn(&self) -> Asn {
Asn {
code: self.code.get_code(),
name: self.name.unwrap_or_default().to_string(),
}
}
}

#[derive(Deserialize)]
#[serde(untagged)]
enum MmdbAsnCode<'a> {
Int(Option<u32>),
Str(Option<&'a str>),
}

impl MmdbAsnCode<'_> {
fn get_code(&self) -> String {
match self {
Self::Int(Some(code)) => code.to_string(),
Self::Str(Some(code)) => code.to_string(),
_ => String::new(),
}
}
}
2 changes: 1 addition & 1 deletion src/networking/types/asn.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
#[derive(Default, Clone, PartialEq, Eq, Hash, Debug)]
pub struct Asn {
/// Autonomous System number
pub number: u32,
pub code: String,
/// Autonomous System name
pub name: String,
}

0 comments on commit 8dfc5da

Please sign in to comment.