Skip to content

Commit

Permalink
Merge pull request #5 from Tunglies/fix-macos-mihomo-kill
Browse files Browse the repository at this point in the history
fix: macos can stop service and kill mihomo now
  • Loading branch information
wonfen authored Feb 14, 2025
2 parents a5e4455 + f17f8e9 commit 72676cc
Showing 1 changed file with 21 additions and 2 deletions.
23 changes: 21 additions & 2 deletions src/service/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,12 @@ use windows_service::{
service_control_handler::{self, ServiceControlHandlerResult},
service_dispatcher, Result,
};
#[cfg(target_os = "macos")]
use clash_verge_service::utils;

#[cfg(windows)]
const SERVICE_TYPE: ServiceType = ServiceType::OWN_PROCESS;
#[cfg(not(target_os = "macos"))]
const SERVICE_NAME: &str = "clash_verge_service";
const LISTEN_PORT: u16 = 33211;

Expand Down Expand Up @@ -101,7 +104,7 @@ pub async fn run_service() -> anyhow::Result<()> {
}

// 停止服务
#[cfg(windows)]
#[cfg(target_os = "windows")]
fn stop_service() -> Result<()> {
let status_handle =
service_control_handler::register(SERVICE_NAME, |_| ServiceControlHandlerResult::NoError)?;
Expand All @@ -118,7 +121,7 @@ fn stop_service() -> Result<()> {

Ok(())
}
#[cfg(not(windows))]
#[cfg(target_os = "linux")]
fn stop_service() -> anyhow::Result<()> {
// systemctl stop clash_verge_service
std::process::Command::new("systemctl")
Expand All @@ -128,6 +131,22 @@ fn stop_service() -> anyhow::Result<()> {
.expect("failed to execute process");
Ok(())
}

#[cfg(target_os = "macos")]
fn stop_service() -> anyhow::Result<()> {
// launchctl stop clash_verge_service
let _ = utils::run_command(
"launchctl",
&[
"stop",
"io.github.clash-verge-rev.clash-verge-rev.service"
],
true
);

Ok(())
}

/// Service Main function
#[cfg(windows)]
pub fn main() -> Result<()> {
Expand Down

0 comments on commit 72676cc

Please sign in to comment.