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)> {
|
||||
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() {
|
||||
println!("No network interfaces found.");
|
||||
return None;
|
||||
}
|
||||
println!("Found {} network interfaces.", networks.len());
|
||||
unsafe {
|
||||
// Erste Abfrage zur Bestimmung der benötigten Puffergröße
|
||||
let mut buffer_size = 0u32;
|
||||
if GetIfTable(null_mut(), &mut buffer_size, 0)
|
||||
!= winapi::shared::winerror::ERROR_INSUFFICIENT_BUFFER
|
||||
{
|
||||
return None;
|
||||
}
|
||||
|
||||
let mut total_rx = 0;
|
||||
let mut total_tx = 0;
|
||||
// Puffer allozieren
|
||||
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 {
|
||||
println!(
|
||||
"Interface: {}, RX: {}, TX: {}",
|
||||
h,
|
||||
data.received(),
|
||||
data.transmitted()
|
||||
);
|
||||
total_rx += data.received();
|
||||
total_tx += data.transmitted();
|
||||
}
|
||||
// Tatsächliche Daten abrufen
|
||||
if GetIfTable(if_table, &mut buffer_size, 0) != 0 {
|
||||
return None;
|
||||
}
|
||||
|
||||
if total_rx == 0 && total_tx == 0 {
|
||||
None
|
||||
} else {
|
||||
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;
|
||||
// Daten auswerten
|
||||
let mut rx_total = 0u64;
|
||||
let mut tx_total = 0u64;
|
||||
|
||||
unsafe {
|
||||
// Erste Abfrage zur Bestimmung der benötigten Puffergröße
|
||||
let mut buffer_size = 0u32;
|
||||
if GetIfTable(null_mut(), &mut buffer_size, 0)
|
||||
!= winapi::shared::winerror::ERROR_INSUFFICIENT_BUFFER
|
||||
{
|
||||
return None;
|
||||
}
|
||||
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;
|
||||
}
|
||||
|
||||
// Puffer allozieren
|
||||
let mut buffer = vec![0u8; buffer_size as usize];
|
||||
let if_table = buffer.as_mut_ptr() as *mut MIB_IFTABLE;
|
||||
|
||||
// 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);
|
||||
}
|
||||
if rx_total == 0 && tx_total == 0 {
|
||||
return None;
|
||||
} else {
|
||||
return Some((rx_total, tx_total));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
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]
|
||||
async fn main() -> Result<(), Box<dyn Error>> {
|
||||
|
Reference in New Issue
Block a user