main: check for admin at init, good for windows, bad for unix

This commit is contained in:
hinto-janaiyo 2022-12-09 22:06:42 -05:00
parent e7de536f18
commit 929d80c61d
No known key found for this signature in database
GPG key ID: B1C5A64B80691E45
3 changed files with 27 additions and 8 deletions

View file

@ -119,7 +119,7 @@ Exceptions (there are always exceptions...):
- P2Pool hashes are in UPPERCASE - P2Pool hashes are in UPPERCASE
## Why does Gupax need to be Admin? (on Windows) ## Why does Gupax need to be Admin? (on Windows)
**Simple TL;DR:** Because Windows. **TL;DR:** Because Windows.
**Slightly more detailed TL;DR:** Rust does not have mature Win32 API wrapper libraries. Although Microsoft has an official ["Rust" library](https://github.com/microsoft/windows-rs), it is quite low-level and using it within Gupax would mean re-implementing a lot of Rust's STDLIB process module code. **Slightly more detailed TL;DR:** Rust does not have mature Win32 API wrapper libraries. Although Microsoft has an official ["Rust" library](https://github.com/microsoft/windows-rs), it is quite low-level and using it within Gupax would mean re-implementing a lot of Rust's STDLIB process module code.

View file

@ -92,14 +92,16 @@ pub const PASSWORD_HIDE: &str = "Toggle hiding/showing the password";
// OS specific // OS specific
#[cfg(target_os = "windows")] #[cfg(target_os = "windows")]
pub const OS: &'static str = " Windows"; pub const OS: &str = " Windows";
#[cfg(target_os = "windows")] #[cfg(target_os = "windows")]
pub const OS_NAME: &'static str = "Windows"; pub const OS_NAME: &str = "Windows";
#[cfg(target_os = "windows")]
pub const WINDOWS_NOT_ADMIN: &str = "XMRig will most likely mine slower than normal without Administrator permissions. Please consider restarting Gupax as an Administrator.";
#[cfg(target_os = "macos")] #[cfg(target_os = "macos")]
pub const OS: &'static str = " macOS"; pub const OS: &str = " macOS";
#[cfg(target_os = "macos")] #[cfg(target_os = "macos")]
pub const OS_NAME: &'static str = "macOS"; pub const OS_NAME: &str = "macOS";
#[cfg(target_os = "linux")] #[cfg(target_os = "linux")]
pub const OS: &str = "🐧 Linux"; pub const OS: &str = "🐧 Linux";

View file

@ -131,6 +131,7 @@ pub struct App {
dir: String, // Directory [Gupax] binary is in dir: String, // Directory [Gupax] binary is in
resolution: Vec2, // Frame resolution resolution: Vec2, // Frame resolution
os: &'static str, // OS os: &'static str, // OS
admin: bool, // Are we admin? (for Windows)
os_data_path: PathBuf, // OS data path (e.g: ~/.local/share/gupax) os_data_path: PathBuf, // OS data path (e.g: ~/.local/share/gupax)
state_path: PathBuf, // State file path state_path: PathBuf, // State file path
node_path: PathBuf, // Node file path node_path: PathBuf, // Node file path
@ -190,6 +191,7 @@ impl App {
alpha: 0, alpha: 0,
no_startup: false, no_startup: false,
now, now,
admin: false,
exe: String::new(), exe: String::new(),
dir: String::new(), dir: String::new(),
resolution: Vec2::new(APP_DEFAULT_HEIGHT, APP_DEFAULT_WIDTH), resolution: Vec2::new(APP_DEFAULT_HEIGHT, APP_DEFAULT_WIDTH),
@ -344,8 +346,9 @@ impl App {
// Check for privilege. Should be Admin on [Windows] and NOT root on Unix. // Check for privilege. Should be Admin on [Windows] and NOT root on Unix.
#[cfg(target_os = "windows")] #[cfg(target_os = "windows")]
if !is_elevated::is_elevated() { if !is_elevated::is_elevated() {
app.admin = false;
error!("Windows | Admin user not detected!"); error!("Windows | Admin user not detected!");
app.error_state.set(format!("Gupax was not launched as Administrator!\nBe warned, XMRig might have less hashrate!"), ErrorFerris::Sudo, ErrorButtons::Okay); app.error_state.set(format!("Gupax was not launched as Administrator!\nBe warned, XMRig might have less hashrate!"), ErrorFerris::Sudo, ErrorButtons::WindowsAdmin);
} }
#[cfg(target_family = "unix")] #[cfg(target_family = "unix")]
if sudo_check::check() != sudo_check::RunningAs::User { if sudo_check::check() != sudo_check::RunningAs::User {
@ -392,6 +395,7 @@ pub enum ErrorButtons {
Okay, Okay,
Quit, Quit,
Sudo, Sudo,
WindowsAdmin,
} }
#[derive(Clone, Copy, Debug, PartialEq, Eq)] #[derive(Clone, Copy, Debug, PartialEq, Eq)]
@ -893,7 +897,10 @@ impl eframe::App for App {
Happy => ui.add_sized([width, height], Label::new("--- Success! ---")), Happy => ui.add_sized([width, height], Label::new("--- Success! ---")),
_ => ui.add_sized([width, height], Label::new("--- Gupax has encountered an error! ---")), _ => ui.add_sized([width, height], Label::new("--- Gupax has encountered an error! ---")),
}; };
ui.add_sized([width, height], Label::new(&self.error_state.msg)) let height = height/2.0;
ui.add_sized([width, height], Label::new(&self.error_state.msg));
// Show GitHub rant link for Windows admin problems.
ui.add_sized([width, height], Hyperlink::from_label_and_url("[Why does Gupax need to be Admin? (on Windows)]", "https://github.com/hinto-janaiyo/gupax/tree/main/src#why-does-gupax-need-to-be-admin-on-windows"))
}, },
}; };
let height = ui.available_height(); let height = ui.available_height();
@ -986,7 +993,7 @@ impl eframe::App for App {
self.error_state.reset(); self.error_state.reset();
} }
}, },
Okay => if esc || ui.add_sized([width, height], Button::new("Okay")).clicked() { self.error_state.reset(); }, Okay|WindowsAdmin => if esc || ui.add_sized([width, height], Button::new("Okay")).clicked() { self.error_state.reset(); },
Quit => if ui.add_sized([width, height], Button::new("Quit")).clicked() { exit(1); }, Quit => if ui.add_sized([width, height], Button::new("Quit")).clicked() { exit(1); },
} }
})}); })});
@ -1046,6 +1053,16 @@ impl eframe::App for App {
}; };
ui.separator(); ui.separator();
// [OS] // [OS]
// Check if admin for windows.
// Unix SHOULDN'T be running as root, and the check is done when
// [App] is initialized, so no reason to check here.
#[cfg(target_os = "windows")]
if self.admin {
ui.add_sized([width, height], Label::new(self.os));
} else {
ui.add_sized([width, height], Label::new(RichText::new(self.os).color(RED))).on_hover_text(WINDOWS_NOT_ADMIN);
}
#[cfg(target_family = "unix")]
ui.add_sized([width, height], Label::new(self.os)); ui.add_sized([width, height], Label::new(self.os));
ui.separator(); ui.separator();
// [P2Pool/XMRig] Status // [P2Pool/XMRig] Status