Skip to content

Commit

Permalink
implement dropdowns for host filters
Browse files Browse the repository at this point in the history
  • Loading branch information
GyulyVGC committed Dec 28, 2024
1 parent c3e1392 commit e4eeb25
Show file tree
Hide file tree
Showing 6 changed files with 118 additions and 11 deletions.
27 changes: 20 additions & 7 deletions src/gui/pages/inspect_page.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ use crate::gui::styles::text::TextType;
use crate::gui::styles::text_input::TextInputType;
use crate::gui::types::message::Message;
use crate::networking::types::address_port_pair::AddressPortPair;
use crate::networking::types::host_data_states::HostStates;
use crate::networking::types::info_address_port_pair::InfoAddressPortPair;
use crate::networking::types::traffic_direction::TrafficDirection;
use crate::report::get_report_entries::get_searched_entries;
Expand Down Expand Up @@ -90,7 +91,7 @@ pub fn inspect_page(sniffer: &Sniffer) -> Container<Message, StyleType> {
.push(
Container::new(host_filters_col(
&sniffer.search,
&sniffer.combobox_states,
&sniffer.host_data_states.states,
font,
language,
))
Expand Down Expand Up @@ -315,7 +316,7 @@ fn row_report_entry<'a>(

fn host_filters_col<'a>(
search_params: &'a SearchParameters,
combobox_states: &'a combo_box::State<String>,
host_states: &'a HostStates,
font: Font,
language: Language,
) -> Column<'a, Message, StyleType> {
Expand All @@ -336,15 +337,27 @@ fn host_filters_col<'a>(

let combobox_country = filter_combobox(
FilterInputType::Country,
combobox_states,
&host_states.countries,
search_params.clone(),
font,
)
.width(95);
let combobox_domain =
filter_input(FilterInputType::Domain, search_params.clone(), font).width(190);
let combobox_as_name =
filter_input(FilterInputType::AsName, search_params.clone(), font).width(190);

let combobox_domain = filter_combobox(
FilterInputType::Domain,
&host_states.domains,
search_params.clone(),
font,
)
.width(190);

let combobox_as_name = filter_combobox(
FilterInputType::AsName,
&host_states.asns,
search_params.clone(),
font,
)
.width(190);

let container_country = Row::new()
.spacing(5)
Expand Down
14 changes: 10 additions & 4 deletions src/gui/sniffer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ use std::time::Duration;
use iced::keyboard::key::Named;
use iced::keyboard::{Event, Key, Modifiers};
use iced::mouse::Event::ButtonPressed;
use iced::widget::{combo_box, Column};
use iced::widget::Column;
use iced::window::{Id, Level};
use iced::Event::{Keyboard, Window};
use iced::{window, Element, Point, Size, Subscription, Task};
Expand Down Expand Up @@ -46,6 +46,7 @@ use crate::mmdb::types::mmdb_reader::MmdbReader;
use crate::networking::types::capture_context::CaptureContext;
use crate::networking::types::filters::Filters;
use crate::networking::types::host::Host;
use crate::networking::types::host_data_states::HostDataStates;
use crate::networking::types::ip_collection::AddressCollection;
use crate::networking::types::my_device::MyDevice;
use crate::networking::types::my_link_type::MyLinkType;
Expand Down Expand Up @@ -125,8 +126,8 @@ pub struct Sniffer {
pub thumbnail: bool,
/// Window id
pub id: Option<Id>,
/// Combobox states for host filter dropdowns
pub combobox_states: combo_box::State<String>,
/// Host data for filter dropdowns (comboboxes)
pub host_data_states: HostDataStates,
}

impl Sniffer {
Expand Down Expand Up @@ -169,7 +170,7 @@ impl Sniffer {
export_pcap: ExportPcap::default(),
thumbnail: false,
id: None,
combobox_states: combo_box::State::new(vec!["IT".to_string(), "US".to_string()]),
host_data_states: HostDataStates::default(),
}
}

Expand Down Expand Up @@ -664,6 +665,8 @@ impl Sniffer {
{
return self.update(Message::Waiting);
}
// update host dropdowns
self.host_data_states.update_states(&self.search);
Task::none()
}

Expand Down Expand Up @@ -707,6 +710,7 @@ impl Sniffer {
let filters = self.filters.clone();
let country_mmdb_reader = self.country_mmdb_reader.clone();
let asn_mmdb_reader = self.asn_mmdb_reader.clone();
let host_data = self.host_data_states.data.clone();
self.device.link_type = capture_context.my_link_type();
thread::Builder::new()
.name("thread_parse_packets".to_string())
Expand All @@ -719,6 +723,7 @@ impl Sniffer {
&country_mmdb_reader,
&asn_mmdb_reader,
capture_context,
&host_data,
);
})
.unwrap();
Expand All @@ -733,6 +738,7 @@ impl Sniffer {
self.unread_notifications = 0;
self.search = SearchParameters::default();
self.page_number = 1;
self.host_data_states = HostDataStates::default();
self.update(Message::HideModal)
}

