From 0b6155bab82b75d2f6d8a57d48e960453973eb68 Mon Sep 17 00:00:00 2001 From: rustdesk Date: Thu, 28 Apr 2022 04:22:46 +0800 Subject: [PATCH] looks robust now --- src/platform/macos.rs | 41 ++++++++++++++++++++--------------------- src/server.rs | 9 ++++++++- 2 files changed, 28 insertions(+), 22 deletions(-) diff --git a/src/platform/macos.rs b/src/platform/macos.rs index 2eab0bc1a..0bfef0e5b 100644 --- a/src/platform/macos.rs +++ b/src/platform/macos.rs @@ -137,23 +137,14 @@ pub fn is_installed_daemon(prompt: bool) -> bool { log::error!("run osascript failed: {}", e); } _ => { - if std::path::Path::new(&agent_plist_file).exists() { - std::process::Command::new("osascript") - .arg("-e") - .arg( - PRIVILEGES_SCRIPTS_DIR - .get_file("run.scpt") - .unwrap() - .contents_utf8() - .unwrap(), - ) - .arg(&format!( - "sleep 0.5; launchctl load -w {};", - agent_plist_file, - )) - .spawn() + let installed = std::path::Path::new(&agent_plist_file).exists(); + log::info!("Agent file {} installed: {}", agent_plist_file, installed); + if installed { + log::info!("launch server"); + std::process::Command::new("launchctl") + .args(&["load", "-w", &agent_plist_file]) + .status() .ok(); - std::thread::sleep(std::time::Duration::from_millis(100)); // avoid exit crash std::process::exit(0); } } @@ -184,8 +175,18 @@ pub fn uninstall() -> bool { _ => { let agent = format!("{}_server.plist", crate::get_full_name()); let agent_plist_file = format!("/Library/LaunchAgents/{}", agent); - if !std::path::Path::new(&agent_plist_file).exists() { + let uninstalled = !std::path::Path::new(&agent_plist_file).exists(); + log::info!( + "Agent file {} uninstalled: {}", + agent_plist_file, + uninstalled + ); + if uninstalled { crate::ipc::set_option("stop-service", "Y"); + std::process::Command::new("launchctl") + .args(&["remove", &format!("{}_server", crate::get_full_name())]) + .status() + .ok(); std::process::Command::new("osascript") .arg("-e") .arg( @@ -196,13 +197,11 @@ pub fn uninstall() -> bool { .unwrap(), ) .arg(&format!( - "sleep 0.5; launchctl remove {}_server; sleep 0.5; open /Applications/{}.app", + "sleep 0.5; open /Applications/{}.app", crate::get_full_name(), - crate::get_app_name() )) .spawn() .ok(); - std::thread::sleep(std::time::Duration::from_millis(100)); // avoid exit crash std::process::exit(0); } } @@ -514,7 +513,7 @@ pub fn is_installed() -> bool { return p .to_str() .unwrap_or_default() - .contains(&format!("/Applications/{}.app", crate::get_app_name())); + .starts_with(&format!("/Applications/{}.app", crate::get_app_name())); } false } diff --git a/src/server.rs b/src/server.rs index a1108ff1c..9cc783208 100644 --- a/src/server.rs +++ b/src/server.rs @@ -335,7 +335,14 @@ async fn sync_and_watch_config_dir() { let mut cfg0 = (Config::get(), Config2::get()); let mut synced = false; - for i in 1..=30 { + let tries = + if std::env::args().len() == 2 && std::env::args().nth(1) == Some("--server".to_owned()) { + 30 + } else { + 1 + }; + log::debug!("#tries of ipc service connection: {}", tries); + for i in 1..=tries { sleep(i as f32 * 0.3).await; match crate::ipc::connect(1000, "_service").await { Ok(mut conn) => {