mirror of
https://github.com/weyne85/rustdesk.git
synced 2025-10-29 17:00:05 +00:00
refactor linux install/uninstall service as windows
This commit is contained in:
@@ -179,6 +179,12 @@ pub fn core_main() -> Option<Vec<String>> {
|
||||
log::info!("start --server with user {}", crate::username());
|
||||
#[cfg(any(target_os = "linux", target_os = "windows"))]
|
||||
{
|
||||
#[cfg(target_os = "linux")]
|
||||
if crate::platform::is_root() {
|
||||
hbb_common::allow_err!(crate::platform::run_as_user(vec!["--tray"], None));
|
||||
} else {
|
||||
hbb_common::allow_err!(crate::run_me(vec!["--tray"]));
|
||||
}
|
||||
crate::start_server(true);
|
||||
return None;
|
||||
}
|
||||
|
||||
@@ -3,6 +3,7 @@ use desktop::Desktop;
|
||||
pub use hbb_common::platform::linux::*;
|
||||
use hbb_common::{
|
||||
allow_err, bail,
|
||||
config::Config,
|
||||
libc::{c_char, c_int, c_long, c_void},
|
||||
log,
|
||||
message_proto::Resolution,
|
||||
@@ -306,10 +307,15 @@ fn force_stop_server() {
|
||||
}
|
||||
|
||||
pub fn start_os_service() {
|
||||
check_if_stop_service();
|
||||
stop_rustdesk_servers();
|
||||
stop_subprocess();
|
||||
start_uinput_service();
|
||||
|
||||
std::thread::spawn(|| {
|
||||
allow_err!(crate::ipc::start(crate::POSTFIX_SERVICE));
|
||||
});
|
||||
|
||||
let running = Arc::new(AtomicBool::new(true));
|
||||
let r = running.clone();
|
||||
let mut desktop = Desktop::default();
|
||||
@@ -1075,3 +1081,86 @@ impl WakeLock {
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
fn has_cmd(cmd: &str) -> bool {
|
||||
std::process::Command::new("which")
|
||||
.arg(cmd)
|
||||
.status()
|
||||
.map(|x| x.success())
|
||||
.unwrap_or_default()
|
||||
}
|
||||
|
||||
pub fn run_cmds_pkexec(cmds: &str) -> bool {
|
||||
const DONE: &str = "RUN_CMDS_PKEXEC_DONE";
|
||||
if let Ok(output) = std::process::Command::new("pkexec")
|
||||
.arg("sh")
|
||||
.arg("-c")
|
||||
.arg(&format!("{cmds};echo {DONE}"))
|
||||
.output()
|
||||
{
|
||||
let out = String::from_utf8_lossy(&output.stdout);
|
||||
log::debug!("cmds: {cmds}");
|
||||
log::debug!("output: {out}");
|
||||
out.contains(DONE)
|
||||
} else {
|
||||
false
|
||||
}
|
||||
}
|
||||
|
||||
pub fn run_me_with(secs: u32) {
|
||||
let exe = std::env::current_exe()
|
||||
.unwrap_or("".into())
|
||||
.to_string_lossy()
|
||||
.to_string();
|
||||
std::process::Command::new("sh")
|
||||
.arg("-c")
|
||||
.arg(&format!("sleep {secs}; {exe}"))
|
||||
.spawn()
|
||||
.ok();
|
||||
}
|
||||
|
||||
pub fn uninstall_service(show_new_window: bool) -> bool {
|
||||
if !has_cmd("systemctl") {
|
||||
return false;
|
||||
}
|
||||
log::info!("Uninstalling service...");
|
||||
Config::set_option("stop-service".into(), "Y".into());
|
||||
if !run_cmds_pkexec("systemctl disable rustdesk; systemctl stop rustdesk") {
|
||||
Config::set_option("stop-service".into(), "".into());
|
||||
return true;
|
||||
}
|
||||
if show_new_window {
|
||||
run_me_with(2);
|
||||
}
|
||||
std::process::exit(0);
|
||||
}
|
||||
|
||||
pub fn install_service() -> bool {
|
||||
if !has_cmd("systemctl") {
|
||||
return false;
|
||||
}
|
||||
log::info!("Installing service...");
|
||||
let home = std::env::var("HOME").unwrap_or_default();
|
||||
let cp = if home != "/root" {
|
||||
format!("cp -f {home}/.config/rustdesk/RustDesk.toml /root/.config/rustdesk/; cp -f {home}/.config/rustdesk/RustDesk2.toml /root/.config/rustdesk/;")
|
||||
} else {
|
||||
"".to_owned()
|
||||
};
|
||||
Config::set_option("stop-service".into(), "".into());
|
||||
if !run_cmds_pkexec(&format!(
|
||||
"{cp}systemctl enable rustdesk; systemctl start rustdesk"
|
||||
)) {
|
||||
Config::set_option("stop-service".into(), "Y".into());
|
||||
return true;
|
||||
}
|
||||
run_me_with(2);
|
||||
std::process::exit(0);
|
||||
}
|
||||
|
||||
fn check_if_stop_service() {
|
||||
if Config::get_option("stop-service".into()) == "Y" {
|
||||
allow_err!(run_cmds(
|
||||
"systemctl disable rustdesk; systemctl stop rustdesk"
|
||||
));
|
||||
}
|
||||
}
|
||||
|
||||
@@ -2151,7 +2151,7 @@ impl Drop for WakeLock {
|
||||
}
|
||||
}
|
||||
|
||||
pub fn uninstall_service(show_new_window: bool) -> ResultType<()> {
|
||||
pub fn uninstall_service(show_new_window: bool) -> bool {
|
||||
log::info!("Uninstalling service...");
|
||||
let filter = format!(" /FI \"PID ne {}\"", get_current_pid());
|
||||
Config::set_option("stop-service".into(), "Y".into());
|
||||
@@ -2167,13 +2167,14 @@ pub fn uninstall_service(show_new_window: bool) -> ResultType<()> {
|
||||
);
|
||||
if let Err(err) = run_cmds(cmds, false, "uninstall") {
|
||||
Config::set_option("stop-service".into(), "".into());
|
||||
bail!(err);
|
||||
log::debug!("{err}");
|
||||
return true;
|
||||
}
|
||||
run_after_run_cmds(!show_new_window);
|
||||
std::process::exit(0);
|
||||
}
|
||||
|
||||
pub fn install_service() -> ResultType<()> {
|
||||
pub fn install_service() -> bool {
|
||||
log::info!("Installing service...");
|
||||
let (_, _, _, exe) = get_install_info();
|
||||
let tmp_path = std::env::temp_dir().to_string_lossy().to_string();
|
||||
@@ -2198,7 +2199,8 @@ if exist \"{tray_shortcut}\" del /f /q \"{tray_shortcut}\"
|
||||
if let Err(err) = run_cmds(cmds, false, "install") {
|
||||
Config::set_option("stop-service".into(), "Y".into());
|
||||
crate::ipc::EXIT_RECV_CLOSE.store(true, Ordering::Relaxed);
|
||||
bail!(err);
|
||||
log::debug!("{err}");
|
||||
return true;
|
||||
}
|
||||
run_after_run_cmds(false);
|
||||
std::process::exit(0);
|
||||
|
||||
@@ -388,7 +388,7 @@ pub async fn start_server(is_server: bool) {
|
||||
if crate::platform::current_is_wayland() {
|
||||
allow_err!(input_service::setup_uinput(0, 1920, 0, 1080).await);
|
||||
}
|
||||
#[cfg(target_os = "macos")]
|
||||
#[cfg(any(target_os = "macos", target_os = "linux"))]
|
||||
tokio::spawn(async { sync_and_watch_config_dir().await });
|
||||
crate::RendezvousMediator::start_all().await;
|
||||
} else {
|
||||
@@ -460,7 +460,7 @@ pub async fn start_ipc_url_server() {
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(target_os = "macos")]
|
||||
#[cfg(any(target_os = "macos", target_os = "linux"))]
|
||||
async fn sync_and_watch_config_dir() {
|
||||
if crate::platform::is_root() {
|
||||
return;
|
||||
|
||||
24
src/tray.rs
24
src/tray.rs
@@ -1,24 +1,15 @@
|
||||
/// Start a tray icon in Linux
|
||||
///
|
||||
/// [Block]
|
||||
/// This function will block current execution, show the tray icon and handle events.
|
||||
#[cfg(target_os = "linux")]
|
||||
pub fn start_tray() {}
|
||||
|
||||
#[cfg(any(target_os = "macos", target_os = "windows"))]
|
||||
pub fn start_tray() {
|
||||
use hbb_common::{allow_err, log};
|
||||
allow_err!(make_tray());
|
||||
}
|
||||
|
||||
#[cfg(any(target_os = "macos", target_os = "windows"))]
|
||||
pub fn make_tray() -> hbb_common::ResultType<()> {
|
||||
// https://github.com/tauri-apps/tray-icon/blob/dev/examples/tao.rs
|
||||
use hbb_common::anyhow::Context;
|
||||
use tao::event_loop::{ControlFlow, EventLoopBuilder};
|
||||
use tray_icon::{
|
||||
menu::{Menu, MenuEvent, MenuItem},
|
||||
ClickEvent, TrayEvent, TrayIconBuilder,
|
||||
TrayEvent, TrayIconBuilder,
|
||||
};
|
||||
let icon;
|
||||
#[cfg(target_os = "macos")]
|
||||
@@ -31,7 +22,7 @@ pub fn make_tray() -> hbb_common::ResultType<()> {
|
||||
_ => DARK,
|
||||
};
|
||||
}
|
||||
#[cfg(target_os = "windows")]
|
||||
#[cfg(not(target_os = "macos"))]
|
||||
{
|
||||
icon = include_bytes!("../res/tray-icon.ico");
|
||||
}
|
||||
@@ -83,6 +74,9 @@ pub fn make_tray() -> hbb_common::ResultType<()> {
|
||||
.spawn()
|
||||
.ok();
|
||||
}
|
||||
// xdg-open?
|
||||
#[cfg(target_os = "linux")]
|
||||
crate::run_me::<&str>(vec![]).ok();
|
||||
};
|
||||
|
||||
event_loop.run(move |_event, _, control_flow| {
|
||||
@@ -99,16 +93,16 @@ pub fn make_tray() -> hbb_common::ResultType<()> {
|
||||
if event.id == quit_i.id() {
|
||||
#[cfg(target_os = "macos")]
|
||||
crate::platform::macos::uninstall(false);
|
||||
#[cfg(any(target_os = "linux", target_os = "windows"))]
|
||||
crate::platform::uninstall_service(false).ok();
|
||||
#[cfg(not(target_os = "macos"))]
|
||||
crate::platform::uninstall_service(false);
|
||||
} else if event.id == open_i.id() {
|
||||
open_func();
|
||||
}
|
||||
}
|
||||
|
||||
if let Ok(event) = tray_channel.try_recv() {
|
||||
if let Ok(_event) = tray_channel.try_recv() {
|
||||
#[cfg(target_os = "windows")]
|
||||
if event.event == ClickEvent::Left {
|
||||
if _event.event == tray_icon::ClickEvent::Left {
|
||||
open_func();
|
||||
}
|
||||
}
|
||||
|
||||
@@ -278,13 +278,17 @@ pub fn set_option(key: String, value: String) {
|
||||
return;
|
||||
}
|
||||
}
|
||||
#[cfg(any(target_os = "windows"))]
|
||||
#[cfg(any(target_os = "windows", target_os = "linux"))]
|
||||
{
|
||||
if crate::platform::is_installed() {
|
||||
if value == "Y" {
|
||||
allow_err!(crate::platform::uninstall_service(true));
|
||||
if crate::platform::uninstall_service(true) {
|
||||
return;
|
||||
}
|
||||
} else {
|
||||
allow_err!(crate::platform::install_service());
|
||||
if crate::platform::install_service() {
|
||||
return;
|
||||
}
|
||||
}
|
||||
return;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user