Skip to content

Commit

Permalink
refactored MMDB related items
Browse files Browse the repository at this point in the history
  • Loading branch information
GyulyVGC committed Nov 30, 2023
1 parent 3673f53 commit 0fd3a28
Show file tree
Hide file tree
Showing 12 changed files with 68 additions and 51 deletions.
19 changes: 0 additions & 19 deletions src/countries/country_utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ use iced::widget::tooltip::Position;
use iced::widget::Svg;
use iced::widget::Tooltip;
use iced::{Font, Length, Renderer};
use maxminddb::{geoip2, MaxMindDBError};

use crate::countries::flags_pictures::{
AD, AE, AF, AG, AI, AL, AM, AO, AQ, AR, AS, AT, AU, AW, AX, AZ, BA, BB, BD, BE, BF, BG, BH, BI,
Expand All @@ -25,26 +24,8 @@ use crate::networking::types::traffic_type::TrafficType;
use crate::translations::translations_2::{
local_translation, unknown_translation, your_network_adapter_translation,
};
use crate::utils::asn::MmdbReader;
use crate::{Language, StyleType};

pub const COUNTRY_MMDB: &[u8] = include_bytes!("../../resources/DB/GeoLite2-Country.mmdb");

pub fn get_country(address_to_lookup: &str, country_db_reader: &MmdbReader) -> Country {
let country_result: Result<geoip2::Country, MaxMindDBError> = match country_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(res1) = country_result {
if let Some(res2) = res1.country {
if let Some(res3) = res2.iso_code {
return Country::from_str(res3);
}
}
}
Country::ZZ // unknown
}

fn get_flag_from_country(
country: Country,
width: f32,
Expand Down
2 changes: 1 addition & 1 deletion src/gui/pages/settings_advanced_page.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,13 +19,13 @@ use crate::gui::styles::text::TextType;
use crate::gui::styles::text_input::TextInputType;
use crate::gui::styles::types::custom_palette::CustomPalette;
use crate::gui::types::message::Message;
use crate::mmdb::types::mmdb_reader::MmdbReader;
use crate::translations::translations_2::country_translation;
use crate::translations::translations_3::{
advanced_settings_translation, custom_style_translation, file_path_translation,
info_mmdb_paths_translation, mmdb_paths_translation, params_not_editable_translation,
restore_defaults_translation, scale_factor_translation,
};
use crate::utils::asn::MmdbReader;
use crate::utils::formatted_strings::get_default_report_directory;
use crate::utils::types::icon::Icon;
use crate::{ConfigAdvancedSettings, Language, Sniffer, Status, StyleType};
Expand Down
16 changes: 10 additions & 6 deletions src/gui/types/sniffer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,14 +12,16 @@ use pcap::Device;

use crate::chart::manage_chart_data::update_charts_data;
use crate::configs::types::config_window::ConfigWindow;
use crate::countries::country_utils::COUNTRY_MMDB;
use crate::gui::components::types::my_modal::MyModal;
use crate::gui::pages::types::running_page::RunningPage;
use crate::gui::pages::types::settings_page::SettingsPage;
use crate::gui::styles::types::custom_palette::{CustomPalette, ExtraStyles};
use crate::gui::styles::types::gradient_type::GradientType;
use crate::gui::types::message::Message;
use crate::gui::types::status::Status;
use crate::mmdb::asn::ASN_MMDB;
use crate::mmdb::country::COUNTRY_MMDB;
use crate::mmdb::types::mmdb_reader::MmdbReader;
use crate::networking::manage_packets::get_capture_result;
use crate::networking::types::filters::Filters;
use crate::networking::types::host::Host;
Expand All @@ -32,7 +34,6 @@ use crate::report::get_report_entries::get_searched_entries;
use crate::report::types::report_sort_type::ReportSortType;
use crate::secondary_threads::parse_packets::parse_packets;
use crate::translations::types::language::Language;
use crate::utils::asn::{mmdb_reader, MmdbReader, ASN_MMDB};
use crate::utils::formatted_strings::{get_default_report_directory, push_pcap_file_name};
use crate::utils::types::web_page::WebPage;
use crate::{
Expand Down Expand Up @@ -137,11 +138,14 @@ impl Sniffer {
last_focus_time: std::time::Instant::now(),
advanced_settings: configs.advanced_settings.clone(),
window: configs.window,
country_mmdb_reader: Arc::new(mmdb_reader(
country_mmdb_reader: Arc::new(MmdbReader::from(
&configs.advanced_settings.mmdb_country,
COUNTRY_MMDB,
)),
asn_mmdb_reader: Arc::new(mmdb_reader(&configs.advanced_settings.mmdb_asn, ASN_MMDB)),
asn_mmdb_reader: Arc::new(MmdbReader::from(
&configs.advanced_settings.mmdb_asn,
ASN_MMDB,
)),
}
}

Expand Down Expand Up @@ -283,11 +287,11 @@ impl Sniffer {
}
Message::CustomCountryDb(db) => {
self.advanced_settings.mmdb_country = db.clone();
self.country_mmdb_reader = Arc::new(mmdb_reader(&db, COUNTRY_MMDB));
self.country_mmdb_reader = Arc::new(MmdbReader::from(&db, COUNTRY_MMDB));
}
Message::CustomAsnDb(db) => {
self.advanced_settings.mmdb_asn = db.clone();
self.asn_mmdb_reader = Arc::new(mmdb_reader(&db, ASN_MMDB));
self.asn_mmdb_reader = Arc::new(MmdbReader::from(&db, ASN_MMDB));
}
Message::CustomReportDirectory(directory) => {
let path = push_pcap_file_name(PathBuf::from(directory));
Expand Down
1 change: 1 addition & 0 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ mod cli;
mod configs;
mod countries;
mod gui;
mod mmdb;
mod networking;
mod notifications;
mod report;
Expand Down
24 changes: 4 additions & 20 deletions src/utils/asn.rs → src/mmdb/asn.rs
Original file line number Diff line number Diff line change
@@ -1,28 +1,12 @@
use maxminddb::{geoip2, MaxMindDBError, Reader};
use maxminddb::{geoip2, MaxMindDBError};

use crate::mmdb::types::mmdb_reader::MmdbReader;
use crate::networking::types::asn::Asn;

pub const ASN_MMDB: &[u8] = include_bytes!("../../resources/DB/GeoLite2-ASN.mmdb");

pub enum MmdbReader {
Default(Reader<&'static [u8]>),
Custom(Reader<Vec<u8>>),
}

pub fn mmdb_reader(mmdb_path: &String, default_mmdb: &'static [u8]) -> MmdbReader {
let default_reader = maxminddb::Reader::from_source(default_mmdb).unwrap();
if mmdb_path.is_empty() {
MmdbReader::Default(default_reader)
} else {
let custom_reader_result = maxminddb::Reader::open_readfile(mmdb_path);
if let Ok(custom_reader) = custom_reader_result {
return MmdbReader::Custom(custom_reader);
}
MmdbReader::Default(default_reader)
}
}

pub fn asn(address_to_lookup: &str, asn_db_reader: &MmdbReader) -> Asn {
#[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 {
MmdbReader::Default(reader) => reader.lookup(address_to_lookup.parse().unwrap()),
MmdbReader::Custom(reader) => reader.lookup(address_to_lookup.parse().unwrap()),
Expand Down
22 changes: 22 additions & 0 deletions src/mmdb/country.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
use maxminddb::{geoip2, MaxMindDBError};

use crate::countries::types::country::Country;
use crate::mmdb::types::mmdb_reader::MmdbReader;

pub const COUNTRY_MMDB: &[u8] = include_bytes!("../../resources/DB/GeoLite2-Country.mmdb");

#[allow(clippy::module_name_repetitions)]
pub fn get_country(address_to_lookup: &str, country_db_reader: &MmdbReader) -> Country {
let country_result: Result<geoip2::Country, MaxMindDBError> = match country_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(res1) = country_result {
if let Some(res2) = res1.country {
if let Some(res3) = res2.iso_code {
return Country::from_str(res3);
}
}
}
Country::ZZ // unknown
}
3 changes: 3 additions & 0 deletions src/mmdb/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
pub mod asn;
pub mod country;
pub mod types;
21 changes: 21 additions & 0 deletions src/mmdb/types/mmdb_reader.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
use maxminddb::Reader;

pub enum MmdbReader {
Default(Reader<&'static [u8]>),
Custom(Reader<Vec<u8>>),
}

impl MmdbReader {
pub fn from(mmdb_path: &String, default_mmdb: &'static [u8]) -> MmdbReader {
let default_reader = maxminddb::Reader::from_source(default_mmdb).unwrap();
if mmdb_path.is_empty() {
MmdbReader::Default(default_reader)
} else {
let custom_reader_result = maxminddb::Reader::open_readfile(mmdb_path);
if let Ok(custom_reader) = custom_reader_result {
return MmdbReader::Custom(custom_reader);
}
MmdbReader::Default(default_reader)
}
}
}
1 change: 1 addition & 0 deletions src/mmdb/types/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
pub mod mmdb_reader;
7 changes: 4 additions & 3 deletions src/networking/manage_packets.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,9 @@ use dns_lookup::lookup_addr;
use etherparse::{Ethernet2Header, IpHeader, PacketHeaders, TransportHeader};
use pcap::{Active, Address, Capture, Device};

use crate::countries::country_utils::get_country;
use crate::mmdb::asn::get_asn;
use crate::mmdb::country::get_country;
use crate::mmdb::types::mmdb_reader::MmdbReader;
use crate::networking::types::address_port_pair::AddressPortPair;
use crate::networking::types::app_protocol::from_port_to_application_protocol;
use crate::networking::types::data_info_host::DataInfoHost;
Expand All @@ -16,7 +18,6 @@ use crate::networking::types::info_address_port_pair::InfoAddressPortPair;
use crate::networking::types::my_device::MyDevice;
use crate::networking::types::traffic_direction::TrafficDirection;
use crate::networking::types::traffic_type::TrafficType;
use crate::utils::asn::{asn, MmdbReader};
use crate::utils::formatted_strings::get_domain_from_r_dns;
use crate::IpVersion::{IPv4, IPv6};
use crate::{AppProtocol, InfoTraffic, IpVersion, TransProtocol};
Expand Down Expand Up @@ -262,7 +263,7 @@ pub fn reverse_dns_lookup(
);
let is_local = is_local_connection(&address_to_lookup, &my_interface_addresses);
let country = get_country(&address_to_lookup, country_db_reader);
let asn = asn(&address_to_lookup, asn_db_reader);
let asn = get_asn(&address_to_lookup, asn_db_reader);
let r_dns = if let Ok(result) = lookup_result {
if result.is_empty() {
address_to_lookup.clone()
Expand Down
2 changes: 1 addition & 1 deletion src/secondary_threads/parse_packets.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,14 @@ use std::thread;
use etherparse::PacketHeaders;
use pcap::{Active, Capture};

use crate::mmdb::types::mmdb_reader::MmdbReader;
use crate::networking::manage_packets::{
analyze_headers, get_address_to_lookup, modify_or_insert_in_map, reverse_dns_lookup,
};
use crate::networking::types::data_info::DataInfo;
use crate::networking::types::filters::Filters;
use crate::networking::types::info_address_port_pair::InfoAddressPortPair;
use crate::networking::types::my_device::MyDevice;
use crate::utils::asn::MmdbReader;
use crate::InfoTraffic;

/// The calling thread enters in a loop in which it waits for network packets, parses them according
Expand Down
1 change: 0 additions & 1 deletion src/utils/mod.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,2 @@
pub mod asn;
pub mod formatted_strings;
pub mod types;

0 comments on commit 0fd3a28

Please sign in to comment.