Skip to content

Commit dac861c

Browse files
committed
feat: add top downloader/uploader into a summary
1 parent 406d435 commit dac861c

File tree

4 files changed

+73
-12
lines changed

4 files changed

+73
-12
lines changed

Cargo.lock

+1-1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[package]
22
name = "netscanner"
3-
version = "0.5.4"
3+
version = "0.6.0"
44
edition = "2021"
55
description = "Network Scanner"
66
license = "MIT"

src/components/sniff.rs

+70-9
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ use color_eyre::eyre::Result;
22
use color_eyre::owo_colors::OwoColorize;
33
use dns_lookup::{lookup_addr, lookup_host};
44

5+
use ipnetwork::IpNetwork;
56
use pnet::{
67
datalink::NetworkInterface,
78
packet::{
@@ -39,13 +40,13 @@ pub struct IPTraffic {
3940
pub struct Sniffer {
4041
active_tab: TabsEnum,
4142
action_tx: Option<UnboundedSender<Action>>,
42-
active_interface: Option<NetworkInterface>,
4343
list_state: ListState,
4444
scrollbar_state: ScrollbarState,
4545
traffic_ips: Vec<IPTraffic>,
4646
scrollview_state: ScrollViewState,
4747
udp_sum: f64,
4848
tcp_sum: f64,
49+
active_inft_ips: Vec<IpNetwork>,
4950
}
5051

5152
impl Default for Sniffer {
@@ -59,13 +60,13 @@ impl Sniffer {
5960
Self {
6061
active_tab: TabsEnum::Discovery,
6162
action_tx: None,
62-
active_interface: None,
6363
list_state: ListState::default().with_selected(Some(0)),
6464
scrollbar_state: ScrollbarState::new(0),
6565
traffic_ips: Vec::new(),
6666
scrollview_state: ScrollViewState::new(),
6767
udp_sum: 0.0,
6868
tcp_sum: 0.0,
69+
active_inft_ips: Vec::new(),
6970
}
7071
}
7172

@@ -228,7 +229,7 @@ impl Sniffer {
228229
Rect {
229230
x: area.x + 2,
230231
y: area.y + 2,
231-
width: area.width / 2,
232+
width: area.width,
232233
height: 1,
233234
},
234235
);
@@ -240,19 +241,79 @@ impl Sniffer {
240241
f.render_widget(
241242
total_upload,
242243
Rect {
243-
x: area.x + (area.width / 2) + 2,
244-
y: area.y + 2,
245-
width: area.width / 2,
244+
x: area.x + 2,
245+
y: area.y + 3,
246+
width: area.width,
246247
height: 1,
247248
},
248249
);
249250

250-
let top_uploader = Line::from(vec!["Top uploader:".into()]);
251+
let a_intfs = self.active_inft_ips.clone();
252+
let tu = self
253+
.traffic_ips
254+
.iter()
255+
.filter(|item| {
256+
let t_ip = item.ip.to_string();
257+
for i_ip in a_intfs.clone() {
258+
if i_ip.ip().to_string() == t_ip {
259+
return false;
260+
}
261+
}
262+
true
263+
})
264+
.max_by_key(|t| t.upload as u64);
265+
266+
let mut tu_ip = String::from("");
267+
let mut tu_name = String::from("");
268+
if tu.is_some() {
269+
tu_ip = tu.unwrap().ip.to_string();
270+
tu_name = format!(" ({})", tu.unwrap().hostname);
271+
}
272+
let top_uploader = Line::from(vec![
273+
"Top uploader: ".into(),
274+
tu_ip.blue(),
275+
tu_name.magenta(),
276+
]);
251277
f.render_widget(
252278
top_uploader,
253279
Rect {
254280
x: area.x + 2,
255-
y: area.y + 4,
281+
y: area.y + 5,
282+
width: area.width,
283+
height: 1,
284+
},
285+
);
286+
287+
let td = self
288+
.traffic_ips
289+
.iter()
290+
.filter(|item| {
291+
let t_ip = item.ip.to_string();
292+
for i_ip in a_intfs.clone() {
293+
if i_ip.ip().to_string() == t_ip {
294+
return false;
295+
}
296+
}
297+
true
298+
})
299+
.max_by_key(|t| t.download as u64);
300+
301+
let mut td_ip = String::from("");
302+
let mut td_name = String::from("");
303+
if td.is_some() {
304+
td_ip = td.unwrap().ip.to_string();
305+
td_name = format!(" ({})", tu.unwrap().hostname);
306+
}
307+
let top_downloader = Line::from(vec![
308+
"Top downloader: ".into(),
309+
td_ip.blue(),
310+
td_name.magenta(),
311+
]);
312+
f.render_widget(
313+
top_downloader,
314+
Rect {
315+
x: area.x + 2,
316+
y: area.y + 6,
256317
width: area.width,
257318
height: 1,
258319
},
@@ -291,7 +352,7 @@ impl Component for Sniffer {
291352
}
292353

293354
if let Action::ActiveInterface(ref interface) = action {
294-
self.active_interface = Some(interface.clone());
355+
self.active_inft_ips = interface.ips.clone();
295356
}
296357

297358
if let Action::PacketDump(time, packet, packet_type) = action {

src/widgets/scroll_traffic.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ impl StatefulWidget for TrafficScroll {
3131
.border_style(Style::default().fg(Color::Rgb(100, 100, 100)))
3232
.title_style(Style::default().fg(Color::Blue))
3333
.title(Line::from(vec![
34-
format!("|{}|", item.ip).blue(),
34+
format!("{}", item.ip).blue(),
3535
format!(" ({})", item.hostname.clone()).magenta(),
3636
]));
3737
scrollview.render_widget(b, b_rect);

0 commit comments

Comments
 (0)