Skip to main content

rustnet_monitor/
lib.rs

1//! RustNet Monitor Library
2//!
3//! A cross-platform network monitoring library built with Rust.
4
5pub mod app;
6pub mod config;
7pub mod filter;
8pub mod network;
9pub mod ui;
10
11/// Check if the current process is running with Administrator privileges (Windows only)
12#[cfg(target_os = "windows")]
13pub fn is_admin() -> bool {
14    use windows::Win32::Foundation::HANDLE;
15    use windows::Win32::Security::{
16        GetTokenInformation, TOKEN_ELEVATION, TOKEN_QUERY, TokenElevation,
17    };
18    use windows::Win32::System::Threading::{GetCurrentProcess, OpenProcessToken};
19
20    unsafe {
21        let mut token_handle = HANDLE::default();
22
23        // Open the process token
24        if OpenProcessToken(GetCurrentProcess(), TOKEN_QUERY, &mut token_handle).is_err() {
25            return false;
26        }
27
28        let mut elevation = TOKEN_ELEVATION::default();
29        let mut return_length = 0u32;
30
31        // Get the elevation information
32        let result = GetTokenInformation(
33            token_handle,
34            TokenElevation,
35            Some(&mut elevation as *mut _ as *mut _),
36            std::mem::size_of::<TOKEN_ELEVATION>() as u32,
37            &mut return_length,
38        );
39
40        // Close the token handle
41        let _ = windows::Win32::Foundation::CloseHandle(token_handle);
42
43        if result.is_err() {
44            return false;
45        }
46
47        elevation.TokenIsElevated != 0
48    }
49}