still not robust

This commit is contained in:
rustdesk 2022-04-28 03:25:39 +08:00
parent bbe902d92e
commit 8995011d45
6 changed files with 64 additions and 47 deletions

View File

@ -131,7 +131,7 @@ pub fn is_installed_daemon(prompt: bool) -> bool {
.arg(daemon_plist_body)
.arg(agent_plist_body)
.arg(&get_active_username())
.output()
.status()
{
Err(e) => {
log::error!("run osascript failed: {}", e);
@ -148,9 +148,8 @@ pub fn is_installed_daemon(prompt: bool) -> bool {
.unwrap(),
)
.arg(&format!(
"sleep 0.5; launchctl load -w {}; sleep 0.5; open /Applications/{}.app",
"sleep 0.5; launchctl load -w {};",
agent_plist_file,
crate::get_app_name()
))
.spawn()
.ok();
@ -177,7 +176,7 @@ pub fn uninstall() -> bool {
.arg("-e")
.arg(script_body)
.arg(&get_active_username())
.output()
.status()
{
Err(e) => {
log::error!("run osascript failed: {}", e);
@ -401,21 +400,40 @@ pub fn lock_screen() {
}
pub fn start_os_service() {
let exe = std::env::current_exe().unwrap_or_default();
let tm0 = hbb_common::get_modified_time(&exe);
log::info!("{}", crate::username());
std::thread::spawn(move || loop {
let exe = std::env::current_exe().unwrap_or_default();
let tm0 = hbb_common::get_modified_time(&exe);
loop {
std::thread::sleep(std::time::Duration::from_millis(300));
if hbb_common::get_modified_time(&exe) != tm0 {
log::info!("{:?} updated, will restart", exe);
let now = hbb_common::get_modified_time(&exe);
if now != tm0 && now != std::time::UNIX_EPOCH {
// sleep a while to wait for resources file ready
std::thread::sleep(std::time::Duration::from_millis(300));
println!("{:?} updated, will restart", exe);
// this won't kill myself
std::process::Command::new("pkill")
.args(&["-f", exe.to_str().unwrap_or("")])
.output()
.args(&["-f", &crate::get_app_name()])
.status()
.ok();
std::process::exit(0); // self not killed by above pkill
println!("The others killed");
// launchctl load/unload/start agent not work in daemon, show not priviledged.
// sudo launchctl asuser 501 open -n also not allowed.
std::process::Command::new("launchctl")
.args(&[
"asuser",
&get_active_userid(),
"open",
"-a",
&exe.to_str().unwrap_or(""),
"--args",
"--server",
])
.status()
.ok();
// if above spawn, we may need sleep for a while here.
std::process::exit(0);
}
}
});
@ -493,10 +511,10 @@ pub fn block_input(_v: bool) -> bool {
pub fn is_installed() -> bool {
if let Ok(p) = std::env::current_exe() {
return p.to_str().unwrap_or_default().contains(&format!(
"/Applications/{}.app",
crate::get_app_name(),
));
return p
.to_str()
.unwrap_or_default()
.contains(&format!("/Applications/{}.app", crate::get_app_name()));
}
false
}

View File

@ -15,5 +15,9 @@
<true/>
<key>WorkingDirectory</key>
<string>/Applications/RustDesk.app/Contents/MacOS/</string>
<key>StandardErrorPath</key>
<string>/tmp/rustdesk_service.err</string>
<key>StandardOutPath</key>
<string>/tmp/rustdesk_service.out</string>
</dict>
</plist>

View File

@ -3,7 +3,4 @@ set sh2 to "/bin/rm /Library/LaunchDaemons/com.carriez.RustDesk_service.plist;"
set sh3 to "/bin/rm /Library/LaunchAgents/com.carriez.RustDesk_server.plist;"
set sh to sh1 & sh2 & sh3
do shell script sh with prompt "RustDesk want to unload daemon" with administrator privileges
set sh5 to "[ ! -f /Library/LaunchAgents/com.carriez.RustDesk_server.plist ] && launchctl remove com.carriez.RustDesk_server && sleep 1 && open /Applications/RustDesk.app"
do shell script sh5
do shell script sh with prompt "RustDesk want to unload daemon" with administrator privileges

View File

@ -333,36 +333,41 @@ async fn sync_and_watch_config_dir() {
return;
}
for i in 1..=6 {
let mut cfg0 = (Config::get(), Config2::get());
let mut synced = false;
for i in 1..=30 {
sleep(i as f32 * 0.3).await;
match crate::ipc::connect(1000, "_service").await {
Ok(mut conn) => {
if conn.send(&Data::SyncConfig(None)).await.is_ok() {
if let Ok(Some(data)) = conn.next_timeout(1000).await {
match data {
Data::SyncConfig(Some((config, config2))) => {
let _chk = crate::ipc::CheckIfRestart::new();
Config::set(config);
Config2::set(config2);
log::info!("sync config from root");
}
_ => {}
if !synced {
if conn.send(&Data::SyncConfig(None)).await.is_ok() {
if let Ok(Some(data)) = conn.next_timeout(1000).await {
match data {
Data::SyncConfig(Some((config, config2))) => {
let _chk = crate::ipc::CheckIfRestart::new();
Config::set(config);
Config2::set(config2);
log::info!("sync config from root");
synced = true;
}
_ => {}
};
};
};
}
}
let mut cfg0 = (Config::get(), Config2::get());
loop {
sleep(0.3).await;
let cfg = (Config::get(), Config2::get());
if cfg != cfg0 {
cfg0 = cfg;
log::info!("config updated, sync to root");
match conn.send(&Data::SyncConfig(Some(cfg0.clone()))).await {
Err(e) => {
log::error!("sync config to root failed: {}", e);
break;
}
_ => {
cfg0 = cfg;
conn.next_timeout(1000).await.ok();
}
}

View File

@ -153,8 +153,6 @@ pub fn start(args: &mut [String]) {
page
));
if is_server {
#[cfg(target_os = "macos")]
macos::ignore_first_time_awake();
frame.collapse(true);
frame.run_loop();
} else {

View File

@ -42,12 +42,8 @@ impl DelegateState {
}
}
static mut IGNORE_FIRST_TIME: bool = false;
pub fn ignore_first_time_awake() {
unsafe {
IGNORE_FIRST_TIME = true;
}
lazy_static::lazy_static! {
static ref START_TM: std::sync::Mutex<std::time::Instant> = std::sync::Mutex::new(std::time::Instant::now());
}
impl AppHandler for Rc<Host> {
@ -59,11 +55,9 @@ impl AppHandler for Rc<Host> {
let _ = self.call_function("awake", &make_args![]);
let _ = self.call_function("showSettings", &make_args![]);
} else if cmd == AWAKE {
unsafe {
if IGNORE_FIRST_TIME {
IGNORE_FIRST_TIME = false;
return;
}
if START_TM.lock().unwrap().elapsed().as_millis() < 1000 {
hbb_common::log::debug!("First click on docker icon {:?}", START_TM.lock().unwrap().elapsed());
return;
}
let _ = self.call_function("awake", &make_args![]);
}
@ -72,6 +66,7 @@ impl AppHandler for Rc<Host> {
// https://github.com/xi-editor/druid/blob/master/druid-shell/src/platform/mac/application.rs
unsafe fn set_delegate(handler: Option<Box<dyn AppHandler>>) {
*START_TM.lock().unwrap() = std::time::Instant::now();
let mut decl =
ClassDecl::new("AppDelegate", class!(NSObject)).expect("App Delegate definition failed");
decl.add_ivar::<*mut c_void>(APP_HANDLER_IVAR);