Expand Down
6 changes: 6 additions & 0 deletions src/networking/manage_packets.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ use crate::mmdb::types::mmdb_reader::MmdbReader;
use crate::networking::types::address_port_pair::AddressPortPair;
use crate::networking::types::data_info_host::DataInfoHost;
use crate::networking::types::host::Host;
use crate::networking::types::host_data_states::HostData;
use crate::networking::types::icmp_type::{IcmpType, IcmpTypeV4, IcmpTypeV6};
use crate::networking::types::info_address_port_pair::InfoAddressPortPair;
use crate::networking::types::my_device::MyDevice;
Expand Down Expand Up @@ -304,6 +305,7 @@ pub fn reverse_dns_lookup(
my_device: &MyDevice,
country_db_reader: &Arc<MmdbReader>,
asn_db_reader: &Arc<MmdbReader>,
host_data: &Arc<Mutex<HostData>>,
) {
let address_to_lookup = get_address_to_lookup(key, traffic_direction);
let my_interface_addresses = my_device.addresses.lock().unwrap().clone();
Expand Down Expand Up @@ -359,6 +361,10 @@ pub fn reverse_dns_lookup(
is_local,
traffic_type,
});

// update host data states including the new host
host_data.lock().unwrap().update(&new_host);

// check if the newly resolved host was featured in the favorites (possible in case of already existing host)
if info_traffic_lock.favorite_hosts.contains(&new_host) {
info_traffic_lock.favorites_last_interval.insert(new_host);
Expand Down
77 changes: 77 additions & 0 deletions src/networking/types/host_data_states.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
use crate::countries::types::country::Country;
use crate::networking::types::host::Host;
use crate::report::types::search_parameters::SearchParameters;
use iced::widget::combo_box;
use std::collections::BTreeSet;
use std::net::IpAddr;
use std::sync::{Arc, Mutex};

/// Struct to contain all the sets of data related to network hosts
///
/// It also stores combobox states for the host-related filters
#[derive(Default)]
pub struct HostDataStates {
pub data: Arc<Mutex<HostData>>,
pub states: HostStates,
}

impl HostDataStates {
pub fn update_states(&mut self, search: &SearchParameters) {
let states = &mut self.states;
let mut data = self.data.lock().unwrap();

if data.domains.1 {
states.domains = combo_box::State::with_selection(
data.domains.0.iter().cloned().collect(),
Some(&search.domain),
);
data.domains.1 = false;
}

if data.asns.1 {
states.asns = combo_box::State::with_selection(
data.asns.0.iter().cloned().collect(),
Some(&search.as_name),
);
data.asns.1 = false;
}

if data.countries.1 {
states.countries = combo_box::State::with_selection(
data.countries.0.iter().cloned().collect(),
Some(&search.country),
);
data.countries.1 = false;
}
}
}

#[derive(Default)]
pub struct HostData {
pub domains: (BTreeSet<String>, bool),
pub asns: (BTreeSet<String>, bool),
pub countries: (BTreeSet<String>, bool),
}

impl HostData {
pub fn update(&mut self, host: &Host) {
if !host.domain.is_empty() && host.domain.parse::<IpAddr>().is_err() {
self.domains.1 = self.domains.0.insert(host.domain.clone());
}

if !host.asn.name.is_empty() {
self.asns.1 = self.asns.0.insert(host.asn.name.clone());
}

if host.country != Country::ZZ {
self.countries.1 = self.countries.0.insert(host.country.to_string());
}
}
}

#[derive(Default)]
pub struct HostStates {
pub domains: combo_box::State<String>,
pub asns: combo_box::State<String>,
pub countries: combo_box::State<String>,
}
1 change: 1 addition & 0 deletions src/networking/types/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ pub mod data_info;
pub mod data_info_host;
pub mod filters;
pub mod host;
pub mod host_data_states;
pub mod icmp_type;
pub mod info_address_port_pair;
pub mod info_traffic;
Expand Down
4 changes: 4 additions & 0 deletions src/secondary_threads/parse_packets.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ use crate::networking::manage_packets::{
use crate::networking::types::capture_context::CaptureContext;
use crate::networking::types::data_info::DataInfo;
use crate::networking::types::filters::Filters;
use crate::networking::types::host_data_states::HostData;
use crate::networking::types::icmp_type::IcmpType;
use crate::networking::types::info_address_port_pair::InfoAddressPortPair;
use crate::networking::types::my_device::MyDevice;
Expand All @@ -33,6 +34,7 @@ pub fn parse_packets(
country_mmdb_reader: &Arc<MmdbReader>,
asn_mmdb_reader: &Arc<MmdbReader>,
capture_context: CaptureContext,
host_data: &Arc<Mutex<HostData>>,
) {
let my_link_type = capture_context.my_link_type();
let (mut cap, mut savefile) = capture_context.consume();
Expand Down Expand Up @@ -135,6 +137,7 @@ pub fn parse_packets(
let device2 = device.clone();
let country_db_reader_2 = country_mmdb_reader.clone();
let asn_db_reader_2 = asn_mmdb_reader.clone();
let host_data2 = host_data.clone();
thread::Builder::new()
.name("thread_reverse_dns_lookup".to_string())
.spawn(move || {
Expand All @@ -145,6 +148,7 @@ pub fn parse_packets(
&device2,
&country_db_reader_2,
&asn_db_reader_2,
&host_data2,
);
})
.unwrap();
Expand Down

0 comments on commit e4eeb25

Please sign in to comment.