From 59987f637c806ad86b5e642da9205287a071d741 Mon Sep 17 00:00:00 2001 From: fufesou Date: Fri, 12 May 2023 16:08:15 +0800 Subject: [PATCH 1/2] linux, wrap pkexec Signed-off-by: fufesou --- src/platform/linux.rs | 28 +++++++++++++++++++++---- src/plugin/manager.rs | 49 +++++++------------------------------------ src/ui_interface.rs | 11 +++++----- 3 files changed, 37 insertions(+), 51 deletions(-) diff --git a/src/platform/linux.rs b/src/platform/linux.rs index 5ca8052f3..edb61c075 100644 --- a/src/platform/linux.rs +++ b/src/platform/linux.rs @@ -648,6 +648,10 @@ pub fn quit_gui() { unsafe { gtk_main_quit() }; } +pub fn exec_privileged(args: &[&str]) -> ResultType { + Ok(Command::new("pkexec").args(args).spawn()?) +} + pub fn check_super_user_permission() -> ResultType { let file = "/usr/share/rustdesk/files/polkit"; let arg; @@ -656,11 +660,11 @@ pub fn check_super_user_permission() -> ResultType { } else { arg = "echo"; } - let status = Command::new("pkexec").arg(arg).status()?; + let status = exec_privileged(&[arg])?.wait()?; Ok(status.success() && status.code() == Some(0)) } -pub fn elevate(args: Vec<&str>) -> ResultType> { +pub fn elevate(args: Vec<&str>) -> ResultType { let cmd = std::env::current_exe()?; match cmd.to_str() { Some(cmd) => { @@ -670,8 +674,24 @@ pub fn elevate(args: Vec<&str>) -> ResultType> { if is_opensuse() { args_with_exe.insert(0, "-E"); } - let task = Command::new("pkexec").args(args_with_exe).spawn()?; - Ok(Some(task)) + let res = match exec_privileged(&args_with_exe)?.wait() { + Ok(status) => { + if status.success() { + true + } else { + log::error!( + "Failed to wait install process, process status: {:?}", + status + ); + false + } + } + Err(e) => { + log::error!("Failed to wait install process, error: {}", e); + false + } + }; + Ok(res) } None => { hbb_common::bail!("Failed to get current exe as str"); diff --git a/src/plugin/manager.rs b/src/plugin/manager.rs index 9fd0f7861..e474fbec3 100644 --- a/src/plugin/manager.rs +++ b/src/plugin/manager.rs @@ -80,7 +80,11 @@ fn get_source_plugins() -> HashMap { match toml::from_str::(&text) { Ok(manager_meta) => { for meta in manager_meta.plugins.iter() { - if !meta.platforms.to_uppercase().contains(&PLUGIN_PLATFORM.to_uppercase()) { + if !meta + .platforms + .to_uppercase() + .contains(&PLUGIN_PLATFORM.to_uppercase()) + { continue; } plugins.insert( @@ -166,10 +170,10 @@ fn elevate_install( } else { format!("--plugin-install {} {}", plugin_id, plugin_url) }; - Ok(crate::platform::elevate(&args)?) + crate::platform::elevate(&args) } -#[cfg(target_os = "linux")] +#[cfg(any(target_os = "linux", target_os = "macos"))] fn elevate_install( plugin_id: &str, plugin_url: &str, @@ -179,44 +183,7 @@ fn elevate_install( if !same_plugin_exists { args.push(&plugin_url); } - let allowed_install = match crate::platform::elevate(args) { - Ok(Some(mut child)) => match child.wait() { - Ok(status) => { - if status.success() { - true - } else { - log::error!( - "Failed to wait install process, process status: {:?}", - status - ); - false - } - } - Err(e) => { - log::error!("Failed to wait install process, error: {}", e); - false - } - }, - Ok(None) => false, - Err(e) => { - log::error!("Failed to run install process, error: {}", e); - false - } - }; - Ok(allowed_install) -} - -#[cfg(target_os = "macos")] -fn elevate_install( - plugin_id: &str, - plugin_url: &str, - same_plugin_exists: bool, -) -> ResultType { - let mut args = vec!["--plugin-install", plugin_id]; - if !same_plugin_exists { - args.push(&plugin_url); - } - Ok(crate::platform::elevate(args)?) + crate::platform::elevate(args) } pub fn install_plugin(id: &str) -> ResultType<()> { diff --git a/src/ui_interface.rs b/src/ui_interface.rs index 10f2982f2..a6fc39a88 100644 --- a/src/ui_interface.rs +++ b/src/ui_interface.rs @@ -77,12 +77,11 @@ pub fn install_me(_options: String, _path: String, _silent: bool, _debug: bool) pub fn update_me(_path: String) { #[cfg(target_os = "linux")] { - std::process::Command::new("pkexec") - .args(&["apt", "install", "-f", &_path]) - .spawn() - .ok(); - std::fs::remove_file(&_path).ok(); - crate::run_me(Vec::<&str>::new()).ok(); + allow_err!(crate::platform::linux::exec_privileged(&[ + "apt", "install", "-f", &_path + ])); + allow_err!(std::fs::remove_file(&_path)); + allow_err!(crate::run_me(Vec::<&str>::new())); } #[cfg(windows)] { From bc6ed22006dc0a5227af67c5ccb71e463888ca65 Mon Sep 17 00:00:00 2001 From: fufesou Date: Fri, 12 May 2023 16:29:10 +0800 Subject: [PATCH 2/2] fix word spelling Signed-off-by: fufesou --- flutter/lib/desktop/pages/install_page.dart | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/flutter/lib/desktop/pages/install_page.dart b/flutter/lib/desktop/pages/install_page.dart index 7e1fe9b38..70d279ca7 100644 --- a/flutter/lib/desktop/pages/install_page.dart +++ b/flutter/lib/desktop/pages/install_page.dart @@ -24,13 +24,13 @@ class _InstallPageState extends State { void initState() { super.initState(); Get.put(tabController); - const lable = "install"; + const label = "install"; tabController.add(TabInfo( - key: lable, - label: lable, + key: label, + label: label, closable: false, page: _InstallPageBody( - key: const ValueKey(lable), + key: const ValueKey(label), ))); }