Skip to content

Commit

Permalink
create struct MmdbReaders and other minor improvements
Browse files Browse the repository at this point in the history
  • Loading branch information
GyulyVGC committed Dec 31, 2024
1 parent bdaf08c commit bcff939
Show file tree
Hide file tree
Showing 11 changed files with 52 additions and 55 deletions.
6 changes: 3 additions & 3 deletions src/gui/components/footer.rs
Original file line number Diff line number Diff line change
@@ -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;
Expand Down Expand Up @@ -28,7 +28,7 @@ pub fn footer<'a>(
color_gradient: GradientType,
font: Font,
font_footer: Font,
newer_release_available: &Arc<Mutex<Option<bool>>>,
newer_release_available: &Mutex<Option<bool>>,
) -> Container<'a, Message, StyleType> {
if thumbnail {
return thumbnail_footer();
Expand Down Expand Up @@ -136,7 +136,7 @@ fn get_release_details<'a>(
language: Language,
font: Font,
font_footer: Font,
newer_release_available: &Arc<Mutex<Option<bool>>>,
newer_release_available: &Mutex<Option<bool>>,
) -> Row<'a, Message, StyleType> {
let mut ret_val = Row::new()
.align_y(Alignment::Center)
Expand Down
2 changes: 1 addition & 1 deletion src/gui/pages/inspect_page.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
18 changes: 7 additions & 11 deletions src/gui/pages/settings_general_page.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
use std::sync::Arc;

use iced::widget::text::LineHeight;
use iced::widget::tooltip::Position;
use iced::widget::{
Expand All @@ -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::{
Expand Down Expand Up @@ -91,8 +89,7 @@ fn column_all_general_setting(sniffer: &Sniffer, font: Font) -> Column<Message,
font,
&mmdb_country,
&mmdb_asn,
&sniffer.country_mmdb_reader,
&sniffer.asn_mmdb_reader,
&sniffer.mmdb_readers,
));

column
Expand Down Expand Up @@ -249,8 +246,7 @@ fn mmdb_settings<'a>(
font: Font,
country_path: &str,
asn_path: &str,
country_reader: &Arc<MmdbReader>,
asn_reader: &Arc<MmdbReader>,
mmdb_readers: &MmdbReaders,
) -> Column<'a, Message, StyleType> {
Column::new()
.spacing(5)
Expand All @@ -266,7 +262,7 @@ fn mmdb_settings<'a>(
font,
Message::CustomCountryDb,
country_path,
country_reader,
&mmdb_readers.country,
country_translation(language),
language,
))
Expand All @@ -275,7 +271,7 @@ fn mmdb_settings<'a>(
font,
Message::CustomAsnDb,
asn_path,
asn_reader,
&mmdb_readers.asn,
"ASN",
language,
))
Expand All @@ -286,14 +282,14 @@ fn mmdb_selection_row<'a>(
font: Font,
message: fn(String) -> Message,
custom_path: &str,
mmdb_reader: &Arc<MmdbReader>,
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,
}
Expand Down
6 changes: 3 additions & 3 deletions src/gui/pages/thumbnail_page.rs
Original file line number Diff line number Diff line change
@@ -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};
Expand Down Expand Up @@ -61,7 +61,7 @@ pub fn thumbnail_page(sniffer: &Sniffer) -> Container<Message, StyleType> {
}

fn host_col<'a>(
info_traffic: &Arc<Mutex<InfoTraffic>>,
info_traffic: &Mutex<InfoTraffic>,
chart_type: ChartType,
font: Font,
) -> Column<'a, Message, StyleType> {
Expand Down Expand Up @@ -103,7 +103,7 @@ fn host_col<'a>(
}

fn service_col<'a>(
info_traffic: &Arc<Mutex<InfoTraffic>>,
info_traffic: &Mutex<InfoTraffic>,
chart_type: ChartType,
font: Font,
) -> Column<'a, Message, StyleType> {
Expand Down
24 changes: 11 additions & 13 deletions src/gui/sniffer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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<MmdbReader>,
/// MMDB reader for ASN
pub asn_mmdb_reader: Arc<MmdbReader>,
/// MMDB readers for country and ASN
pub mmdb_readers: MmdbReaders,
/// Time-related events
pub timing_events: TimingEvents,
/// Information about PCAP file export
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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
Expand All @@ -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 => {
Expand Down Expand Up @@ -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()
Expand All @@ -720,8 +719,7 @@ impl Sniffer {
&device,
&filters,
&info_traffic_mutex,
&country_mmdb_reader,
&asn_mmdb_reader,
&mmdb_readers,
capture_context,
&host_data,
);
Expand Down
7 changes: 7 additions & 0 deletions src/mmdb/types/mmdb_reader.rs
Original file line number Diff line number Diff line change
@@ -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<MmdbReader>,
pub asn: Arc<MmdbReader>,
}

