refactor: replace isServiceAvailable with getRunningMode for service status checks

This commit is contained in:
Tunglies
2025-05-26 16:08:16 +08:00
parent 2b89f07fe5
commit 32ee1b36d2
6 changed files with 61 additions and 125 deletions

View File

@@ -2,8 +2,10 @@ use super::CmdResult;
use crate::module::mihomo::MihomoManager;
use once_cell::sync::Lazy;
use parking_lot::Mutex;
use std::sync::atomic::{AtomicBool, Ordering};
use std::time::{Duration, Instant};
use std::{
sync::atomic::{AtomicBool, Ordering},
time::{Duration, Instant},
};
static LAST_REFRESH_TIME: Lazy<Mutex<Option<Instant>>> = Lazy::new(|| Mutex::new(None));
static IS_REFRESHING: AtomicBool = AtomicBool::new(false);

View File

@@ -715,8 +715,8 @@ pub async fn check_service_needs_reinstall() -> bool {
logging!(error, Type::Service, true, "检查服务版本失败: {}", err);
// 检查服务是否可用
match is_service_running().await {
Ok(true) => {
match is_service_available().await {
Ok(()) => {
log::info!(target: "app", "服务正在运行但版本检查失败: {}", err);
/* logging!(
info,
@@ -846,7 +846,7 @@ pub(super) async fn run_core_by_service(config_file: &PathBuf) -> Result<()> {
};
if version_check {
if let Ok(true) = is_service_running().await {
if is_service_available().await.is_ok() {
log::info!(target: "app", "服务已在运行且版本匹配,尝试使用");
return start_with_existing_service(config_file).await;
}
@@ -947,14 +947,14 @@ pub(super) async fn stop_core_by_service() -> Result<()> {
}
/// 检查服务是否正在运行
pub async fn is_service_running() -> Result<bool> {
pub async fn is_service_available() -> Result<()> {
logging!(info, Type::Service, true, "开始检查服务是否正在运行");
match check_ipc_service_status().await {
Ok(resp) => {
if resp.code == 0 && resp.msg == "ok" && resp.data.is_some() {
logging!(info, Type::Service, true, "服务正在运行");
Ok(true)
Ok(())
} else {
logging!(
warn,
@@ -964,74 +964,12 @@ pub async fn is_service_running() -> Result<bool> {
resp.code,
resp.msg
);
Ok(false)
Ok(())
}
}
Err(err) => {
logging!(error, Type::Service, true, "检查服务运行状态失败: {}", err);
/*
let error_type = err.root_cause().to_string();
logging!(
error,
Type::Service,
true,
"连接失败的根本原因: {}",
error_type
);
let socket_path = if cfg!(windows) {
r"\\.\pipe\clash-verge-service"
} else {
"/tmp/clash-verge-service.sock"
};
if cfg!(windows) {
logging!(
info,
Type::Service,
true,
"检查Windows命名管道: {}",
socket_path
);
} else {
let socket_exists = std::path::Path::new(socket_path).exists();
logging!(
info,
Type::Service,
true,
"检查Unix套接字文件: {} 是否存在: {}",
socket_path,
socket_exists
);
}
*/
Ok(false)
}
}
}
pub async fn is_service_available() -> Result<()> {
logging!(info, Type::Service, true, "开始检查服务是否可用");
match check_ipc_service_status().await {
Ok(resp) => {
if resp.code == 0 && resp.msg == "ok" && resp.data.is_some() {
logging!(info, Type::Service, true, "服务可用");
} else {
logging!(
warn,
Type::Service,
true,
"服务返回异常: code={}, msg={}",
resp.code,
resp.msg
);
}
Ok(())
}
Err(err) => {
logging!(error, Type::Service, true, "服务不可用: {}", err);
bail!("服务不可用: {}", err)
Err(err)
}
}
}

View File

@@ -23,11 +23,13 @@ use parking_lot::Mutex;
use parking_lot::RwLock;
#[cfg(target_os = "macos")]
pub use speed_rate::{SpeedRate, Traffic};
use std::fs;
use std::sync::atomic::{AtomicBool, Ordering};
#[cfg(target_os = "macos")]
use std::sync::Arc;
use std::time::{Duration, Instant};
use std::{
fs,
sync::atomic::{AtomicBool, Ordering},
time::{Duration, Instant},
};
use tauri::{
menu::{CheckMenuItem, IsMenuItem, MenuEvent, MenuItem, PredefinedMenuItem, Submenu},
tray::{MouseButton, MouseButtonState, TrayIconEvent},

View File

@@ -20,7 +20,7 @@ import {
import { useVerge } from "@/hooks/use-verge";
import { useSystemState } from "@/hooks/use-system-state";
import { showNotice } from "@/services/noticeService";
import { isServiceAvailable } from "@/services/cmds";
import { getRunningMode } from "@/services/cmds";
import { mutate } from "swr";
const LOCAL_STORAGE_TAB_KEY = "clash-verge-proxy-active-tab";
@@ -150,7 +150,8 @@ export const ProxyTunCard: FC = () => {
const updateLocalStatus = async () => {
try {
const serviceStatus = await isServiceAvailable();
const runningMode = await getRunningMode();
const serviceStatus = runningMode === "Service";
setLocalServiceOk(serviceStatus);
mutate("isServiceAvailable", serviceStatus, false);
} catch (error) {
@@ -165,7 +166,7 @@ export const ProxyTunCard: FC = () => {
const isTunAvailable = localServiceOk || isAdminMode;
const handleError = (err: Error) => {
showNotice('error', err.message || err.toString());
showNotice("error", err.message || err.toString());
};
const handleTabChange = (tab: string) => {

View File

@@ -23,7 +23,6 @@ import {
uninstallService,
restartCore,
stopCore,
isServiceAvailable,
} from "@/services/cmds";
import { useLockFn } from "ahooks";
import { Button, Tooltip } from "@mui/material";
@@ -45,16 +44,10 @@ const SettingSystem = ({ onError }: Props) => {
const { data: sysproxy } = useSWR("getSystemProxy", getSystemProxy);
const { data: autoproxy } = useSWR("getAutotemProxy", getAutotemProxy);
const { data: serviceOk, mutate: mutateServiceAvailable } = useSWR(
"isServiceAvailable",
isServiceAvailable
);
const { isAdminMode, isServiceMode, mutateRunningMode } = useSystemState();
const { isAdminMode, isSidecarMode, mutateRunningMode } = useSystemState();
// +++ isTunAvailable 现在使用 SWR 的 serviceOk
const isTunAvailable = serviceOk || isAdminMode;
// +++ isTunAvailable 现在使用 SWR 的 isServiceMode
const isTunAvailable = isServiceMode || isAdminMode;
const sysproxyRef = useRef<DialogRef>(null);
const tunRef = useRef<DialogRef>(null);
@@ -80,19 +73,17 @@ const SettingSystem = ({ onError }: Props) => {
// 抽象服务操作逻辑
const handleServiceOperation = useLockFn(
async (
{
beforeMsg,
action,
actionMsg,
successMsg,
}: {
beforeMsg: string;
action: () => Promise<void>;
actionMsg: string;
successMsg: string;
}
) => {
async ({
beforeMsg,
action,
actionMsg,
successMsg,
}: {
beforeMsg: string;
action: () => Promise<void>;
actionMsg: string;
successMsg: string;
}) => {
try {
showNotice("info", beforeMsg);
await stopCore();
@@ -102,19 +93,17 @@ const SettingSystem = ({ onError }: Props) => {
showNotice("info", t("Restarting Core..."));
await restartCore();
await mutateRunningMode();
await mutateServiceAvailable();
} catch (err: any) {
showNotice("error", err.message || err.toString());
try {
showNotice("info", t("Try running core as Sidecar..."));
await restartCore();
await mutateRunningMode();
await mutateServiceAvailable();
} catch (e: any) {
showNotice("error", e?.message || e?.toString());
}
}
}
},
);
// 卸载系统服务
@@ -145,7 +134,7 @@ const SettingSystem = ({ onError }: Props) => {
<WarningRounded sx={{ color: "warning.main", mr: 1 }} />
</Tooltip>
)}
{!serviceOk && !isAdminMode && (
{!isServiceMode && !isAdminMode && (
<Tooltip title={t("Install Service")}>
<Button
variant="outlined"
@@ -158,20 +147,19 @@ const SettingSystem = ({ onError }: Props) => {
</Button>
</Tooltip>
)}
{serviceOk && (
<Tooltip title={t("Uninstall Service")}>
<Button
// variant="outlined"
color="secondary"
size="small"
onClick={onUninstallService}
sx={{ mr: 1, minWidth: "32px", p: "4px" }}
>
{isServiceMode && (
<Tooltip title={t("Uninstall Service")}>
<Button
// variant="outlined"
color="secondary"
size="small"
onClick={onUninstallService}
sx={{ mr: 1, minWidth: "32px", p: "4px" }}
>
<DeleteForeverRounded fontSize="small" />
</Button>
</Tooltip>
)
}
</Button>
</Tooltip>
)}
</>
}
>
@@ -187,7 +175,9 @@ const SettingSystem = ({ onError }: Props) => {
onGuard={(e) => {
if (!isTunAvailable) {
showNotice("error", t("TUN requires Service Mode or Admin Mode"));
return Promise.reject(new Error(t("TUN requires Service Mode or Admin Mode")));
return Promise.reject(
new Error(t("TUN requires Service Mode or Admin Mode")),
);
}
return patchVerge({ enable_tun_mode: e });
}}

View File

@@ -23,12 +23,14 @@ export function useSystemState() {
});
// 获取系统服务状态
const isServiceMode = runningMode === "Service";
const { data: isServiceOk = false } = useSWR(
"isServiceAvailable",
isServiceAvailable,
{
suspense: false,
revalidateOnFocus: false,
isPaused: () => !isServiceMode, // 仅在 Service 模式下请求
},
);
@@ -36,7 +38,8 @@ export function useSystemState() {
runningMode,
isAdminMode,
isSidecarMode: runningMode === "Sidecar",
mutateRunningMode,
isServiceMode: runningMode === "Service",
isServiceOk,
mutateRunningMode,
};
}