From aacffd979be293765603e0abebd9a985da0448b6 Mon Sep 17 00:00:00 2001 From: rustdesk Date: Wed, 19 Jul 2023 14:45:11 +0800 Subject: [PATCH] make config in exe name has highest priority, also overwrite config if installation, https://github.com/rustdesk/rustdesk-server-pro/issues/21#issuecomment-1638259580, not tested yet --- libs/hbb_common/src/config.rs | 10 ++++- src/common.rs | 26 ++++++------ src/core_main.rs | 2 + src/platform/windows.rs | 77 +++++------------------------------ src/server.rs | 2 - src/ui_interface.rs | 2 +- 6 files changed, 35 insertions(+), 84 deletions(-) diff --git a/libs/hbb_common/src/config.rs b/libs/hbb_common/src/config.rs index 8d322c462..8f2e180a0 100644 --- a/libs/hbb_common/src/config.rs +++ b/libs/hbb_common/src/config.rs @@ -49,6 +49,7 @@ lazy_static::lazy_static! { Some(key) if !key.is_empty() => key, _ => "", }.to_owned())); + pub static ref EXE_RENDEZVOUS_SERVER: Arc> = Default::default(); pub static ref APP_NAME: Arc> = Arc::new(RwLock::new("RustDesk".to_owned())); static ref KEY_PAIR: Arc>> = Default::default(); static ref HW_CODEC_CONFIG: Arc> = Arc::new(RwLock::new(HwCodecConfig::load())); @@ -604,7 +605,10 @@ impl Config { } pub fn get_rendezvous_server() -> String { - let mut rendezvous_server = Self::get_option("custom-rendezvous-server"); + let mut rendezvous_server = EXE_RENDEZVOUS_SERVER.read().unwrap().clone(); + if rendezvous_server.is_empty() { + rendezvous_server = Self::get_option("custom-rendezvous-server"); + } if rendezvous_server.is_empty() { rendezvous_server = PROD_RENDEZVOUS_SERVER.read().unwrap().clone(); } @@ -624,6 +628,10 @@ impl Config { } pub fn get_rendezvous_servers() -> Vec { + let s = EXE_RENDEZVOUS_SERVER.read().unwrap().clone(); + if !s.is_empty() { + return vec![s]; + } let s = Self::get_option("custom-rendezvous-server"); if !s.is_empty() { return vec![s]; diff --git a/src/common.rs b/src/common.rs index ef96f0a37..8f84509ae 100644 --- a/src/common.rs +++ b/src/common.rs @@ -833,15 +833,15 @@ pub fn is_setup(name: &str) -> bool { } pub fn get_custom_rendezvous_server(custom: String) -> String { - if !custom.is_empty() { - return custom; - } #[cfg(windows)] - if let Some(lic) = crate::platform::windows::get_license() { + if let Ok(lic) = crate::platform::windows::get_license_from_exe_name() { if !lic.host.is_empty() { return lic.host.clone(); } } + if !custom.is_empty() { + return custom; + } if !config::PROD_RENDEZVOUS_SERVER.read().unwrap().is_empty() { return config::PROD_RENDEZVOUS_SERVER.read().unwrap().clone(); } @@ -849,15 +849,15 @@ pub fn get_custom_rendezvous_server(custom: String) -> String { } pub fn get_api_server(api: String, custom: String) -> String { - if !api.is_empty() { - return api.to_owned(); - } #[cfg(windows)] - if let Some(lic) = crate::platform::windows::get_license() { + if let Ok(lic) = crate::platform::windows::get_license_from_exe_name() { if !lic.api.is_empty() { return lic.api.clone(); } } + if !api.is_empty() { + return api.to_owned(); + } let api = option_env!("API_SERVER").unwrap_or_default(); if !api.is_empty() { return api.into(); @@ -982,6 +982,10 @@ pub fn decode64>(input: T) -> Result, base64::DecodeError } pub async fn get_key(sync: bool) -> String { + #[cfg(windows)] + if let Ok(lic) = crate::platform::windows::get_license_from_exe_name() { + return lic.key; + } #[cfg(target_os = "ios")] let mut key = Config::get_option("key"); #[cfg(not(target_os = "ios"))] @@ -991,12 +995,6 @@ pub async fn get_key(sync: bool) -> String { let mut options = crate::ipc::get_options_async().await; options.remove("key").unwrap_or_default() }; - if key.is_empty() { - #[cfg(windows)] - if let Some(lic) = crate::platform::windows::get_license() { - return lic.key; - } - } if key.is_empty() && !option_env!("RENDEZVOUS_SERVER").unwrap_or("").is_empty() { key = config::RS_PUB_KEY.to_owned(); } diff --git a/src/core_main.rs b/src/core_main.rs index 391b0f3ef..2121f46cd 100644 --- a/src/core_main.rs +++ b/src/core_main.rs @@ -25,6 +25,8 @@ macro_rules! my_println{ /// If it returns [`Some`], then the process will continue, and flutter gui will be started. #[cfg(not(any(target_os = "android", target_os = "ios")))] pub fn core_main() -> Option> { + #[cfg(windows)] + crate::platform::windows::bootstrap(); let mut args = Vec::new(); let mut flutter_args = Vec::new(); let mut i = 0; diff --git a/src/platform/windows.rs b/src/platform/windows.rs index 8734cccf7..16c4ead2f 100644 --- a/src/platform/windows.rs +++ b/src/platform/windows.rs @@ -927,35 +927,6 @@ pub fn copy_exe_cmd(src_exe: &str, exe: &str, path: &str) -> String { ) } -/* // update_me has bad compatibility, so disable it. -pub fn update_me() -> ResultType<()> { - let (_, path, _, exe) = get_install_info(); - let src_exe = std::env::current_exe()?.to_str().unwrap_or("").to_owned(); - let cmds = format!( - " - chcp 65001 - sc stop {app_name} - taskkill /F /IM {broker_exe} - taskkill /F /IM {app_name}.exe /FI \"PID ne {cur_pid}\" - {copy_exe} - sc start {app_name} - {lic} - ", - copy_exe = copy_exe_cmd(&src_exe, &exe, &path), - broker_exe = WIN_MAG_INJECTED_PROCESS_EXE, - app_name = crate::get_app_name(), - lic = register_licence(), - cur_pid = get_current_pid(), - ); - run_cmds(cmds, false, "update")?; - run_after_run_cmds(false); - std::process::Command::new(&exe) - .args(&["--remove", &src_exe]) - .spawn()?; - Ok(()) -} -*/ - fn get_after_install(exe: &str) -> String { let app_name = crate::get_app_name(); let ext = app_name.to_lowercase(); @@ -1093,6 +1064,15 @@ if exist \"{tmp_path}\\{app_name} Tray.lnk\" del /f /q \"{tmp_path}\\{app_name} "".to_owned() }; + // potential bug here: if run_cmd cancelled, but config file is changed. + if let Ok(lic) = crate::platform::windows::get_license_from_exe_name() { + if !lic.host.is_empty() { + Config::set_option("key".into(), lic.key); + Config::set_option("custom-rendezvous-server".into(), lic.host); + Config::set_option("api-server".into(), lic.api); + } + } + let cmds = format!( " {uninstall_str} @@ -1113,7 +1093,6 @@ reg add {subkey} /f /v VersionBuild /t REG_DWORD /d {version_build} reg add {subkey} /f /v UninstallString /t REG_SZ /d \"\\\"{exe}\\\" --uninstall\" reg add {subkey} /f /v EstimatedSize /t REG_DWORD /d {size} reg add {subkey} /f /v WindowsInstaller /t REG_DWORD /d 0 -{lic} cscript \"{mk_shortcut}\" cscript \"{uninstall_shortcut}\" cscript \"{tray_shortcut}\" @@ -1128,7 +1107,6 @@ copy /Y \"{tmp_path}\\Uninstall {app_name}.lnk\" \"{path}\\\" ", version=crate::VERSION, build_date=crate::BUILD_DATE, - lic=register_licence(), after_install=get_after_install(&exe), sleep=if debug { "timeout 300" @@ -1347,7 +1325,7 @@ fn get_reg_of(subkey: &str, name: &str) -> String { "".to_owned() } -fn get_license_from_exe_name() -> ResultType { +pub fn get_license_from_exe_name() -> ResultType { let mut exe = std::env::current_exe()?.to_str().unwrap_or("").to_owned(); // if defined portable appname entry, replace original executable name with it. if let Ok(portable_exe) = std::env::var(PORTABLE_APPNAME_RUNTIME_ENV_KEY) { @@ -1362,42 +1340,9 @@ pub fn is_win_server() -> bool { unsafe { is_windows_server() > 0 } } -pub fn get_license() -> Option { - let mut lic: License = Default::default(); - if let Ok(tmp) = get_license_from_exe_name() { - lic = tmp; - } else { - lic.key = get_reg("Key"); - lic.host = get_reg("Host"); - lic.api = get_reg("Api"); - } - if lic.key.is_empty() || lic.host.is_empty() { - return None; - } - Some(lic) -} - pub fn bootstrap() { - if let Some(lic) = get_license() { - *config::PROD_RENDEZVOUS_SERVER.write().unwrap() = lic.host.clone(); - } -} - -fn register_licence() -> String { - let (subkey, _, _, _) = get_install_info(); if let Ok(lic) = get_license_from_exe_name() { - format!( - " - reg add {subkey} /f /v Key /t REG_SZ /d \"{key}\" - reg add {subkey} /f /v Host /t REG_SZ /d \"{host}\" - reg add {subkey} /f /v Api /t REG_SZ /d \"{api}\" - ", - key = &lic.key, - host = &lic.host, - api = &lic.api, - ) - } else { - "".to_owned() + *config::EXE_RENDEZVOUS_SERVER.write().unwrap() = lic.host.clone(); } } diff --git a/src/server.rs b/src/server.rs index b2d0b593f..9f41b1ddc 100644 --- a/src/server.rs +++ b/src/server.rs @@ -379,8 +379,6 @@ pub async fn start_server(is_server: bool) { std::process::exit(-1); } }); - #[cfg(windows)] - crate::platform::windows::bootstrap(); input_service::fix_key_down_timeout_loop(); crate::hbbs_http::sync::start(); #[cfg(target_os = "linux")] diff --git a/src/ui_interface.rs b/src/ui_interface.rs index 982436b53..9ea367486 100644 --- a/src/ui_interface.rs +++ b/src/ui_interface.rs @@ -118,7 +118,7 @@ pub fn show_run_without_install() -> bool { #[inline] pub fn get_license() -> String { #[cfg(windows)] - if let Some(lic) = crate::platform::windows::get_license() { + if let Ok(lic) = crate::platform::windows::get_license_from_exe_name() { #[cfg(feature = "flutter")] return format!("Key: {}\nHost: {}\nApi: {}", lic.key, lic.host, lic.api); // default license format is html formed (sciter)