From 10ddbaa38f37644bf0bbf1cd8526cc8993fec450 Mon Sep 17 00:00:00 2001 From: GyulyVGC Date: Sat, 21 Feb 2026 13:39:37 +0100 Subject: [PATCH] create struct Program and include program in connection details page --- src/gui/pages/connection_details_page.rs | 14 +++- src/networking/manage_packets.rs | 3 +- .../types/info_address_port_pair.rs | 4 +- src/networking/types/info_traffic.rs | 11 +-- src/networking/types/mod.rs | 1 + src/networking/types/program.rs | 82 +++++++++++++++++++ src/report/types/search_parameters.rs | 7 +- 7 files changed, 105 insertions(+), 17 deletions(-) create mode 100644 src/networking/types/program.rs diff --git a/src/gui/pages/connection_details_page.rs b/src/gui/pages/connection_details_page.rs index 4428a07c..2df6cebf 100644 --- a/src/gui/pages/connection_details_page.rs +++ b/src/gui/pages/connection_details_page.rs @@ -34,6 +34,7 @@ use crate::translations::translations_2::{ use crate::translations::translations_3::{ copy_translation, messages_translation, service_translation, }; +use crate::translations::translations_5::program_translation; use crate::utils::formatted_strings::{get_formatted_timestamp, get_socket_address}; use crate::utils::types::icon::Icon; use crate::{Language, Protocol, Sniffer, StyleType}; @@ -191,10 +192,15 @@ fn col_info<'a>( )); if !is_icmp && !is_arp { - ret_val = ret_val.push(TextType::highlighted_subtitle_with_desc( - service_translation(language), - &val.service.to_string(), - )); + ret_val = ret_val + .push(TextType::highlighted_subtitle_with_desc( + service_translation(language), + &val.service.to_string(), + )) + .push(TextType::highlighted_subtitle_with_desc( + program_translation(language), + &val.program.to_string(), + )); } ret_val = ret_val.push(TextType::highlighted_subtitle_with_desc( diff --git a/src/networking/manage_packets.rs b/src/networking/manage_packets.rs index dff36bbf..59f3bd7c 100644 --- a/src/networking/manage_packets.rs +++ b/src/networking/manage_packets.rs @@ -15,6 +15,7 @@ use crate::networking::types::icmp_type::{IcmpType, IcmpTypeV4, IcmpTypeV6}; use crate::networking::types::info_address_port_pair::InfoAddressPortPair; use crate::networking::types::info_traffic::InfoTraffic; use crate::networking::types::ip_blacklist::IpBlacklist; +use crate::networking::types::program::Program; use crate::networking::types::service::Service; use crate::networking::types::service_query::ServiceQuery; use crate::networking::types::traffic_direction::TrafficDirection; @@ -331,7 +332,7 @@ pub fn modify_or_insert_in_map( HashMap::new() }, is_blacklisted, - program: None, + program: Program::NotApplicable, }); (new_info.traffic_direction, new_info.service) diff --git a/src/networking/types/info_address_port_pair.rs b/src/networking/types/info_address_port_pair.rs index 2cebbd04..47eed547 100644 --- a/src/networking/types/info_address_port_pair.rs +++ b/src/networking/types/info_address_port_pair.rs @@ -6,10 +6,10 @@ use crate::networking::types::arp_type::ArpType; use crate::networking::types::data_info::DataInfo; use crate::networking::types::data_representation::DataRepr; use crate::networking::types::icmp_type::IcmpType; +use crate::networking::types::program::Program; use crate::networking::types::traffic_direction::TrafficDirection; use crate::report::types::sort_type::SortType; use crate::utils::types::timestamp::Timestamp; -use listeners::Process; use std::cmp::Ordering; use std::collections::HashMap; @@ -41,7 +41,7 @@ pub struct InfoAddressPortPair { /// Whether the remote address is blacklisted pub is_blacklisted: bool, /// The program associated to this pair - pub program: Option, + pub program: Program, } impl InfoAddressPortPair { diff --git a/src/networking/types/info_traffic.rs b/src/networking/types/info_traffic.rs index 3fa27fab..95ab53b7 100644 --- a/src/networking/types/info_traffic.rs +++ b/src/networking/types/info_traffic.rs @@ -6,6 +6,7 @@ use crate::networking::types::data_info_host::DataInfoHost; use crate::networking::types::data_representation::DataRepr; use crate::networking::types::host::Host; use crate::networking::types::info_address_port_pair::InfoAddressPortPair; +use crate::networking::types::program::Program; use crate::networking::types::program_lookup::ProgramLookup; use crate::utils::types::timestamp::Timestamp; use std::collections::HashMap; @@ -47,10 +48,10 @@ impl InfoTraffic { Entry::Occupied(mut o) => { if let Some(program_lookup) = program_lookup_opt && let Some(local_port) = local_port - && o.get().program.is_none() + && !o.get().program.is_known() { - let program = program_lookup.lookup(local_port, false); - o.get_mut().program = program; + let proc = program_lookup.lookup(local_port, false); + o.get_mut().program = Program::from_proc(proc); } o.get_mut().refresh(value); @@ -61,8 +62,8 @@ impl InfoTraffic { if let Some(program_lookup) = program_lookup_opt && let Some(local_port) = local_port { - let program = program_lookup.lookup(local_port, true); - new_value.program = program; + let proc = program_lookup.lookup(local_port, true); + new_value.program = Program::from_proc(proc); } v.insert(new_value); diff --git a/src/networking/types/mod.rs b/src/networking/types/mod.rs index 694bf652..0a7b1b42 100644 --- a/src/networking/types/mod.rs +++ b/src/networking/types/mod.rs @@ -16,6 +16,7 @@ pub mod ip_blacklist; pub mod ip_collection; pub mod my_device; pub mod my_link_type; +pub mod program; pub mod program_lookup; pub mod protocol; pub mod service; diff --git a/src/networking/types/program.rs b/src/networking/types/program.rs new file mode 100644 index 00000000..f651ed3c --- /dev/null +++ b/src/networking/types/program.rs @@ -0,0 +1,82 @@ +use listeners::Process; + +/// Program / App. +#[derive(Debug, Clone, PartialEq, Eq, Hash, Default)] +pub enum Program { + /// A known program. + Name(String), + /// Not identified + Unknown, + /// Not applicable (ARP and ICMP) + #[default] + NotApplicable, +} + +impl Program { + pub fn to_string_with_equal_prefix(&self) -> String { + match self { + Program::Name(_) | Program::NotApplicable => ["=", &self.to_string()].concat(), + Program::Unknown => self.to_string(), + } + } + + pub fn is_known(&self) -> bool { + matches!(self, Program::Name(_)) + } + + pub fn from_proc(proc: Option) -> Self { + proc.map_or(Program::Unknown, |proc| Program::Name(proc.name)) + } +} + +impl std::fmt::Display for Program { + fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result { + match self { + Program::Name(name) => write!(f, "{name}"), + Program::Unknown => write!(f, "?"), + Program::NotApplicable => write!(f, "-"), + } + } +} + +#[cfg(test)] +mod tests { + use super::*; + + #[test] + fn test_program_display_unknown() { + assert_eq!(Program::Unknown.to_string(), "?"); + } + + #[test] + fn test_program_display_not_applicable() { + assert_eq!(Program::NotApplicable.to_string(), "-"); + } + + #[test] + fn test_program_display_known() { + assert_eq!( + Program::Name("Telegram".to_string()).to_string(), + "Telegram" + ); + assert_eq!( + Program::Name("Google Chrome Helper".to_string()).to_string(), + "Google Chrome Helper" + ); + } + + #[test] + fn test_program_to_string_with_equal_prefix() { + assert_eq!( + Program::Name("Telegram".to_string()).to_string_with_equal_prefix(), + "=Telegram" + ); + assert_eq!( + Program::Name("Google Chrome Helper".to_string()).to_string_with_equal_prefix(), + "=Google Chrome Helper" + ); + assert_eq!(Program::NotApplicable.to_string_with_equal_prefix(), "=-"); + // unknown should not have the prefix + assert_eq!(Program::Unknown.to_string_with_equal_prefix(), "?"); + } +} diff --git a/src/report/types/search_parameters.rs b/src/report/types/search_parameters.rs index 4bf6d50c..59fc0c1c 100644 --- a/src/report/types/search_parameters.rs +++ b/src/report/types/search_parameters.rs @@ -199,10 +199,7 @@ impl FilterInputType { .asn .name .clone(), - FilterInputType::Program => value - .program - .as_ref() - .map_or(String::new(), |p| p.name.clone()), + FilterInputType::Program => value.program.to_string(), } } @@ -294,7 +291,7 @@ impl FilterInputType { ..search_params.clone() }, FilterInputType::Program => SearchParameters { - program: new_value, + program: new_value.trim().to_string(), ..search_params.clone() }, }