From ba4bc3a6456da0816660d2e53d42ee76a0b51874 Mon Sep 17 00:00:00 2001 From: 21pages Date: Sun, 30 Oct 2022 18:45:44 +0800 Subject: [PATCH 1/3] fix windows install hang, avoid kill self Signed-off-by: 21pages --- src/lang/en.rs | 1 + src/platform/windows.rs | 17 +++++++++++++---- 2 files changed, 14 insertions(+), 4 deletions(-) diff --git a/src/lang/en.rs b/src/lang/en.rs index 7c95f8abb..72aa45853 100644 --- a/src/lang/en.rs +++ b/src/lang/en.rs @@ -34,5 +34,6 @@ pub static ref T: std::collections::HashMap<&'static str, &'static str> = ("uac_warning", "Temporarily denied access due to elevation request, please wait for the remote user to accept the UAC dialog. To avoid this problem, it is recommended to install the software on the remote device or run it with administrator privileges."), ("elevated_foreground_window_warning", "Temporarily unable to use the mouse and keyboard, because the current window of the remote desktop requires higher privilege to operate, you can request the remote user to minimize the current window. To avoid this problem, it is recommended to install the software on the remote device or run it with administrator privileges."), ("JumpLink", "View"), + ("Stop service", "Stop Service"), ].iter().cloned().collect(); } diff --git a/src/platform/windows.rs b/src/platform/windows.rs index 53fb60bd8..3fc25e208 100644 --- a/src/platform/windows.rs +++ b/src/platform/windows.rs @@ -21,7 +21,10 @@ use winapi::{ errhandlingapi::GetLastError, handleapi::CloseHandle, minwinbase::STILL_ACTIVE, - processthreadsapi::{GetCurrentProcess, GetExitCodeProcess, OpenProcess, OpenProcessToken}, + processthreadsapi::{ + GetCurrentProcess, GetCurrentProcessId, GetExitCodeProcess, OpenProcess, + OpenProcessToken, + }, securitybaseapi::GetTokenInformation, shellapi::ShellExecuteA, winbase::*, @@ -905,7 +908,7 @@ pub fn update_me() -> ResultType<()> { chcp 65001 sc stop {app_name} taskkill /F /IM {broker_exe} - taskkill /F /IM {app_name}.exe + taskkill /F /IM {app_name}.exe /FI \"PID ne {cur_pid}\" {copy_exe} \"{src_exe}\" --extract \"{path}\" sc start {app_name} @@ -917,6 +920,7 @@ pub fn update_me() -> ResultType<()> { path = path, app_name = crate::get_app_name(), lic = register_licence(), + cur_pid = get_current_pid(), ); std::thread::sleep(std::time::Duration::from_millis(1000)); run_cmds(cmds, false, "update")?; @@ -1178,13 +1182,14 @@ fn get_before_uninstall() -> String { sc stop {app_name} sc delete {app_name} taskkill /F /IM {broker_exe} - taskkill /F /IM {app_name}.exe + taskkill /F /IM {app_name}.exe /FI \"PID ne {cur_pid}\" reg delete HKEY_CLASSES_ROOT\\.{ext} /f netsh advfirewall firewall delete rule name=\"{app_name} Service\" ", app_name = app_name, broker_exe = crate::ui::win_privacy::INJECTED_PROCESS_EXE, - ext = ext + ext = ext, + cur_pid = get_current_pid(), ) } @@ -1613,3 +1618,7 @@ pub fn is_foreground_window_elevated() -> ResultType { is_elevated(Some(process_id)) } } + +fn get_current_pid() -> u32 { + unsafe { GetCurrentProcessId() } +} From 7dec4f8a041f285342e2896b4e309f1e23dffd7e Mon Sep 17 00:00:00 2001 From: 21pages Date: Mon, 24 Oct 2022 20:45:34 +0800 Subject: [PATCH 2/3] copy broker when update Signed-off-by: 21pages --- src/platform/windows.rs | 28 ++++++++++++++++------------ 1 file changed, 16 insertions(+), 12 deletions(-) diff --git a/src/platform/windows.rs b/src/platform/windows.rs index 3fc25e208..ed9ac7e9b 100644 --- a/src/platform/windows.rs +++ b/src/platform/windows.rs @@ -881,23 +881,35 @@ fn get_install_info_with_subkey(subkey: String) -> (String, String, String, Stri (subkey, path, start_menu, exe) } -pub fn copy_exe_cmd(src_exe: &str, _exe: &str, _path: &str) -> String { +pub fn copy_exe_cmd(src_exe: &str, _exe: &str, path: &str) -> String { #[cfg(feature = "flutter")] - return format!( + let main_exe = format!( "XCOPY \"{}\" \"{}\" /Y /E /H /C /I /K /R /Z", PathBuf::from(src_exe) .parent() .unwrap() .to_string_lossy() .to_string(), - _path + path ); #[cfg(not(feature = "flutter"))] - return format!( + let main_exe = format!( "copy /Y \"{src_exe}\" \"{exe}\"", src_exe = src_exe, exe = _exe ); + + return format!( + " + {main_exe} + copy /Y \"{ORIGIN_PROCESS_EXE}\" \"{path}\\{broker_exe}\" + \"{src_exe}\" --extract \"{path}\" + ", + main_exe = main_exe, + path = path, + ORIGIN_PROCESS_EXE = crate::ui::win_privacy::ORIGIN_PROCESS_EXE, + broker_exe = crate::ui::win_privacy::INJECTED_PROCESS_EXE, + ); } pub fn update_me() -> ResultType<()> { @@ -910,14 +922,11 @@ pub fn update_me() -> ResultType<()> { taskkill /F /IM {broker_exe} taskkill /F /IM {app_name}.exe /FI \"PID ne {cur_pid}\" {copy_exe} - \"{src_exe}\" --extract \"{path}\" sc start {app_name} {lic} ", - src_exe = src_exe, copy_exe = copy_exe_cmd(&src_exe, &exe, &path), broker_exe = crate::ui::win_privacy::INJECTED_PROCESS_EXE, - path = path, app_name = crate::get_app_name(), lic = register_licence(), cur_pid = get_current_pid(), @@ -1091,8 +1100,6 @@ if exist \"{tmp_path}\\{app_name} Tray.lnk\" del /f /q \"{tmp_path}\\{app_name} chcp 65001 md \"{path}\" {copy_exe} -copy /Y \"{ORIGIN_PROCESS_EXE}\" \"{path}\\{broker_exe}\" -\"{src_exe}\" --extract \"{path}\" reg add {subkey} /f reg add {subkey} /f /v DisplayIcon /t REG_SZ /d \"{exe}\" reg add {subkey} /f /v DisplayName /t REG_SZ /d \"{app_name}\" @@ -1123,10 +1130,7 @@ sc delete {app_name} ", uninstall_str=uninstall_str, path=path, - src_exe=src_exe, exe=exe, - ORIGIN_PROCESS_EXE = crate::ui::win_privacy::ORIGIN_PROCESS_EXE, - broker_exe=crate::ui::win_privacy::INJECTED_PROCESS_EXE, subkey=subkey, app_name=crate::get_app_name(), version=crate::VERSION, From cc30b9f8a5360648a6c5e3850d5027602d9c27c0 Mon Sep 17 00:00:00 2001 From: 21pages Date: Sun, 30 Oct 2022 19:42:33 +0800 Subject: [PATCH 3/3] remove startup elevation, update impersonate_system Signed-off-by: 21pages --- Cargo.lock | 2 +- flutter/lib/desktop/pages/desktop_home_page.dart | 9 --------- src/core_main.rs | 5 ----- src/ui/index.tis | 6 ------ 4 files changed, 1 insertion(+), 21 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 159eee6e5..0bc360fe7 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -2559,7 +2559,7 @@ dependencies = [ [[package]] name = "impersonate_system" version = "0.1.0" -source = "git+https://github.com/21pages/impersonate-system#af4a82050580217a434c2024e181a98de24823ec" +source = "git+https://github.com/21pages/impersonate-system#c48f37a8fd17413b2a4ba655c3873bdc5c8d25aa" dependencies = [ "cc", ] diff --git a/flutter/lib/desktop/pages/desktop_home_page.dart b/flutter/lib/desktop/pages/desktop_home_page.dart index b65525159..152463c68 100644 --- a/flutter/lib/desktop/pages/desktop_home_page.dart +++ b/flutter/lib/desktop/pages/desktop_home_page.dart @@ -412,15 +412,6 @@ class _DesktopHomePageState extends State @override void initState() { super.initState(); - Timer(const Duration(seconds: 1), () async { - final installed = bind.mainIsInstalled(); - final root = await bind.mainIsRoot(); - final release = await bind.mainIsRelease(); - if (Platform.isWindows && release && !installed && !root) { - msgBox('custom-elevation-nocancel', 'Prompt', 'elevation_prompt', '', - gFFI.dialogManager); - } - }); Timer(const Duration(seconds: 5), () async { updateUrl = await bind.mainGetSoftwareUpdateUrl(); if (updateUrl.isNotEmpty) setState(() {}); diff --git a/src/core_main.rs b/src/core_main.rs index 8c6430ef8..46088da5c 100644 --- a/src/core_main.rs +++ b/src/core_main.rs @@ -85,11 +85,6 @@ pub fn core_main() -> Option> { .ok(); } } - #[cfg(windows)] - #[cfg(not(debug_assertions))] - if !crate::platform::is_installed() && args.is_empty() { - crate::platform::elevate_or_run_as_system(is_setup, _is_elevate, _is_run_as_system); - } if args.is_empty() { std::thread::spawn(move || crate::start_server(false)); } else { diff --git a/src/ui/index.tis b/src/ui/index.tis index 45e881096..31781c35e 100644 --- a/src/ui/index.tis +++ b/src/ui/index.tis @@ -1242,9 +1242,3 @@ function refreshCurrentUser() { function getHttpHeaders() { return "Authorization: Bearer " + handler.get_local_option("access_token"); } - -$(body).timer(1000, function check_elevation(){ - if (is_win && handler.is_release() && !handler.is_installed() && !handler.is_root()) { - msgbox("custom-elevation-nocancel", "Prompt", "elevation_prompt"); - } -}); \ No newline at end of file