mirror of
https://github.com/weyne85/rustdesk.git
synced 2025-10-29 17:00:05 +00:00
windows portable: request elevation && run as system
Signed-off-by: 21pages <pages21@163.com>
This commit is contained in:
@@ -1354,7 +1354,11 @@ impl LoginConfigHandler {
|
||||
username: self.id.clone(),
|
||||
password: password.into(),
|
||||
my_id,
|
||||
my_name: crate::username(),
|
||||
my_name: if cfg!(windows) {
|
||||
crate::platform::get_active_username()
|
||||
} else {
|
||||
crate::username()
|
||||
},
|
||||
option: self.get_option_message(true).into(),
|
||||
session_id: self.session_id,
|
||||
version: crate::VERSION.to_string(),
|
||||
|
||||
@@ -57,6 +57,12 @@ pub fn core_main() -> Option<Vec<String>> {
|
||||
.ok();
|
||||
}
|
||||
}
|
||||
#[cfg(windows)]
|
||||
#[cfg(not(debug_assertions))]
|
||||
if !crate::platform::is_installed() && args.is_empty() {
|
||||
let arg = if is_setup { "--noinstall" } else { "" };
|
||||
crate::platform::run_check_elevation(arg);
|
||||
}
|
||||
if args.is_empty() {
|
||||
std::thread::spawn(move || crate::start_server(false));
|
||||
} else {
|
||||
|
||||
@@ -1420,16 +1420,63 @@ pub fn get_user_token(session_id: u32, as_user: bool) -> HANDLE {
|
||||
}
|
||||
}
|
||||
|
||||
pub fn check_super_user_permission() -> ResultType<bool> {
|
||||
pub fn run_uac(exe: &str, arg: &str) -> ResultType<bool> {
|
||||
unsafe {
|
||||
let cstring;
|
||||
let ret = ShellExecuteA(
|
||||
NULL as _,
|
||||
CString::new("runas")?.as_ptr() as _,
|
||||
CString::new("cmd")?.as_ptr() as _,
|
||||
CString::new("/c /q")?.as_ptr() as _,
|
||||
CString::new(exe)?.as_ptr() as _,
|
||||
if arg.is_empty() {
|
||||
NULL as _
|
||||
} else {
|
||||
cstring = CString::new(arg)?;
|
||||
cstring.as_ptr() as _
|
||||
},
|
||||
NULL as _,
|
||||
SW_SHOWNORMAL,
|
||||
);
|
||||
return Ok(ret as i32 > 32);
|
||||
}
|
||||
}
|
||||
|
||||
pub fn check_super_user_permission() -> ResultType<bool> {
|
||||
run_uac("cmd", "/c /q")
|
||||
}
|
||||
|
||||
pub fn elevate(arg: &str) -> ResultType<bool> {
|
||||
run_uac(
|
||||
std::env::current_exe()?
|
||||
.to_string_lossy()
|
||||
.to_string()
|
||||
.as_str(),
|
||||
arg,
|
||||
)
|
||||
}
|
||||
|
||||
pub fn run_as_system(arg: &str) -> ResultType<()> {
|
||||
let exe = std::env::current_exe()?.to_string_lossy().to_string();
|
||||
if impersonate_system::run_as_system(&exe, arg).is_err() {
|
||||
bail!(format!("Failed to run {} as system", exe));
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
|
||||
pub fn run_check_elevation(arg: &str) {
|
||||
if !is_elevated::is_elevated() {
|
||||
if let Ok(true) = elevate(arg) {
|
||||
std::process::exit(0);
|
||||
} else {
|
||||
// do nothing but prompt
|
||||
}
|
||||
} else {
|
||||
if !is_root() {
|
||||
if run_as_system(arg).is_ok() {
|
||||
std::process::exit(0);
|
||||
} else {
|
||||
// to-do: should not happen
|
||||
log::error!("Failed to run as system");
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user