Merge pull request #3102 from 21pages/fix-elevate

Fix elevation
This commit is contained in:
RustDesk 2023-02-07 16:00:20 +08:00 committed by GitHub
commit e9ffe504fa
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 36 additions and 15 deletions

View File

@ -14,6 +14,7 @@ use std::{
};
pub(super) const APP_TYPE_MAIN: &str = "main";
pub(super) const APP_TYPE_CM: &str = "cm";
pub(super) const APP_TYPE_DESKTOP_REMOTE: &str = "remote";
pub(super) const APP_TYPE_DESKTOP_FILE_TRANSFER: &str = "file transfer";
pub(super) const APP_TYPE_DESKTOP_PORT_FORWARD: &str = "port forward";
@ -528,11 +529,7 @@ pub mod connection_manager {
assert!(h.get("name").is_none());
h.insert("name", name);
if let Some(s) = GLOBAL_EVENT_STREAM
.read()
.unwrap()
.get(super::APP_TYPE_MAIN)
{
if let Some(s) = GLOBAL_EVENT_STREAM.read().unwrap().get(super::APP_TYPE_CM) {
s.add(serde_json::ser::to_string(&h).unwrap_or("".to_owned()));
};
}

View File

@ -11,6 +11,7 @@ use std::io::prelude::*;
use std::{
ffi::OsString,
fs, io, mem,
os::windows::process::CommandExt,
path::PathBuf,
sync::{Arc, Mutex},
time::{Duration, Instant},
@ -1644,6 +1645,29 @@ pub fn is_elevated(process_id: Option<DWORD>) -> ResultType<bool> {
}
}
#[inline]
fn filter_foreground_window(process_id: DWORD) -> ResultType<bool> {
if let Ok(output) = std::process::Command::new("tasklist")
.args(vec![
"/SVC",
"/NH",
"/FI",
&format!("PID eq {}", process_id),
])
.creation_flags(CREATE_NO_WINDOW)
.output()
{
let s = String::from_utf8_lossy(&output.stdout)
.to_string()
.to_lowercase();
Ok(["Taskmgr", "mmc", "regedit"]
.iter()
.any(|name| s.contains(&name.to_string().to_lowercase())))
} else {
bail!("run tasklist failed");
}
}
pub fn is_foreground_window_elevated() -> ResultType<bool> {
unsafe {
let mut process_id: DWORD = 0;
@ -1651,7 +1675,12 @@ pub fn is_foreground_window_elevated() -> ResultType<bool> {
if process_id == 0 {
bail!("Failed to get processId, errno {}", GetLastError())
}
is_elevated(Some(process_id))
let elevated = is_elevated(Some(process_id))?;
if elevated {
filter_foreground_window(process_id)
} else {
Ok(false)
}
}
}

View File

@ -118,11 +118,9 @@ impl SharedMemory {
fn flink(name: String) -> ResultType<String> {
let disk = std::env::var("SystemDrive").unwrap_or("C:".to_string());
let mut dir = PathBuf::from(disk);
let dir1 = dir.join("ProgramData");
let dir2 = std::env::var("TEMP")
.map(|d| PathBuf::from(d))
.unwrap_or(dir.join("Windows").join("Temp"));
let dir1 = PathBuf::from(format!("{}\\ProgramData", disk));
let dir2 = PathBuf::from(format!("{}\\Windows\\Temp", disk));
let mut dir;
if dir1.exists() {
dir = dir1;
} else if dir2.exists() {

View File

@ -954,10 +954,7 @@ pub fn get_current_display() -> ResultType<(usize, usize, Display)> {
fn start_uac_elevation_check() {
static START: Once = Once::new();
START.call_once(|| {
if !crate::platform::is_installed()
&& !crate::platform::is_root()
&& !crate::portable_service::client::running()
{
if !crate::platform::is_installed() && !crate::platform::is_root() {
std::thread::spawn(|| loop {
std::thread::sleep(std::time::Duration::from_secs(1));
if let Ok(uac) = crate::ui::win_privacy::is_process_consent_running() {