-
Notifications
You must be signed in to change notification settings - Fork 4
/
speed.rs
31 lines (29 loc) · 911 Bytes
/
speed.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
use std::{thread, time::Duration};
use netraffic::{Filter, Traffic};
fn main() {
let mut traffic = Traffic::new();
let rule = "port 5000 or tcp port 443";
traffic.add_listener(Filter::new("eth0".to_string(), rule.to_string()));
loop {
let pre = traffic.get_data().get(rule).unwrap().total;
thread::sleep(Duration::from_millis(1000));
let bytes = (traffic.get_data().get(rule).unwrap().total - pre) as f64;
println!(
"speed: {:.2?} {}/s",
if bytes >= 1000.0 * 1000.0 {
bytes / (1000.0 * 1000.0)
} else if bytes >= 1000.0 {
bytes / 1000.0
} else {
bytes
},
if bytes >= 1000.0 * 1000.0 {
"MB"
} else if bytes >= 1000.0 {
"KB"
} else {
"B"
}
);
}
}