feat: kill clash by pid

This commit is contained in:
GyDi 2022-09-05 16:30:29 +08:00 committed by GitHub
parent ff2c1bf8ed
commit fcee41f00d
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 109 additions and 2 deletions

74
src-tauri/Cargo.lock generated
View File

@ -511,6 +511,7 @@ dependencies = [
"serde", "serde",
"serde_json", "serde_json",
"serde_yaml", "serde_yaml",
"sysinfo",
"tauri", "tauri",
"tauri-build", "tauri-build",
"tokio", "tokio",
@ -674,6 +675,31 @@ dependencies = [
"crossbeam-utils", "crossbeam-utils",
] ]
[[package]]
name = "crossbeam-deque"
version = "0.8.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "715e8152b692bba2d374b53d4875445368fdf21a94751410af607a5ac677d1fc"
dependencies = [
"cfg-if 1.0.0",
"crossbeam-epoch",
"crossbeam-utils",
]
[[package]]
name = "crossbeam-epoch"
version = "0.9.10"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "045ebe27666471bb549370b4b0b3e51b07f56325befa4284db65fc89c02511b1"
dependencies = [
"autocfg",
"cfg-if 1.0.0",
"crossbeam-utils",
"memoffset",
"once_cell",
"scopeguard",
]
[[package]] [[package]]
name = "crossbeam-utils" name = "crossbeam-utils"
version = "0.8.10" version = "0.8.10"
@ -2244,6 +2270,15 @@ dependencies = [
"minimal-lexical", "minimal-lexical",
] ]
[[package]]
name = "ntapi"
version = "0.3.7"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "c28774a7fd2fbb4f0babd8237ce554b73af68021b5f695a3cebd6c59bac0980f"
dependencies = [
"winapi",
]
[[package]] [[package]]
name = "num-derive" name = "num-derive"
version = "0.3.3" version = "0.3.3"
@ -2955,6 +2990,30 @@ dependencies = [
"cty", "cty",
] ]
[[package]]
name = "rayon"
version = "1.5.3"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "bd99e5772ead8baa5215278c9b15bf92087709e9c1b2d1f97cdb5a183c933a7d"
dependencies = [
"autocfg",
"crossbeam-deque",
"either",
"rayon-core",
]
[[package]]
name = "rayon-core"
version = "1.9.3"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "258bcdb5ac6dad48491bb2992db6b7cf74878b0384908af124823d118c99683f"
dependencies = [
"crossbeam-channel",
"crossbeam-deque",
"crossbeam-utils",
"num_cpus",
]
[[package]] [[package]]
name = "redox_syscall" name = "redox_syscall"
version = "0.2.13" version = "0.2.13"
@ -3650,6 +3709,21 @@ dependencies = [
"unicode-ident", "unicode-ident",
] ]
[[package]]
name = "sysinfo"
version = "0.26.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "4ae2421f3e16b3afd4aa692d23b83d0ba42ee9b0081d5deeb7d21428d7195fb1"
dependencies = [
"cfg-if 1.0.0",
"core-foundation-sys",
"libc",
"ntapi",
"once_cell",
"rayon",
"winapi",
]
[[package]] [[package]]
name = "system-deps" name = "system-deps"
version = "5.0.0" version = "5.0.0"

View File

@ -23,6 +23,7 @@ dunce = "1.0.2"
log4rs = "1.0.0" log4rs = "1.0.0"
nanoid = "0.4.0" nanoid = "0.4.0"
chrono = "0.4.19" chrono = "0.4.19"
sysinfo = "0.26.2"
serde_json = "1.0" serde_json = "1.0"
serde_yaml = "0.8" serde_yaml = "0.8"
auto-launch = "0.3" auto-launch = "0.3"

View File

@ -53,6 +53,9 @@ impl Core {
/// initialize the core state /// initialize the core state
pub fn init(&self, app_handle: tauri::AppHandle) { pub fn init(&self, app_handle: tauri::AppHandle) {
// kill old clash process
Service::kill_old_clash();
let verge = self.verge.lock(); let verge = self.verge.lock();
let clash_core = verge.clash_core.clone(); let clash_core = verge.clash_core.clone();

View File

@ -4,6 +4,8 @@ use crate::utils::{config, dirs};
use anyhow::{bail, Result}; use anyhow::{bail, Result};
use reqwest::header::HeaderMap; use reqwest::header::HeaderMap;
use serde_yaml::Mapping; use serde_yaml::Mapping;
use std::fs;
use std::io::Write;
use std::{collections::HashMap, time::Duration}; use std::{collections::HashMap, time::Duration};
use tauri::api::process::{Command, CommandChild, CommandEvent}; use tauri::api::process::{Command, CommandChild, CommandEvent};
use tokio::time::sleep; use tokio::time::sleep;
@ -99,6 +101,14 @@ impl Service {
let cmd = Command::new_sidecar(clash_core)?; let cmd = Command::new_sidecar(clash_core)?;
let (mut rx, cmd_child) = cmd.args(["-d", app_dir]).spawn()?; let (mut rx, cmd_child) = cmd.args(["-d", app_dir]).spawn()?;
// 将pid写入文件中
let pid = cmd_child.pid();
log_if_err!(|| -> Result<()> {
let path = dirs::clash_pid_path();
fs::File::create(path)?.write(format!("{pid}").as_bytes())?;
Ok(())
}());
self.sidecar = Some(cmd_child); self.sidecar = Some(cmd_child);
// clash log // clash log
@ -217,6 +227,23 @@ impl Service {
Ok((server, headers)) Ok((server, headers))
} }
/// kill old clash process
pub fn kill_old_clash() {
use sysinfo::{Pid, PidExt, ProcessExt, System, SystemExt};
if let Ok(pid) = fs::read(dirs::clash_pid_path()) {
if let Ok(pid) = String::from_utf8_lossy(&pid).parse() {
let mut system = System::new();
system.refresh_all();
let proc = system.process(Pid::from_u32(pid));
if let Some(proc) = proc {
proc.kill();
}
}
}
}
} }
impl Drop for Service { impl Drop for Service {

View File

@ -15,7 +15,6 @@ static VERGE_CONFIG: &str = "verge.yaml";
static PROFILE_YAML: &str = "profiles.yaml"; static PROFILE_YAML: &str = "profiles.yaml";
static PROFILE_TEMP: &str = "clash-verge-runtime.yaml"; static PROFILE_TEMP: &str = "clash-verge-runtime.yaml";
#[cfg(windows)]
static mut RESOURCE_DIR: Option<PathBuf> = None; static mut RESOURCE_DIR: Option<PathBuf> = None;
/// portable flag /// portable flag
@ -65,7 +64,6 @@ pub fn app_resources_dir(package_info: &PackageInfo) -> PathBuf {
.unwrap() .unwrap()
.join("resources"); .join("resources");
#[cfg(windows)]
unsafe { unsafe {
RESOURCE_DIR = Some(res_dir.clone()); RESOURCE_DIR = Some(res_dir.clone());
} }
@ -103,6 +101,10 @@ pub fn profiles_temp_path() -> PathBuf {
return app_home_dir().join(PROFILE_TEMP); return app_home_dir().join(PROFILE_TEMP);
} }
pub fn clash_pid_path() -> PathBuf {
unsafe { RESOURCE_DIR.clone().unwrap().join("clash.pid") }
}
#[cfg(windows)] #[cfg(windows)]
static SERVICE_PATH: &str = "clash-verge-service.exe"; static SERVICE_PATH: &str = "clash-verge-service.exe";