From bcff93914bba05b8768a056e9f74b4fac8b90e64 Mon Sep 17 00:00:00 2001 From: GyulyVGC Date: Tue, 31 Dec 2024 16:09:24 +0100 Subject: [PATCH] create struct MmdbReaders and other minor improvements --- src/gui/components/footer.rs | 6 +++--- src/gui/pages/inspect_page.rs | 2 +- src/gui/pages/settings_general_page.rs | 18 +++++++----------- src/gui/pages/thumbnail_page.rs | 6 +++--- src/gui/sniffer.rs | 24 +++++++++++------------- src/mmdb/types/mmdb_reader.rs | 7 +++++++ src/networking/manage_packets.rs | 17 ++++++++--------- src/notifications/notify_and_log.rs | 4 ++-- src/report/get_report_entries.rs | 6 +++--- src/secondary_threads/check_updates.rs | 4 ++-- src/secondary_threads/parse_packets.rs | 13 +++++-------- 11 files changed, 52 insertions(+), 55 deletions(-) diff --git a/src/gui/components/footer.rs b/src/gui/components/footer.rs index 0b4b0f22..c7efcd0b 100644 --- a/src/gui/components/footer.rs +++ b/src/gui/components/footer.rs @@ -1,6 +1,6 @@ //! GUI bottom footer -use std::sync::{Arc, Mutex}; +use std::sync::Mutex; use iced::widget::text::LineHeight; use iced::widget::tooltip::Position; @@ -28,7 +28,7 @@ pub fn footer<'a>( color_gradient: GradientType, font: Font, font_footer: Font, - newer_release_available: &Arc>>, + newer_release_available: &Mutex>, ) -> Container<'a, Message, StyleType> { if thumbnail { return thumbnail_footer(); @@ -136,7 +136,7 @@ fn get_release_details<'a>( language: Language, font: Font, font_footer: Font, - newer_release_available: &Arc>>, + newer_release_available: &Mutex>, ) -> Row<'a, Message, StyleType> { let mut ret_val = Row::new() .align_y(Alignment::Center) diff --git a/src/gui/pages/inspect_page.rs b/src/gui/pages/inspect_page.rs index 4c9d7813..db824d05 100644 --- a/src/gui/pages/inspect_page.rs +++ b/src/gui/pages/inspect_page.rs @@ -486,7 +486,7 @@ fn filter_combobox( move |new_value| Message::Search(filter_input_type.new_search(&search_params, new_value)); let mut combobox = ComboBox::new(combo_box_state, "", Some(&filter_value), update_fn.clone()) - .on_input(update_fn.clone()) + .on_input(update_fn) .padding([2, 5]) .size(FONT_SIZE_FOOTER) .font(font) diff --git a/src/gui/pages/settings_general_page.rs b/src/gui/pages/settings_general_page.rs index ab432b51..1b90e46c 100644 --- a/src/gui/pages/settings_general_page.rs +++ b/src/gui/pages/settings_general_page.rs @@ -1,5 +1,3 @@ -use std::sync::Arc; - use iced::widget::text::LineHeight; use iced::widget::tooltip::Position; use iced::widget::{ @@ -16,7 +14,7 @@ use crate::gui::styles::container::ContainerType; use crate::gui::styles::style_constants::FONT_SIZE_SUBTITLE; use crate::gui::styles::text::TextType; use crate::gui::types::message::Message; -use crate::mmdb::types::mmdb_reader::MmdbReader; +use crate::mmdb::types::mmdb_reader::{MmdbReader, MmdbReaders}; use crate::translations::translations::language_translation; use crate::translations::translations_2::country_translation; use crate::translations::translations_3::{ @@ -91,8 +89,7 @@ fn column_all_general_setting(sniffer: &Sniffer, font: Font) -> Column( font: Font, country_path: &str, asn_path: &str, - country_reader: &Arc, - asn_reader: &Arc, + mmdb_readers: &MmdbReaders, ) -> Column<'a, Message, StyleType> { Column::new() .spacing(5) @@ -266,7 +262,7 @@ fn mmdb_settings<'a>( font, Message::CustomCountryDb, country_path, - country_reader, + &mmdb_readers.country, country_translation(language), language, )) @@ -275,7 +271,7 @@ fn mmdb_settings<'a>( font, Message::CustomAsnDb, asn_path, - asn_reader, + &mmdb_readers.asn, "ASN", language, )) @@ -286,14 +282,14 @@ fn mmdb_selection_row<'a>( font: Font, message: fn(String) -> Message, custom_path: &str, - mmdb_reader: &Arc, + mmdb_reader: &MmdbReader, caption: &str, language: Language, ) -> Row<'a, Message, StyleType> { let is_error = if custom_path.is_empty() { false } else { - match **mmdb_reader { + match *mmdb_reader { MmdbReader::Default(_) => true, MmdbReader::Custom(_) => false, } diff --git a/src/gui/pages/thumbnail_page.rs b/src/gui/pages/thumbnail_page.rs index d4359f29..09752edc 100644 --- a/src/gui/pages/thumbnail_page.rs +++ b/src/gui/pages/thumbnail_page.rs @@ -1,6 +1,6 @@ use std::cmp::min; use std::net::IpAddr; -use std::sync::{Arc, Mutex}; +use std::sync::Mutex; use iced::widget::{lazy, vertical_space, Column, Container, Row, Rule, Space, Text}; use iced::{Alignment, Font, Length}; @@ -61,7 +61,7 @@ pub fn thumbnail_page(sniffer: &Sniffer) -> Container { } fn host_col<'a>( - info_traffic: &Arc>, + info_traffic: &Mutex, chart_type: ChartType, font: Font, ) -> Column<'a, Message, StyleType> { @@ -103,7 +103,7 @@ fn host_col<'a>( } fn service_col<'a>( - info_traffic: &Arc>, + info_traffic: &Mutex, chart_type: ChartType, font: Font, ) -> Column<'a, Message, StyleType> { diff --git a/src/gui/sniffer.rs b/src/gui/sniffer.rs index e4fdfa59..33fb3c9f 100644 --- a/src/gui/sniffer.rs +++ b/src/gui/sniffer.rs @@ -42,7 +42,7 @@ use crate::gui::types::message::Message; use crate::gui::types::timing_events::TimingEvents; use crate::mmdb::asn::ASN_MMDB; use crate::mmdb::country::COUNTRY_MMDB; -use crate::mmdb::types::mmdb_reader::MmdbReader; +use crate::mmdb::types::mmdb_reader::{MmdbReader, MmdbReaders}; use crate::networking::types::capture_context::CaptureContext; use crate::networking::types::filters::Filters; use crate::networking::types::host::Host; @@ -114,10 +114,8 @@ pub struct Sniffer { pub search: SearchParameters, /// Current page number of inspect search results pub page_number: usize, - /// MMDB reader for countries - pub country_mmdb_reader: Arc, - /// MMDB reader for ASN - pub asn_mmdb_reader: Arc, + /// MMDB readers for country and ASN + pub mmdb_readers: MmdbReaders, /// Time-related events pub timing_events: TimingEvents, /// Information about PCAP file export @@ -164,8 +162,10 @@ impl Sniffer { unread_notifications: 0, search: SearchParameters::default(), page_number: 1, - country_mmdb_reader: Arc::new(MmdbReader::from(&mmdb_country, COUNTRY_MMDB)), - asn_mmdb_reader: Arc::new(MmdbReader::from(&mmdb_asn, ASN_MMDB)), + mmdb_readers: MmdbReaders { + country: Arc::new(MmdbReader::from(&mmdb_country, COUNTRY_MMDB)), + asn: Arc::new(MmdbReader::from(&mmdb_asn, ASN_MMDB)), + }, timing_events: TimingEvents::default(), export_pcap: ExportPcap::default(), thumbnail: false, @@ -423,7 +423,7 @@ impl Sniffer { .settings .mmdb_country .clone_from(&db); - self.country_mmdb_reader = Arc::new(MmdbReader::from(&db, COUNTRY_MMDB)); + self.mmdb_readers.country = Arc::new(MmdbReader::from(&db, COUNTRY_MMDB)); } Message::CustomAsnDb(db) => { self.configs @@ -432,7 +432,7 @@ impl Sniffer { .settings .mmdb_asn .clone_from(&db); - self.asn_mmdb_reader = Arc::new(MmdbReader::from(&db, ASN_MMDB)); + self.mmdb_readers.asn = Arc::new(MmdbReader::from(&db, ASN_MMDB)); } Message::QuitWrapper => return self.quit_wrapper(), Message::Quit => { @@ -708,8 +708,7 @@ impl Sniffer { // no pcap error let current_capture_id = self.current_capture_id.clone(); let filters = self.filters.clone(); - let country_mmdb_reader = self.country_mmdb_reader.clone(); - let asn_mmdb_reader = self.asn_mmdb_reader.clone(); + let mmdb_readers = self.mmdb_readers.clone(); let host_data = self.host_data_states.data.clone(); self.device.link_type = capture_context.my_link_type(); thread::Builder::new() @@ -720,8 +719,7 @@ impl Sniffer { &device, &filters, &info_traffic_mutex, - &country_mmdb_reader, - &asn_mmdb_reader, + &mmdb_readers, capture_context, &host_data, ); diff --git a/src/mmdb/types/mmdb_reader.rs b/src/mmdb/types/mmdb_reader.rs index 8ffd295a..c3c6038d 100644 --- a/src/mmdb/types/mmdb_reader.rs +++ b/src/mmdb/types/mmdb_reader.rs @@ -1,8 +1,15 @@ use std::net::IpAddr; +use std::sync::Arc; use maxminddb::{MaxMindDBError, Reader}; use serde::Deserialize; +#[derive(Clone)] +pub struct MmdbReaders { + pub country: Arc, + pub asn: Arc, +} + pub enum MmdbReader { Default(Reader<&'static [u8]>), Custom(Reader>), diff --git a/src/networking/manage_packets.rs b/src/networking/manage_packets.rs index 9a8d5f61..7f4576bd 100644 --- a/src/networking/manage_packets.rs +++ b/src/networking/manage_packets.rs @@ -1,7 +1,7 @@ use std::collections::HashMap; use std::net::{IpAddr, Ipv4Addr, Ipv6Addr}; use std::str::FromStr; -use std::sync::{Arc, Mutex}; +use std::sync::Mutex; use chrono::Local; use dns_lookup::lookup_addr; @@ -10,7 +10,7 @@ use pcap::{Address, Device}; use crate::mmdb::asn::get_asn; use crate::mmdb::country::get_country; -use crate::mmdb::types::mmdb_reader::MmdbReader; +use crate::mmdb::types::mmdb_reader::MmdbReaders; use crate::networking::types::address_port_pair::AddressPortPair; use crate::networking::types::data_info_host::DataInfoHost; use crate::networking::types::host::Host; @@ -211,7 +211,7 @@ pub fn get_service(key: &AddressPortPair, traffic_direction: TrafficDirection) - /// Function to insert the source and destination of a packet into the shared map containing the analyzed traffic. pub fn modify_or_insert_in_map( - info_traffic_mutex: &Arc>, + info_traffic_mutex: &Mutex, key: &AddressPortPair, my_device: &MyDevice, mac_addresses: (Option, Option), @@ -299,13 +299,12 @@ pub fn modify_or_insert_in_map( } pub fn reverse_dns_lookup( - info_traffic: &Arc>, + info_traffic: &Mutex, key: &AddressPortPair, traffic_direction: TrafficDirection, my_device: &MyDevice, - country_db_reader: &Arc, - asn_db_reader: &Arc, - host_data: &Arc>, + mmdb_readers: &MmdbReaders, + host_data: &Mutex, ) { let address_to_lookup = get_address_to_lookup(key, traffic_direction); let my_interface_addresses = my_device.addresses.lock().unwrap().clone(); @@ -321,8 +320,8 @@ pub fn reverse_dns_lookup( ); let is_loopback = is_loopback(&address_to_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 = get_asn(&address_to_lookup, asn_db_reader); + let country = get_country(&address_to_lookup, &mmdb_readers.country); + let asn = get_asn(&address_to_lookup, &mmdb_readers.asn); let r_dns = if let Ok(result) = lookup_result { if result.is_empty() { address_to_lookup.clone() diff --git a/src/notifications/notify_and_log.rs b/src/notifications/notify_and_log.rs index 2c7aff31..03d87219 100644 --- a/src/notifications/notify_and_log.rs +++ b/src/notifications/notify_and_log.rs @@ -1,4 +1,4 @@ -use std::sync::{Arc, Mutex}; +use std::sync::Mutex; use chrono::Local; @@ -15,7 +15,7 @@ use crate::{InfoTraffic, RunTimeData}; pub fn notify_and_log( runtime_data: &mut RunTimeData, notifications: Notifications, - info_traffic: &Arc>, + info_traffic: &Mutex, ) -> usize { let mut already_emitted_sound = false; let mut emitted_notifications = 0; diff --git a/src/report/get_report_entries.rs b/src/report/get_report_entries.rs index 2425bbb4..367a3363 100644 --- a/src/report/get_report_entries.rs +++ b/src/report/get_report_entries.rs @@ -1,5 +1,5 @@ use std::cmp::min; -use std::sync::{Arc, Mutex}; +use std::sync::Mutex; use crate::networking::manage_packets::get_address_to_lookup; use crate::networking::types::address_port_pair::AddressPortPair; @@ -66,7 +66,7 @@ pub fn get_searched_entries( } pub fn get_host_entries( - info_traffic: &Arc>, + info_traffic: &Mutex, chart_type: ChartType, sort_type: SortType, ) -> Vec<(Host, DataInfoHost)> { @@ -83,7 +83,7 @@ pub fn get_host_entries( } pub fn get_service_entries( - info_traffic: &Arc>, + info_traffic: &Mutex, chart_type: ChartType, sort_type: SortType, ) -> Vec<(Service, DataInfo)> { diff --git a/src/secondary_threads/check_updates.rs b/src/secondary_threads/check_updates.rs index ec84bd75..6de1b780 100644 --- a/src/secondary_threads/check_updates.rs +++ b/src/secondary_threads/check_updates.rs @@ -1,4 +1,4 @@ -use std::sync::{Arc, Mutex}; +use std::sync::{ Mutex}; use std::thread; use std::time::Duration; @@ -14,7 +14,7 @@ struct AppVersion { /// Calls a method to check if a newer release of Sniffnet is available on GitHub /// and updates application status accordingly -pub fn set_newer_release_status(newer_release_available: &Arc>>) { +pub fn set_newer_release_status(newer_release_available: &Mutex>) { let result = is_newer_release_available(6, 30); *newer_release_available.lock().unwrap() = result; } diff --git a/src/secondary_threads/parse_packets.rs b/src/secondary_threads/parse_packets.rs index a5481e82..fdc1a67d 100644 --- a/src/secondary_threads/parse_packets.rs +++ b/src/secondary_threads/parse_packets.rs @@ -9,7 +9,7 @@ use etherparse::err::{Layer, LenError}; use etherparse::{LaxPacketHeaders, LenSource}; use pcap::Packet; -use crate::mmdb::types::mmdb_reader::MmdbReader; +use crate::mmdb::types::mmdb_reader::MmdbReaders; use crate::networking::manage_packets::{ analyze_headers, get_address_to_lookup, modify_or_insert_in_map, reverse_dns_lookup, }; @@ -27,12 +27,11 @@ use crate::InfoTraffic; /// The calling thread enters a loop in which it waits for network packets, parses them according /// to the user specified filters, and inserts them into the shared map variable. pub fn parse_packets( - current_capture_id: &Arc>, + current_capture_id: &Mutex, device: &MyDevice, filters: &Filters, info_traffic_mutex: &Arc>, - country_mmdb_reader: &Arc, - asn_mmdb_reader: &Arc, + mmdb_readers: &MmdbReaders, capture_context: CaptureContext, host_data: &Arc>, ) { @@ -135,8 +134,7 @@ pub fn parse_packets( let key2 = key.clone(); let info_traffic2 = info_traffic_mutex.clone(); let device2 = device.clone(); - let country_db_reader_2 = country_mmdb_reader.clone(); - let asn_db_reader_2 = asn_mmdb_reader.clone(); + let mmdb_readers_2 = mmdb_readers.clone(); let host_data2 = host_data.clone(); thread::Builder::new() .name("thread_reverse_dns_lookup".to_string()) @@ -146,8 +144,7 @@ pub fn parse_packets( &key2, new_info.traffic_direction, &device2, - &country_db_reader_2, - &asn_db_reader_2, + &mmdb_readers_2, &host_data2, ); })