Skip to content

Commit e3377a0

Browse files
committed
favorite notifications: show the amount of exchanged data
1 parent d838ab4 commit e3377a0

10 files changed

Lines changed: 101 additions & 106 deletions

File tree

src/gui/pages/notifications_page.rs

Lines changed: 15 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,11 @@ use iced::widget::{Space, button, vertical_space};
77
use iced::{Alignment, Font, Length};
88
use std::fmt::Write;
99

10-
use crate::countries::country_utils::get_flag_tooltip;
10+
use crate::chart::types::chart_type::ChartType;
1111
use crate::gui::components::header::get_button_settings;
1212
use crate::gui::components::tab::get_pages_tabs;
1313
use crate::gui::components::types::my_modal::MyModal;
14+
use crate::gui::pages::overview_page::host_bar;
1415
use crate::gui::pages::types::settings_page::SettingsPage;
1516
use crate::gui::styles::container::ContainerType;
1617
use crate::gui::styles::scrollbar::ScrollbarType;
@@ -289,28 +290,18 @@ fn bytes_notification_log<'a>(
289290

290291
fn favorite_notification_log<'a>(
291292
logged_notification: FavoriteTransmitted,
293+
chart_type: ChartType,
292294
language: Language,
293295
font: Font,
294296
) -> Container<'a, Message, StyleType> {
295-
let country = logged_notification.host.country;
296-
let asn = &logged_notification.host.asn;
297-
298-
let mut domain_asn_str = logged_notification.host.domain;
299-
if !asn.name.is_empty() {
300-
let _ = write!(domain_asn_str, " - {}", asn.name);
301-
}
302-
303-
let row_flag_details = Row::new()
304-
.align_y(Alignment::Center)
305-
.spacing(5)
306-
.push(get_flag_tooltip(
307-
country,
308-
&logged_notification.data_info_host,
309-
language,
310-
font,
311-
false,
312-
))
313-
.push(Text::new(domain_asn_str).font(font));
297+
let host_bar = host_bar(
298+
&logged_notification.host,
299+
&logged_notification.data_info_host,
300+
chart_type,
301+
logged_notification.data_info_host.data_info,
302+
font,
303+
language,
304+
);
314305

315306
let content = Row::new()
316307
.spacing(30)
@@ -339,12 +330,8 @@ fn favorite_notification_log<'a>(
339330
.font(font),
340331
),
341332
)
342-
.push(
343-
Column::new()
344-
.spacing(7)
345-
.width(Length::Fill)
346-
.push(row_flag_details),
347-
);
333+
.push(Column::new().spacing(7).width(Length::Fill).push(host_bar));
334+
348335
Container::new(content)
349336
.height(120)
350337
.width(800)
@@ -378,6 +365,7 @@ fn logged_notifications<'a>(sniffer: &Sniffer) -> Column<'a, Message, StyleType>
378365
let ConfigSettings {
379366
style, language, ..
380367
} = sniffer.configs.settings;
368+
let chart_type = sniffer.traffic_chart.chart_type;
381369
let font = style.get_extension().font;
382370
let mut ret_val = Column::new()
383371
.width(830)
@@ -394,7 +382,7 @@ fn logged_notifications<'a>(sniffer: &Sniffer) -> Column<'a, Message, StyleType>
394382
bytes_notification_log(byte_threshold_exceeded.clone(), language, font)
395383
}
396384
LoggedNotification::FavoriteTransmitted(favorite_transmitted) => {
397-
favorite_notification_log(favorite_transmitted.clone(), language, font)
385+
favorite_notification_log(favorite_transmitted.clone(), chart_type, language, font)
398386
}
399387
});
400388
}

src/gui/pages/overview_page.rs

