reset to old net detection
This commit is contained in:
@@ -645,99 +645,82 @@ fn get_disk_info() -> (f64, f64, f64) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
fn get_network_traffic() -> Option<(u64, u64)> {
|
fn get_network_traffic() -> Option<(u64, u64)> {
|
||||||
let networks = Networks::new_with_refreshed_list();
|
#[cfg(target_os = "windows")]
|
||||||
|
{
|
||||||
|
use std::ptr::null_mut;
|
||||||
|
use winapi::shared::ifmib::{MIB_IFROW, MIB_IFTABLE};
|
||||||
|
use winapi::um::iphlpapi::GetIfTable;
|
||||||
|
|
||||||
if networks.is_empty() {
|
unsafe {
|
||||||
println!("No network interfaces found.");
|
// Erste Abfrage zur Bestimmung der benötigten Puffergröße
|
||||||
return None;
|
let mut buffer_size = 0u32;
|
||||||
}
|
if GetIfTable(null_mut(), &mut buffer_size, 0)
|
||||||
println!("Found {} network interfaces.", networks.len());
|
!= winapi::shared::winerror::ERROR_INSUFFICIENT_BUFFER
|
||||||
|
{
|
||||||
|
return None;
|
||||||
|
}
|
||||||
|
|
||||||
let mut total_rx = 0;
|
// Puffer allozieren
|
||||||
let mut total_tx = 0;
|
let mut buffer = vec![0u8; buffer_size as usize];
|
||||||
|
let if_table = buffer.as_mut_ptr() as *mut MIB_IFTABLE;
|
||||||
|
|
||||||
for (h, data) in &networks {
|
// Tatsächliche Daten abrufen
|
||||||
println!(
|
if GetIfTable(if_table, &mut buffer_size, 0) != 0 {
|
||||||
"Interface: {}, RX: {}, TX: {}",
|
return None;
|
||||||
h,
|
}
|
||||||
data.received(),
|
|
||||||
data.transmitted()
|
|
||||||
);
|
|
||||||
total_rx += data.received();
|
|
||||||
total_tx += data.transmitted();
|
|
||||||
}
|
|
||||||
|
|
||||||
if total_rx == 0 && total_tx == 0 {
|
// Daten auswerten
|
||||||
None
|
let mut rx_total = 0u64;
|
||||||
} else {
|
let mut tx_total = 0u64;
|
||||||
Some((total_rx, total_tx))
|
|
||||||
}
|
|
||||||
}
|
|
||||||
/*
|
|
||||||
#[cfg(target_os = "windows")]
|
|
||||||
fn get_network_traffic() -> Option<(u64, u64)> {
|
|
||||||
use std::ptr::null_mut;
|
|
||||||
use winapi::shared::ifmib::{MIB_IFROW, MIB_IFTABLE};
|
|
||||||
use winapi::um::iphlpapi::GetIfTable;
|
|
||||||
|
|
||||||
unsafe {
|
for i in 0..(*if_table).dwNumEntries {
|
||||||
// Erste Abfrage zur Bestimmung der benötigten Puffergröße
|
let row = &*((*if_table).table.as_ptr().offset(i as isize));
|
||||||
let mut buffer_size = 0u32;
|
rx_total += row.dwInOctets as u64;
|
||||||
if GetIfTable(null_mut(), &mut buffer_size, 0)
|
tx_total += row.dwOutOctets as u64;
|
||||||
!= winapi::shared::winerror::ERROR_INSUFFICIENT_BUFFER
|
}
|
||||||
{
|
|
||||||
return None;
|
|
||||||
}
|
|
||||||
|
|
||||||
// Puffer allozieren
|
if rx_total == 0 && tx_total == 0 {
|
||||||
let mut buffer = vec![0u8; buffer_size as usize];
|
return None;
|
||||||
let if_table = buffer.as_mut_ptr() as *mut MIB_IFTABLE;
|
} else {
|
||||||
|
return Some((rx_total, tx_total));
|
||||||
// Tatsächliche Daten abrufen
|
|
||||||
if GetIfTable(if_table, &mut buffer_size, 0) != 0 {
|
|
||||||
return None;
|
|
||||||
}
|
|
||||||
|
|
||||||
// Daten auswerten
|
|
||||||
let mut rx_total = 0u64;
|
|
||||||
let mut tx_total = 0u64;
|
|
||||||
|
|
||||||
for i in 0..(*if_table).dwNumEntries {
|
|
||||||
let row = &*((*if_table).table.as_ptr().offset(i as isize));
|
|
||||||
rx_total += row.dwInOctets as u64;
|
|
||||||
tx_total += row.dwOutOctets as u64;
|
|
||||||
}
|
|
||||||
|
|
||||||
Some((rx_total, tx_total))
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
#[cfg(target_os = "linux")]
|
|
||||||
fn get_network_traffic() -> Option<(u64, u64)> {
|
|
||||||
// Bessere Methode mit sysfs
|
|
||||||
let mut rx_total = 0u64;
|
|
||||||
let mut tx_total = 0u64;
|
|
||||||
|
|
||||||
if let Ok(dir) = fs::read_dir("/sys/class/net") {
|
|
||||||
for entry in dir.flatten() {
|
|
||||||
let iface = entry.file_name();
|
|
||||||
let iface_name = iface.to_string_lossy();
|
|
||||||
|
|
||||||
// Ignoriere virtuelle Interfaces
|
|
||||||
if !iface_name.starts_with("lo") && !iface_name.starts_with("virbr") {
|
|
||||||
if let (Ok(rx), Ok(tx)) = (
|
|
||||||
fs::read_to_string(entry.path().join("statistics/rx_bytes")),
|
|
||||||
fs::read_to_string(entry.path().join("statistics/tx_bytes")),
|
|
||||||
) {
|
|
||||||
rx_total += rx.trim().parse::<u64>().unwrap_or(0);
|
|
||||||
tx_total += tx.trim().parse::<u64>().unwrap_or(0);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
Some((rx_total, tx_total))
|
#[cfg(target_os = "linux")]
|
||||||
}*/
|
{
|
||||||
|
use std::fs;
|
||||||
|
|
||||||
|
let mut rx_total = 0u64;
|
||||||
|
let mut tx_total = 0u64;
|
||||||
|
if let Ok(dir) = fs::read_dir("/sys/class/net") {
|
||||||
|
for entry in dir.flatten() {
|
||||||
|
let iface = entry.file_name();
|
||||||
|
let iface_name = iface.to_string_lossy();
|
||||||
|
|
||||||
|
// Ignoriere virtuelle Interfaces
|
||||||
|
if !iface_name.starts_with("lo") && !iface_name.starts_with("virbr") {
|
||||||
|
if let (Ok(rx), Ok(tx)) = (
|
||||||
|
fs::read_to_string(entry.path().join("statistics/rx_bytes")),
|
||||||
|
fs::read_to_string(entry.path().join("statistics/tx_bytes")),
|
||||||
|
) {
|
||||||
|
rx_total += rx.trim().parse::<u64>().unwrap_or(0);
|
||||||
|
tx_total += tx.trim().parse::<u64>().unwrap_or(0);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if rx_total == 0 && tx_total == 0 {
|
||||||
|
return None;
|
||||||
|
} else {
|
||||||
|
return Some((rx_total, tx_total));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
#[cfg(not(any(target_os = "windows", target_os = "linux")))]
|
||||||
|
None
|
||||||
|
}
|
||||||
|
|
||||||
#[tokio::main]
|
#[tokio::main]
|
||||||
async fn main() -> Result<(), Box<dyn Error>> {
|
async fn main() -> Result<(), Box<dyn Error>> {
|
||||||
|
Reference in New Issue
Block a user