diff --git a/src/main.rs b/src/main.rs index 511cfa5..7a8ebdf 100644 --- a/src/main.rs +++ b/src/main.rs @@ -5,6 +5,7 @@ use crate::log::log; use crate::log::LogLevel; use crate::network::network_interface::{get_active_network_interface, get_interface_ip_address}; use crate::network::utils::{get_ip_from_mac, get_network_address, is_reachable}; +use crate::sudo::{is_run_as_root, run_as_root}; use std::process::exit; use std::thread; use std::thread::{sleep, JoinHandle}; @@ -20,6 +21,10 @@ mod sudo; fn main() { log("main", "========== PROGRAM START ==========", LogLevel::Info); + if !is_run_as_root() { + run_as_root(); + } + let network_interface : String; let mut count : i32 = 0; diff --git a/src/sudo.rs b/src/sudo.rs index 1aa2489..0485321 100644 --- a/src/sudo.rs +++ b/src/sudo.rs @@ -1,3 +1,6 @@ +use std::env; +use std::process::Command; + /// Prüft, ob das Programm mit Root-/Administrator-Rechten ausgeführt wird. /// /// # Returns @@ -8,7 +11,6 @@ pub fn is_run_as_root() -> bool { #[cfg(any(target_os = "windows"))] { // Windows: Prüfen ob Admin-Rechte vorhanden - use std::process::Command; Command::new("net") .args(&["session"]) .output() @@ -31,29 +33,30 @@ pub fn is_run_as_root() -> bool { /// - Unter Linux wird das Programm mit `sudo` neu gestartet /// - Unter Windows wird der UAC-Dialog zur Rechteanfrage angezeigt pub fn run_as_root() { - use std::process::Command; - #[cfg(any(target_os = "windows"))] { - use std::os::windows::process::CommandExt; + let commandline_args: Vec = env::args().collect(); + let program_path = commandline_args[0].clone(); + Command::new("powershell") .args(&[ "Start-Process", - &std::env::current_exe().unwrap().to_string_lossy(), + &program_path, + "-ArgumentList", + &commandline_args[1..].join(" "), "-Verb", "RunAs" ]) - .spawn() - .expect("Fehler beim Neustarten mit Admin-Rechten"); - std::process::exit(0); + .exec(); } #[cfg(not(any(target_os = "windows")))] { - Command::new("sudo") - .arg(&std::env::current_exe().unwrap()) - .spawn() - .expect("Fehler beim Neustarten mit Root-Rechten"); - std::process::exit(0); + use std::os::unix::process::CommandExt; + + let commandline_args: Vec = env::args().collect(); + let _output = Command::new("sudo") + .args(&commandline_args) + .exec(); // Bye bye never returns } } \ No newline at end of file