From d26c3e9856c605d605754b39cf63bcb5788be428 Mon Sep 17 00:00:00 2001 From: DragonSlayer_14 Date: Thu, 21 Aug 2025 00:13:55 +0200 Subject: [PATCH] =?UTF-8?q?Feat:=20=C3=9Cberarbeitet=20Root-/Admin-Pr?= =?UTF-8?q?=C3=BCfung=20und=20Neustarts,=20integriert=20Aufruf=20in=20`mai?= =?UTF-8?q?n`?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/main.rs | 5 +++++ src/sudo.rs | 29 ++++++++++++++++------------- 2 files changed, 21 insertions(+), 13 deletions(-) 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