diff --git a/Cargo.toml b/Cargo.toml index 85669b9..f14fe79 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -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", ] } diff --git a/src/lib.rs b/src/lib.rs index 1abc56d..9bc2461 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -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::() 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 + } +}