pub enum MmdbReader {
Default(Reader<&'static [u8]>),
Custom(Reader<Vec<u8>>),
Expand Down
17 changes: 8 additions & 9 deletions src/networking/manage_packets.rs
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -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;
Expand Down Expand Up @@ -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<Mutex<InfoTraffic>>,
info_traffic_mutex: &Mutex<InfoTraffic>,
key: &AddressPortPair,
my_device: &MyDevice,
mac_addresses: (Option<String>, Option<String>),
Expand Down Expand Up @@ -299,13 +299,12 @@ pub fn modify_or_insert_in_map(
}

pub fn reverse_dns_lookup(
info_traffic: &Arc<Mutex<InfoTraffic>>,
info_traffic: &Mutex<InfoTraffic>,
key: &AddressPortPair,
traffic_direction: TrafficDirection,
my_device: &MyDevice,
country_db_reader: &Arc<MmdbReader>,
asn_db_reader: &Arc<MmdbReader>,
host_data: &Arc<Mutex<HostData>>,
mmdb_readers: &MmdbReaders,
host_data: &Mutex<HostData>,
) {
let address_to_lookup = get_address_to_lookup(key, traffic_direction);
let my_interface_addresses = my_device.addresses.lock().unwrap().clone();
Expand All @@ -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()
Expand Down
4 changes: 2 additions & 2 deletions src/notifications/notify_and_log.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use std::sync::{Arc, Mutex};
use std::sync::Mutex;

use chrono::Local;

Expand All @@ -15,7 +15,7 @@ use crate::{InfoTraffic, RunTimeData};
pub fn notify_and_log(
runtime_data: &mut RunTimeData,
notifications: Notifications,
info_traffic: &Arc<Mutex<InfoTraffic>>,
info_traffic: &Mutex<InfoTraffic>,
) -> usize {
let mut already_emitted_sound = false;
let mut emitted_notifications = 0;
Expand Down
6 changes: 3 additions & 3 deletions src/report/get_report_entries.rs
Original file line number Diff line number Diff line change
@@ -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;
Expand Down Expand Up @@ -66,7 +66,7 @@ pub fn get_searched_entries(
}

pub fn get_host_entries(
info_traffic: &Arc<Mutex<InfoTraffic>>,
info_traffic: &Mutex<InfoTraffic>,
chart_type: ChartType,
sort_type: SortType,
) -> Vec<(Host, DataInfoHost)> {
Expand All @@ -83,7 +83,7 @@ pub fn get_host_entries(
}

pub fn get_service_entries(
info_traffic: &Arc<Mutex<InfoTraffic>>,
info_traffic: &Mutex<InfoTraffic>,
chart_type: ChartType,
sort_type: SortType,
) -> Vec<(Service, DataInfo)> {
Expand Down
4 changes: 2 additions & 2 deletions src/secondary_threads/check_updates.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use std::sync::{Arc, Mutex};
use std::sync::{ Mutex};
use std::thread;
use std::time::Duration;

Expand All @@ -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<Mutex<Option<bool>>>) {
pub fn set_newer_release_status(newer_release_available: &Mutex<Option<bool>>) {
let result = is_newer_release_available(6, 30);
*newer_release_available.lock().unwrap() = result;
}
Expand Down
13 changes: 5 additions & 8 deletions src/secondary_threads/parse_packets.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
};
Expand All @@ -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<Mutex<usize>>,
current_capture_id: &Mutex<usize>,
device: &MyDevice,
filters: &Filters,
info_traffic_mutex: &Arc<Mutex<InfoTraffic>>,
country_mmdb_reader: &Arc<MmdbReader>,
asn_mmdb_reader: &Arc<MmdbReader>,
mmdb_readers: &MmdbReaders,
capture_context: CaptureContext,
host_data: &Arc<Mutex<HostData>>,
) {
Expand Down Expand Up @@ -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())
Expand All @@ -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,
);
})
Expand Down

0 comments on commit bcff939

Please sign in to comment.