Files
clash-proxy/src-tauri/src/core/service.rs

300 lines
8.0 KiB
Rust
Raw Normal View History

2022-11-17 20:19:40 +08:00
use crate::config::Config;
use crate::utils::dirs;
use anyhow::{bail, Context, Result};
use serde::{Deserialize, Serialize};
use std::collections::HashMap;
2022-11-18 20:15:34 +08:00
use std::path::PathBuf;
2022-11-17 20:19:40 +08:00
use std::time::Duration;
use std::{env::current_exe, process::Command as StdCommand};
use tokio::time::sleep;
2024-03-31 16:16:23 +08:00
// Windows only
2022-11-17 20:19:40 +08:00
const SERVICE_URL: &str = "http://127.0.0.1:33211";
#[derive(Debug, Deserialize, Serialize, Clone)]
pub struct ResponseBody {
pub core_type: Option<String>,
pub bin_path: String,
pub config_dir: String,
pub log_file: String,
}
#[derive(Debug, Deserialize, Serialize, Clone)]
pub struct JsonResponse {
pub code: u64,
pub msg: String,
pub data: Option<ResponseBody>,
}
/// Install the Clash Verge Service
/// 该函数应该在协程或者线程中执行避免UAC弹窗阻塞主线程
///
2024-03-31 16:16:23 +08:00
#[cfg(target_os = "windows")]
2022-11-17 20:19:40 +08:00
pub async fn install_service() -> Result<()> {
2024-03-31 16:16:23 +08:00
use deelevate::{PrivilegeLevel, Token};
use runas::Command as RunasCommand;
use std::os::windows::process::CommandExt;
2022-11-18 20:15:34 +08:00
let binary_path = dirs::service_path()?;
2022-11-17 20:19:40 +08:00
let install_path = binary_path.with_file_name("install-service.exe");
if !install_path.exists() {
bail!("installer exe not found");
}
let token = Token::with_current_process()?;
let level = token.privilege_level()?;
let status = match level {
2022-11-22 22:01:34 +08:00
PrivilegeLevel::NotPrivileged => RunasCommand::new(install_path).show(false).status()?,
2022-11-17 20:19:40 +08:00
_ => StdCommand::new(install_path)
.creation_flags(0x08000000)
.status()?,
};
if !status.success() {
bail!(
"failed to install service with status {}",
status.code().unwrap()
);
}
Ok(())
}
2024-03-31 16:16:23 +08:00
#[cfg(target_os = "linux")]
pub async fn install_service() -> Result<()> {
use users::get_effective_uid;
let binary_path = dirs::service_path()?;
let installer_path = binary_path.with_file_name("install-service");
if !installer_path.exists() {
bail!("installer not found");
}
let elevator = crate::utils::unix_helper::linux_elevator();
let status = match get_effective_uid() {
0 => StdCommand::new(installer_path).status()?,
_ => StdCommand::new(elevator)
.arg("sh")
.arg("-c")
.arg(installer_path)
.status()?,
2024-03-31 16:16:23 +08:00
};
if !status.success() {
bail!(
"failed to install service with status {}",
status.code().unwrap()
);
}
Ok(())
}
#[cfg(target_os = "macos")]
pub async fn install_service() -> Result<()> {
let binary_path = dirs::service_path()?;
let installer_path = binary_path.with_file_name("install-service");
if !installer_path.exists() {
bail!("installer not found");
}
2024-04-30 20:20:39 +08:00
let shell = installer_path.to_string_lossy().replace(" ", "\\\\ ");
let command = format!(r#"do shell script "{shell}" with administrator privileges"#);
let status = StdCommand::new("osascript")
.args(vec!["-e", &command])
.status()?;
if !status.success() {
bail!(
"failed to install service with status {}",
status.code().unwrap()
);
}
Ok(())
}
2022-11-17 20:19:40 +08:00
/// Uninstall the Clash Verge Service
/// 该函数应该在协程或者线程中执行避免UAC弹窗阻塞主线程
2024-03-31 16:16:23 +08:00
#[cfg(target_os = "windows")]
2022-11-17 20:19:40 +08:00
pub async fn uninstall_service() -> Result<()> {
2024-03-31 16:16:23 +08:00
use deelevate::{PrivilegeLevel, Token};
use runas::Command as RunasCommand;
use std::os::windows::process::CommandExt;
2022-11-18 20:15:34 +08:00
let binary_path = dirs::service_path()?;
2022-11-17 20:19:40 +08:00
let uninstall_path = binary_path.with_file_name("uninstall-service.exe");
if !uninstall_path.exists() {
bail!("uninstaller exe not found");
}
let token = Token::with_current_process()?;
let level = token.privilege_level()?;
let status = match level {
2022-11-22 22:01:34 +08:00
PrivilegeLevel::NotPrivileged => RunasCommand::new(uninstall_path).show(false).status()?,
2022-11-17 20:19:40 +08:00
_ => StdCommand::new(uninstall_path)
.creation_flags(0x08000000)
.status()?,
};
if !status.success() {
bail!(
"failed to uninstall service with status {}",
status.code().unwrap()
);
}
Ok(())
}
2024-03-31 16:16:23 +08:00
#[cfg(target_os = "linux")]
pub async fn uninstall_service() -> Result<()> {
use users::get_effective_uid;
let binary_path = dirs::service_path()?;
let uninstaller_path = binary_path.with_file_name("uninstall-service");
if !uninstaller_path.exists() {
bail!("uninstaller not found");
}
let elevator = crate::utils::unix_helper::linux_elevator();
let status = match get_effective_uid() {
0 => StdCommand::new(uninstaller_path).status()?,
_ => StdCommand::new(elevator)
.arg("sh")
.arg("-c")
.arg(uninstaller_path)
.status()?,
2024-03-31 16:16:23 +08:00
};
if !status.success() {
bail!(
"failed to install service with status {}",
status.code().unwrap()
);
}
Ok(())
}
#[cfg(target_os = "macos")]
pub async fn uninstall_service() -> Result<()> {
let binary_path = dirs::service_path()?;
let uninstaller_path = binary_path.with_file_name("uninstall-service");
if !uninstaller_path.exists() {
bail!("uninstaller not found");
}
2024-04-13 15:12:41 +08:00
let shell = uninstaller_path.to_string_lossy().replace(" ", "\\\\ ");
let command = format!(r#"do shell script "{shell}" with administrator privileges"#);
let status = StdCommand::new("osascript")
.args(vec!["-e", &command])
.status()?;
if !status.success() {
bail!(
"failed to install service with status {}",
status.code().unwrap()
);
}
Ok(())
}
2022-11-17 20:19:40 +08:00
/// check the windows service status
pub async fn check_service() -> Result<JsonResponse> {
let url = format!("{SERVICE_URL}/get_clash");
let response = reqwest::ClientBuilder::new()
.no_proxy()
.build()?
.get(url)
.send()
2022-11-18 22:08:06 +08:00
.await
.context("failed to connect to the Clash Verge Service")?
2022-11-17 20:19:40 +08:00
.json::<JsonResponse>()
.await
2022-11-18 22:08:06 +08:00
.context("failed to parse the Clash Verge Service response")?;
2022-11-17 20:19:40 +08:00
Ok(response)
}
/// start the clash by service
2022-11-18 20:15:34 +08:00
pub(super) async fn run_core_by_service(config_file: &PathBuf) -> Result<()> {
2022-11-17 20:19:40 +08:00
let status = check_service().await?;
if status.code == 0 {
stop_core_by_service().await?;
sleep(Duration::from_secs(1)).await;
}
let clash_core = { Config::verge().latest().clash_core.clone() };
let clash_core = clash_core.unwrap_or("clash".into());
let bin_ext = if cfg!(windows) { ".exe" } else { "" };
2024-03-31 16:16:23 +08:00
let clash_bin = format!("{clash_core}{bin_ext}");
2022-11-17 20:19:40 +08:00
let bin_path = current_exe()?.with_file_name(clash_bin);
let bin_path = dirs::path_to_str(&bin_path)?;
2022-11-18 20:15:34 +08:00
let config_dir = dirs::app_home_dir()?;
2022-11-17 20:19:40 +08:00
let config_dir = dirs::path_to_str(&config_dir)?;
2022-11-18 20:15:34 +08:00
let log_path = dirs::service_log_file()?;
2022-11-17 20:19:40 +08:00
let log_path = dirs::path_to_str(&log_path)?;
2022-11-18 20:15:34 +08:00
let config_file = dirs::path_to_str(config_file)?;
2022-11-17 20:19:40 +08:00
let mut map = HashMap::new();
map.insert("core_type", clash_core.as_str());
map.insert("bin_path", bin_path);
map.insert("config_dir", config_dir);
2022-11-18 20:15:34 +08:00
map.insert("config_file", config_file);
2022-11-17 20:19:40 +08:00
map.insert("log_file", log_path);
let url = format!("{SERVICE_URL}/start_clash");
let res = reqwest::ClientBuilder::new()
.no_proxy()
.build()?
.post(url)
.json(&map)
.send()
.await?
.json::<JsonResponse>()
.await
.context("failed to connect to the Clash Verge Service")?;
if res.code != 0 {
bail!(res.msg);
}
Ok(())
}
/// stop the clash by service
pub(super) async fn stop_core_by_service() -> Result<()> {
let url = format!("{SERVICE_URL}/stop_clash");
let res = reqwest::ClientBuilder::new()
.no_proxy()
.build()?
.post(url)
.send()
.await?
.json::<JsonResponse>()
.await
.context("failed to connect to the Clash Verge Service")?;
if res.code != 0 {
bail!(res.msg);
}
Ok(())
}