Lines changed: 61 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ use crate::gui::styles::types::palette_extension::PaletteExtension;
1818
use crate::gui::types::message::Message;
1919
use crate::networking::types::capture_context::CaptureSource;
2020
use crate::networking::types::data_info::DataInfo;
21+
use crate::networking::types::data_info_host::DataInfoHost;
2122
use crate::networking::types::filters::Filters;
2223
use crate::networking::types::host::Host;
2324
use crate::report::get_report_entries::{get_host_entries, get_service_entries};
@@ -256,50 +257,21 @@ fn col_host<'a>(sniffer: &Sniffer) -> Column<'a, Message, StyleType> {
256257
.unwrap_or_default();
257258

258259
for (host, data_info_host) in &entries {
259-
let (incoming_bar_len, outgoing_bar_len) = get_bars_length(
260-
chart_type,
261-
&first_entry_data_info,
262-
&data_info_host.data_info,
263-
);
264-
265260
let star_button = get_star_button(data_info_host.is_favorite, host.clone());
266261

267-
let host_bar = Column::new()
268-
.spacing(1)
269-
.push(
270-
Row::new()
271-
.push(Text::new(host.domain.clone()).font(font))
272-
.push(
273-
Text::new(if host.asn.name.is_empty() {
274-
String::new()
275-
} else {
276-
format!(" - {}", host.asn.name)
277-
})
278-
.font(font),
279-
)
280-
.push(horizontal_space())
281-
.push(
282-
Text::new(if chart_type.eq(&ChartType::Packets) {
283-
data_info_host.data_info.tot_packets().to_string()
284-
} else {
285-
ByteMultiple::formatted_string(data_info_host.data_info.tot_bytes())
286-
})
287-
.font(font),
288-
),
289-
)
290-
.push(get_bars(incoming_bar_len, outgoing_bar_len));
262+
let host_bar = host_bar(
263+
host,
264+
data_info_host,
265+
chart_type,
266+
first_entry_data_info,
267+
font,
268+
language,
269+
);
291270

292271
let content = Row::new()
293272
.align_y(Alignment::Center)
294273
.spacing(5)
295274
.push(star_button)
296-
.push(get_flag_tooltip(
297-
host.country,
298-
data_info_host,
299-
language,
300-
font,
301-
false,
302-
))
303275
.push(host_bar);
304276

305277
scroll_host = scroll_host.push(
@@ -426,6 +398,58 @@ fn col_service<'a>(sniffer: &Sniffer) -> Column<'a, Message, StyleType> {
426398
)
427399
}
428400

401+
pub fn host_bar<'a>(
402+
host: &Host,
403+
data_info_host: &DataInfoHost,
404+
chart_type: ChartType,
405+
first_entry_data_info: DataInfo,
406+
font: Font,
407+
language: Language,
408+
) -> Row<'a, Message, StyleType> {
409+
let (incoming_bar_len, outgoing_bar_len) = get_bars_length(
410+
chart_type,
411+
&first_entry_data_info,
412+
&data_info_host.data_info,
413+
);
414+
415+
Row::new()
416+
.align_y(Alignment::Center)
417+
.spacing(5)
418+
.push(get_flag_tooltip(
419+
host.country,
420+
data_info_host,
421+
language,
422+
font,
423+
false,
424+
))
425+
.push(
426+
Column::new()
427+
.spacing(1)
428+
.push(
429+
Row::new()
430+
.push(Text::new(host.domain.clone()).font(font))
431+
.push(
432+
Text::new(if host.asn.name.is_empty() {
433+
String::new()
434+
} else {
435+
format!(" - {}", host.asn.name)
436+
})
437+
.font(font),
438+
)
439+
.push(horizontal_space())
440+
.push(
441+
Text::new(if chart_type.eq(&ChartType::Packets) {
442+
data_info_host.data_info.tot_packets().to_string()
443+
} else {
444+
ByteMultiple::formatted_string(data_info_host.data_info.tot_bytes())
445+
})
446+
.font(font),
447+
),
448+
)
449+
.push(get_bars(incoming_bar_len, outgoing_bar_len)),
450+
)
451+
}
452+
429453
fn col_info<'a>(sniffer: &Sniffer) -> Container<'a, Message, StyleType> {
430454
let ConfigSettings {
431455
style, language, ..

src/gui/sniffer.rs

Lines changed: 13 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1109,6 +1109,15 @@ impl Sniffer {
11091109
rdns,
11101110
} = host_msg;
11111111

1112+
let data_info_host = DataInfoHost {
1113+
data_info: other_data,
1114+
is_favorite: false,
1115+
is_loopback,
1116+
is_local,
1117+
is_bogon,
1118+
traffic_type,
1119+
};
1120+
11121121
self.info_traffic
11131122
.hosts
11141123
.entry(host.clone())
@@ -1119,14 +1128,7 @@ impl Sniffer {
11191128
data_info_host.is_bogon = is_bogon;
11201129
data_info_host.traffic_type = traffic_type;
11211130
})
1122-
.or_insert_with(|| DataInfoHost {
1123-
data_info: other_data,
1124-
is_favorite: false,
1125-
is_loopback,
1126-
is_local,
1127-
is_bogon,
1128-
traffic_type,
1129-
});
1131+
.or_insert(data_info_host);
11301132

11311133
self.addresses_resolved
11321134
.insert(address_to_lookup, (rdns, host.clone()));
@@ -1136,7 +1138,9 @@ impl Sniffer {
11361138

11371139
// check if the newly resolved host was featured in the favorites (possible in case of already existing host)
11381140
if self.favorite_hosts.contains(&host) {
1139-
self.info_traffic.favorites_last_interval.insert(host);
1141+
self.info_traffic
1142+
.favorites_last_interval
1143+
.insert((host, data_info_host));
11401144
}
11411145
}
11421146

src/networking/manage_packets.rs

Lines changed: 0 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,9 @@
11
use std::collections::HashMap;
22
use std::net::{IpAddr, Ipv4Addr, Ipv6Addr};
3-
use std::sync::{Arc, Mutex};
43

54
use etherparse::{EtherType, LaxPacketHeaders, LinkHeader, NetHeaders, TransportHeader};
65
use pcap::Address;
76

8-
use crate::networking::parse_packets::AddressesResolutionState;
97
use crate::networking::types::address_port_pair::AddressPortPair;
108
use crate::networking::types::arp_type::ArpType;
119
use crate::networking::types::bogon::is_bogon;
@@ -250,7 +248,6 @@ pub fn get_service(
250248
}
251249

252250
/// Function to insert the source and destination of a packet into the map containing the analyzed traffic
253-
#[allow(clippy::too_many_arguments)]
254251
pub fn modify_or_insert_in_map(
255252
info_traffic_msg: &mut InfoTrafficMessage,
256253
key: &AddressPortPair,
@@ -259,7 +256,6 @@ pub fn modify_or_insert_in_map(
259256
icmp_type: IcmpType,
260257
arp_type: ArpType,
261258
exchanged_bytes: u128,
262-
resolutions_state: &Arc<Mutex<AddressesResolutionState>>,
263259
) -> (TrafficDirection, Service) {
264260
let mut traffic_direction = TrafficDirection::default();
265261
let mut service = Service::Unknown;
@@ -280,16 +276,6 @@ pub fn modify_or_insert_in_map(
280276
);
281277
// determine upper layer service
282278
service = get_service(key, traffic_direction, my_interface_addresses);
283-
// consider all hosts as potential favorites, then they'll be filtered in InfoTraffic::refresh
284-
if let Some(host) = resolutions_state
285-
.lock()
286-
.unwrap()
287-
.addresses_resolved
288-
.get(&get_address_to_lookup(key, traffic_direction))
289-
.cloned()
290-
{
291-
info_traffic_msg.potential_favorites.insert(host);
292-
}
293279
}
294280

295281
let timestamp = info_traffic_msg.last_packet_timestamp;

src/networking/parse_packets.rs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -152,7 +152,6 @@ pub fn parse_packets(
152152
icmp_type,
153153
arp_type,
154154
exchanged_bytes,
155-
&resolutions_state,
156155
);
157156

158157
info_traffic_msg.add_packet(exchanged_bytes, traffic_direction);

src/networking/types/data_info.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ use std::time::Instant;
88

99
/// Amount of exchanged data (packets and bytes) incoming and outgoing, with the timestamp of the latest occurrence
1010
// data fields are private to make them only editable via the provided methods: needed to correctly refresh timestamps
11-
#[derive(Clone, Copy, Debug)]
11+
#[derive(Clone, Copy, Debug, Eq, PartialEq, Hash)]
1212
pub struct DataInfo {
1313
/// Incoming packets
1414
incoming_packets: u128,

src/networking/types/data_info_host.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ use crate::networking::types::data_info::DataInfo;
44
use crate::networking::types::traffic_type::TrafficType;
55

66
/// Host-related information.
7-
#[derive(Clone, Copy, Default, Debug)]
7+
#[derive(Clone, Copy, Default, Debug, Eq, PartialEq, Hash)]
88
pub struct DataInfoHost {
99
/// Incoming and outgoing packets and bytes
1010
pub data_info: DataInfo,

src/networking/types/info_traffic.rs

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ pub struct InfoTraffic {
4343
/// Map of the hosts with their data info
4444
pub hosts: HashMap<Host, DataInfoHost>,
4545
/// Collection of favorite hosts that exchanged data in the last interval
46-
pub favorites_last_interval: HashSet<Host>,
46+
pub favorites_last_interval: HashSet<(Host, DataInfoHost)>,
4747
}
4848

4949
impl InfoTraffic {
@@ -82,18 +82,19 @@ impl InfoTraffic {
8282
.or_insert(value);
8383
}
8484

85+
self.favorites_last_interval = msg
86+
.hosts
87+
.iter()
88+
.filter(|(h, _)| favorites.contains(h))
89+
.map(|(h, data)| (h.clone(), *data))
90+
.collect();
91+
8592
for (key, value) in msg.hosts {
8693
self.hosts
8794
.entry(key)
8895
.and_modify(|x| x.refresh(&value))
8996
.or_insert(value);
9097
}
91-
92-
self.favorites_last_interval = msg
93-
.potential_favorites
94-
.into_iter()
95-
.filter(|h| favorites.contains(h))
96-
.collect();
9798
}
9899

99100
pub fn get_thumbnail_data(&self, chart_type: ChartType) -> (u128, u128, u128, u128) {
@@ -141,8 +142,6 @@ pub struct InfoTrafficMessage {
141142
pub services: HashMap<Service, DataInfo>,
142143
/// Map of the hosts with their data info
143144
pub hosts: HashMap<Host, DataInfoHost>,
144-
/// Collection of potentially favorite hosts that exchanged data in the last interval
145-
pub potential_favorites: HashSet<Host>,
146145
}
147146

148147
impl InfoTrafficMessage {

src/networking/types/traffic_type.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/// Enum representing the possible traffic type (unicast, multicast or broadcast).
2-
#[derive(Clone, Copy, PartialEq, Eq, Debug)]
2+
#[derive(Clone, Copy, PartialEq, Eq, Debug, Hash)]
33
pub enum TrafficType {
44
/// Unicast traffic
55
Unicast,

src/notifications/notify_and_log.rs

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
use crate::InfoTraffic;
22
use crate::networking::types::capture_context::CaptureSource;
3-
use crate::networking::types::data_info_host::DataInfoHost;
43
use crate::notifications::types::logged_notification::{
54
BytesThresholdExceeded, FavoriteTransmitted, LoggedNotification, PacketsThresholdExceeded,
65
};
@@ -71,17 +70,13 @@ pub fn notify_and_log(
7170
if notifications.favorite_notification.notify_on_favorite
7271
&& !info_traffic.favorites_last_interval.is_empty()
7372
{
74-
for host in info_traffic.favorites_last_interval.clone() {
73+
for (host, data_info_host) in info_traffic.favorites_last_interval.clone() {
7574
//log this notification
7675
emitted_notifications += 1;
7776
if logged_notifications.len() >= 30 {
7877
logged_notifications.pop_back();
7978
}
8079

81-
let data_info_host = *info_traffic
82-
.hosts
83-
.get(&host)
84-
.unwrap_or(&DataInfoHost::default());
8580
logged_notifications.push_front(LoggedNotification::FavoriteTransmitted(
8681
FavoriteTransmitted {
8782
host,

0 commit comments

Comments
 (0)