Ref: Passt die Funktion an, sodass sie das aktive Netzwerkinterface zurückgibt.
This commit is contained in:
21
src/main.rs
21
src/main.rs
@@ -3,7 +3,7 @@ use std::thread::sleep;
|
||||
use std::time::Duration;
|
||||
use crate::log::LogLevel;
|
||||
use crate::log::log;
|
||||
use crate::network::network_card::is_network_card_up;
|
||||
use crate::network::network_interface::get_active_network_interface;
|
||||
|
||||
mod log;
|
||||
mod network;
|
||||
@@ -11,20 +11,27 @@ mod network;
|
||||
fn main() {
|
||||
log("main", "========== PROGRAMM START ==========", LogLevel::Info);
|
||||
|
||||
let mut network_interface = String::new();
|
||||
|
||||
let mut count = 0;
|
||||
loop {
|
||||
if count >= 10 {
|
||||
let interface_str = get_active_network_interface().unwrap().trim().to_string();
|
||||
|
||||
if !interface_str.is_empty() {
|
||||
network_interface = interface_str;
|
||||
break;
|
||||
} else if count >= 10 {
|
||||
log("main", "Couldn't find active network card, exiting.", LogLevel::Error);
|
||||
exit(1);
|
||||
}
|
||||
if is_network_card_up() {
|
||||
break;
|
||||
}
|
||||
|
||||
log("main", "No active network card found, waiting 10 seconds.", LogLevel::Warn);
|
||||
log("main", "No active network card found, waiting 1 second.", LogLevel::Warn);
|
||||
count = count+1;
|
||||
sleep(Duration::from_secs(10));
|
||||
sleep(Duration::from_secs(1));
|
||||
}
|
||||
|
||||
log("main", &format!("Active network interface found: {}", network_interface), LogLevel::Info);
|
||||
|
||||
|
||||
log("main", "========== PROGRAMM END ==========", LogLevel::Info);
|
||||
}
|
||||
|
||||
@@ -1 +1,2 @@
|
||||
pub mod network_card;
|
||||
pub mod network_interface;
|
||||
@@ -6,9 +6,8 @@ use std::process::Command;
|
||||
/// und nicht nur die Loopback-Schnittstelle.
|
||||
///
|
||||
/// Rückgabe:
|
||||
/// - true: Mindestens eine NIC ist aktiv
|
||||
/// - false: Keine aktive NIC gefunden oder Fehler beim Prüfen
|
||||
pub fn is_network_card_up() -> bool {
|
||||
/// - Option<String>: Name der aktiven Netzwerkkarte oder None wenn keine gefunden
|
||||
pub fn get_active_network_interface() -> Option<String> {
|
||||
#[cfg(target_os = "linux")]
|
||||
{
|
||||
use std::fs;
|
||||
@@ -59,7 +58,7 @@ pub fn is_network_card_up() -> bool {
|
||||
continue;
|
||||
}
|
||||
|
||||
// Optional: bekannte „logische“ Typen via Verzeichnis-Erkennung ausschließen
|
||||
// Optional: bekannte „logische" Typen via Verzeichnis-Erkennung ausschließen
|
||||
// (Bridges, VLANs, Macvlan etc.)
|
||||
if iface_path.join("bridge").exists()
|
||||
|| iface_path.join("bonding").exists()
|
||||
@@ -72,7 +71,7 @@ pub fn is_network_card_up() -> bool {
|
||||
|
||||
// Wenn wir hier sind, haben wir eine aktive physische NIC
|
||||
log::log("network_status", "Found active network interface.", LogLevel::Info);
|
||||
return true;
|
||||
return Some(ifname);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -82,7 +81,7 @@ pub fn is_network_card_up() -> bool {
|
||||
|
||||
if let Ok(out) = output {
|
||||
if !out.status.success() {
|
||||
return false;
|
||||
return None;
|
||||
}
|
||||
let stdout = String::from_utf8_lossy(&out.stdout);
|
||||
for line in stdout.lines() {
|
||||
@@ -91,13 +90,13 @@ pub fn is_network_card_up() -> bool {
|
||||
let name = ifname.trim();
|
||||
if name != "lo" {
|
||||
log::log("network_status", "Found active network interface.", LogLevel::Info);
|
||||
return true;
|
||||
return Some(name.to_string());
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
false
|
||||
None
|
||||
}
|
||||
|
||||
#[cfg(target_os = "windows")]
|
||||
@@ -105,8 +104,8 @@ pub fn is_network_card_up() -> bool {
|
||||
// Windows: PowerShell verwenden, um aktive Adapter zu finden (Status = Up).
|
||||
// Wir ignorieren Loopback/Pseudo-Interfaces, indem wir Physical=true filtern.
|
||||
let ps_cmd = r#"
|
||||
$adapters = Get-NetAdapter -Physical | Where-Object {$_.Status -eq 'Up'}
|
||||
if ($adapters -and $adapters.Count -ge 1) { 'UP' } else { 'DOWN' }
|
||||
$adapters = Get-NetAdapter -Physical | Where-Object {$_.Status -eq 'Up'} | Select-Object -First 1
|
||||
if ($adapters) { $adapters.Name } else { 'NONE' }
|
||||
"#;
|
||||
let output = Command::new("powershell")
|
||||
.args(["-NoProfile", "-NonInteractive", "-Command", ps_cmd])
|
||||
@@ -114,23 +113,21 @@ if ($adapters -and $adapters.Count -ge 1) { 'UP' } else { 'DOWN' }
|
||||
|
||||
if let Ok(out) = output {
|
||||
if !out.status.success() {
|
||||
return false;
|
||||
return None;
|
||||
}
|
||||
let stdout = String::from_utf8_lossy(&out.stdout).to_ascii_uppercase();
|
||||
let stdout = String::from_utf8_lossy(&out.stdout).trim().to_string();
|
||||
|
||||
return if stdout.contains("UP") {
|
||||
if stdout != "NONE" {
|
||||
log::log("network_status", "Found active network interface.", LogLevel::Info);
|
||||
true
|
||||
} else {
|
||||
false
|
||||
return Some(stdout);
|
||||
}
|
||||
}
|
||||
false
|
||||
None
|
||||
}
|
||||
|
||||
#[cfg(not(any(target_os = "linux", target_os = "windows")))]
|
||||
{
|
||||
log::log("network_status", "OS not supported.", LogLevel::Error);
|
||||
false
|
||||
None
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user