Feat: Überarbeitet Root-/Admin-Prüfung und Neustarts, integriert Aufruf in main
This commit is contained in:
@@ -5,6 +5,7 @@ use crate::log::log;
|
|||||||
use crate::log::LogLevel;
|
use crate::log::LogLevel;
|
||||||
use crate::network::network_interface::{get_active_network_interface, get_interface_ip_address};
|
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::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::process::exit;
|
||||||
use std::thread;
|
use std::thread;
|
||||||
use std::thread::{sleep, JoinHandle};
|
use std::thread::{sleep, JoinHandle};
|
||||||
@@ -20,6 +21,10 @@ mod sudo;
|
|||||||
fn main() {
|
fn main() {
|
||||||
log("main", "========== PROGRAM START ==========", LogLevel::Info);
|
log("main", "========== PROGRAM START ==========", LogLevel::Info);
|
||||||
|
|
||||||
|
if !is_run_as_root() {
|
||||||
|
run_as_root();
|
||||||
|
}
|
||||||
|
|
||||||
let network_interface : String;
|
let network_interface : String;
|
||||||
|
|
||||||
let mut count : i32 = 0;
|
let mut count : i32 = 0;
|
||||||
|
|||||||
29
src/sudo.rs
29
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.
|
/// Prüft, ob das Programm mit Root-/Administrator-Rechten ausgeführt wird.
|
||||||
///
|
///
|
||||||
/// # Returns
|
/// # Returns
|
||||||
@@ -8,7 +11,6 @@ pub fn is_run_as_root() -> bool {
|
|||||||
#[cfg(any(target_os = "windows"))]
|
#[cfg(any(target_os = "windows"))]
|
||||||
{
|
{
|
||||||
// Windows: Prüfen ob Admin-Rechte vorhanden
|
// Windows: Prüfen ob Admin-Rechte vorhanden
|
||||||
use std::process::Command;
|
|
||||||
Command::new("net")
|
Command::new("net")
|
||||||
.args(&["session"])
|
.args(&["session"])
|
||||||
.output()
|
.output()
|
||||||
@@ -31,29 +33,30 @@ pub fn is_run_as_root() -> bool {
|
|||||||
/// - Unter Linux wird das Programm mit `sudo` neu gestartet
|
/// - Unter Linux wird das Programm mit `sudo` neu gestartet
|
||||||
/// - Unter Windows wird der UAC-Dialog zur Rechteanfrage angezeigt
|
/// - Unter Windows wird der UAC-Dialog zur Rechteanfrage angezeigt
|
||||||
pub fn run_as_root() {
|
pub fn run_as_root() {
|
||||||
use std::process::Command;
|
|
||||||
|
|
||||||
#[cfg(any(target_os = "windows"))]
|
#[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")
|
Command::new("powershell")
|
||||||
.args(&[
|
.args(&[
|
||||||
"Start-Process",
|
"Start-Process",
|
||||||
&std::env::current_exe().unwrap().to_string_lossy(),
|
&program_path,
|
||||||
|
"-ArgumentList",
|
||||||
|
&commandline_args[1..].join(" "),
|
||||||
"-Verb",
|
"-Verb",
|
||||||
"RunAs"
|
"RunAs"
|
||||||
])
|
])
|
||||||
.spawn()
|
.exec();
|
||||||
.expect("Fehler beim Neustarten mit Admin-Rechten");
|
|
||||||
std::process::exit(0);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
#[cfg(not(any(target_os = "windows")))]
|
#[cfg(not(any(target_os = "windows")))]
|
||||||
{
|
{
|
||||||
Command::new("sudo")
|
use std::os::unix::process::CommandExt;
|
||||||
.arg(&std::env::current_exe().unwrap())
|
|
||||||
.spawn()
|
let commandline_args: Vec<String> = env::args().collect();
|
||||||
.expect("Fehler beim Neustarten mit Root-Rechten");
|
let _output = Command::new("sudo")
|
||||||
std::process::exit(0);
|
.args(&commandline_args)
|
||||||
|
.exec(); // Bye bye never returns
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
Reference in New Issue
Block a user