chore: try to fix service not started on linux

This commit is contained in:
huzibaca
2024-10-18 22:30:29 +08:00
parent a013fe663c
commit 8389826e30
12 changed files with 396 additions and 54 deletions

View File

@@ -83,7 +83,7 @@ impl CoreManager {
// 服务模式
if service::check_service().await.is_ok() {
log::debug!(target: "app", "stop the core by service");
log::info!(target: "app", "stop the core by service");
service::stop_core_by_service().await?;
}
*running = false;
@@ -94,7 +94,7 @@ impl CoreManager {
pub async fn start_core(&self) -> Result<()> {
let mut running = self.running.lock().await;
if *running {
log::debug!("core is running");
log::info!("core is running");
return Ok(());
}
@@ -102,7 +102,7 @@ impl CoreManager {
// 服务模式
if service::check_service().await.is_ok() {
log::debug!(target: "app", "try to run core in service mode");
log::info!(target: "app", "try to run core in service mode");
service::run_core_by_service(&config_path).await?;
*running = true;
}
@@ -126,7 +126,7 @@ impl CoreManager {
bail!("invalid clash core name \"{clash_core}\"");
}
log::debug!(target: "app", "change core to `{clash_core}`");
log::info!(target: "app", "change core to `{clash_core}`");
Config::verge().draft().clash_core = Some(clash_core);

View File

@@ -161,7 +161,7 @@ impl Drop for Hotkey {
fn drop(&mut self) {
let app_handle = handle::Handle::global().app_handle().unwrap();
if let Err(e) = app_handle.global_shortcut().unregister_all() {
log::error!("Error unregistering all hotkeys: {:?}", e);
log::error!(target:"app", "Error unregistering all hotkeys: {:?}", e);
}
}
}

View File

@@ -28,6 +28,8 @@ pub struct JsonResponse {
#[cfg(target_os = "windows")]
pub async fn reinstall_service() -> Result<()> {
log::info!(target:"app", "reinstall service");
use deelevate::{PrivilegeLevel, Token};
use runas::Command as RunasCommand;
use std::os::windows::process::CommandExt;
@@ -37,11 +39,11 @@ pub async fn reinstall_service() -> Result<()> {
let uninstall_path = binary_path.with_file_name("uninstall-service.exe");
if !install_path.exists() {
bail!("installer exe not found");
bail!(format!("installer not found: {install_path:?}"));
}
if !uninstall_path.exists() {
bail!("uninstaller exe not found");
bail!(format!("uninstaller not found: {uninstall_path:?}"));
}
let token = Token::with_current_process()?;
@@ -72,35 +74,39 @@ pub async fn reinstall_service() -> Result<()> {
#[cfg(target_os = "linux")]
pub async fn reinstall_service() -> Result<()> {
log::info!(target:"app", "reinstall service");
use users::get_effective_uid;
let binary_path = dirs::service_path()?;
let install_path = binary_path.with_file_name("install-service");
let uninstall_path = binary_path.with_file_name("uninstall-service");
let install_path = tauri::utils::platform::current_exe()?.with_file_name("install-service");
let uninstall_path = tauri::utils::platform::current_exe()?.with_file_name("uninstall-service");
if !install_path.exists() {
bail!("installer not found");
bail!(format!("installer not found: {install_path:?}"));
}
if !uninstall_path.exists() {
bail!("uninstaller not found");
bail!(format!("uninstaller not found: {uninstall_path:?}"));
}
let install_shell: String = install_path.to_string_lossy().replace(" ", "\\ ");
let uninstall_shell: String = uninstall_path.to_string_lossy().replace(" ", "\\ ");
let _ = match get_effective_uid() {
0 => StdCommand::new(uninstall_path).status()?,
_ => StdCommand::new("sudo")
let elevator = crate::utils::help::linux_elevator();
let status = match get_effective_uid() {
0 => StdCommand::new(uninstall_shell).status()?,
_ => StdCommand::new(elevator)
.arg("sh")
.arg("-c")
.arg(uninstall_shell)
.status()?,
};
log::info!(target:"app", "status code:{}", status.code().unwrap());
let elevator = crate::utils::help::linux_elevator();
let status = match get_effective_uid() {
0 => StdCommand::new(install_shell).status()?,
_ => StdCommand::new("sudo")
_ => StdCommand::new(elevator)
.arg("sh")
.arg("-c")
.arg(install_shell)
@@ -119,16 +125,18 @@ pub async fn reinstall_service() -> Result<()> {
#[cfg(target_os = "macos")]
pub async fn reinstall_service() -> Result<()> {
log::info!(target:"app", "reinstall service");
let binary_path = dirs::service_path()?;
let install_path = binary_path.with_file_name("install-service");
let uninstall_path = binary_path.with_file_name("uninstall-service");
if !install_path.exists() {
bail!("installer not found");
bail!(format!("installer not found: {install_path:?}"));
}
if !uninstall_path.exists() {
bail!("uninstaller not found");
bail!(format!("uninstaller not found: {uninstall_path:?}"));
}
let install_shell: String = install_path.to_string_lossy().replace(" ", "\\ ");
@@ -193,6 +201,8 @@ pub(super) async fn run_core_by_service(config_file: &PathBuf) -> Result<()> {
map.insert("config_file", config_file);
map.insert("log_file", log_path);
log::info!(target:"app", "start service: {:?}", map.clone());
let url = format!("{SERVICE_URL}/start_clash");
let _ = reqwest::ClientBuilder::new()
.no_proxy()