mirror of
https://github.com/weyne85/rustdesk.git
synced 2025-10-29 17:00:05 +00:00
refactor mac service
This commit is contained in:
@@ -240,7 +240,7 @@ pub fn start_os_service() {
|
||||
if let Some(ps) = server.take().as_mut() {
|
||||
allow_err!(ps.kill());
|
||||
}
|
||||
println!("Exit");
|
||||
log::info!("Exit");
|
||||
}
|
||||
|
||||
fn get_active_userid() -> String {
|
||||
|
||||
@@ -16,7 +16,7 @@ use core_graphics::{
|
||||
display::{kCGNullWindowID, kCGWindowListOptionOnScreenOnly, CGWindowListCopyWindowInfo},
|
||||
window::{kCGWindowName, kCGWindowOwnerPID},
|
||||
};
|
||||
use hbb_common::{allow_err, bail, log};
|
||||
use hbb_common::{bail, log};
|
||||
use include_dir::{include_dir, Dir};
|
||||
use objc::{class, msg_send, sel, sel_impl};
|
||||
use scrap::{libc::c_void, quartz::ffi::*};
|
||||
@@ -102,55 +102,38 @@ 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());
|
||||
if !prompt {
|
||||
if !std::path::Path::new("/Library/LaunchDaemons/com.carriez.rustdesk.daemon.plist")
|
||||
.exists()
|
||||
{
|
||||
if !std::path::Path::new(&format!("/Library/LaunchDaemons/{}", daemon)).exists() {
|
||||
return false;
|
||||
}
|
||||
|
||||
if !std::path::Path::new("/Library/LaunchAgents/com.carriez.rustdesk.agent.root.plist")
|
||||
.exists()
|
||||
{
|
||||
if !std::path::Path::new(&format!("/Library/LaunchAgents/{}", agent)).exists() {
|
||||
return false;
|
||||
}
|
||||
|
||||
if !std::path::Path::new("/Library/LaunchAgents/com.carriez.rustdesk.agent.user.plist")
|
||||
.exists()
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
let install_script = PRIVILEGES_SCRIPTS_DIR.get_file("install.scpt").unwrap();
|
||||
let install_script_body = install_script.contents_utf8().unwrap();
|
||||
|
||||
let daemon_plist = PRIVILEGES_SCRIPTS_DIR
|
||||
.get_file("com.carriez.rustdesk.daemon.plist")
|
||||
.unwrap();
|
||||
let daemon_plist = PRIVILEGES_SCRIPTS_DIR.get_file(&daemon).unwrap();
|
||||
let daemon_plist_body = daemon_plist.contents_utf8().unwrap();
|
||||
|
||||
let root_agent_plist = PRIVILEGES_SCRIPTS_DIR
|
||||
.get_file("com.carriez.rustdesk.agent.root.plist")
|
||||
.unwrap();
|
||||
let root_agent_plist_body = root_agent_plist.contents_utf8().unwrap();
|
||||
|
||||
let user_agent_plist = PRIVILEGES_SCRIPTS_DIR
|
||||
.get_file("com.carriez.rustdesk.agent.user.plist")
|
||||
.unwrap();
|
||||
let user_agent_plist_body = user_agent_plist.contents_utf8().unwrap();
|
||||
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(root_agent_plist_body)
|
||||
.arg(user_agent_plist_body)
|
||||
.arg(agent_plist_body)
|
||||
.arg(&get_active_username())
|
||||
.spawn()
|
||||
{
|
||||
Ok(mut proc) => proc.wait().is_ok(),
|
||||
Ok(mut proc) => {
|
||||
std::process::exit(0);
|
||||
}
|
||||
Err(e) => {
|
||||
log::error!("run osascript failed: {}", e);
|
||||
false
|
||||
@@ -158,10 +141,14 @@ pub fn is_installed_daemon(prompt: bool) -> bool {
|
||||
}
|
||||
}
|
||||
|
||||
pub fn launch_or_stop_daemon(launch: bool) {
|
||||
let mut script_filename = "launch_service.scpt";
|
||||
if !launch {
|
||||
script_filename = "stop_service.scpt";
|
||||
pub fn launch(load: 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";
|
||||
}
|
||||
|
||||
let script_file = PRIVILEGES_SCRIPTS_DIR.get_file(script_filename).unwrap();
|
||||
@@ -363,39 +350,66 @@ pub fn lock_screen() {
|
||||
}
|
||||
|
||||
pub fn start_os_service() {
|
||||
let mut server: Option<std::process::Child> = None;
|
||||
let mut uid = "".to_owned();
|
||||
loop {
|
||||
let tmp = get_active_userid();
|
||||
let mut start_new = false;
|
||||
if tmp != uid && !tmp.is_empty() {
|
||||
uid = tmp;
|
||||
log::info!("active uid: {}", uid);
|
||||
if let Some(ps) = server.as_mut() {
|
||||
allow_err!(ps.kill());
|
||||
}
|
||||
}
|
||||
if let Some(ps) = server.as_mut() {
|
||||
match ps.try_wait() {
|
||||
Ok(Some(_)) => {
|
||||
server = None;
|
||||
start_new = true;
|
||||
}
|
||||
_ => {}
|
||||
}
|
||||
} else {
|
||||
start_new = true;
|
||||
}
|
||||
if start_new {
|
||||
match crate::run_me(vec!["--server"]) {
|
||||
Ok(ps) => server = Some(ps),
|
||||
Err(err) => {
|
||||
log::error!("Failed to start server: {}", err);
|
||||
}
|
||||
}
|
||||
}
|
||||
std::thread::sleep(std::time::Duration::from_millis(super::SERVICE_INTERVAL));
|
||||
log::info!("{}", crate::username());
|
||||
if let Err(err) = crate::ipc::start("_service") {
|
||||
log::error!("Failed to start ipc_service: {}", err);
|
||||
}
|
||||
|
||||
/* // somehow, below works fine if user logged in, but mouse/keyboard not work under prelogin.
|
||||
// one solution to run --server as agent in prelogin,
|
||||
// and run --server under --service via run_as_user if not in prelogin
|
||||
// so that no multiple --server if multiple logged-in users
|
||||
use std::sync::{
|
||||
atomic::{AtomicBool, Ordering},
|
||||
Arc,
|
||||
};
|
||||
let running = Arc::new(AtomicBool::new(true));
|
||||
let r = running.clone();
|
||||
let mut uid = "".to_owned();
|
||||
let mut server: Option<std::process::Child> = None;
|
||||
if let Err(err) = ctrlc::set_handler(move || {
|
||||
r.store(false, Ordering::SeqCst);
|
||||
}) {
|
||||
println!("Failed to set Ctrl-C handler: {}", err);
|
||||
}
|
||||
while running.load(Ordering::SeqCst) {
|
||||
let tmp = get_active_userid();
|
||||
let mut start_new = false;
|
||||
if tmp != uid && !tmp.is_empty() {
|
||||
uid = tmp;
|
||||
log::info!("active uid: {}", uid);
|
||||
if let Some(ps) = server.as_mut() {
|
||||
hbb_common::allow_err!(ps.kill());
|
||||
}
|
||||
}
|
||||
if let Some(ps) = server.as_mut() {
|
||||
match ps.try_wait() {
|
||||
Ok(Some(_)) => {
|
||||
server = None;
|
||||
start_new = true;
|
||||
}
|
||||
_ => {}
|
||||
}
|
||||
} else {
|
||||
start_new = true;
|
||||
}
|
||||
if start_new {
|
||||
match run_as_user("--server") {
|
||||
Ok(Some(ps)) => server = Some(ps),
|
||||
Err(err) => {
|
||||
log::error!("Failed to start server: {}", err);
|
||||
}
|
||||
_ => { /*no hapen*/ }
|
||||
}
|
||||
}
|
||||
std::thread::sleep(std::time::Duration::from_millis(super::SERVICE_INTERVAL));
|
||||
}
|
||||
|
||||
if let Some(ps) = server.take().as_mut() {
|
||||
hbb_common::allow_err!(ps.kill());
|
||||
}
|
||||
log::info!("Exit");
|
||||
*/
|
||||
}
|
||||
|
||||
pub fn toggle_blank_screen(_v: bool) {
|
||||
@@ -407,13 +421,11 @@ pub fn block_input(_v: bool) -> bool {
|
||||
}
|
||||
|
||||
pub fn is_installed() -> bool {
|
||||
true
|
||||
}
|
||||
|
||||
pub fn start_daemon() {
|
||||
log::info!("{}", crate::username());
|
||||
if let Err(err) = crate::ipc::start("_daemon") {
|
||||
log::error!("Failed to start ipc_daemon: {}", err);
|
||||
std::process::exit(-1);
|
||||
if let Ok(p) = std::env::current_exe() {
|
||||
return p.to_str().unwrap_or_default().contains(&format!(
|
||||
"/Applications/{}.app",
|
||||
hbb_common::config::APP_NAME
|
||||
));
|
||||
}
|
||||
false
|
||||
}
|
||||
|
||||
@@ -3,10 +3,11 @@
|
||||
<plist version="1.0">
|
||||
<dict>
|
||||
<key>Label</key>
|
||||
<string>com.carriez.rustdesk.agent.root</string>
|
||||
<string>com.carriez.RustDesk_server</string>
|
||||
<key>LimitLoadToSessionType</key>
|
||||
<array>
|
||||
<string>LoginWindow</string>
|
||||
<string>LoginWindow</string>
|
||||
<string>Aqua</string>
|
||||
</array>
|
||||
<key>KeepAlive</key>
|
||||
<dict>
|
||||
@@ -25,4 +26,4 @@
|
||||
<key>WorkingDirectory</key>
|
||||
<string>/Applications/RustDesk.app/Contents/MacOS/</string>
|
||||
</dict>
|
||||
</plist>
|
||||
</plist>
|
||||
@@ -3,17 +3,17 @@
|
||||
<plist version="1.0">
|
||||
<dict>
|
||||
<key>Label</key>
|
||||
<string>com.carriez.rustdesk.daemon</string>
|
||||
<string>com.carriez.RustDesk_service</string>
|
||||
<key>KeepAlive</key>
|
||||
<true/>
|
||||
<key>ProgramArguments</key>
|
||||
<array>
|
||||
<string>/Applications/RustDesk.app/Contents/MacOS/rustdesk</string>
|
||||
<string>--daemon</string>
|
||||
<string>--service</string>
|
||||
</array>
|
||||
<key>RunAtLoad</key>
|
||||
<true/>
|
||||
<key>WorkingDirectory</key>
|
||||
<string>/Applications/RustDesk.app/Contents/MacOS/</string>
|
||||
</dict>
|
||||
</plist>
|
||||
</plist>
|
||||
@@ -1,28 +0,0 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
|
||||
<plist version="1.0">
|
||||
<dict>
|
||||
<key>Label</key>
|
||||
<string>com.carriez.rustdesk.agent.user</string>
|
||||
<key>LimitLoadToSessionType</key>
|
||||
<array>
|
||||
<string>Aqua</string>
|
||||
</array>
|
||||
<key>KeepAlive</key>
|
||||
<dict>
|
||||
<key>SuccessfulExit</key>
|
||||
<false />
|
||||
<key>AfterInitialDemand</key>
|
||||
<false />
|
||||
</dict>
|
||||
<key>RunAtLoad</key>
|
||||
<true />
|
||||
<key>ProgramArguments</key>
|
||||
<array>
|
||||
<string>/Applications/RustDesk.app/Contents/MacOS/rustdesk</string>
|
||||
<string>--server</string>
|
||||
</array>
|
||||
<key>WorkingDirectory</key>
|
||||
<string>/Applications/RustDesk.app/Contents/MacOS/</string>
|
||||
</dict>
|
||||
</plist>
|
||||
@@ -1,19 +1,19 @@
|
||||
on run {daemon_file, root_agent_file, user_agent_file}
|
||||
on run {daemon_file, agent_file, user}
|
||||
|
||||
set sh1 to "echo " & quoted form of daemon_file & " > /Library/LaunchDaemons/com.carriez.rustdesk.daemon.plist && chown root:wheel /Library/LaunchDaemons/com.carriez.rustdesk.daemon.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 root_agent_file & " > /Library/LaunchAgents/com.carriez.rustdesk.agent.root.plist && chown root:wheel /Library/LaunchAgents/com.carriez.rustdesk.agent.root.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 "echo " & quoted form of user_agent_file & " > /Library/LaunchAgents/com.carriez.rustdesk.agent.user.plist && chown root:wheel /Library/LaunchAgents/com.carriez.rustdesk.agent.user.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 "launchctl load -w /Library/LaunchDaemons/com.carriez.rustdesk.daemon.plist;"
|
||||
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 load -w /Library/LaunchAgents/com.carriez.rustdesk.agent.root.plist;"
|
||||
|
||||
set sh6 to "launchctl load -w /Library/LaunchAgents/com.carriez.rustdesk.agent.user.plist;"
|
||||
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 sh to sh1 & sh2 & sh3 & sh4 & sh5
|
||||
|
||||
do shell script sh with prompt "RustDesk want to install services" with administrator privileges
|
||||
do shell script sh with prompt "RustDesk want to install daemon and agent" with administrator privileges
|
||||
do shell script sh6
|
||||
end run
|
||||
end run
|
||||
|
||||
@@ -1,7 +0,0 @@
|
||||
set sh1 to "launchctl load -w /Library/LaunchAgents/com.carriez.rustdesk.agent.root.plist;"
|
||||
|
||||
set sh2 to "launchctl load -w /Library/LaunchAgents/com.carriez.rustdesk.agent.user.plist;"
|
||||
|
||||
do shell script sh1 with prompt "RustDesk want to launch services" with administrator privileges
|
||||
|
||||
do shell script sh2
|
||||
6
src/platform/privileges_scripts/load.scpt
Normal file
6
src/platform/privileges_scripts/load.scpt
Normal file
@@ -0,0 +1,6 @@
|
||||
set sh1 to "launchctl load -w /Library/LaunchDaemons/com.carriez.rustdesk_service.plist;"
|
||||
set sh2 to "launchctl load -w /Library/LaunchAgents/com.carriez.rustdesk_server.plist;"
|
||||
|
||||
do shell script sh1 with prompt "RustDesk want to launch daemon" with administrator privileges
|
||||
do shell script sh2
|
||||
|
||||
@@ -1,7 +0,0 @@
|
||||
set sh1 to "launchctl unload -w /Library/LaunchAgents/com.carriez.rustdesk.agent.root.plist;"
|
||||
|
||||
set sh2 to "launchctl unload -w /Library/LaunchAgents/com.carriez.rustdesk.agent.user.plist;"
|
||||
|
||||
do shell script sh1 with prompt "RustDesk want to stop services" with administrator privileges
|
||||
|
||||
do shell script sh2
|
||||
6
src/platform/privileges_scripts/unload.scpt
Normal file
6
src/platform/privileges_scripts/unload.scpt
Normal file
@@ -0,0 +1,6 @@
|
||||
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
|
||||
|
||||
Reference in New Issue
Block a user