new mac service and local config

This commit is contained in:
rustdesk
2022-04-26 11:19:45 +08:00
parent 05ac26e5fe
commit 3566b0ee7a
15 changed files with 308 additions and 419 deletions

View File

@@ -104,11 +104,12 @@ pub fn is_can_screen_recording(prompt: bool) -> bool {
pub fn is_installed_daemon(prompt: bool) -> bool {
let daemon = format!("{}_service.plist", crate::get_full_name());
let agent = format!("{}_server.plist", crate::get_full_name());
let agent_plist_file = format!("/Library/LaunchAgents/{}", agent);
if !prompt {
if !std::path::Path::new(&format!("/Library/LaunchDaemons/{}", daemon)).exists() {
return false;
}
if !std::path::Path::new(&format!("/Library/LaunchAgents/{}", agent)).exists() {
if !std::path::Path::new(&agent_plist_file).exists() {
return false;
}
return true;
@@ -123,43 +124,92 @@ pub fn is_installed_daemon(prompt: bool) -> bool {
let agent_plist = PRIVILEGES_SCRIPTS_DIR.get_file(&agent).unwrap();
let agent_plist_body = agent_plist.contents_utf8().unwrap();
match std::process::Command::new("osascript")
.arg("-e")
.arg(install_script_body)
.arg(daemon_plist_body)
.arg(agent_plist_body)
.arg(&get_active_username())
.spawn()
{
Ok(_) => {
std::process::exit(0);
std::thread::spawn(move || {
match std::process::Command::new("osascript")
.arg("-e")
.arg(install_script_body)
.arg(daemon_plist_body)
.arg(agent_plist_body)
.arg(&get_active_username())
.output()
{
Err(e) => {
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 {}; sleep 0.5; open /Applications/{}.app",
agent_plist_file,
crate::get_app_name()
))
.spawn()
.ok();
std::thread::sleep(std::time::Duration::from_millis(100)); // avoid exit crash
std::process::exit(0);
}
}
}
Err(e) => {
log::error!("run osascript failed: {}", e);
false
}
}
});
false
}
pub fn launch(load: bool) {
pub fn uninstall() -> bool {
// to-do: do together with win/linux about refactory start/stop service
if !is_installed() || !is_installed_daemon(false) {
return;
}
let mut script_filename = "load.scpt";
if !load {
script_filename = "unload.scpt";
if !is_installed_daemon(false) {
return false;
}
let script_file = PRIVILEGES_SCRIPTS_DIR.get_file(script_filename).unwrap();
let script_file = PRIVILEGES_SCRIPTS_DIR.get_file("uninstall.scpt").unwrap();
let script_body = script_file.contents_utf8().unwrap();
std::process::Command::new("osascript")
.arg("-e")
.arg(script_body)
.arg(&get_active_username())
.spawn()
.ok();
std::thread::spawn(move || {
match std::process::Command::new("osascript")
.arg("-e")
.arg(script_body)
.arg(&get_active_username())
.output()
{
Err(e) => {
log::error!("run osascript failed: {}", e);
}
_ => {
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() {
crate::ipc::set_option("stop-service", "Y");
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 remove {}_server; 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);
}
}
}
});
true
}
pub fn get_cursor_pos() -> Option<(i32, i32)> {
@@ -352,6 +402,24 @@ pub fn lock_screen() {
pub fn start_os_service() {
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);
std::process::Command::new("pkill")
.args(&["-f", exe.to_str().unwrap_or("")])
.output()
.ok();
std::process::exit(0); // self not killed by above pkill
}
}
});
if let Err(err) = crate::ipc::start("_service") {
log::error!("Failed to start ipc_service: {}", err);
}
@@ -425,7 +493,7 @@ pub fn is_installed() -> bool {
if let Ok(p) = std::env::current_exe() {
return p.to_str().unwrap_or_default().contains(&format!(
"/Applications/{}.app",
hbb_common::config::APP_NAME
crate::get_app_name(),
));
}
false

View File

@@ -1,19 +1,16 @@
on run {daemon_file, agent_file, user}
set sh1 to "echo " & quoted form of daemon_file & " > /Library/LaunchDaemons/com.carriez.RustDesk_service.plist && chown root:wheel /Library/LaunchDaemons/com.carriez.RustDesk_service.plist;"
set sh1 to "echo " & quoted form of daemon_file & " > /Library/LaunchDaemons/com.carriez.RustDesk_service.plist && chown root:wheel /Library/LaunchDaemons/com.carriez.RustDesk_service.plist;"
set sh2 to "echo " & quoted form of agent_file & " > /Library/LaunchAgents/com.carriez.RustDesk_server.plist && chown root:wheel /Library/LaunchAgents/com.carriez.RustDesk_server.plist;"
set sh2 to "echo " & quoted form of agent_file & " > /Library/LaunchAgents/com.carriez.RustDesk_server.plist && chown root:wheel /Library/LaunchAgents/com.carriez.RustDesk_server.plist;"
set sh3 to "cp -rf /Users/" & user & "/Library/Preferences/com.carriez.RustDesk/RustDesk.toml /var/root/Library/Preferences/com.carriez.RustDesk/;"
set sh4 to "cp -rf /Users/" & user & "/Library/Preferences/com.carriez.RustDesk/RustDesk2.toml /var/root/Library/Preferences/com.carriez.RustDesk/;"
set sh5 to "launchctl unload -w /Library/LaunchAgents/com.carriez.RustDesk_server.plist; launchctl load -w /Library/LaunchDaemons/com.carriez.RustDesk_service.plist;"
set sh6 to "pkill -f rustdesk; launchctl unload -w /Library/LaunchAgents/com.carriez.RustDesk_server.plist; launchctl load -w /Library/LaunchAgents/com.carriez.RustDesk_server.plist; open /Applications/RustDesk.app"
set sh5 to "launchctl load -w /Library/LaunchDaemons/com.carriez.RustDesk_service.plist;"
set sh to sh1 & sh2 & sh3 & sh4 & sh5
set sh to sh1 & sh2 & sh3 & sh4 & sh5
do shell script sh with prompt "RustDesk want to install daemon and agent" with administrator privileges
do shell script sh6
do shell script sh with prompt "RustDesk want to install daemon and agent" with administrator privileges
end run

View File

@@ -1,16 +0,0 @@
on run {user}
set sh1 to "cp -rf /Users/" & user & "/Library/Preferences/com.carriez.RustDesk/RustDesk.toml /var/root/Library/Preferences/com.carriez.RustDesk/;"
set sh2 to "cp -rf /Users/" & user & "/Library/Preferences/com.carriez.RustDesk/RustDesk2.toml /var/root/Library/Preferences/com.carriez.RustDesk/;"
set sh3 to "launchctl load -w /Library/LaunchDaemons/com.carriez.rustdesk_service.plist;"
set sh4 to "launchctl load -w /Library/LaunchAgents/com.carriez.rustdesk_server.plist;"
set sh to sh1 & sh2 & sh3
do shell script sh with prompt "RustDesk want to launch daemon" with administrator privileges
do shell script sh4
end run

View File

@@ -1,6 +0,0 @@
set sh1 to "launchctl unload -w /Library/LaunchDaemons/com.carriez.rustdesk_service.plist;"
set sh2 to "launchctl unload -w /Library/LaunchAgents/com.carriez.rustdesk_server.plist;"
do shell script sh1 with prompt "RustDesk want to unload daemon" with administrator privileges
do shell script sh2