Feat: Überarbeitet Root-/Admin-Prüfung und Neustarts, integriert Aufruf in main

This commit is contained in:
2025-08-21 00:13:55 +02:00
parent 4373b60193
commit d26c3e9856
2 changed files with 21 additions and 13 deletions

View File

@@ -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;

View File

@@ -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<String> = 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<String> = env::args().collect();
let _output = Command::new("sudo")
.args(&commandline_args)
.exec(); // Bye bye never returns
}
}