fix: add is_admin() function for Windows builds

The Windows build was failing because ui.rs called crate::is_admin()
but the function didn't exist. Added the implementation using Windows
Security API to check if the process has elevated privileges.

Also added Win32_Security feature to windows crate dependencies.
This commit is contained in:
Marco Cadetg
2025-12-06 17:33:10 +01:00
parent b476ae1cba
commit 47f32be906
2 changed files with 40 additions and 0 deletions

View File

@@ -60,6 +60,7 @@ windows = { version = "0.62", features = [
"Win32_NetworkManagement_IpHelper",
"Win32_NetworkManagement_Ndis",
"Win32_Networking_WinSock",
"Win32_Security",
"Win32_System_LibraryLoader",
"Win32_System_Threading",
] }
@@ -82,6 +83,7 @@ windows = { version = "0.62", features = [
"Win32_NetworkManagement_IpHelper",
"Win32_NetworkManagement_Ndis",
"Win32_Networking_WinSock",
"Win32_Security",
"Win32_System_LibraryLoader",
"Win32_System_Threading",
] }

View File

@@ -7,3 +7,41 @@ pub mod config;
pub mod filter;
pub mod network;
pub mod ui;
/// Check if the current process is running with Administrator privileges (Windows only)
#[cfg(target_os = "windows")]
pub fn is_admin() -> bool {
use windows::Win32::Foundation::HANDLE;
use windows::Win32::Security::{GetTokenInformation, TokenElevation, TOKEN_ELEVATION, TOKEN_QUERY};
use windows::Win32::System::Threading::{GetCurrentProcess, OpenProcessToken};
unsafe {
let mut token_handle = HANDLE::default();
// Open the process token
if OpenProcessToken(GetCurrentProcess(), TOKEN_QUERY, &mut token_handle).is_err() {
return false;
}
let mut elevation = TOKEN_ELEVATION::default();
let mut return_length = 0u32;
// Get the elevation information
let result = GetTokenInformation(
token_handle,
TokenElevation,
Some(&mut elevation as *mut _ as *mut _),
std::mem::size_of::<TOKEN_ELEVATION>() as u32,
&mut return_length,
);
// Close the token handle
let _ = windows::Win32::Foundation::CloseHandle(token_handle);
if result.is_err() {
return false;
}
elevation.TokenIsElevated != 0
}
}