refactor: invock mihomo api by use tauri-plugin-mihomo (#4926)
* feat: add tauri-plugin-mihomo * refactor: invock mihomo api by use tauri-plugin-mihomo * chore: todo * chore: update * chore: update * chore: update * chore: update * fix: incorrect delay status and update pretty config * chore: update * chore: remove cache * chore: update * chore: update * fix: app freezed when change group proxy * chore: update * chore: update * chore: add rustfmt.toml to tauri-plugin-mihomo * chore: happy clippy * refactor: connect mihomo websocket * chore: update * chore: update * fix: parse bigint to number * chore: update * Revert "fix: parse bigint to number" This reverts commit 74c006522e23aa52cf8979a8fb47d2b1ae0bb043. * chore: use number instead of bigint * chore: cleanup * fix: rule data not refresh when switch profile * chore: update * chore: cleanup * chore: update * fix: traffic graph data display * feat: add ipc connection pool * chore: update * chore: clippy * fix: incorrect delay status * fix: typo * fix: empty proxies tray menu * chore: clippy * chore: import tauri-plugin-mihomo by using git repo * chore: cleanup * fix: mihomo api * fix: incorrect delay status * chore: update tauri-plugin-mihomo dep chore: update
This commit is contained in:
@@ -4,8 +4,8 @@ import dayjs from "dayjs";
|
||||
import { t } from "i18next";
|
||||
import { useImperativeHandle, useState, type Ref } from "react";
|
||||
|
||||
import { deleteConnection } from "@/services/cmds";
|
||||
import parseTraffic from "@/utils/parse-traffic";
|
||||
import { closeConnections } from "tauri-plugin-mihomo-api";
|
||||
|
||||
export interface ConnectionDetailRef {
|
||||
open: (detail: IConnectionsItem) => void;
|
||||
@@ -97,7 +97,7 @@ const InnerConnectionDetail = ({ data, onClose }: InnerProps) => {
|
||||
{ label: t("Type"), value: `${metadata.type}(${metadata.network})` },
|
||||
];
|
||||
|
||||
const onDelete = useLockFn(async () => deleteConnection(data.id));
|
||||
const onDelete = useLockFn(async () => closeConnections(data.id));
|
||||
|
||||
return (
|
||||
<Box sx={{ userSelect: "text", color: theme.palette.text.secondary }}>
|
||||
|
||||
@@ -10,8 +10,8 @@ import {
|
||||
import { useLockFn } from "ahooks";
|
||||
import dayjs from "dayjs";
|
||||
|
||||
import { deleteConnection } from "@/services/cmds";
|
||||
import parseTraffic from "@/utils/parse-traffic";
|
||||
import { closeConnections } from "tauri-plugin-mihomo-api";
|
||||
|
||||
const Tag = styled("span")(({ theme }) => ({
|
||||
fontSize: "10px",
|
||||
@@ -34,7 +34,7 @@ export const ConnectionItem = (props: Props) => {
|
||||
|
||||
const { id, metadata, chains, start, curUpload, curDownload } = value;
|
||||
|
||||
const onDelete = useLockFn(async () => deleteConnection(id));
|
||||
const onDelete = useLockFn(async () => closeConnections(id));
|
||||
const showTraffic = curUpload! >= 100 || curDownload! >= 100;
|
||||
|
||||
return (
|
||||
|
||||
@@ -53,7 +53,7 @@ export const ClashInfoCard = () => {
|
||||
{t("Mixed Port")}
|
||||
</Typography>
|
||||
<Typography variant="body2" fontWeight="medium">
|
||||
{clashConfig["mixed-port"] || "-"}
|
||||
{clashConfig.mixedPort || "-"}
|
||||
</Typography>
|
||||
</Stack>
|
||||
<Divider />
|
||||
|
||||
@@ -7,10 +7,11 @@ import { Box, Paper, Stack, Typography } from "@mui/material";
|
||||
import { useLockFn } from "ahooks";
|
||||
import { useMemo } from "react";
|
||||
import { useTranslation } from "react-i18next";
|
||||
import { closeAllConnections } from "tauri-plugin-mihomo-api";
|
||||
|
||||
import { useVerge } from "@/hooks/use-verge";
|
||||
import { useAppData } from "@/providers/app-data-context";
|
||||
import { closeAllConnections, patchClashMode } from "@/services/cmds";
|
||||
import { patchClashMode } from "@/services/cmds";
|
||||
|
||||
export const ClashModeCard = () => {
|
||||
const { t } = useTranslation();
|
||||
|
||||
@@ -36,8 +36,8 @@ import { EnhancedCard } from "@/components/home/enhanced-card";
|
||||
import { useProxySelection } from "@/hooks/use-proxy-selection";
|
||||
import { useVerge } from "@/hooks/use-verge";
|
||||
import { useAppData } from "@/providers/app-data-context";
|
||||
import { getGroupProxyDelays, providerHealthCheck } from "@/services/cmds";
|
||||
import delayManager from "@/services/delay";
|
||||
import { delayGroup, healthcheckProxyProvider } from "tauri-plugin-mihomo-api";
|
||||
|
||||
// 本地存储的键名
|
||||
const STORAGE_KEY_GROUP = "clash-verge-selected-proxy-group";
|
||||
@@ -466,7 +466,7 @@ export const CurrentProxyCard = () => {
|
||||
if (providers.size > 0) {
|
||||
console.log(`[CurrentProxyCard] 开始测试提供者节点`);
|
||||
await Promise.allSettled(
|
||||
[...providers].map((p) => providerHealthCheck(p)),
|
||||
[...providers].map((p) => healthcheckProxyProvider(p)),
|
||||
);
|
||||
}
|
||||
|
||||
@@ -478,7 +478,7 @@ export const CurrentProxyCard = () => {
|
||||
try {
|
||||
await Promise.race([
|
||||
delayManager.checkListDelay(proxyNames, groupName, timeout),
|
||||
getGroupProxyDelays(groupName, url, timeout),
|
||||
delayGroup(groupName, url, timeout),
|
||||
]);
|
||||
console.log(`[CurrentProxyCard] 延迟测试完成,组: ${groupName}`);
|
||||
} catch (error) {
|
||||
|
||||
@@ -92,7 +92,7 @@ export const EnhancedCanvasTrafficGraph = memo(
|
||||
const { t } = useTranslation();
|
||||
|
||||
// 使用增强版全局流量数据管理
|
||||
const { dataPoints, getDataForTimeRange, isDataFresh, samplerStats } =
|
||||
const { dataPoints, getDataForTimeRange, samplerStats } =
|
||||
useTrafficGraphDataEnhanced();
|
||||
|
||||
// 基础状态
|
||||
@@ -865,6 +865,7 @@ export const EnhancedCanvasTrafficGraph = memo(
|
||||
}}
|
||||
onMouseMove={handleMouseMove}
|
||||
onMouseLeave={handleMouseLeave}
|
||||
onClick={toggleStyle}
|
||||
/>
|
||||
|
||||
{/* 控制层覆盖 */}
|
||||
@@ -962,8 +963,8 @@ export const EnhancedCanvasTrafficGraph = memo(
|
||||
lineHeight: 1.2,
|
||||
}}
|
||||
>
|
||||
Points: {displayData.length} | Fresh: {isDataFresh ? "✓" : "✗"} |
|
||||
Compressed: {samplerStats.compressedBufferSize}
|
||||
Points: {displayData.length} | Compressed:{" "}
|
||||
{samplerStats.compressedBufferSize}
|
||||
</Box>
|
||||
|
||||
{/* 悬浮提示框 */}
|
||||
@@ -988,6 +989,7 @@ export const EnhancedCanvasTrafficGraph = memo(
|
||||
boxShadow: "0 4px 12px rgba(0,0,0,0.15)",
|
||||
backdropFilter: "none",
|
||||
opacity: 1,
|
||||
whiteSpace: "nowrap",
|
||||
}}
|
||||
>
|
||||
<Box color="text.secondary" mb={0.2}>
|
||||
|
||||
@@ -7,7 +7,6 @@ import {
|
||||
MemoryRounded,
|
||||
} from "@mui/icons-material";
|
||||
import {
|
||||
Box,
|
||||
Grid,
|
||||
PaletteColor,
|
||||
Paper,
|
||||
@@ -15,16 +14,16 @@ import {
|
||||
alpha,
|
||||
useTheme,
|
||||
} from "@mui/material";
|
||||
import { ReactNode, memo, useCallback, useMemo, useRef } from "react";
|
||||
import { useRef, memo, useMemo } from "react";
|
||||
import { ReactNode } from "react";
|
||||
import { useTranslation } from "react-i18next";
|
||||
import useSWR from "swr";
|
||||
|
||||
import { TrafficErrorBoundary } from "@/components/common/traffic-error-boundary";
|
||||
import { useTrafficDataEnhanced } from "@/hooks/use-traffic-monitor";
|
||||
import { useConnectionData } from "@/hooks/use-connection-data";
|
||||
import { useMemoryData } from "@/hooks/use-memory-data";
|
||||
import { useTrafficData } from "@/hooks/use-traffic-data";
|
||||
import { useVerge } from "@/hooks/use-verge";
|
||||
import { useVisibility } from "@/hooks/use-visibility";
|
||||
import { useAppData } from "@/providers/app-data-context";
|
||||
import { gc, isDebugEnabled } from "@/services/cmds";
|
||||
import parseTraffic from "@/utils/parse-traffic";
|
||||
|
||||
import {
|
||||
@@ -148,51 +147,33 @@ export const EnhancedTrafficStats = () => {
|
||||
const trafficRef = useRef<EnhancedCanvasTrafficGraphRef>(null);
|
||||
const pageVisible = useVisibility();
|
||||
|
||||
// 使用AppDataProvider
|
||||
const { connections } = useAppData();
|
||||
const {
|
||||
response: { data: traffic },
|
||||
} = useTrafficData();
|
||||
|
||||
// 使用增强版的统一流量数据Hook
|
||||
const { traffic, memory, isLoading, isDataFresh, hasValidData } =
|
||||
useTrafficDataEnhanced();
|
||||
const {
|
||||
response: { data: memory },
|
||||
} = useMemoryData();
|
||||
|
||||
const {
|
||||
response: { data: connections },
|
||||
} = useConnectionData();
|
||||
|
||||
// 是否显示流量图表
|
||||
const trafficGraph = verge?.traffic_graph ?? true;
|
||||
|
||||
// 检查是否支持调试
|
||||
// TODO: merge this hook with layout-traffic.tsx
|
||||
const { data: isDebug } = useSWR(
|
||||
`clash-verge-rev-internal://isDebugEnabled`,
|
||||
() => isDebugEnabled(),
|
||||
{
|
||||
// default value before is fetched
|
||||
fallbackData: false,
|
||||
},
|
||||
);
|
||||
|
||||
// Canvas组件现在直接从全局Hook获取数据,无需手动添加数据点
|
||||
|
||||
// 执行垃圾回收
|
||||
const handleGarbageCollection = useCallback(async () => {
|
||||
if (isDebug) {
|
||||
try {
|
||||
await gc();
|
||||
console.log("[Debug] 垃圾回收已执行");
|
||||
} catch (err) {
|
||||
console.error("[Debug] 垃圾回收失败:", err);
|
||||
}
|
||||
}
|
||||
}, [isDebug]);
|
||||
|
||||
// 使用useMemo计算解析后的流量数据
|
||||
const parsedData = useMemo(() => {
|
||||
const [up, upUnit] = parseTraffic(traffic?.raw?.up_rate || 0);
|
||||
const [down, downUnit] = parseTraffic(traffic?.raw?.down_rate || 0);
|
||||
const [inuse, inuseUnit] = parseTraffic(memory?.raw?.inuse || 0);
|
||||
const [up, upUnit] = parseTraffic(traffic?.up || 0);
|
||||
const [down, downUnit] = parseTraffic(traffic?.down || 0);
|
||||
const [inuse, inuseUnit] = parseTraffic(memory?.inuse || 0);
|
||||
const [uploadTotal, uploadTotalUnit] = parseTraffic(
|
||||
connections.uploadTotal,
|
||||
connections?.uploadTotal,
|
||||
);
|
||||
const [downloadTotal, downloadTotalUnit] = parseTraffic(
|
||||
connections.downloadTotal,
|
||||
connections?.downloadTotal,
|
||||
);
|
||||
|
||||
return {
|
||||
@@ -206,7 +187,7 @@ export const EnhancedTrafficStats = () => {
|
||||
uploadTotalUnit,
|
||||
downloadTotal,
|
||||
downloadTotalUnit,
|
||||
connectionsCount: connections.count,
|
||||
connectionsCount: connections?.connections.length,
|
||||
};
|
||||
}, [traffic, memory, connections]);
|
||||
|
||||
@@ -228,33 +209,10 @@ export const EnhancedTrafficStats = () => {
|
||||
>
|
||||
<div style={{ height: "100%", position: "relative" }}>
|
||||
<EnhancedCanvasTrafficGraph ref={trafficRef} />
|
||||
{isDebug && (
|
||||
<div
|
||||
style={{
|
||||
position: "absolute",
|
||||
top: "2px",
|
||||
left: "2px",
|
||||
zIndex: 10,
|
||||
backgroundColor: "rgba(0,0,0,0.5)",
|
||||
color: "white",
|
||||
fontSize: "8px",
|
||||
padding: "2px 4px",
|
||||
borderRadius: "4px",
|
||||
}}
|
||||
>
|
||||
DEBUG: {trafficRef.current ? "图表已初始化" : "图表未初始化"}
|
||||
<br />
|
||||
状态: {isDataFresh ? "active" : "inactive"}
|
||||
<br />
|
||||
数据新鲜度: {traffic?.is_fresh ? "Fresh" : "Stale"}
|
||||
<br />
|
||||
{new Date().toISOString().slice(11, 19)}
|
||||
</div>
|
||||
)}
|
||||
</div>
|
||||
</Paper>
|
||||
);
|
||||
}, [trafficGraph, pageVisible, theme.palette.divider, isDebug]);
|
||||
}, [trafficGraph, pageVisible, theme.palette.divider]);
|
||||
|
||||
// 使用useMemo计算统计卡片配置
|
||||
const statCards = useMemo(
|
||||
@@ -300,10 +258,10 @@ export const EnhancedTrafficStats = () => {
|
||||
value: parsedData.inuse,
|
||||
unit: parsedData.inuseUnit,
|
||||
color: "error" as const,
|
||||
onClick: isDebug ? handleGarbageCollection : undefined,
|
||||
onClick: undefined,
|
||||
},
|
||||
],
|
||||
[t, parsedData, isDebug, handleGarbageCollection],
|
||||
[t, parsedData],
|
||||
);
|
||||
|
||||
return (
|
||||
@@ -320,28 +278,11 @@ export const EnhancedTrafficStats = () => {
|
||||
</Grid>
|
||||
)}
|
||||
{/* 统计卡片区域 */}
|
||||
{statCards.map((card, index) => (
|
||||
<Grid key={index} size={4}>
|
||||
<CompactStatCard {...card} />
|
||||
{statCards.map((card, _index) => (
|
||||
<Grid key={card.title} size={4}>
|
||||
<CompactStatCard {...(card as StatCardProps)} />
|
||||
</Grid>
|
||||
))}
|
||||
|
||||
{/* 数据状态指示器(调试用)*/}
|
||||
{isDebug && (
|
||||
<Grid size={12}>
|
||||
<Box
|
||||
sx={{
|
||||
p: 1,
|
||||
bgcolor: "action.hover",
|
||||
borderRadius: 1,
|
||||
fontSize: "0.75rem",
|
||||
}}
|
||||
>
|
||||
数据状态: {isDataFresh ? "新鲜" : "过期"} | 有效数据:{" "}
|
||||
{hasValidData ? "是" : "否"} | 加载中: {isLoading ? "是" : "否"}
|
||||
</Box>
|
||||
</Grid>
|
||||
)}
|
||||
</Grid>
|
||||
</TrafficErrorBoundary>
|
||||
);
|
||||
|
||||
@@ -6,34 +6,19 @@ import {
|
||||
import { Box, Typography } from "@mui/material";
|
||||
import { useEffect, useRef } from "react";
|
||||
import { useTranslation } from "react-i18next";
|
||||
import useSWR from "swr";
|
||||
|
||||
import { LightweightTrafficErrorBoundary } from "@/components/common/traffic-error-boundary";
|
||||
import { useClashInfo } from "@/hooks/use-clash";
|
||||
import { useTrafficDataEnhanced } from "@/hooks/use-traffic-monitor";
|
||||
import { useVerge } from "@/hooks/use-verge";
|
||||
import { useVisibility } from "@/hooks/use-visibility";
|
||||
import { isDebugEnabled, gc, startTrafficService } from "@/services/cmds";
|
||||
import parseTraffic from "@/utils/parse-traffic";
|
||||
|
||||
import { TrafficGraph, type TrafficRef } from "./traffic-graph";
|
||||
import { useTrafficData } from "@/hooks/use-traffic-data";
|
||||
import { useMemoryData } from "@/hooks/use-memory-data";
|
||||
|
||||
// setup the traffic
|
||||
export const LayoutTraffic = () => {
|
||||
const { data: isDebug } = useSWR(
|
||||
"clash-verge-rev-internal://isDebugEnabled",
|
||||
() => isDebugEnabled(),
|
||||
{
|
||||
// default value before is fetched
|
||||
fallbackData: false,
|
||||
},
|
||||
);
|
||||
|
||||
if (isDebug) {
|
||||
console.debug("[Traffic][LayoutTraffic] 组件正在渲染");
|
||||
}
|
||||
const { t } = useTranslation();
|
||||
const { clashInfo } = useClashInfo();
|
||||
const { verge } = useVerge();
|
||||
|
||||
// whether hide traffic graph
|
||||
@@ -42,31 +27,19 @@ export const LayoutTraffic = () => {
|
||||
const trafficRef = useRef<TrafficRef>(null);
|
||||
const pageVisible = useVisibility();
|
||||
|
||||
// 使用增强版的统一流量数据Hook
|
||||
const { traffic, memory } = useTrafficDataEnhanced();
|
||||
|
||||
// 启动流量服务
|
||||
useEffect(() => {
|
||||
console.log(
|
||||
"[Traffic][LayoutTraffic] useEffect 触发,clashInfo:",
|
||||
clashInfo,
|
||||
"pageVisible:",
|
||||
pageVisible,
|
||||
);
|
||||
|
||||
// 简化条件,只要组件挂载就尝试启动服务
|
||||
console.log("[Traffic][LayoutTraffic] 开始启动流量服务");
|
||||
startTrafficService().catch((error) => {
|
||||
console.error("[Traffic][LayoutTraffic] 启动流量服务失败:", error);
|
||||
});
|
||||
}, []); // 移除依赖,只在组件挂载时启动一次
|
||||
const {
|
||||
response: { data: traffic },
|
||||
} = useTrafficData();
|
||||
const {
|
||||
response: { data: memory },
|
||||
} = useMemoryData();
|
||||
|
||||
// 监听数据变化,为图表添加数据点
|
||||
useEffect(() => {
|
||||
if (traffic?.raw && trafficRef.current) {
|
||||
if (trafficRef.current) {
|
||||
trafficRef.current.appendData({
|
||||
up: traffic.raw.up_rate || 0,
|
||||
down: traffic.raw.down_rate || 0,
|
||||
up: traffic?.up || 0,
|
||||
down: traffic?.down || 0,
|
||||
});
|
||||
}
|
||||
}, [traffic]);
|
||||
@@ -75,9 +48,9 @@ export const LayoutTraffic = () => {
|
||||
const displayMemory = verge?.enable_memory_usage ?? true;
|
||||
|
||||
// 使用parseTraffic统一处理转换,保持与首页一致的显示格式
|
||||
const [up, upUnit] = parseTraffic(traffic?.raw?.up_rate || 0);
|
||||
const [down, downUnit] = parseTraffic(traffic?.raw?.down_rate || 0);
|
||||
const [inuse, inuseUnit] = parseTraffic(memory?.raw?.inuse || 0);
|
||||
const [up, upUnit] = parseTraffic(traffic?.up || 0);
|
||||
const [down, downUnit] = parseTraffic(traffic?.down || 0);
|
||||
const [inuse, inuseUnit] = parseTraffic(memory?.inuse || 0);
|
||||
|
||||
const boxStyle: any = {
|
||||
display: "flex",
|
||||
@@ -114,18 +87,16 @@ export const LayoutTraffic = () => {
|
||||
|
||||
<Box display="flex" flexDirection="column" gap={0.75}>
|
||||
<Box
|
||||
title={`${t("Upload Speed")} ${traffic?.is_fresh ? "" : "(Stale)"}`}
|
||||
title={`${t("Upload Speed")}`}
|
||||
{...boxStyle}
|
||||
sx={{
|
||||
...boxStyle.sx,
|
||||
opacity: traffic?.is_fresh ? 1 : 0.6,
|
||||
// opacity: traffic?.is_fresh ? 1 : 0.6,
|
||||
}}
|
||||
>
|
||||
<ArrowUpwardRounded
|
||||
{...iconStyle}
|
||||
color={
|
||||
(traffic?.raw?.up_rate || 0) > 0 ? "secondary" : "disabled"
|
||||
}
|
||||
color={(traffic?.up || 0) > 0 ? "secondary" : "disabled"}
|
||||
/>
|
||||
<Typography {...valStyle} color="secondary">
|
||||
{up}
|
||||
@@ -134,18 +105,16 @@ export const LayoutTraffic = () => {
|
||||
</Box>
|
||||
|
||||
<Box
|
||||
title={`${t("Download Speed")} ${traffic?.is_fresh ? "" : "(Stale)"}`}
|
||||
title={`${t("Download Speed")}`}
|
||||
{...boxStyle}
|
||||
sx={{
|
||||
...boxStyle.sx,
|
||||
opacity: traffic?.is_fresh ? 1 : 0.6,
|
||||
// opacity: traffic?.is_fresh ? 1 : 0.6,
|
||||
}}
|
||||
>
|
||||
<ArrowDownwardRounded
|
||||
{...iconStyle}
|
||||
color={
|
||||
(traffic?.raw?.down_rate || 0) > 0 ? "primary" : "disabled"
|
||||
}
|
||||
color={(traffic?.down || 0) > 0 ? "primary" : "disabled"}
|
||||
/>
|
||||
<Typography {...valStyle} color="primary">
|
||||
{down}
|
||||
@@ -155,15 +124,15 @@ export const LayoutTraffic = () => {
|
||||
|
||||
{displayMemory && (
|
||||
<Box
|
||||
title={`${t(isDebug ? "Memory Cleanup" : "Memory Usage")} ${memory?.is_fresh ? "" : "(Stale)"} ${"usage_percent" in (memory?.formatted || {}) && memory.formatted.usage_percent ? `(${memory.formatted.usage_percent.toFixed(1)}%)` : ""}`}
|
||||
title={`${t("Memory Usage")} `}
|
||||
{...boxStyle}
|
||||
sx={{
|
||||
cursor: isDebug ? "pointer" : "auto",
|
||||
opacity: memory?.is_fresh ? 1 : 0.6,
|
||||
cursor: "auto",
|
||||
// opacity: memory?.is_fresh ? 1 : 0.6,
|
||||
}}
|
||||
color={isDebug ? "success.main" : "disabled"}
|
||||
color={"disabled"}
|
||||
onClick={async () => {
|
||||
isDebug && (await gc());
|
||||
// isDebug && (await gc());
|
||||
}}
|
||||
>
|
||||
<MemoryRounded {...iconStyle} />
|
||||
|
||||
@@ -20,26 +20,12 @@ import { useLockFn } from "ahooks";
|
||||
import dayjs from "dayjs";
|
||||
import { useState } from "react";
|
||||
import { useTranslation } from "react-i18next";
|
||||
import { updateProxyProvider } from "tauri-plugin-mihomo-api";
|
||||
|
||||
import { useAppData } from "@/providers/app-data-context";
|
||||
import { proxyProviderUpdate } from "@/services/cmds";
|
||||
import { showNotice } from "@/services/noticeService";
|
||||
import parseTraffic from "@/utils/parse-traffic";
|
||||
|
||||
// 定义代理提供者类型
|
||||
interface ProxyProviderItem {
|
||||
name?: string;
|
||||
proxies: any[];
|
||||
updatedAt: number;
|
||||
vehicleType: string;
|
||||
subscriptionInfo?: {
|
||||
Upload: number;
|
||||
Download: number;
|
||||
Total: number;
|
||||
Expire: number;
|
||||
};
|
||||
}
|
||||
|
||||
// 样式化组件 - 类型框
|
||||
const TypeBox = styled(Box)<{ component?: React.ElementType }>(({ theme }) => ({
|
||||
display: "inline-block",
|
||||
@@ -74,7 +60,7 @@ export const ProviderButton = () => {
|
||||
// 设置更新状态
|
||||
setUpdating((prev) => ({ ...prev, [name]: true }));
|
||||
|
||||
await proxyProviderUpdate(name);
|
||||
await updateProxyProvider(name);
|
||||
|
||||
// 刷新数据
|
||||
await refreshProxy();
|
||||
@@ -115,7 +101,7 @@ export const ProviderButton = () => {
|
||||
// 改为串行逐个更新所有provider
|
||||
for (const name of allProviders) {
|
||||
try {
|
||||
await proxyProviderUpdate(name);
|
||||
await updateProxyProvider(name);
|
||||
// 每个更新完成后更新状态
|
||||
setUpdating((prev) => ({ ...prev, [name]: false }));
|
||||
} catch (err) {
|
||||
@@ -177,161 +163,164 @@ export const ProviderButton = () => {
|
||||
|
||||
<DialogContent>
|
||||
<List sx={{ py: 0, minHeight: 250 }}>
|
||||
{Object.entries(proxyProviders || {}).map(([key, item]) => {
|
||||
const provider = item as ProxyProviderItem;
|
||||
const time = dayjs(provider.updatedAt);
|
||||
const isUpdating = updating[key];
|
||||
{Object.entries(proxyProviders || {})
|
||||
.sort()
|
||||
.map(([key, item]) => {
|
||||
const provider = item;
|
||||
const time = dayjs(provider.updatedAt);
|
||||
const isUpdating = updating[key];
|
||||
|
||||
// 订阅信息
|
||||
const sub = provider.subscriptionInfo;
|
||||
const hasSubInfo = !!sub;
|
||||
const upload = sub?.Upload || 0;
|
||||
const download = sub?.Download || 0;
|
||||
const total = sub?.Total || 0;
|
||||
const expire = sub?.Expire || 0;
|
||||
// 订阅信息
|
||||
const sub = provider.subscriptionInfo;
|
||||
const hasSubInfo = !!sub;
|
||||
const upload = sub?.Upload || 0;
|
||||
const download = sub?.Download || 0;
|
||||
const total = sub?.Total || 0;
|
||||
const expire = sub?.Expire || 0;
|
||||
|
||||
// 流量使用进度
|
||||
const progress =
|
||||
total > 0
|
||||
? Math.min(
|
||||
Math.round(((download + upload) * 100) / total) + 1,
|
||||
100,
|
||||
)
|
||||
: 0;
|
||||
// 流量使用进度
|
||||
const progress =
|
||||
total > 0
|
||||
? Math.min(
|
||||
Math.round(((download + upload) * 100) / total) + 1,
|
||||
100,
|
||||
)
|
||||
: 0;
|
||||
|
||||
return (
|
||||
<ListItem
|
||||
key={key}
|
||||
sx={[
|
||||
{
|
||||
p: 0,
|
||||
mb: "8px",
|
||||
borderRadius: 2,
|
||||
overflow: "hidden",
|
||||
transition: "all 0.2s",
|
||||
},
|
||||
({ palette: { mode, primary } }) => {
|
||||
const bgcolor = mode === "light" ? "#ffffff" : "#24252f";
|
||||
const hoverColor =
|
||||
mode === "light"
|
||||
? alpha(primary.main, 0.1)
|
||||
: alpha(primary.main, 0.2);
|
||||
return (
|
||||
<ListItem
|
||||
key={key}
|
||||
sx={[
|
||||
{
|
||||
p: 0,
|
||||
mb: "8px",
|
||||
borderRadius: 2,
|
||||
overflow: "hidden",
|
||||
transition: "all 0.2s",
|
||||
},
|
||||
({ palette: { mode, primary } }) => {
|
||||
const bgcolor =
|
||||
mode === "light" ? "#ffffff" : "#24252f";
|
||||
const hoverColor =
|
||||
mode === "light"
|
||||
? alpha(primary.main, 0.1)
|
||||
: alpha(primary.main, 0.2);
|
||||
|
||||
return {
|
||||
backgroundColor: bgcolor,
|
||||
"&:hover": {
|
||||
backgroundColor: hoverColor,
|
||||
},
|
||||
};
|
||||
},
|
||||
]}
|
||||
>
|
||||
<ListItemText
|
||||
sx={{ px: 2, py: 1 }}
|
||||
primary={
|
||||
<Box
|
||||
sx={{
|
||||
display: "flex",
|
||||
justifyContent: "space-between",
|
||||
alignItems: "center",
|
||||
}}
|
||||
>
|
||||
<Typography
|
||||
variant="subtitle1"
|
||||
component="div"
|
||||
noWrap
|
||||
title={key}
|
||||
sx={{ display: "flex", alignItems: "center" }}
|
||||
>
|
||||
<span style={{ marginRight: "8px" }}>{key}</span>
|
||||
<TypeBox component="span">
|
||||
{provider.proxies.length}
|
||||
</TypeBox>
|
||||
<TypeBox component="span">
|
||||
{provider.vehicleType}
|
||||
</TypeBox>
|
||||
</Typography>
|
||||
|
||||
<Typography
|
||||
variant="body2"
|
||||
color="text.secondary"
|
||||
noWrap
|
||||
>
|
||||
<small>{t("Update At")}: </small>
|
||||
{time.fromNow()}
|
||||
</Typography>
|
||||
</Box>
|
||||
}
|
||||
secondary={
|
||||
<>
|
||||
{/* 订阅信息 */}
|
||||
{hasSubInfo && (
|
||||
<>
|
||||
<Box
|
||||
sx={{
|
||||
mb: 1,
|
||||
display: "flex",
|
||||
alignItems: "center",
|
||||
justifyContent: "space-between",
|
||||
}}
|
||||
>
|
||||
<span title={t("Used / Total") as string}>
|
||||
{parseTraffic(upload + download)} /{" "}
|
||||
{parseTraffic(total)}
|
||||
</span>
|
||||
<span title={t("Expire Time") as string}>
|
||||
{parseExpire(expire)}
|
||||
</span>
|
||||
</Box>
|
||||
|
||||
{/* 进度条 */}
|
||||
<LinearProgress
|
||||
variant="determinate"
|
||||
value={progress}
|
||||
sx={{
|
||||
height: 6,
|
||||
borderRadius: 3,
|
||||
opacity: total > 0 ? 1 : 0,
|
||||
}}
|
||||
/>
|
||||
</>
|
||||
)}
|
||||
</>
|
||||
}
|
||||
/>
|
||||
<Divider orientation="vertical" flexItem />
|
||||
<Box
|
||||
sx={{
|
||||
width: 40,
|
||||
display: "flex",
|
||||
justifyContent: "center",
|
||||
alignItems: "center",
|
||||
}}
|
||||
return {
|
||||
backgroundColor: bgcolor,
|
||||
"&:hover": {
|
||||
backgroundColor: hoverColor,
|
||||
},
|
||||
};
|
||||
},
|
||||
]}
|
||||
>
|
||||
<IconButton
|
||||
size="small"
|
||||
color="primary"
|
||||
onClick={() => {
|
||||
updateProvider(key);
|
||||
}}
|
||||
disabled={isUpdating}
|
||||
<ListItemText
|
||||
sx={{ px: 2, py: 1 }}
|
||||
primary={
|
||||
<Box
|
||||
sx={{
|
||||
display: "flex",
|
||||
justifyContent: "space-between",
|
||||
alignItems: "center",
|
||||
}}
|
||||
>
|
||||
<Typography
|
||||
variant="subtitle1"
|
||||
component="div"
|
||||
noWrap
|
||||
title={key}
|
||||
sx={{ display: "flex", alignItems: "center" }}
|
||||
>
|
||||
<span style={{ marginRight: "8px" }}>{key}</span>
|
||||
<TypeBox component="span">
|
||||
{provider.proxies.length}
|
||||
</TypeBox>
|
||||
<TypeBox component="span">
|
||||
{provider.vehicleType}
|
||||
</TypeBox>
|
||||
</Typography>
|
||||
|
||||
<Typography
|
||||
variant="body2"
|
||||
color="text.secondary"
|
||||
noWrap
|
||||
>
|
||||
<small>{t("Update At")}: </small>
|
||||
{time.fromNow()}
|
||||
</Typography>
|
||||
</Box>
|
||||
}
|
||||
secondary={
|
||||
<>
|
||||
{/* 订阅信息 */}
|
||||
{hasSubInfo && (
|
||||
<>
|
||||
<Box
|
||||
sx={{
|
||||
mb: 1,
|
||||
display: "flex",
|
||||
alignItems: "center",
|
||||
justifyContent: "space-between",
|
||||
}}
|
||||
>
|
||||
<span title={t("Used / Total") as string}>
|
||||
{parseTraffic(upload + download)} /{" "}
|
||||
{parseTraffic(total)}
|
||||
</span>
|
||||
<span title={t("Expire Time") as string}>
|
||||
{parseExpire(expire)}
|
||||
</span>
|
||||
</Box>
|
||||
|
||||
{/* 进度条 */}
|
||||
<LinearProgress
|
||||
variant="determinate"
|
||||
value={progress}
|
||||
sx={{
|
||||
height: 6,
|
||||
borderRadius: 3,
|
||||
opacity: total > 0 ? 1 : 0,
|
||||
}}
|
||||
/>
|
||||
</>
|
||||
)}
|
||||
</>
|
||||
}
|
||||
/>
|
||||
<Divider orientation="vertical" flexItem />
|
||||
<Box
|
||||
sx={{
|
||||
animation: isUpdating
|
||||
? "spin 1s linear infinite"
|
||||
: "none",
|
||||
"@keyframes spin": {
|
||||
"0%": { transform: "rotate(0deg)" },
|
||||
"100%": { transform: "rotate(360deg)" },
|
||||
},
|
||||
width: 40,
|
||||
display: "flex",
|
||||
justifyContent: "center",
|
||||
alignItems: "center",
|
||||
}}
|
||||
title={t("Update Provider") as string}
|
||||
>
|
||||
<RefreshRounded />
|
||||
</IconButton>
|
||||
</Box>
|
||||
</ListItem>
|
||||
);
|
||||
})}
|
||||
<IconButton
|
||||
size="small"
|
||||
color="primary"
|
||||
onClick={() => {
|
||||
updateProvider(key);
|
||||
}}
|
||||
disabled={isUpdating}
|
||||
sx={{
|
||||
animation: isUpdating
|
||||
? "spin 1s linear infinite"
|
||||
: "none",
|
||||
"@keyframes spin": {
|
||||
"0%": { transform: "rotate(0deg)" },
|
||||
"100%": { transform: "rotate(360deg)" },
|
||||
},
|
||||
}}
|
||||
title={t("Update Provider") as string}
|
||||
>
|
||||
<RefreshRounded />
|
||||
</IconButton>
|
||||
</Box>
|
||||
</ListItem>
|
||||
);
|
||||
})}
|
||||
</List>
|
||||
</DialogContent>
|
||||
|
||||
|
||||
@@ -34,14 +34,13 @@ import {
|
||||
import { useCallback, useEffect, useRef, useState } from "react";
|
||||
import { useTranslation } from "react-i18next";
|
||||
import useSWR from "swr";
|
||||
|
||||
import { useAppData } from "@/providers/app-data-context";
|
||||
import {
|
||||
closeAllConnections,
|
||||
getProxies,
|
||||
updateProxyAndSync,
|
||||
updateProxyChainConfigInRuntime,
|
||||
} from "@/services/cmds";
|
||||
selectNodeForGroup,
|
||||
} from "tauri-plugin-mihomo-api";
|
||||
|
||||
import { useAppData } from "@/providers/app-data-context";
|
||||
import { calcuProxies, updateProxyChainConfigInRuntime } from "@/services/cmds";
|
||||
|
||||
interface ProxyChainItem {
|
||||
id: string;
|
||||
@@ -204,7 +203,7 @@ export const ProxyChain = ({
|
||||
// 获取当前代理信息以检查连接状态
|
||||
const { data: currentProxies, mutate: mutateProxies } = useSWR(
|
||||
"getProxies",
|
||||
getProxies,
|
||||
calcuProxies,
|
||||
{
|
||||
revalidateOnFocus: true,
|
||||
revalidateIfStale: true,
|
||||
@@ -367,7 +366,7 @@ export const ProxyChain = ({
|
||||
|
||||
const targetGroup = mode === "global" ? "GLOBAL" : selectedGroup;
|
||||
|
||||
await updateProxyAndSync(targetGroup || "GLOBAL", lastNode.name);
|
||||
await selectNodeForGroup(targetGroup || "GLOBAL", lastNode.name);
|
||||
localStorage.setItem("proxy-chain-group", targetGroup || "GLOBAL");
|
||||
localStorage.setItem("proxy-chain-exit-node", lastNode.name);
|
||||
|
||||
|
||||
@@ -14,14 +14,13 @@ import { useCallback, useEffect, useMemo, useRef, useState } from "react";
|
||||
import { useTranslation } from "react-i18next";
|
||||
import { Virtuoso, type VirtuosoHandle } from "react-virtuoso";
|
||||
import useSWR from "swr";
|
||||
import { delayGroup, healthcheckProxyProvider } from "tauri-plugin-mihomo-api";
|
||||
|
||||
import { useProxySelection } from "@/hooks/use-proxy-selection";
|
||||
import { useVerge } from "@/hooks/use-verge";
|
||||
import { useAppData } from "@/providers/app-data-context";
|
||||
import {
|
||||
getGroupProxyDelays,
|
||||
getRuntimeConfig,
|
||||
providerHealthCheck,
|
||||
updateProxyChainConfigInRuntime,
|
||||
} from "@/services/cmds";
|
||||
import delayManager from "@/services/delay";
|
||||
@@ -153,15 +152,14 @@ export const ProxyGroups = (props: Props) => {
|
||||
|
||||
// 添加和清理滚动事件监听器
|
||||
useEffect(() => {
|
||||
const currentScroller = scrollerRef.current;
|
||||
if (currentScroller) {
|
||||
currentScroller.addEventListener("scroll", handleScroll, {
|
||||
passive: true,
|
||||
});
|
||||
return () => {
|
||||
currentScroller.removeEventListener("scroll", handleScroll);
|
||||
};
|
||||
}
|
||||
if (!scrollerRef.current) return;
|
||||
scrollerRef.current.addEventListener("scroll", handleScroll, {
|
||||
passive: true,
|
||||
});
|
||||
|
||||
return () => {
|
||||
scrollerRef.current?.removeEventListener("scroll", handleScroll);
|
||||
};
|
||||
}, [handleScroll]);
|
||||
|
||||
// 滚动到顶部
|
||||
@@ -215,6 +213,7 @@ export const ProxyGroups = (props: Props) => {
|
||||
const currentGroup = getCurrentGroup();
|
||||
const availableGroups = getAvailableGroups();
|
||||
|
||||
// TODO: 频繁点击切换代理节点,导致应用卡死
|
||||
const handleChangeProxy = useCallback(
|
||||
(group: IProxyGroupItem, proxy: IProxyItem) => {
|
||||
if (isChainMode) {
|
||||
@@ -273,7 +272,7 @@ export const ProxyGroups = (props: Props) => {
|
||||
if (providers.size) {
|
||||
console.log(`[ProxyGroups] 发现提供者,数量: ${providers.size}`);
|
||||
Promise.allSettled(
|
||||
[...providers].map((p) => providerHealthCheck(p)),
|
||||
[...providers].map((p) => healthcheckProxyProvider(p)),
|
||||
).then(() => {
|
||||
console.log(`[ProxyGroups] 提供者健康检查完成`);
|
||||
onProxies();
|
||||
@@ -289,7 +288,7 @@ export const ProxyGroups = (props: Props) => {
|
||||
try {
|
||||
await Promise.race([
|
||||
delayManager.checkListDelay(names, groupName, timeout),
|
||||
getGroupProxyDelays(groupName, url, timeout).then((result) => {
|
||||
delayGroup(groupName, url, timeout).then((result) => {
|
||||
console.log(
|
||||
`[ProxyGroups] getGroupProxyDelays返回结果数量:`,
|
||||
Object.keys(result || {}).length,
|
||||
@@ -518,7 +517,7 @@ export const ProxyGroups = (props: Props) => {
|
||||
},
|
||||
}}
|
||||
>
|
||||
{availableGroups.map((group: any, index: number) => (
|
||||
{availableGroups.map((group: any, _index: number) => (
|
||||
<MenuItem
|
||||
key={group.name}
|
||||
onClick={() => handleGroupSelect(group.name)}
|
||||
|
||||
@@ -37,12 +37,12 @@ export const ProxyItemMini = (props: Props) => {
|
||||
return () => {
|
||||
delayManager.removeListener(proxy.name, group.name);
|
||||
};
|
||||
}, [proxy.name, group.name]);
|
||||
}, [isPreset, proxy.name, group.name]);
|
||||
|
||||
useEffect(() => {
|
||||
if (!proxy) return;
|
||||
setDelay(delayManager.getDelayFix(proxy, group.name));
|
||||
}, [proxy]);
|
||||
}, [proxy, group.name]);
|
||||
|
||||
const onDelay = useLockFn(async () => {
|
||||
setDelay(-2);
|
||||
@@ -200,7 +200,7 @@ export const ProxyItemMini = (props: Props) => {
|
||||
</Widget>
|
||||
)}
|
||||
|
||||
{delay > 0 && (
|
||||
{delay >= 0 && (
|
||||
// 显示延迟
|
||||
<Widget
|
||||
className="the-delay"
|
||||
@@ -220,7 +220,7 @@ export const ProxyItemMini = (props: Props) => {
|
||||
{delayManager.formatDelay(delay, timeout)}
|
||||
</Widget>
|
||||
)}
|
||||
{delay !== -2 && delay <= 0 && selected && (
|
||||
{proxy.type !== "Direct" && delay !== -2 && delay < 0 && selected && (
|
||||
// 展示已选择的icon
|
||||
<CheckCircleOutlineRounded
|
||||
className="the-icon"
|
||||
|
||||
@@ -19,19 +19,11 @@ import { useLockFn } from "ahooks";
|
||||
import dayjs from "dayjs";
|
||||
import { useState } from "react";
|
||||
import { useTranslation } from "react-i18next";
|
||||
import { updateRuleProvider } from "tauri-plugin-mihomo-api";
|
||||
|
||||
import { useAppData } from "@/providers/app-data-context";
|
||||
import { ruleProviderUpdate } from "@/services/cmds";
|
||||
import { showNotice } from "@/services/noticeService";
|
||||
|
||||
// 定义规则提供者类型
|
||||
interface RuleProviderItem {
|
||||
behavior: string;
|
||||
ruleCount: number;
|
||||
updatedAt: number;
|
||||
vehicleType: string;
|
||||
}
|
||||
|
||||
// 辅助组件 - 类型框
|
||||
const TypeBox = styled(Box)<{ component?: React.ElementType }>(({ theme }) => ({
|
||||
display: "inline-block",
|
||||
@@ -60,7 +52,7 @@ export const ProviderButton = () => {
|
||||
// 设置更新状态
|
||||
setUpdating((prev) => ({ ...prev, [name]: true }));
|
||||
|
||||
await ruleProviderUpdate(name);
|
||||
await updateRuleProvider(name);
|
||||
|
||||
// 刷新数据
|
||||
await refreshRules();
|
||||
@@ -101,7 +93,7 @@ export const ProviderButton = () => {
|
||||
// 改为串行逐个更新所有provider
|
||||
for (const name of allProviders) {
|
||||
try {
|
||||
await ruleProviderUpdate(name);
|
||||
await updateRuleProvider(name);
|
||||
// 每个更新完成后更新状态
|
||||
setUpdating((prev) => ({ ...prev, [name]: false }));
|
||||
} catch (err) {
|
||||
@@ -160,112 +152,117 @@ export const ProviderButton = () => {
|
||||
|
||||
<DialogContent>
|
||||
<List sx={{ py: 0, minHeight: 250 }}>
|
||||
{Object.entries(ruleProviders || {}).map(([key, item]) => {
|
||||
const provider = item as RuleProviderItem;
|
||||
const time = dayjs(provider.updatedAt);
|
||||
const isUpdating = updating[key];
|
||||
{Object.entries(ruleProviders || {})
|
||||
.sort()
|
||||
.map(([key, item]) => {
|
||||
const provider = item;
|
||||
const time = dayjs(provider.updatedAt);
|
||||
const isUpdating = updating[key];
|
||||
|
||||
return (
|
||||
<ListItem
|
||||
key={key}
|
||||
sx={[
|
||||
{
|
||||
p: 0,
|
||||
mb: "8px",
|
||||
borderRadius: 2,
|
||||
overflow: "hidden",
|
||||
transition: "all 0.2s",
|
||||
},
|
||||
({ palette: { mode, primary } }) => {
|
||||
const bgcolor = mode === "light" ? "#ffffff" : "#24252f";
|
||||
const hoverColor =
|
||||
mode === "light"
|
||||
? alpha(primary.main, 0.1)
|
||||
: alpha(primary.main, 0.2);
|
||||
return (
|
||||
<ListItem
|
||||
key={key}
|
||||
sx={[
|
||||
{
|
||||
p: 0,
|
||||
mb: "8px",
|
||||
borderRadius: 2,
|
||||
overflow: "hidden",
|
||||
transition: "all 0.2s",
|
||||
},
|
||||
({ palette: { mode, primary } }) => {
|
||||
const bgcolor =
|
||||
mode === "light" ? "#ffffff" : "#24252f";
|
||||
const hoverColor =
|
||||
mode === "light"
|
||||
? alpha(primary.main, 0.1)
|
||||
: alpha(primary.main, 0.2);
|
||||
|
||||
return {
|
||||
backgroundColor: bgcolor,
|
||||
"&:hover": {
|
||||
backgroundColor: hoverColor,
|
||||
borderColor: alpha(primary.main, 0.3),
|
||||
},
|
||||
};
|
||||
},
|
||||
]}
|
||||
>
|
||||
<ListItemText
|
||||
sx={{ px: 2, py: 1 }}
|
||||
primary={
|
||||
<Box
|
||||
sx={{
|
||||
display: "flex",
|
||||
justifyContent: "space-between",
|
||||
alignItems: "center",
|
||||
}}
|
||||
>
|
||||
<Typography
|
||||
variant="subtitle1"
|
||||
component="div"
|
||||
noWrap
|
||||
title={key}
|
||||
sx={{ display: "flex", alignItems: "center" }}
|
||||
>
|
||||
<span style={{ marginRight: "8px" }}>{key}</span>
|
||||
<TypeBox component="span">
|
||||
{provider.ruleCount}
|
||||
</TypeBox>
|
||||
</Typography>
|
||||
|
||||
<Typography
|
||||
variant="body2"
|
||||
color="text.secondary"
|
||||
noWrap
|
||||
>
|
||||
<small>{t("Update At")}: </small>
|
||||
{time.fromNow()}
|
||||
</Typography>
|
||||
</Box>
|
||||
}
|
||||
secondary={
|
||||
<Box sx={{ display: "flex" }}>
|
||||
<TypeBox component="span">
|
||||
{provider.vehicleType}
|
||||
</TypeBox>
|
||||
<TypeBox component="span">{provider.behavior}</TypeBox>
|
||||
</Box>
|
||||
}
|
||||
/>
|
||||
<Divider orientation="vertical" flexItem />
|
||||
<Box
|
||||
sx={{
|
||||
width: 40,
|
||||
display: "flex",
|
||||
justifyContent: "center",
|
||||
alignItems: "center",
|
||||
}}
|
||||
return {
|
||||
backgroundColor: bgcolor,
|
||||
"&:hover": {
|
||||
backgroundColor: hoverColor,
|
||||
borderColor: alpha(primary.main, 0.3),
|
||||
},
|
||||
};
|
||||
},
|
||||
]}
|
||||
>
|
||||
<IconButton
|
||||
size="small"
|
||||
color="primary"
|
||||
onClick={() => updateProvider(key)}
|
||||
disabled={isUpdating}
|
||||
<ListItemText
|
||||
sx={{ px: 2, py: 1 }}
|
||||
primary={
|
||||
<Box
|
||||
sx={{
|
||||
display: "flex",
|
||||
justifyContent: "space-between",
|
||||
alignItems: "center",
|
||||
}}
|
||||
>
|
||||
<Typography
|
||||
variant="subtitle1"
|
||||
component="div"
|
||||
noWrap
|
||||
title={key}
|
||||
sx={{ display: "flex", alignItems: "center" }}
|
||||
>
|
||||
<span style={{ marginRight: "8px" }}>{key}</span>
|
||||
<TypeBox component="span">
|
||||
{provider.ruleCount}
|
||||
</TypeBox>
|
||||
</Typography>
|
||||
|
||||
<Typography
|
||||
variant="body2"
|
||||
color="text.secondary"
|
||||
noWrap
|
||||
>
|
||||
<small>{t("Update At")}: </small>
|
||||
{time.fromNow()}
|
||||
</Typography>
|
||||
</Box>
|
||||
}
|
||||
secondary={
|
||||
<Box sx={{ display: "flex" }}>
|
||||
<TypeBox component="span">
|
||||
{provider.vehicleType}
|
||||
</TypeBox>
|
||||
<TypeBox component="span">
|
||||
{provider.behavior}
|
||||
</TypeBox>
|
||||
</Box>
|
||||
}
|
||||
/>
|
||||
<Divider orientation="vertical" flexItem />
|
||||
<Box
|
||||
sx={{
|
||||
animation: isUpdating
|
||||
? "spin 1s linear infinite"
|
||||
: "none",
|
||||
"@keyframes spin": {
|
||||
"0%": { transform: "rotate(0deg)" },
|
||||
"100%": { transform: "rotate(360deg)" },
|
||||
},
|
||||
width: 40,
|
||||
display: "flex",
|
||||
justifyContent: "center",
|
||||
alignItems: "center",
|
||||
}}
|
||||
title={t("Update Provider") as string}
|
||||
>
|
||||
<RefreshRounded />
|
||||
</IconButton>
|
||||
</Box>
|
||||
</ListItem>
|
||||
);
|
||||
})}
|
||||
<IconButton
|
||||
size="small"
|
||||
color="primary"
|
||||
onClick={() => updateProvider(key)}
|
||||
disabled={isUpdating}
|
||||
sx={{
|
||||
animation: isUpdating
|
||||
? "spin 1s linear infinite"
|
||||
: "none",
|
||||
"@keyframes spin": {
|
||||
"0%": { transform: "rotate(0deg)" },
|
||||
"100%": { transform: "rotate(360deg)" },
|
||||
},
|
||||
}}
|
||||
title={t("Update Provider") as string}
|
||||
>
|
||||
<RefreshRounded />
|
||||
</IconButton>
|
||||
</Box>
|
||||
</ListItem>
|
||||
);
|
||||
})}
|
||||
</List>
|
||||
</DialogContent>
|
||||
|
||||
|
||||
@@ -16,16 +16,11 @@ import type { Ref } from "react";
|
||||
import { useImperativeHandle, useState } from "react";
|
||||
import { useTranslation } from "react-i18next";
|
||||
import { mutate } from "swr";
|
||||
import { closeAllConnections, upgradeCore } from "tauri-plugin-mihomo-api";
|
||||
|
||||
import { BaseDialog, DialogRef } from "@/components/base";
|
||||
import { useVerge } from "@/hooks/use-verge";
|
||||
import {
|
||||
changeClashCore,
|
||||
closeAllConnections,
|
||||
forceRefreshClashConfig,
|
||||
restartCore,
|
||||
upgradeCore,
|
||||
} from "@/services/cmds";
|
||||
import { changeClashCore, restartCore } from "@/services/cmds";
|
||||
import { showNotice } from "@/services/noticeService";
|
||||
|
||||
const VALID_CORE = [
|
||||
@@ -66,8 +61,6 @@ export function ClashCoreViewer({ ref }: { ref?: Ref<DialogRef> }) {
|
||||
|
||||
mutateVerge();
|
||||
setTimeout(async () => {
|
||||
// 核心切换后强制刷新配置缓存
|
||||
await forceRefreshClashConfig();
|
||||
mutate("getClashConfig");
|
||||
mutate("getVersion");
|
||||
setChangingCore(null);
|
||||
|
||||
@@ -20,6 +20,7 @@ import {
|
||||
} from "react";
|
||||
import { useTranslation } from "react-i18next";
|
||||
import useSWR, { mutate } from "swr";
|
||||
import { getBaseConfig } from "tauri-plugin-mihomo-api";
|
||||
|
||||
import { BaseDialog, DialogRef, Switch } from "@/components/base";
|
||||
import { BaseFieldset } from "@/components/base/base-fieldset";
|
||||
@@ -29,7 +30,6 @@ import { useVerge } from "@/hooks/use-verge";
|
||||
import { useAppData } from "@/providers/app-data-context";
|
||||
import {
|
||||
getAutotemProxy,
|
||||
getClashConfig,
|
||||
getNetworkInterfacesInfo,
|
||||
getSystemHostname,
|
||||
getSystemProxy,
|
||||
@@ -123,26 +123,21 @@ export const SysproxyViewer = forwardRef<DialogRef>((props, ref) => {
|
||||
return "127.0.0.1,192.168.0.0/16,10.0.0.0/8,172.16.0.0/12,172.29.0.0/16,localhost,*.local,*.crashlytics.com,<local>";
|
||||
};
|
||||
|
||||
const { data: clashConfig } = useSWR("getClashConfig", getClashConfig, {
|
||||
const { data: clashConfig } = useSWR("getClashConfig", getBaseConfig, {
|
||||
revalidateOnFocus: false,
|
||||
revalidateIfStale: true,
|
||||
dedupingInterval: 1000,
|
||||
errorRetryInterval: 5000,
|
||||
});
|
||||
|
||||
const [prevMixedPort, setPrevMixedPort] = useState(
|
||||
clashConfig?.["mixed-port"],
|
||||
);
|
||||
const [prevMixedPort, setPrevMixedPort] = useState(clashConfig?.mixedPort);
|
||||
|
||||
useEffect(() => {
|
||||
if (
|
||||
clashConfig?.["mixed-port"] &&
|
||||
clashConfig?.["mixed-port"] !== prevMixedPort
|
||||
) {
|
||||
setPrevMixedPort(clashConfig?.["mixed-port"]);
|
||||
if (clashConfig?.mixedPort && clashConfig.mixedPort !== prevMixedPort) {
|
||||
setPrevMixedPort(clashConfig.mixedPort);
|
||||
resetSystemProxy();
|
||||
}
|
||||
}, [clashConfig?.["mixed-port"]]);
|
||||
}, [clashConfig?.mixedPort]);
|
||||
|
||||
const resetSystemProxy = async () => {
|
||||
try {
|
||||
@@ -180,7 +175,7 @@ export const SysproxyViewer = forwardRef<DialogRef>((props, ref) => {
|
||||
|
||||
if (isPacMode) {
|
||||
const host = value.proxy_host || "127.0.0.1";
|
||||
const port = verge?.verge_mixed_port || clashConfig["mixed-port"] || 7897;
|
||||
const port = verge?.verge_mixed_port || clashConfig.mixedPort || 7897;
|
||||
return `${host}:${port}`;
|
||||
} else {
|
||||
return systemProxyAddress;
|
||||
@@ -332,7 +327,7 @@ export const SysproxyViewer = forwardRef<DialogRef>((props, ref) => {
|
||||
if (pacContent) {
|
||||
pacContent = pacContent.replace(/%proxy_host%/g, value.proxy_host);
|
||||
// 将 mixed-port 转换为字符串
|
||||
const mixedPortStr = (clashConfig?.["mixed-port"] || "").toString();
|
||||
const mixedPortStr = (clashConfig?.mixedPort || "").toString();
|
||||
pacContent = pacContent.replace(/%mixed-port%/g, mixedPortStr);
|
||||
}
|
||||
|
||||
|
||||
@@ -4,14 +4,15 @@ import { invoke } from "@tauri-apps/api/core";
|
||||
import { useLockFn } from "ahooks";
|
||||
import { useRef, useState } from "react";
|
||||
import { useTranslation } from "react-i18next";
|
||||
import { updateGeo } from "tauri-plugin-mihomo-api";
|
||||
|
||||
import { DialogRef, Switch } from "@/components/base";
|
||||
import { TooltipIcon } from "@/components/base/base-tooltip-icon";
|
||||
import { useClash } from "@/hooks/use-clash";
|
||||
import { useVerge } from "@/hooks/use-verge";
|
||||
import { invoke_uwp_tool } from "@/services/cmds";
|
||||
import { updateGeoData } from "@/services/cmds";
|
||||
import { showNotice } from "@/services/noticeService";
|
||||
import { useClashLog } from "@/services/states";
|
||||
import getSystem from "@/utils/get-system";
|
||||
|
||||
import { ClashCoreViewer } from "./mods/clash-core-viewer";
|
||||
@@ -35,6 +36,7 @@ const SettingClash = ({ onError }: Props) => {
|
||||
|
||||
const { clash, version, mutateClash, patchClash } = useClash();
|
||||
const { verge, patchVerge } = useVerge();
|
||||
const [, setClashLog] = useClashLog();
|
||||
|
||||
const {
|
||||
ipv6,
|
||||
@@ -64,7 +66,7 @@ const SettingClash = ({ onError }: Props) => {
|
||||
};
|
||||
const onUpdateGeo = async () => {
|
||||
try {
|
||||
await updateGeoData();
|
||||
await updateGeo();
|
||||
showNotice("success", t("GeoData Updated"));
|
||||
} catch (err: any) {
|
||||
showNotice("error", err?.response.data.message || err.toString());
|
||||
@@ -186,7 +188,10 @@ const SettingClash = ({ onError }: Props) => {
|
||||
onCatch={onError}
|
||||
onFormat={(e: any) => e.target.value}
|
||||
onChange={(e) => onChangeData({ "log-level": e })}
|
||||
onGuard={(e) => patchClash({ "log-level": e })}
|
||||
onGuard={(e) => {
|
||||
setClashLog((pre: any) => ({ ...pre, logLevel: e }));
|
||||
return patchClash({ "log-level": e });
|
||||
}}
|
||||
>
|
||||
<Select size="small" sx={{ width: 100, "> div": { py: "7.5px" } }}>
|
||||
<MenuItem value="debug">Debug</MenuItem>
|
||||
|
||||
@@ -5,7 +5,7 @@ import { Box, Divider, MenuItem, Menu, styled, alpha } from "@mui/material";
|
||||
import { convertFileSrc } from "@tauri-apps/api/core";
|
||||
import { UnlistenFn } from "@tauri-apps/api/event";
|
||||
import { useLockFn } from "ahooks";
|
||||
import { useEffect, useState } from "react";
|
||||
import { useCallback, useEffect, useState } from "react";
|
||||
import { useTranslation } from "react-i18next";
|
||||
|
||||
import { BaseLoading } from "@/components/base";
|
||||
@@ -44,11 +44,11 @@ export const TestItem = (props: Props) => {
|
||||
const [iconCachePath, setIconCachePath] = useState("");
|
||||
const { addListener } = useListen();
|
||||
|
||||
const onDelay = async () => {
|
||||
const onDelay = useCallback(async () => {
|
||||
setDelay(-2);
|
||||
const result = await cmdTestDelay(url);
|
||||
setDelay(result);
|
||||
};
|
||||
}, [url]);
|
||||
|
||||
useEffect(() => {
|
||||
initIconCachePath();
|
||||
|
||||
@@ -1,12 +1,11 @@
|
||||
import { useLockFn } from "ahooks";
|
||||
import useSWR, { mutate } from "swr";
|
||||
import { getVersion } from "tauri-plugin-mihomo-api";
|
||||
|
||||
import { getVersion } from "@/services/cmds";
|
||||
import {
|
||||
getClashInfo,
|
||||
patchClashConfig,
|
||||
getRuntimeConfig,
|
||||
forceRefreshClashConfig,
|
||||
} from "@/services/cmds";
|
||||
|
||||
export const useClash = () => {
|
||||
@@ -25,11 +24,9 @@ export const useClash = () => {
|
||||
mutateClash();
|
||||
});
|
||||
|
||||
const version = versionData?.premium
|
||||
? `${versionData.version} Premium`
|
||||
: versionData?.meta
|
||||
? `${versionData.version} Mihomo`
|
||||
: versionData?.version || "-";
|
||||
const version = versionData?.meta
|
||||
? `${versionData.version} Mihomo`
|
||||
: versionData?.version || "-";
|
||||
|
||||
return {
|
||||
clash,
|
||||
@@ -123,10 +120,7 @@ export const useClashInfo = () => {
|
||||
|
||||
await patchClashConfig(patch);
|
||||
mutateInfo();
|
||||
// 配置修改后强制刷新缓存
|
||||
await forceRefreshClashConfig();
|
||||
mutate("getClashConfig");
|
||||
// IPC调用不需要刷新axios实例
|
||||
};
|
||||
|
||||
return {
|
||||
|
||||
108
src/hooks/use-connection-data.ts
Normal file
108
src/hooks/use-connection-data.ts
Normal file
@@ -0,0 +1,108 @@
|
||||
import { useLocalStorage } from "foxact/use-local-storage";
|
||||
import { useEffect, useRef } from "react";
|
||||
import { mutate } from "swr";
|
||||
import useSWRSubscription from "swr/subscription";
|
||||
import { MihomoWebSocket } from "tauri-plugin-mihomo-api";
|
||||
|
||||
export const initConnData: IConnections = {
|
||||
uploadTotal: 0,
|
||||
downloadTotal: 0,
|
||||
connections: [],
|
||||
};
|
||||
|
||||
export const useConnectionData = () => {
|
||||
const [date, setDate] = useLocalStorage("mihomo_connection_date", Date.now());
|
||||
const subscriptKey = `getClashConnection-${date}`;
|
||||
|
||||
const ws = useRef<MihomoWebSocket | null>(null);
|
||||
const wsFirstConnection = useRef<boolean>(true);
|
||||
const timeoutRef = useRef<ReturnType<typeof setTimeout>>(null);
|
||||
|
||||
const response = useSWRSubscription<IConnections, any, string | null>(
|
||||
subscriptKey,
|
||||
(_key, { next }) => {
|
||||
const reconnect = async () => {
|
||||
await ws.current?.close();
|
||||
ws.current = null;
|
||||
timeoutRef.current = setTimeout(async () => await connect(), 500);
|
||||
};
|
||||
|
||||
const connect = () =>
|
||||
MihomoWebSocket.connect_connections()
|
||||
.then((ws_) => {
|
||||
ws.current = ws_;
|
||||
if (timeoutRef.current) clearTimeout(timeoutRef.current);
|
||||
|
||||
ws_.addListener(async (msg) => {
|
||||
if (msg.type === "Text") {
|
||||
if (msg.data.startsWith("Websocket error")) {
|
||||
next(msg.data);
|
||||
await reconnect();
|
||||
} else {
|
||||
const data = JSON.parse(msg.data) as IConnections;
|
||||
next(null, (old = initConnData) => {
|
||||
const oldConn = old.connections;
|
||||
const maxLen = data.connections?.length;
|
||||
const connections: IConnectionsItem[] = [];
|
||||
const rest = (data.connections || []).filter((each) => {
|
||||
const index = oldConn.findIndex((o) => o.id === each.id);
|
||||
if (index >= 0 && index < maxLen) {
|
||||
const old = oldConn[index];
|
||||
each.curUpload = each.upload - old.upload;
|
||||
each.curDownload = each.download - old.download;
|
||||
connections[index] = each;
|
||||
return false;
|
||||
}
|
||||
return true;
|
||||
});
|
||||
for (let i = 0; i < maxLen; ++i) {
|
||||
if (!connections[i] && rest.length > 0) {
|
||||
connections[i] = rest.shift()!;
|
||||
connections[i].curUpload = 0;
|
||||
connections[i].curDownload = 0;
|
||||
}
|
||||
}
|
||||
return { ...data, connections };
|
||||
});
|
||||
}
|
||||
}
|
||||
});
|
||||
})
|
||||
.catch((_) => {
|
||||
if (!ws.current) {
|
||||
timeoutRef.current = setTimeout(async () => await connect(), 500);
|
||||
}
|
||||
});
|
||||
|
||||
if (
|
||||
wsFirstConnection.current ||
|
||||
(ws.current && !wsFirstConnection.current)
|
||||
) {
|
||||
wsFirstConnection.current = false;
|
||||
if (ws.current) {
|
||||
ws.current.close();
|
||||
ws.current = null;
|
||||
}
|
||||
connect();
|
||||
}
|
||||
|
||||
return () => {
|
||||
ws.current?.close();
|
||||
};
|
||||
},
|
||||
{
|
||||
fallbackData: initConnData,
|
||||
keepPreviousData: true,
|
||||
},
|
||||
);
|
||||
|
||||
useEffect(() => {
|
||||
mutate(`$sub$${subscriptKey}`);
|
||||
}, [date, subscriptKey]);
|
||||
|
||||
const refreshGetClashConnection = () => {
|
||||
setDate(Date.now());
|
||||
};
|
||||
|
||||
return { response, refreshGetClashConnection };
|
||||
};
|
||||
151
src/hooks/use-log-data-new.ts
Normal file
151
src/hooks/use-log-data-new.ts
Normal file
@@ -0,0 +1,151 @@
|
||||
import dayjs from "dayjs";
|
||||
import { useLocalStorage } from "foxact/use-local-storage";
|
||||
import { useEffect, useRef } from "react";
|
||||
import { mutate } from "swr";
|
||||
import useSWRSubscription from "swr/subscription";
|
||||
import { MihomoWebSocket } from "tauri-plugin-mihomo-api";
|
||||
|
||||
import { getClashLogs } from "@/services/cmds";
|
||||
import { useClashLog } from "@/services/states";
|
||||
|
||||
const MAX_LOG_NUM = 1000;
|
||||
|
||||
export const useLogData = () => {
|
||||
const [clashLog] = useClashLog();
|
||||
const enableLog = clashLog.enable;
|
||||
const logLevel = clashLog.logLevel;
|
||||
|
||||
const [date, setDate] = useLocalStorage("mihomo_logs_date", Date.now());
|
||||
const subscriptKey = enableLog ? `getClashLog-${date}` : null;
|
||||
|
||||
const ws = useRef<MihomoWebSocket | null>(null);
|
||||
const wsFirstConnection = useRef<boolean>(true);
|
||||
const timeoutRef = useRef<ReturnType<typeof setTimeout>>(null);
|
||||
|
||||
const response = useSWRSubscription<ILogItem[], any, string | null>(
|
||||
subscriptKey,
|
||||
(_key, { next }) => {
|
||||
const reconnect = async () => {
|
||||
await ws.current?.close();
|
||||
ws.current = null;
|
||||
timeoutRef.current = setTimeout(async () => await connect(), 500);
|
||||
};
|
||||
|
||||
const connect = () =>
|
||||
MihomoWebSocket.connect_logs(logLevel)
|
||||
.then(async (ws_) => {
|
||||
ws.current = ws_;
|
||||
if (timeoutRef.current) clearTimeout(timeoutRef.current);
|
||||
|
||||
const logs = await getClashLogs();
|
||||
let filterLogs: ILogItem[] = [];
|
||||
switch (logLevel) {
|
||||
case "debug":
|
||||
filterLogs = logs.filter((i) =>
|
||||
["debug", "info", "warning", "error"].includes(i.type),
|
||||
);
|
||||
break;
|
||||
case "info":
|
||||
filterLogs = logs.filter((i) =>
|
||||
["info", "warning", "error"].includes(i.type),
|
||||
);
|
||||
break;
|
||||
case "warning":
|
||||
filterLogs = logs.filter((i) =>
|
||||
["warning", "error"].includes(i.type),
|
||||
);
|
||||
break;
|
||||
case "error":
|
||||
filterLogs = logs.filter((i) => i.type === "error");
|
||||
break;
|
||||
case "silent":
|
||||
filterLogs = [];
|
||||
break;
|
||||
default:
|
||||
filterLogs = logs;
|
||||
break;
|
||||
}
|
||||
next(null, filterLogs);
|
||||
|
||||
const buffer: ILogItem[] = [];
|
||||
let flushTimer: ReturnType<typeof setTimeout> | null = null;
|
||||
const flush = () => {
|
||||
if (buffer.length > 0) {
|
||||
next(null, (l) => {
|
||||
let newList = [...(l ?? []), ...buffer.splice(0)];
|
||||
if (newList.length > MAX_LOG_NUM) {
|
||||
newList = newList.slice(
|
||||
-Math.min(MAX_LOG_NUM, newList.length),
|
||||
);
|
||||
}
|
||||
return newList;
|
||||
});
|
||||
}
|
||||
flushTimer = null;
|
||||
};
|
||||
ws_.addListener(async (msg) => {
|
||||
if (msg.type === "Text") {
|
||||
if (msg.data.startsWith("Websocket error")) {
|
||||
next(msg.data);
|
||||
await reconnect();
|
||||
} else {
|
||||
const data = JSON.parse(msg.data) as ILogItem;
|
||||
data.time = dayjs().format("MM-DD HH:mm:ss");
|
||||
buffer.push(data);
|
||||
|
||||
// flush data
|
||||
if (!flushTimer) {
|
||||
flushTimer = setTimeout(flush, 50);
|
||||
}
|
||||
}
|
||||
}
|
||||
});
|
||||
})
|
||||
.catch((_) => {
|
||||
if (!ws.current) {
|
||||
timeoutRef.current = setTimeout(async () => await connect(), 500);
|
||||
}
|
||||
});
|
||||
|
||||
if (
|
||||
wsFirstConnection.current ||
|
||||
(ws.current && !wsFirstConnection.current)
|
||||
) {
|
||||
wsFirstConnection.current = false;
|
||||
if (ws.current) {
|
||||
ws.current.close();
|
||||
ws.current = null;
|
||||
}
|
||||
connect();
|
||||
}
|
||||
|
||||
return () => {
|
||||
ws.current?.close();
|
||||
};
|
||||
},
|
||||
{
|
||||
fallbackData: [],
|
||||
keepPreviousData: true,
|
||||
},
|
||||
);
|
||||
|
||||
useEffect(() => {
|
||||
mutate(`$sub$${subscriptKey}`);
|
||||
}, [date, subscriptKey]);
|
||||
|
||||
useEffect(() => {
|
||||
if (!logLevel) return;
|
||||
ws.current?.close();
|
||||
setDate(Date.now());
|
||||
}, [logLevel]);
|
||||
|
||||
const refreshGetClashLog = (clear = false) => {
|
||||
if (clear) {
|
||||
mutate(`$sub$${subscriptKey}`, []);
|
||||
} else {
|
||||
setDate(Date.now());
|
||||
}
|
||||
};
|
||||
|
||||
return { response, refreshGetClashLog };
|
||||
};
|
||||
@@ -1,11 +1,11 @@
|
||||
import {
|
||||
useGlobalLogData,
|
||||
clearGlobalLogs,
|
||||
LogLevel,
|
||||
// LogLevel,
|
||||
} from "@/services/global-log-service";
|
||||
|
||||
// 为了向后兼容,导出相同的类型
|
||||
export type { LogLevel };
|
||||
// export type { LogLevel };
|
||||
|
||||
export const useLogData = useGlobalLogData;
|
||||
|
||||
|
||||
84
src/hooks/use-memory-data.ts
Normal file
84
src/hooks/use-memory-data.ts
Normal file
@@ -0,0 +1,84 @@
|
||||
import { useLocalStorage } from "foxact/use-local-storage";
|
||||
import { useEffect, useRef } from "react";
|
||||
import { mutate } from "swr";
|
||||
import useSWRSubscription from "swr/subscription";
|
||||
import { MihomoWebSocket } from "tauri-plugin-mihomo-api";
|
||||
|
||||
export interface IMemoryUsageItem {
|
||||
inuse: number;
|
||||
oslimit?: number;
|
||||
}
|
||||
|
||||
export const useMemoryData = () => {
|
||||
const [date, setDate] = useLocalStorage("mihomo_memory_date", Date.now());
|
||||
const subscriptKey = `getClashMemory-${date}`;
|
||||
|
||||
const ws = useRef<MihomoWebSocket | null>(null);
|
||||
const wsFirstConnection = useRef<boolean>(true);
|
||||
const timeoutRef = useRef<ReturnType<typeof setTimeout>>(null);
|
||||
|
||||
const response = useSWRSubscription<IMemoryUsageItem, any, string | null>(
|
||||
subscriptKey,
|
||||
(_key, { next }) => {
|
||||
const reconnect = async () => {
|
||||
await ws.current?.close();
|
||||
ws.current = null;
|
||||
timeoutRef.current = setTimeout(async () => await connect(), 500);
|
||||
};
|
||||
|
||||
const connect = () =>
|
||||
MihomoWebSocket.connect_memory()
|
||||
.then((ws_) => {
|
||||
ws.current = ws_;
|
||||
if (timeoutRef.current) clearTimeout(timeoutRef.current);
|
||||
|
||||
ws_.addListener(async (msg) => {
|
||||
if (msg.type === "Text") {
|
||||
if (msg.data.startsWith("Websocket error")) {
|
||||
next(msg.data, { inuse: 0 });
|
||||
await reconnect();
|
||||
} else {
|
||||
const data = JSON.parse(msg.data) as IMemoryUsageItem;
|
||||
next(null, data);
|
||||
}
|
||||
}
|
||||
});
|
||||
})
|
||||
.catch((_) => {
|
||||
if (!ws.current) {
|
||||
timeoutRef.current = setTimeout(async () => await connect(), 500);
|
||||
}
|
||||
});
|
||||
|
||||
if (
|
||||
wsFirstConnection.current ||
|
||||
(ws.current && !wsFirstConnection.current)
|
||||
) {
|
||||
wsFirstConnection.current = false;
|
||||
if (ws.current) {
|
||||
ws.current.close();
|
||||
ws.current = null;
|
||||
}
|
||||
connect();
|
||||
}
|
||||
|
||||
return () => {
|
||||
ws.current?.close();
|
||||
};
|
||||
},
|
||||
{
|
||||
fallbackData: { inuse: 0 },
|
||||
keepPreviousData: true,
|
||||
},
|
||||
);
|
||||
|
||||
useEffect(() => {
|
||||
mutate(`$sub$${subscriptKey}`);
|
||||
}, [date, subscriptKey]);
|
||||
|
||||
const refreshGetClashMemory = () => {
|
||||
setDate(Date.now());
|
||||
};
|
||||
|
||||
return { response, refreshGetClashMemory };
|
||||
};
|
||||
@@ -1,12 +1,12 @@
|
||||
import useSWR, { mutate } from "swr";
|
||||
import { selectNodeForGroup } from "tauri-plugin-mihomo-api";
|
||||
|
||||
import {
|
||||
getProfiles,
|
||||
patchProfile,
|
||||
patchProfilesConfig,
|
||||
forceRefreshProxies,
|
||||
} from "@/services/cmds";
|
||||
import { getProxies, updateProxy } from "@/services/cmds";
|
||||
import { calcuProxies } from "@/services/cmds";
|
||||
|
||||
export const useProfiles = () => {
|
||||
const {
|
||||
@@ -72,7 +72,7 @@ export const useProfiles = () => {
|
||||
console.log("[ActivateSelected] 开始处理代理选择");
|
||||
|
||||
const [proxiesData, profileData] = await Promise.all([
|
||||
getProxies(),
|
||||
calcuProxies(),
|
||||
getProfiles(),
|
||||
]);
|
||||
|
||||
@@ -124,7 +124,7 @@ export const useProfiles = () => {
|
||||
`[ActivateSelected] 需要切换代理组 ${name}: ${now} -> ${targetProxy}`,
|
||||
);
|
||||
hasChange = true;
|
||||
updateProxy(name, targetProxy);
|
||||
selectNodeForGroup(name, targetProxy);
|
||||
}
|
||||
|
||||
newSelected.push({ name, now: targetProxy || now });
|
||||
@@ -141,11 +141,8 @@ export const useProfiles = () => {
|
||||
await patchProfile(profileData.current!, { selected: newSelected });
|
||||
console.log("[ActivateSelected] 代理选择配置保存成功");
|
||||
|
||||
// 切换节点后强制刷新后端缓存
|
||||
await forceRefreshProxies();
|
||||
|
||||
setTimeout(() => {
|
||||
mutate("getProxies", getProxies());
|
||||
mutate("getProxies", calcuProxies());
|
||||
}, 100);
|
||||
} catch (error: any) {
|
||||
console.error(
|
||||
|
||||
@@ -1,24 +1,22 @@
|
||||
import { useLockFn } from "ahooks";
|
||||
import { useCallback, useMemo } from "react";
|
||||
import {
|
||||
closeConnections,
|
||||
getConnections,
|
||||
selectNodeForGroup,
|
||||
} from "tauri-plugin-mihomo-api";
|
||||
|
||||
import { useProfiles } from "@/hooks/use-profiles";
|
||||
import { useVerge } from "@/hooks/use-verge";
|
||||
import {
|
||||
updateProxy,
|
||||
updateProxyAndSync,
|
||||
forceRefreshProxies,
|
||||
syncTrayProxySelection,
|
||||
getConnections,
|
||||
deleteConnection,
|
||||
} from "@/services/cmds";
|
||||
import { syncTrayProxySelection } from "@/services/cmds";
|
||||
|
||||
// 缓存连接清理
|
||||
const cleanupConnections = async (previousProxy: string) => {
|
||||
try {
|
||||
const { connections } = await getConnections();
|
||||
const cleanupPromises = connections
|
||||
const cleanupPromises = (connections ?? [])
|
||||
.filter((conn) => conn.chains.includes(previousProxy))
|
||||
.map((conn) => deleteConnection(conn.id));
|
||||
.map((conn) => closeConnections(conn.id));
|
||||
|
||||
if (cleanupPromises.length > 0) {
|
||||
await Promise.allSettled(cleanupPromises);
|
||||
@@ -77,7 +75,8 @@ export const useProxySelection = (options: ProxySelectionOptions = {}) => {
|
||||
await patchCurrent({ selected: current.selected });
|
||||
}
|
||||
|
||||
await updateProxyAndSync(groupName, proxyName);
|
||||
await selectNodeForGroup(groupName, proxyName);
|
||||
await syncTrayProxySelection();
|
||||
console.log(
|
||||
`[ProxySelection] 代理和状态同步完成: ${groupName} -> ${proxyName}`,
|
||||
);
|
||||
@@ -98,8 +97,7 @@ export const useProxySelection = (options: ProxySelectionOptions = {}) => {
|
||||
);
|
||||
|
||||
try {
|
||||
await updateProxy(groupName, proxyName);
|
||||
await forceRefreshProxies();
|
||||
await selectNodeForGroup(groupName, proxyName);
|
||||
await syncTrayProxySelection();
|
||||
onSuccess?.();
|
||||
console.log(
|
||||
|
||||
@@ -1,8 +1,9 @@
|
||||
import useSWR, { mutate } from "swr";
|
||||
import { closeAllConnections } from "tauri-plugin-mihomo-api";
|
||||
|
||||
import { useVerge } from "@/hooks/use-verge";
|
||||
import { useAppData } from "@/providers/app-data-context";
|
||||
import { closeAllConnections, getAutotemProxy } from "@/services/cmds";
|
||||
import { getAutotemProxy } from "@/services/cmds";
|
||||
|
||||
// 系统代理状态检测统一逻辑
|
||||
export const useSystemProxyState = () => {
|
||||
|
||||
84
src/hooks/use-traffic-data.ts
Normal file
84
src/hooks/use-traffic-data.ts
Normal file
@@ -0,0 +1,84 @@
|
||||
import { useLocalStorage } from "foxact/use-local-storage";
|
||||
import { useEffect, useRef } from "react";
|
||||
import { mutate } from "swr";
|
||||
import useSWRSubscription from "swr/subscription";
|
||||
import { MihomoWebSocket } from "tauri-plugin-mihomo-api";
|
||||
|
||||
import { TrafficRef } from "@/components/layout/traffic-graph";
|
||||
|
||||
export const useTrafficData = () => {
|
||||
const [date, setDate] = useLocalStorage("mihomo_traffic_date", Date.now());
|
||||
const subscriptKey = `getClashTraffic-${date}`;
|
||||
|
||||
const trafficRef = useRef<TrafficRef>(null);
|
||||
const ws = useRef<MihomoWebSocket | null>(null);
|
||||
const wsFirstConnection = useRef<boolean>(true);
|
||||
const timeoutRef = useRef<ReturnType<typeof setTimeout>>(null);
|
||||
|
||||
const response = useSWRSubscription<ITrafficItem, any, string | null>(
|
||||
subscriptKey,
|
||||
(_key, { next }) => {
|
||||
const reconnect = async () => {
|
||||
await ws.current?.close();
|
||||
ws.current = null;
|
||||
timeoutRef.current = setTimeout(async () => await connect(), 500);
|
||||
};
|
||||
|
||||
const connect = async () => {
|
||||
MihomoWebSocket.connect_traffic()
|
||||
.then(async (ws_) => {
|
||||
ws.current = ws_;
|
||||
if (timeoutRef.current) clearTimeout(timeoutRef.current);
|
||||
|
||||
ws_.addListener(async (msg) => {
|
||||
if (msg.type === "Text") {
|
||||
if (msg.data.startsWith("Websocket error")) {
|
||||
next(msg.data, { up: 0, down: 0 });
|
||||
await reconnect();
|
||||
} else {
|
||||
const data = JSON.parse(msg.data) as ITrafficItem;
|
||||
trafficRef.current?.appendData(data);
|
||||
next(null, data);
|
||||
}
|
||||
}
|
||||
});
|
||||
})
|
||||
.catch((_) => {
|
||||
if (!ws.current) {
|
||||
timeoutRef.current = setTimeout(async () => await connect(), 500);
|
||||
}
|
||||
});
|
||||
};
|
||||
|
||||
if (
|
||||
wsFirstConnection.current ||
|
||||
(ws.current && !wsFirstConnection.current)
|
||||
) {
|
||||
wsFirstConnection.current = false;
|
||||
if (ws.current) {
|
||||
ws.current.close();
|
||||
ws.current = null;
|
||||
}
|
||||
connect();
|
||||
}
|
||||
|
||||
return () => {
|
||||
ws.current?.close();
|
||||
};
|
||||
},
|
||||
{
|
||||
fallbackData: { up: 0, down: 0 },
|
||||
keepPreviousData: true,
|
||||
},
|
||||
);
|
||||
|
||||
useEffect(() => {
|
||||
mutate(`$sub$${subscriptKey}`);
|
||||
}, [date, subscriptKey]);
|
||||
|
||||
const refreshGetClashTraffic = () => {
|
||||
setDate(Date.now());
|
||||
};
|
||||
|
||||
return { response, refreshGetClashTraffic };
|
||||
};
|
||||
@@ -1,9 +1,9 @@
|
||||
import { useState, useEffect, useRef, useCallback } from "react";
|
||||
import useSWR from "swr";
|
||||
|
||||
import { useClashInfo } from "@/hooks/use-clash";
|
||||
import { useVisibility } from "@/hooks/use-visibility";
|
||||
import { getSystemMonitorOverviewSafe } from "@/services/cmds";
|
||||
// import { useClashInfo } from "@/hooks/use-clash";
|
||||
// import { useVisibility } from "@/hooks/use-visibility";
|
||||
|
||||
import { useTrafficData } from "./use-traffic-data";
|
||||
|
||||
// 增强的流量数据点接口
|
||||
export interface ITrafficDataPoint {
|
||||
@@ -175,14 +175,17 @@ class TrafficDataSampler {
|
||||
// 全局单例
|
||||
const refCounter = new ReferenceCounter();
|
||||
let globalSampler: TrafficDataSampler | null = null;
|
||||
let lastValidData: ISystemMonitorOverview | null = null;
|
||||
// let lastValidData: ISystemMonitorOverview | null = null;
|
||||
|
||||
/**
|
||||
* 增强的流量监控Hook - 支持数据压缩、采样和引用计数
|
||||
*/
|
||||
export const useTrafficMonitorEnhanced = () => {
|
||||
const { clashInfo } = useClashInfo();
|
||||
const pageVisible = useVisibility();
|
||||
// const { clashInfo } = useClashInfo();
|
||||
// const pageVisible = useVisibility();
|
||||
const {
|
||||
response: { data: traffic },
|
||||
} = useTrafficData();
|
||||
|
||||
// 初始化采样器
|
||||
if (!globalSampler) {
|
||||
@@ -230,69 +233,87 @@ export const useTrafficMonitorEnhanced = () => {
|
||||
refCounter.onCountChange(handleCountChange);
|
||||
}, []);
|
||||
|
||||
// 只有在有引用时才启用SWR
|
||||
const shouldFetch = clashInfo && pageVisible && refCounter.getCount() > 0;
|
||||
// const monitorData = useRef<ISystemMonitorOverview | null>(null);
|
||||
useEffect(() => {
|
||||
if (globalSampler) {
|
||||
// 添加到采样器
|
||||
const timestamp = Date.now();
|
||||
const dataPoint: ITrafficDataPoint = {
|
||||
up: traffic?.up || 0,
|
||||
down: traffic?.down || 0,
|
||||
timestamp,
|
||||
name: new Date(timestamp).toLocaleTimeString("en-US", {
|
||||
hour12: false,
|
||||
hour: "2-digit",
|
||||
minute: "2-digit",
|
||||
second: "2-digit",
|
||||
}),
|
||||
};
|
||||
globalSampler.addDataPoint(dataPoint);
|
||||
triggerUpdate();
|
||||
}
|
||||
}, [traffic, triggerUpdate]);
|
||||
|
||||
const { data: monitorData, error } = useSWR<ISystemMonitorOverview>(
|
||||
shouldFetch ? "getSystemMonitorOverviewSafe" : null,
|
||||
getSystemMonitorOverviewSafe,
|
||||
{
|
||||
refreshInterval: shouldFetch ? 1000 : 0, // 只有在需要时才刷新
|
||||
keepPreviousData: true,
|
||||
onSuccess: (data) => {
|
||||
// console.log("[TrafficMonitorEnhanced] 获取到监控数据:", data);
|
||||
// const { data: monitorData, error } = useSWR<ISystemMonitorOverview>(
|
||||
// shouldFetch ? "getSystemMonitorOverviewSafe" : null,
|
||||
// getSystemMonitorOverviewSafe,
|
||||
// {
|
||||
// refreshInterval: shouldFetch ? 1000 : 0, // 只有在需要时才刷新
|
||||
// keepPreviousData: true,
|
||||
// onSuccess: (data) => {
|
||||
// // console.log("[TrafficMonitorEnhanced] 获取到监控数据:", data);
|
||||
|
||||
if (data?.traffic?.raw && globalSampler) {
|
||||
// 保存最后有效数据
|
||||
lastValidData = data;
|
||||
// if (data?.traffic?.raw && globalSampler) {
|
||||
// // 保存最后有效数据
|
||||
// lastValidData = data;
|
||||
|
||||
// 添加到采样器
|
||||
const timestamp = Date.now();
|
||||
const dataPoint: ITrafficDataPoint = {
|
||||
up: data.traffic.raw.up_rate || 0,
|
||||
down: data.traffic.raw.down_rate || 0,
|
||||
timestamp,
|
||||
name: new Date(timestamp).toLocaleTimeString("en-US", {
|
||||
hour12: false,
|
||||
hour: "2-digit",
|
||||
minute: "2-digit",
|
||||
second: "2-digit",
|
||||
}),
|
||||
};
|
||||
// // 添加到采样器
|
||||
// const timestamp = Date.now();
|
||||
// const dataPoint: ITrafficDataPoint = {
|
||||
// up: data.traffic.raw.up_rate || 0,
|
||||
// down: data.traffic.raw.down_rate || 0,
|
||||
// timestamp,
|
||||
// name: new Date(timestamp).toLocaleTimeString("en-US", {
|
||||
// hour12: false,
|
||||
// hour: "2-digit",
|
||||
// minute: "2-digit",
|
||||
// second: "2-digit",
|
||||
// }),
|
||||
// };
|
||||
|
||||
globalSampler.addDataPoint(dataPoint);
|
||||
triggerUpdate();
|
||||
}
|
||||
},
|
||||
onError: (error) => {
|
||||
console.error(
|
||||
"[TrafficMonitorEnhanced] 网络错误,使用最后有效数据. 错误详情:",
|
||||
{
|
||||
message: error?.message || "未知错误",
|
||||
stack: error?.stack || "无堆栈信息",
|
||||
},
|
||||
);
|
||||
// 网络错误时不清空数据,继续使用最后有效值
|
||||
// 但是添加一个错误标记的数据点(流量为0)
|
||||
if (globalSampler) {
|
||||
const timestamp = Date.now();
|
||||
const errorPoint: ITrafficDataPoint = {
|
||||
up: 0,
|
||||
down: 0,
|
||||
timestamp,
|
||||
name: new Date(timestamp).toLocaleTimeString("en-US", {
|
||||
hour12: false,
|
||||
hour: "2-digit",
|
||||
minute: "2-digit",
|
||||
second: "2-digit",
|
||||
}),
|
||||
};
|
||||
globalSampler.addDataPoint(errorPoint);
|
||||
triggerUpdate();
|
||||
}
|
||||
},
|
||||
},
|
||||
);
|
||||
// globalSampler.addDataPoint(dataPoint);
|
||||
// triggerUpdate();
|
||||
// }
|
||||
// },
|
||||
// onError: (error) => {
|
||||
// console.error(
|
||||
// "[TrafficMonitorEnhanced] 网络错误,使用最后有效数据. 错误详情:",
|
||||
// {
|
||||
// message: error?.message || "未知错误",
|
||||
// stack: error?.stack || "无堆栈信息",
|
||||
// },
|
||||
// );
|
||||
// // 网络错误时不清空数据,继续使用最后有效值
|
||||
// // 但是添加一个错误标记的数据点(流量为0)
|
||||
// if (globalSampler) {
|
||||
// const timestamp = Date.now();
|
||||
// const errorPoint: ITrafficDataPoint = {
|
||||
// up: 0,
|
||||
// down: 0,
|
||||
// timestamp,
|
||||
// name: new Date(timestamp).toLocaleTimeString("en-US", {
|
||||
// hour12: false,
|
||||
// hour: "2-digit",
|
||||
// minute: "2-digit",
|
||||
// second: "2-digit",
|
||||
// }),
|
||||
// };
|
||||
// globalSampler.addDataPoint(errorPoint);
|
||||
// triggerUpdate();
|
||||
// }
|
||||
// },
|
||||
// },
|
||||
// );
|
||||
|
||||
// 获取指定时间范围的数据
|
||||
const getDataForTimeRange = useCallback(
|
||||
@@ -324,28 +345,28 @@ export const useTrafficMonitorEnhanced = () => {
|
||||
}, []);
|
||||
|
||||
// 构建返回的监控数据,优先使用当前数据,fallback到最后有效数据
|
||||
const currentData = monitorData || lastValidData;
|
||||
const trafficMonitorData = {
|
||||
traffic: currentData?.traffic || {
|
||||
raw: { up: 0, down: 0, up_rate: 0, down_rate: 0 },
|
||||
formatted: {
|
||||
up_rate: "0B",
|
||||
down_rate: "0B",
|
||||
total_up: "0B",
|
||||
total_down: "0B",
|
||||
},
|
||||
is_fresh: false,
|
||||
},
|
||||
memory: currentData?.memory || {
|
||||
raw: { inuse: 0, oslimit: 0, usage_percent: 0 },
|
||||
formatted: { inuse: "0B", oslimit: "0B", usage_percent: 0 },
|
||||
is_fresh: false,
|
||||
},
|
||||
};
|
||||
// const currentData = monitorData.current || lastValidData;
|
||||
// const trafficMonitorData = {
|
||||
// traffic: currentData?.traffic || {
|
||||
// raw: { up: 0, down: 0, up_rate: 0, down_rate: 0 },
|
||||
// formatted: {
|
||||
// up_rate: "0B",
|
||||
// down_rate: "0B",
|
||||
// total_up: "0B",
|
||||
// total_down: "0B",
|
||||
// },
|
||||
// is_fresh: false,
|
||||
// },
|
||||
// memory: currentData?.memory || {
|
||||
// raw: { inuse: 0, oslimit: 0, usage_percent: 0 },
|
||||
// formatted: { inuse: "0B", oslimit: "0B", usage_percent: 0 },
|
||||
// is_fresh: false,
|
||||
// },
|
||||
// };
|
||||
|
||||
return {
|
||||
// 监控数据
|
||||
monitorData: trafficMonitorData,
|
||||
// monitorData: trafficMonitorData,
|
||||
|
||||
// 图表数据管理
|
||||
graphData: {
|
||||
@@ -355,10 +376,9 @@ export const useTrafficMonitorEnhanced = () => {
|
||||
},
|
||||
|
||||
// 状态信息
|
||||
isLoading: !currentData && !error,
|
||||
error,
|
||||
isDataFresh: currentData?.traffic?.is_fresh || false,
|
||||
hasValidData: !!lastValidData,
|
||||
// isLoading: !currentData,
|
||||
// isDataFresh: currentData?.traffic?.is_fresh || false,
|
||||
// hasValidData: !!lastValidData,
|
||||
|
||||
// 性能统计
|
||||
samplerStats: getSamplerStats(),
|
||||
@@ -369,30 +389,28 @@ export const useTrafficMonitorEnhanced = () => {
|
||||
/**
|
||||
* 轻量级流量数据Hook
|
||||
*/
|
||||
export const useTrafficDataEnhanced = () => {
|
||||
const { monitorData, isLoading, error, isDataFresh, hasValidData } =
|
||||
useTrafficMonitorEnhanced();
|
||||
// export const useTrafficDataEnhanced = () => {
|
||||
// const { monitorData, isLoading, isDataFresh, hasValidData } =
|
||||
// useTrafficMonitorEnhanced();
|
||||
|
||||
return {
|
||||
traffic: monitorData.traffic,
|
||||
memory: monitorData.memory,
|
||||
isLoading,
|
||||
error,
|
||||
isDataFresh,
|
||||
hasValidData,
|
||||
};
|
||||
};
|
||||
// return {
|
||||
// traffic: monitorData.traffic,
|
||||
// memory: monitorData.memory,
|
||||
// isLoading,
|
||||
// isDataFresh,
|
||||
// hasValidData,
|
||||
// };
|
||||
// };
|
||||
|
||||
/**
|
||||
* 图表数据Hook
|
||||
*/
|
||||
export const useTrafficGraphDataEnhanced = () => {
|
||||
const { graphData, isDataFresh, samplerStats, referenceCount } =
|
||||
const { graphData, samplerStats, referenceCount } =
|
||||
useTrafficMonitorEnhanced();
|
||||
|
||||
return {
|
||||
...graphData,
|
||||
isDataFresh,
|
||||
samplerStats,
|
||||
referenceCount,
|
||||
};
|
||||
|
||||
15
src/main.tsx
15
src/main.tsx
@@ -3,14 +3,11 @@
|
||||
import "./assets/styles/index.scss";
|
||||
|
||||
import { ResizeObserver } from "@juggle/resize-observer";
|
||||
if (!window.ResizeObserver) {
|
||||
window.ResizeObserver = ResizeObserver;
|
||||
}
|
||||
|
||||
import { ComposeContextProvider } from "foxact/compose-context-provider";
|
||||
import React from "react";
|
||||
import { createRoot } from "react-dom/client";
|
||||
import { BrowserRouter } from "react-router-dom";
|
||||
import { MihomoWebSocket } from "tauri-plugin-mihomo-api";
|
||||
|
||||
import { BaseErrorBoundary } from "./components/base";
|
||||
import Layout from "./pages/_layout";
|
||||
@@ -22,6 +19,10 @@ import {
|
||||
UpdateStateProvider,
|
||||
} from "./services/states";
|
||||
|
||||
if (!window.ResizeObserver) {
|
||||
window.ResizeObserver = ResizeObserver;
|
||||
}
|
||||
|
||||
const mainElementId = "root";
|
||||
const container = document.getElementById(mainElementId);
|
||||
|
||||
@@ -90,3 +91,9 @@ window.addEventListener("error", (event) => {
|
||||
window.addEventListener("unhandledrejection", (event) => {
|
||||
console.error("[main.tsx] 未处理的Promise拒绝:", event.reason);
|
||||
});
|
||||
|
||||
// 页面关闭/刷新事件
|
||||
window.addEventListener("beforeunload", async () => {
|
||||
// 强制清理所有 WebSocket 实例, 防止内存泄漏
|
||||
await MihomoWebSocket.cleanupAll();
|
||||
});
|
||||
|
||||
@@ -1,26 +1,33 @@
|
||||
import { List, Paper, ThemeProvider, SvgIcon } from "@mui/material";
|
||||
import { List, Paper, SvgIcon, ThemeProvider } from "@mui/material";
|
||||
import { invoke } from "@tauri-apps/api/core";
|
||||
import { listen } from "@tauri-apps/api/event";
|
||||
import { getCurrentWebviewWindow } from "@tauri-apps/api/webviewWindow";
|
||||
import dayjs from "dayjs";
|
||||
import relativeTime from "dayjs/plugin/relativeTime";
|
||||
import { useLocalStorage } from "foxact/use-local-storage";
|
||||
import { useEffect, useCallback, useState, useRef } from "react";
|
||||
import React from "react";
|
||||
import React, { useCallback, useEffect, useRef, useState } from "react";
|
||||
import { useTranslation } from "react-i18next";
|
||||
import { useLocation, useRoutes, useNavigate } from "react-router-dom";
|
||||
import { useLocation, useNavigate, useRoutes } from "react-router-dom";
|
||||
import { SWRConfig, mutate } from "swr";
|
||||
|
||||
import iconDark from "@/assets/image/icon_dark.svg?react";
|
||||
import iconLight from "@/assets/image/icon_light.svg?react";
|
||||
import LogoSvg from "@/assets/image/logo.svg?react";
|
||||
import { NoticeManager } from "@/components/base/NoticeManager";
|
||||
import { LayoutItem } from "@/components/layout/layout-item";
|
||||
import { LayoutTraffic } from "@/components/layout/layout-traffic";
|
||||
import { UpdateButton } from "@/components/layout/update-button";
|
||||
import { useCustomTheme } from "@/components/layout/use-custom-theme";
|
||||
import { useClashInfo } from "@/hooks/use-clash";
|
||||
import { useConnectionData } from "@/hooks/use-connection-data";
|
||||
import { useI18n } from "@/hooks/use-i18n";
|
||||
import { useListen } from "@/hooks/use-listen";
|
||||
import { useLogData } from "@/hooks/use-log-data-new";
|
||||
import { useMemoryData } from "@/hooks/use-memory-data";
|
||||
import { useTrafficData } from "@/hooks/use-traffic-data";
|
||||
import { useVerge } from "@/hooks/use-verge";
|
||||
import { getAxios } from "@/services/api";
|
||||
import { forceRefreshClashConfig } from "@/services/cmds";
|
||||
import { useThemeMode, useEnableLog } from "@/services/states";
|
||||
import { showNotice } from "@/services/noticeService";
|
||||
import { useClashLog, useThemeMode } from "@/services/states";
|
||||
import getSystem from "@/utils/get-system";
|
||||
|
||||
import { routers } from "./_routers";
|
||||
@@ -28,19 +35,6 @@ import { routers } from "./_routers";
|
||||
import "dayjs/locale/ru";
|
||||
import "dayjs/locale/zh-cn";
|
||||
|
||||
import { useListen } from "@/hooks/use-listen";
|
||||
|
||||
import { listen } from "@tauri-apps/api/event";
|
||||
|
||||
import { useClashInfo } from "@/hooks/use-clash";
|
||||
import { initGlobalLogService } from "@/services/global-log-service";
|
||||
|
||||
import { invoke } from "@tauri-apps/api/core";
|
||||
|
||||
import { showNotice } from "@/services/noticeService";
|
||||
import { NoticeManager } from "@/components/base/NoticeManager";
|
||||
import { LogLevel } from "@/hooks/use-log-data";
|
||||
|
||||
const appWindow = getCurrentWebviewWindow();
|
||||
export const portableFlag = false;
|
||||
|
||||
@@ -157,14 +151,20 @@ const handleNoticeMessage = (
|
||||
};
|
||||
|
||||
const Layout = () => {
|
||||
useTrafficData();
|
||||
useMemoryData();
|
||||
useConnectionData();
|
||||
useLogData();
|
||||
const mode = useThemeMode();
|
||||
const isDark = mode === "light" ? false : true;
|
||||
const { t } = useTranslation();
|
||||
const { theme } = useCustomTheme();
|
||||
const { verge } = useVerge();
|
||||
const { clashInfo } = useClashInfo();
|
||||
const [enableLog] = useEnableLog();
|
||||
const [logLevel] = useLocalStorage<LogLevel>("log:log-level", "info");
|
||||
const [clashLog] = useClashLog();
|
||||
const enableLog = clashLog.enable;
|
||||
const logLevel = clashLog.logLevel;
|
||||
// const [logLevel] = useLocalStorage<LogLevel>("log:log-level", "info");
|
||||
const { language, start_page } = verge ?? {};
|
||||
const { switchLanguage } = useI18n();
|
||||
const navigate = useNavigate();
|
||||
@@ -193,19 +193,17 @@ const Layout = () => {
|
||||
);
|
||||
|
||||
// 初始化全局日志服务
|
||||
useEffect(() => {
|
||||
if (clashInfo) {
|
||||
initGlobalLogService(enableLog, logLevel);
|
||||
}
|
||||
}, [clashInfo, enableLog, logLevel]);
|
||||
// useEffect(() => {
|
||||
// if (clashInfo) {
|
||||
// initGlobalLogService(enableLog, logLevel);
|
||||
// }
|
||||
// }, [clashInfo, enableLog, logLevel]);
|
||||
|
||||
// 设置监听器
|
||||
useEffect(() => {
|
||||
const listeners = [
|
||||
addListener("verge://refresh-clash-config", async () => {
|
||||
await getAxios(true);
|
||||
// 后端配置变更事件触发,强制刷新配置缓存
|
||||
await forceRefreshClashConfig();
|
||||
mutate("getProxies");
|
||||
mutate("getVersion");
|
||||
mutate("getClashConfig");
|
||||
@@ -521,15 +519,16 @@ const Layout = () => {
|
||||
borderTopRightRadius: "0px",
|
||||
}}
|
||||
onContextMenu={(e) => {
|
||||
if (
|
||||
OS === "windows" &&
|
||||
!["input", "textarea"].includes(
|
||||
e.currentTarget.tagName.toLowerCase(),
|
||||
) &&
|
||||
!e.currentTarget.isContentEditable
|
||||
) {
|
||||
e.preventDefault();
|
||||
}
|
||||
// TODO: 禁止右键菜单
|
||||
// if (
|
||||
// OS === "windows" &&
|
||||
// !["input", "textarea"].includes(
|
||||
// e.currentTarget.tagName.toLowerCase(),
|
||||
// ) &&
|
||||
// !e.currentTarget.isContentEditable
|
||||
// ) {
|
||||
// e.preventDefault();
|
||||
// }
|
||||
}}
|
||||
sx={[
|
||||
({ palette }) => ({ bgcolor: palette.background.paper }),
|
||||
|
||||
@@ -9,6 +9,7 @@ import { useLockFn } from "ahooks";
|
||||
import { useCallback, useMemo, useRef, useState } from "react";
|
||||
import { useTranslation } from "react-i18next";
|
||||
import { Virtuoso } from "react-virtuoso";
|
||||
import { closeAllConnections } from "tauri-plugin-mihomo-api";
|
||||
|
||||
import { BaseEmpty, BasePage } from "@/components/base";
|
||||
import { BaseSearchBox } from "@/components/base/base-search-box";
|
||||
@@ -19,9 +20,8 @@ import {
|
||||
} from "@/components/connection/connection-detail";
|
||||
import { ConnectionItem } from "@/components/connection/connection-item";
|
||||
import { ConnectionTable } from "@/components/connection/connection-table";
|
||||
import { useConnectionData } from "@/hooks/use-connection-data";
|
||||
import { useVisibility } from "@/hooks/use-visibility";
|
||||
import { useAppData } from "@/providers/app-data-context";
|
||||
import { closeAllConnections } from "@/services/cmds";
|
||||
import { useConnectionSetting } from "@/services/states";
|
||||
import parseTraffic from "@/utils/parse-traffic";
|
||||
|
||||
@@ -39,8 +39,9 @@ const ConnectionsPage = () => {
|
||||
const [match, setMatch] = useState(() => (_: string) => true);
|
||||
const [curOrderOpt, setOrderOpt] = useState("Default");
|
||||
|
||||
// 使用全局数据
|
||||
const { connections } = useAppData();
|
||||
const {
|
||||
response: { data: connections },
|
||||
} = useConnectionData();
|
||||
|
||||
const [setting, setSetting] = useConnectionSetting();
|
||||
|
||||
@@ -72,30 +73,30 @@ const ConnectionsPage = () => {
|
||||
if (isPaused) {
|
||||
return (
|
||||
frozenData ?? {
|
||||
uploadTotal: connections.uploadTotal,
|
||||
downloadTotal: connections.downloadTotal,
|
||||
connections: connections.data,
|
||||
uploadTotal: connections?.uploadTotal,
|
||||
downloadTotal: connections?.downloadTotal,
|
||||
connections: connections?.connections,
|
||||
}
|
||||
);
|
||||
}
|
||||
|
||||
return {
|
||||
uploadTotal: connections.uploadTotal,
|
||||
downloadTotal: connections.downloadTotal,
|
||||
connections: connections.data,
|
||||
uploadTotal: connections?.uploadTotal,
|
||||
downloadTotal: connections?.downloadTotal,
|
||||
connections: connections?.connections,
|
||||
};
|
||||
}, [isPaused, frozenData, connections, pageVisible]);
|
||||
|
||||
const [filterConn] = useMemo(() => {
|
||||
const orderFunc = orderOpts[curOrderOpt];
|
||||
let conns = displayData.connections.filter((conn) => {
|
||||
let conns = displayData.connections?.filter((conn) => {
|
||||
const { host, destinationIP, process } = conn.metadata;
|
||||
return (
|
||||
match(host || "") || match(destinationIP || "") || match(process || "")
|
||||
);
|
||||
});
|
||||
|
||||
if (orderFunc) conns = orderFunc(conns);
|
||||
if (orderFunc) conns = orderFunc(conns ?? []);
|
||||
|
||||
return [conns];
|
||||
}, [displayData, match, curOrderOpt, orderOpts]);
|
||||
@@ -112,9 +113,9 @@ const ConnectionsPage = () => {
|
||||
setIsPaused((prev) => {
|
||||
if (!prev) {
|
||||
setFrozenData({
|
||||
uploadTotal: connections.uploadTotal,
|
||||
downloadTotal: connections.downloadTotal,
|
||||
connections: connections.data,
|
||||
uploadTotal: connections?.uploadTotal ?? 0,
|
||||
downloadTotal: connections?.downloadTotal ?? 0,
|
||||
connections: connections?.connections ?? [],
|
||||
});
|
||||
} else {
|
||||
setFrozenData(null);
|
||||
@@ -206,7 +207,7 @@ const ConnectionsPage = () => {
|
||||
<BaseSearchBox onSearch={handleSearch} />
|
||||
</Box>
|
||||
|
||||
{filterConn.length === 0 ? (
|
||||
{!filterConn || filterConn.length === 0 ? (
|
||||
<BaseEmpty />
|
||||
) : isTableLayout ? (
|
||||
<ConnectionTable
|
||||
|
||||
@@ -3,7 +3,6 @@ import {
|
||||
PauseCircleOutlineRounded,
|
||||
} from "@mui/icons-material";
|
||||
import { Box, Button, IconButton, MenuItem } from "@mui/material";
|
||||
import { useLocalStorage } from "foxact/use-local-storage";
|
||||
import { useMemo, useState } from "react";
|
||||
import { useTranslation } from "react-i18next";
|
||||
import { Virtuoso } from "react-virtuoso";
|
||||
@@ -13,27 +12,22 @@ import { BaseSearchBox } from "@/components/base/base-search-box";
|
||||
import { SearchState } from "@/components/base/base-search-box";
|
||||
import { BaseStyledSelect } from "@/components/base/base-styled-select";
|
||||
import LogItem from "@/components/log/log-item";
|
||||
import { LogLevel } from "@/hooks/use-log-data";
|
||||
import {
|
||||
useGlobalLogData,
|
||||
clearGlobalLogs,
|
||||
changeLogLevel,
|
||||
toggleLogEnabled,
|
||||
} from "@/services/global-log-service";
|
||||
import { useEnableLog } from "@/services/states";
|
||||
|
||||
// 后端通过 /logs?level={level} 进行筛选,前端不再需要手动筛选日志级别
|
||||
import { useLogData } from "@/hooks/use-log-data-new";
|
||||
import { toggleLogEnabled } from "@/services/global-log-service";
|
||||
import { LogFilter, useClashLog } from "@/services/states";
|
||||
|
||||
const LogPage = () => {
|
||||
const { t } = useTranslation();
|
||||
const [enableLog, setEnableLog] = useEnableLog();
|
||||
const [logLevel, setLogLevel] = useLocalStorage<LogLevel>(
|
||||
"log:log-level",
|
||||
"info",
|
||||
);
|
||||
const [clashLog, setClashLog] = useClashLog();
|
||||
const enableLog = clashLog.enable;
|
||||
const logState = clashLog.logFilter;
|
||||
|
||||
const [match, setMatch] = useState(() => (_: string) => true);
|
||||
const logData = useGlobalLogData("all");
|
||||
const [searchState, setSearchState] = useState<SearchState>();
|
||||
const {
|
||||
response: { data: logData },
|
||||
refreshGetClashLog,
|
||||
} = useLogData();
|
||||
|
||||
const filterLogs = useMemo(() => {
|
||||
if (!logData || logData.length === 0) {
|
||||
@@ -49,18 +43,21 @@ const LogPage = () => {
|
||||
|
||||
const matchesSearch = match(searchText);
|
||||
|
||||
return matchesSearch;
|
||||
return (
|
||||
(logState == "all" ? true : data.type.includes(logState)) &&
|
||||
matchesSearch
|
||||
);
|
||||
});
|
||||
}, [logData, match]);
|
||||
}, [logData, logState, match]);
|
||||
|
||||
const handleLogLevelChange = (newLevel: LogLevel) => {
|
||||
setLogLevel(newLevel);
|
||||
changeLogLevel(newLevel);
|
||||
const handleLogLevelChange = (newLevel: string) => {
|
||||
setClashLog((pre: any) => ({ ...pre, logFilter: newLevel }));
|
||||
// changeLogLevel(newLevel);
|
||||
};
|
||||
|
||||
const handleToggleLog = async () => {
|
||||
await toggleLogEnabled();
|
||||
setEnableLog(!enableLog);
|
||||
setClashLog((pre: any) => ({ ...pre, enable: !enableLog }));
|
||||
};
|
||||
|
||||
return (
|
||||
@@ -92,7 +89,8 @@ const LogPage = () => {
|
||||
size="small"
|
||||
variant="contained"
|
||||
onClick={() => {
|
||||
clearGlobalLogs();
|
||||
refreshGetClashLog(true);
|
||||
// clearGlobalLogs();
|
||||
}}
|
||||
>
|
||||
{t("Clear")}
|
||||
@@ -111,14 +109,14 @@ const LogPage = () => {
|
||||
}}
|
||||
>
|
||||
<BaseStyledSelect
|
||||
value={logLevel}
|
||||
onChange={(e) => handleLogLevelChange(e.target.value as LogLevel)}
|
||||
value={logState}
|
||||
onChange={(e) => handleLogLevelChange(e.target.value as LogFilter)}
|
||||
>
|
||||
<MenuItem value="all">ALL</MenuItem>
|
||||
<MenuItem value="debug">DEBUG</MenuItem>
|
||||
<MenuItem value="info">INFO</MenuItem>
|
||||
<MenuItem value="warning">WARNING</MenuItem>
|
||||
<MenuItem value="error">ERROR</MenuItem>
|
||||
<MenuItem value="warn">WARN</MenuItem>
|
||||
<MenuItem value="err">ERROR</MenuItem>
|
||||
</BaseStyledSelect>
|
||||
<BaseSearchBox
|
||||
onSearch={(matcher, state) => {
|
||||
|
||||
@@ -34,6 +34,7 @@ import { useCallback, useEffect, useMemo, useRef, useState } from "react";
|
||||
import { useTranslation } from "react-i18next";
|
||||
import { useLocation } from "react-router-dom";
|
||||
import useSWR, { mutate } from "swr";
|
||||
import { closeAllConnections } from "tauri-plugin-mihomo-api";
|
||||
|
||||
import { BasePage, DialogRef } from "@/components/base";
|
||||
import { BaseStyledTextField } from "@/components/base/base-styled-text-field";
|
||||
@@ -47,7 +48,6 @@ import { ConfigViewer } from "@/components/setting/mods/config-viewer";
|
||||
import { useListen } from "@/hooks/use-listen";
|
||||
import { useProfiles } from "@/hooks/use-profiles";
|
||||
import {
|
||||
closeAllConnections,
|
||||
createProfile,
|
||||
deleteProfile,
|
||||
enhanceProfiles,
|
||||
|
||||
@@ -3,14 +3,13 @@ import { useLockFn } from "ahooks";
|
||||
import { useEffect, useMemo, useState } from "react";
|
||||
import { useTranslation } from "react-i18next";
|
||||
import useSWR from "swr";
|
||||
import { closeAllConnections, getBaseConfig } from "tauri-plugin-mihomo-api";
|
||||
|
||||
import { BasePage } from "@/components/base";
|
||||
import { ProviderButton } from "@/components/proxy/provider-button";
|
||||
import { ProxyGroups } from "@/components/proxy/proxy-groups";
|
||||
import { useVerge } from "@/hooks/use-verge";
|
||||
import {
|
||||
closeAllConnections,
|
||||
getClashConfig,
|
||||
getRuntimeProxyChainConfig,
|
||||
patchClashMode,
|
||||
updateProxyChainConfigInRuntime,
|
||||
@@ -33,7 +32,7 @@ const ProxyPage = () => {
|
||||
|
||||
const { data: clashConfig, mutate: mutateClash } = useSWR(
|
||||
"getClashConfig",
|
||||
getClashConfig,
|
||||
getBaseConfig,
|
||||
{
|
||||
revalidateOnFocus: false,
|
||||
revalidateIfStale: true,
|
||||
|
||||
@@ -74,7 +74,7 @@ const RulesPage = () => {
|
||||
<BaseSearchBox onSearch={(match) => setMatch(() => match)} />
|
||||
</Box>
|
||||
|
||||
{filteredRules.length > 0 ? (
|
||||
{filteredRules && filteredRules.length > 0 ? (
|
||||
<>
|
||||
<Virtuoso
|
||||
ref={virtuosoRef}
|
||||
|
||||
@@ -1,22 +1,28 @@
|
||||
import { createContext, use } from "react";
|
||||
import {
|
||||
BaseConfig,
|
||||
ProxyProvider,
|
||||
Rule,
|
||||
RuleProvider,
|
||||
} from "tauri-plugin-mihomo-api";
|
||||
|
||||
export interface AppDataContextType {
|
||||
proxies: any;
|
||||
clashConfig: any;
|
||||
rules: any[];
|
||||
clashConfig: BaseConfig;
|
||||
rules: Rule[];
|
||||
sysproxy: any;
|
||||
runningMode?: string;
|
||||
uptime: number;
|
||||
proxyProviders: any;
|
||||
ruleProviders: any;
|
||||
connections: {
|
||||
data: ConnectionWithSpeed[];
|
||||
count: number;
|
||||
uploadTotal: number;
|
||||
downloadTotal: number;
|
||||
};
|
||||
traffic: { up: number; down: number };
|
||||
memory: { inuse: number };
|
||||
proxyProviders: Record<string, ProxyProvider>;
|
||||
ruleProviders: Record<string, RuleProvider>;
|
||||
// connections: {
|
||||
// data: ConnectionWithSpeed[];
|
||||
// count: number;
|
||||
// uploadTotal: number;
|
||||
// downloadTotal: number;
|
||||
// };
|
||||
// traffic: { up: number; down: number };
|
||||
// memory: { inuse: number };
|
||||
systemProxyAddress: string;
|
||||
|
||||
refreshProxy: () => Promise<any>;
|
||||
|
||||
@@ -1,30 +1,24 @@
|
||||
import { listen } from "@tauri-apps/api/event";
|
||||
import React, { useCallback, useEffect, useMemo, useRef } from "react";
|
||||
import React, { useCallback, useEffect, useMemo } from "react";
|
||||
import useSWR from "swr";
|
||||
|
||||
import { useClashInfo } from "@/hooks/use-clash";
|
||||
import { useVerge } from "@/hooks/use-verge";
|
||||
import { useVisibility } from "@/hooks/use-visibility";
|
||||
import {
|
||||
forceRefreshProxies,
|
||||
getAppUptime,
|
||||
getClashConfig,
|
||||
getConnections,
|
||||
getMemoryData,
|
||||
getProxies,
|
||||
getProxyProviders,
|
||||
getBaseConfig,
|
||||
getRuleProviders,
|
||||
getRules,
|
||||
} from "tauri-plugin-mihomo-api";
|
||||
|
||||
// import { useClashInfo } from "@/hooks/use-clash";
|
||||
import { useVerge } from "@/hooks/use-verge";
|
||||
// import { useVisibility } from "@/hooks/use-visibility";
|
||||
import {
|
||||
calcuProxies,
|
||||
calcuProxyProviders,
|
||||
getAppUptime,
|
||||
getRunningMode,
|
||||
getSystemProxy,
|
||||
getTrafficData,
|
||||
} from "@/services/cmds";
|
||||
|
||||
import {
|
||||
AppDataContext,
|
||||
type ConnectionSpeedData,
|
||||
type ConnectionWithSpeed,
|
||||
} from "./app-data-context";
|
||||
import { AppDataContext, AppDataContextType } from "./app-data-context";
|
||||
|
||||
// 全局数据提供者组件
|
||||
export const AppDataProvider = ({
|
||||
@@ -32,60 +26,60 @@ export const AppDataProvider = ({
|
||||
}: {
|
||||
children: React.ReactNode;
|
||||
}) => {
|
||||
const pageVisible = useVisibility();
|
||||
const { clashInfo } = useClashInfo();
|
||||
// const pageVisible = useVisibility();
|
||||
// const { clashInfo } = useClashInfo();
|
||||
const { verge } = useVerge();
|
||||
|
||||
// 存储上一次连接数据用于速度计算
|
||||
const previousConnectionsRef = useRef<Map<string, ConnectionSpeedData>>(
|
||||
new Map(),
|
||||
);
|
||||
// const previousConnectionsRef = useRef<Map<string, ConnectionSpeedData>>(
|
||||
// new Map(),
|
||||
// );
|
||||
|
||||
// 计算连接速度的函数
|
||||
const calculateConnectionSpeeds = (
|
||||
currentConnections: IConnectionsItem[],
|
||||
): ConnectionWithSpeed[] => {
|
||||
const now = Date.now();
|
||||
const currentMap = new Map<string, ConnectionSpeedData>();
|
||||
// const calculateConnectionSpeeds = (
|
||||
// currentConnections: IConnectionsItem[],
|
||||
// ): ConnectionWithSpeed[] => {
|
||||
// const now = Date.now();
|
||||
// const currentMap = new Map<string, ConnectionSpeedData>();
|
||||
|
||||
return currentConnections.map((conn) => {
|
||||
const connWithSpeed: ConnectionWithSpeed = {
|
||||
...conn,
|
||||
curUpload: 0,
|
||||
curDownload: 0,
|
||||
};
|
||||
// return currentConnections.map((conn) => {
|
||||
// const connWithSpeed: ConnectionWithSpeed = {
|
||||
// ...conn,
|
||||
// curUpload: 0,
|
||||
// curDownload: 0,
|
||||
// };
|
||||
|
||||
const currentData: ConnectionSpeedData = {
|
||||
id: conn.id,
|
||||
upload: conn.upload,
|
||||
download: conn.download,
|
||||
timestamp: now,
|
||||
};
|
||||
// const currentData: ConnectionSpeedData = {
|
||||
// id: conn.id,
|
||||
// upload: conn.upload,
|
||||
// download: conn.download,
|
||||
// timestamp: now,
|
||||
// };
|
||||
|
||||
currentMap.set(conn.id, currentData);
|
||||
// currentMap.set(conn.id, currentData);
|
||||
|
||||
const previousData = previousConnectionsRef.current.get(conn.id);
|
||||
if (previousData) {
|
||||
const timeDiff = (now - previousData.timestamp) / 1000; // 转换为秒
|
||||
// const previousData = previousConnectionsRef.current.get(conn.id);
|
||||
// if (previousData) {
|
||||
// const timeDiff = (now - previousData.timestamp) / 1000; // 转换为秒
|
||||
|
||||
if (timeDiff > 0) {
|
||||
const uploadDiff = conn.upload - previousData.upload;
|
||||
const downloadDiff = conn.download - previousData.download;
|
||||
// if (timeDiff > 0) {
|
||||
// const uploadDiff = conn.upload - previousData.upload;
|
||||
// const downloadDiff = conn.download - previousData.download;
|
||||
|
||||
// 计算每秒速度 (字节/秒)
|
||||
connWithSpeed.curUpload = Math.max(0, uploadDiff / timeDiff);
|
||||
connWithSpeed.curDownload = Math.max(0, downloadDiff / timeDiff);
|
||||
}
|
||||
}
|
||||
// // 计算每秒速度 (字节/秒)
|
||||
// connWithSpeed.curUpload = Math.max(0, uploadDiff / timeDiff);
|
||||
// connWithSpeed.curDownload = Math.max(0, downloadDiff / timeDiff);
|
||||
// }
|
||||
// }
|
||||
|
||||
return connWithSpeed;
|
||||
});
|
||||
};
|
||||
// return connWithSpeed;
|
||||
// });
|
||||
// };
|
||||
|
||||
// 基础数据 - 中频率更新 (5秒)
|
||||
const { data: proxiesData, mutate: refreshProxy } = useSWR(
|
||||
"getProxies",
|
||||
getProxies,
|
||||
calcuProxies,
|
||||
{
|
||||
refreshInterval: 5000,
|
||||
revalidateOnFocus: true,
|
||||
@@ -177,19 +171,27 @@ export const AppDataProvider = ({
|
||||
lastProfileId = newProfileId;
|
||||
lastUpdateTime = now;
|
||||
|
||||
scheduleTimeout(() => {
|
||||
void forceRefreshProxies()
|
||||
.catch((error) => {
|
||||
console.warn("[AppDataProvider] forceRefreshProxies 失败:", error);
|
||||
})
|
||||
.finally(() => {
|
||||
scheduleTimeout(() => {
|
||||
refreshProxy().catch((error) => {
|
||||
console.warn("[AppDataProvider] 普通刷新也失败:", error);
|
||||
});
|
||||
}, 200);
|
||||
});
|
||||
}, 0);
|
||||
// 刷新规则数据
|
||||
refreshRules().catch((error) =>
|
||||
console.warn("[AppDataProvider] 规则刷新失败:", error),
|
||||
);
|
||||
refreshRuleProviders().catch((error) =>
|
||||
console.warn("[AppDataProvider] 规则提供者刷新失败:", error),
|
||||
);
|
||||
|
||||
// scheduleTimeout(() => {
|
||||
// void forceRefreshProxies()
|
||||
// .catch((error) => {
|
||||
// console.warn("[AppDataProvider] forceRefreshProxies 失败:", error);
|
||||
// })
|
||||
// .finally(() => {
|
||||
// scheduleTimeout(() => {
|
||||
// refreshProxy().catch((error) => {
|
||||
// console.warn("[AppDataProvider] 普通刷新也失败:", error);
|
||||
// });
|
||||
// }, 200);
|
||||
// });
|
||||
// }, 0);
|
||||
};
|
||||
|
||||
const handleRefreshClash = () => {
|
||||
@@ -205,11 +207,11 @@ export const AppDataProvider = ({
|
||||
scheduleTimeout(async () => {
|
||||
try {
|
||||
console.log("[AppDataProvider] Clash刷新 - 强制刷新代理缓存");
|
||||
await withTimeout(
|
||||
forceRefreshProxies(),
|
||||
8000,
|
||||
"forceRefreshProxies timeout",
|
||||
);
|
||||
// await withTimeout(
|
||||
// forceRefreshProxies(),
|
||||
// 8000,
|
||||
// "forceRefreshProxies timeout",
|
||||
// );
|
||||
await refreshProxy();
|
||||
} catch (error) {
|
||||
console.error(
|
||||
@@ -240,27 +242,27 @@ export const AppDataProvider = ({
|
||||
}, 100);
|
||||
};
|
||||
|
||||
const handleForceRefreshProxies = () => {
|
||||
console.log("[AppDataProvider] 强制代理刷新事件");
|
||||
// const handleForceRefreshProxies = () => {
|
||||
// console.log("[AppDataProvider] 强制代理刷新事件");
|
||||
|
||||
void forceRefreshProxies()
|
||||
.then(() => {
|
||||
console.log("[AppDataProvider] 强制刷新代理缓存完成");
|
||||
return refreshProxy();
|
||||
})
|
||||
.then(() => {
|
||||
console.log("[AppDataProvider] 前端代理数据刷新完成");
|
||||
})
|
||||
.catch((error) => {
|
||||
console.warn("[AppDataProvider] 强制代理刷新失败:", error);
|
||||
refreshProxy().catch((fallbackError) => {
|
||||
console.warn(
|
||||
"[AppDataProvider] 普通代理刷新也失败:",
|
||||
fallbackError,
|
||||
);
|
||||
});
|
||||
});
|
||||
};
|
||||
// void forceRefreshProxies()
|
||||
// .then(() => {
|
||||
// console.log("[AppDataProvider] 强制刷新代理缓存完成");
|
||||
// return refreshProxy();
|
||||
// })
|
||||
// .then(() => {
|
||||
// console.log("[AppDataProvider] 前端代理数据刷新完成");
|
||||
// })
|
||||
// .catch((error) => {
|
||||
// console.warn("[AppDataProvider] 强制代理刷新失败:", error);
|
||||
// refreshProxy().catch((fallbackError) => {
|
||||
// console.warn(
|
||||
// "[AppDataProvider] 普通代理刷新也失败:",
|
||||
// fallbackError,
|
||||
// );
|
||||
// });
|
||||
// });
|
||||
// };
|
||||
|
||||
const initializeListeners = async () => {
|
||||
try {
|
||||
@@ -282,15 +284,15 @@ export const AppDataProvider = ({
|
||||
"verge://refresh-proxy-config",
|
||||
handleRefreshProxy,
|
||||
);
|
||||
const unlistenForceRefresh = await listen(
|
||||
"verge://force-refresh-proxies",
|
||||
handleForceRefreshProxies,
|
||||
);
|
||||
// const unlistenForceRefresh = await listen(
|
||||
// "verge://force-refresh-proxies",
|
||||
// handleForceRefreshProxies,
|
||||
// );
|
||||
|
||||
registerCleanup(() => {
|
||||
unlistenClash();
|
||||
unlistenProxy();
|
||||
unlistenForceRefresh();
|
||||
// unlistenForceRefresh();
|
||||
});
|
||||
} catch (error) {
|
||||
console.warn("[AppDataProvider] 设置 Tauri 事件监听器失败:", error);
|
||||
@@ -298,7 +300,7 @@ export const AppDataProvider = ({
|
||||
const fallbackHandlers: Array<[string, EventListener]> = [
|
||||
["verge://refresh-clash-config", handleRefreshClash],
|
||||
["verge://refresh-proxy-config", handleRefreshProxy],
|
||||
["verge://force-refresh-proxies", handleForceRefreshProxies],
|
||||
// ["verge://force-refresh-proxies", handleForceRefreshProxies],
|
||||
];
|
||||
|
||||
fallbackHandlers.forEach(([eventName, handler]) => {
|
||||
@@ -322,7 +324,7 @@ export const AppDataProvider = ({
|
||||
|
||||
const { data: clashConfig, mutate: refreshClashConfig } = useSWR(
|
||||
"getClashConfig",
|
||||
getClashConfig,
|
||||
getBaseConfig,
|
||||
{
|
||||
refreshInterval: 60000, // 60秒刷新间隔,减少频繁请求
|
||||
revalidateOnFocus: false,
|
||||
@@ -334,7 +336,7 @@ export const AppDataProvider = ({
|
||||
// 提供者数据
|
||||
const { data: proxyProviders, mutate: refreshProxyProviders } = useSWR(
|
||||
"getProxyProviders",
|
||||
getProxyProviders,
|
||||
calcuProxyProviders,
|
||||
{
|
||||
revalidateOnFocus: false,
|
||||
revalidateOnReconnect: false,
|
||||
@@ -384,86 +386,93 @@ export const AppDataProvider = ({
|
||||
|
||||
// 高频率更新数据 (2秒)
|
||||
const { data: uptimeData } = useSWR("appUptime", getAppUptime, {
|
||||
// TODO: 运行时间
|
||||
refreshInterval: 2000,
|
||||
revalidateOnFocus: false,
|
||||
suspense: false,
|
||||
});
|
||||
|
||||
// 连接数据 - 使用IPC轮询更新并计算速度
|
||||
const {
|
||||
data: connectionsData = {
|
||||
connections: [],
|
||||
uploadTotal: 0,
|
||||
downloadTotal: 0,
|
||||
},
|
||||
} = useSWR(
|
||||
clashInfo && pageVisible ? "getConnections" : null,
|
||||
async () => {
|
||||
const data = await getConnections();
|
||||
const rawConnections: IConnectionsItem[] = data.connections || [];
|
||||
// const {
|
||||
// data: connectionsData = {
|
||||
// connections: [],
|
||||
// uploadTotal: 0,
|
||||
// downloadTotal: 0,
|
||||
// },
|
||||
// } = useSWR(
|
||||
// clashInfo && pageVisible ? "getConnections" : null,
|
||||
// async () => {
|
||||
// const data = await getConnections();
|
||||
// const rawConnections =
|
||||
// data.connections?.map((item) => {
|
||||
// // TODO: transform bigint to number
|
||||
// return { ...item, upload: 0, download: 0 } as IConnectionsItem;
|
||||
// }) || [];
|
||||
|
||||
// 计算带速度的连接数据
|
||||
const connectionsWithSpeed = calculateConnectionSpeeds(rawConnections);
|
||||
// // 计算带速度的连接数据
|
||||
// const connectionsWithSpeed = calculateConnectionSpeeds(rawConnections);
|
||||
|
||||
// 更新上一次数据的引用
|
||||
const currentMap = new Map<string, ConnectionSpeedData>();
|
||||
const now = Date.now();
|
||||
rawConnections.forEach((conn) => {
|
||||
currentMap.set(conn.id, {
|
||||
id: conn.id,
|
||||
upload: conn.upload,
|
||||
download: conn.download,
|
||||
timestamp: now,
|
||||
});
|
||||
});
|
||||
previousConnectionsRef.current = currentMap;
|
||||
// // 更新上一次数据的引用
|
||||
// const currentMap = new Map<string, ConnectionSpeedData>();
|
||||
// const now = Date.now();
|
||||
// rawConnections.forEach((conn) => {
|
||||
// currentMap.set(conn.id, {
|
||||
// id: conn.id,
|
||||
// upload: conn.upload,
|
||||
// download: conn.download,
|
||||
// timestamp: now,
|
||||
// });
|
||||
// });
|
||||
// previousConnectionsRef.current = currentMap;
|
||||
|
||||
return {
|
||||
connections: connectionsWithSpeed,
|
||||
uploadTotal: data.uploadTotal || 0,
|
||||
downloadTotal: data.downloadTotal || 0,
|
||||
};
|
||||
},
|
||||
{
|
||||
refreshInterval: 1000, // 1秒刷新一次
|
||||
fallbackData: { connections: [], uploadTotal: 0, downloadTotal: 0 },
|
||||
keepPreviousData: true,
|
||||
onError: (error) => {
|
||||
console.error("[Connections] IPC 获取数据错误:", error);
|
||||
},
|
||||
},
|
||||
);
|
||||
// return {
|
||||
// connections: connectionsWithSpeed,
|
||||
// uploadTotal: data.uploadTotal || 0,
|
||||
// downloadTotal: data.downloadTotal || 0,
|
||||
// };
|
||||
// },
|
||||
// {
|
||||
// refreshInterval: 1000, // 1秒刷新一次
|
||||
// fallbackData: { connections: [], uploadTotal: 0, downloadTotal: 0 },
|
||||
// keepPreviousData: true,
|
||||
// onError: (error) => {
|
||||
// console.error("[Connections] IPC 获取数据错误:", error);
|
||||
// },
|
||||
// },
|
||||
// );
|
||||
|
||||
// 流量数据 - 使用IPC轮询更新
|
||||
const { data: trafficData = { up: 0, down: 0 } } = useSWR(
|
||||
clashInfo && pageVisible ? "getTrafficData" : null,
|
||||
getTrafficData,
|
||||
{
|
||||
refreshInterval: 1000, // 1秒刷新一次
|
||||
fallbackData: { up: 0, down: 0 },
|
||||
keepPreviousData: true,
|
||||
onSuccess: () => {
|
||||
// console.log("[Traffic][AppDataProvider] IPC 获取到流量数据:", data);
|
||||
},
|
||||
onError: (error) => {
|
||||
console.error("[Traffic][AppDataProvider] IPC 获取数据错误:", error);
|
||||
},
|
||||
},
|
||||
);
|
||||
// const trafficData = { up: 0, down: 0 };
|
||||
// const { data: trafficData = { up: 0, down: 0 } } = useSWR(
|
||||
// clashInfo && pageVisible ? "getTrafficData" : null,
|
||||
// getTrafficData,
|
||||
// {
|
||||
// refreshInterval: 1000, // 1秒刷新一次
|
||||
// fallbackData: { up: 0, down: 0 },
|
||||
// keepPreviousData: true,
|
||||
// onSuccess: () => {
|
||||
// // console.log("[Traffic][AppDataProvider] IPC 获取到流量数据:", data);
|
||||
// },
|
||||
// onError: (error) => {
|
||||
// console.error("[Traffic][AppDataProvider] IPC 获取数据错误:", error);
|
||||
// },
|
||||
// },
|
||||
// );
|
||||
|
||||
// 内存数据 - 使用IPC轮询更新
|
||||
const { data: memoryData = { inuse: 0 } } = useSWR(
|
||||
clashInfo && pageVisible ? "getMemoryData" : null,
|
||||
getMemoryData,
|
||||
{
|
||||
refreshInterval: 2000, // 2秒刷新一次
|
||||
fallbackData: { inuse: 0 },
|
||||
keepPreviousData: true,
|
||||
onError: (error) => {
|
||||
console.error("[Memory] IPC 获取数据错误:", error);
|
||||
},
|
||||
},
|
||||
);
|
||||
// const memoryData = { inuse: 0 };
|
||||
// const { data: memoryData = { inuse: 0 } } = useSWR(
|
||||
// clashInfo && pageVisible ? "getMemoryData" : null,
|
||||
// getMemoryData,
|
||||
// {
|
||||
// refreshInterval: 2000, // 2秒刷新一次
|
||||
// fallbackData: { inuse: 0 },
|
||||
// keepPreviousData: true,
|
||||
// onError: (error) => {
|
||||
// console.error("[Memory] IPC 获取数据错误:", error);
|
||||
// },
|
||||
// },
|
||||
// );
|
||||
|
||||
// 提供统一的刷新方法
|
||||
const refreshAll = useCallback(async () => {
|
||||
@@ -496,7 +505,7 @@ export const AppDataProvider = ({
|
||||
// PAC模式:显示我们期望设置的代理地址
|
||||
const proxyHost = verge.proxy_host || "127.0.0.1";
|
||||
const proxyPort =
|
||||
verge.verge_mixed_port || clashConfig["mixed-port"] || 7897;
|
||||
verge.verge_mixed_port || clashConfig.mixedPort || 7897;
|
||||
return `${proxyHost}:${proxyPort}`;
|
||||
} else {
|
||||
// HTTP代理模式:优先使用系统地址,但如果格式不正确则使用期望地址
|
||||
@@ -511,7 +520,7 @@ export const AppDataProvider = ({
|
||||
// 系统地址无效,返回期望的代理地址
|
||||
const proxyHost = verge.proxy_host || "127.0.0.1";
|
||||
const proxyPort =
|
||||
verge.verge_mixed_port || clashConfig["mixed-port"] || 7897;
|
||||
verge.verge_mixed_port || clashConfig.mixedPort || 7897;
|
||||
return `${proxyHost}:${proxyPort}`;
|
||||
}
|
||||
}
|
||||
@@ -521,26 +530,26 @@ export const AppDataProvider = ({
|
||||
// 数据
|
||||
proxies: proxiesData,
|
||||
clashConfig,
|
||||
rules: rulesData || [],
|
||||
rules: rulesData?.rules || [],
|
||||
sysproxy,
|
||||
runningMode,
|
||||
uptime: uptimeData || 0,
|
||||
|
||||
// 提供者数据
|
||||
proxyProviders: proxyProviders || {},
|
||||
ruleProviders: ruleProviders || {},
|
||||
ruleProviders: ruleProviders?.providers || {},
|
||||
|
||||
// 连接数据
|
||||
connections: {
|
||||
data: connectionsData.connections || [],
|
||||
count: connectionsData.connections?.length || 0,
|
||||
uploadTotal: connectionsData.uploadTotal || 0,
|
||||
downloadTotal: connectionsData.downloadTotal || 0,
|
||||
},
|
||||
// connections: {
|
||||
// data: connectionsData.connections || [],
|
||||
// count: connectionsData.connections?.length || 0,
|
||||
// uploadTotal: connectionsData.uploadTotal || 0,
|
||||
// downloadTotal: connectionsData.downloadTotal || 0,
|
||||
// },
|
||||
|
||||
// 实时流量数据
|
||||
traffic: trafficData,
|
||||
memory: memoryData,
|
||||
// traffic: trafficData,
|
||||
// memory: memoryData,
|
||||
|
||||
systemProxyAddress: calculateSystemProxyAddress(),
|
||||
|
||||
@@ -552,7 +561,7 @@ export const AppDataProvider = ({
|
||||
refreshProxyProviders,
|
||||
refreshRuleProviders,
|
||||
refreshAll,
|
||||
};
|
||||
} as AppDataContextType;
|
||||
}, [
|
||||
proxiesData,
|
||||
clashConfig,
|
||||
@@ -560,9 +569,9 @@ export const AppDataProvider = ({
|
||||
sysproxy,
|
||||
runningMode,
|
||||
uptimeData,
|
||||
connectionsData,
|
||||
trafficData,
|
||||
memoryData,
|
||||
// connectionsData,
|
||||
// trafficData,
|
||||
// memoryData,
|
||||
proxyProviders,
|
||||
ruleProviders,
|
||||
verge,
|
||||
|
||||
@@ -1,4 +1,6 @@
|
||||
import { invoke } from "@tauri-apps/api/core";
|
||||
import dayjs from "dayjs";
|
||||
import { getProxies, getProxyProviders } from "tauri-plugin-mihomo-api";
|
||||
|
||||
import { showNotice } from "@/services/noticeService";
|
||||
|
||||
@@ -107,64 +109,11 @@ export async function patchClashMode(payload: string) {
|
||||
return invoke<void>("patch_clash_mode", { payload });
|
||||
}
|
||||
|
||||
// New IPC-based API functions to replace HTTP API calls
|
||||
export async function getVersion() {
|
||||
return invoke<{
|
||||
premium: boolean;
|
||||
meta?: boolean;
|
||||
version: string;
|
||||
}>("get_clash_version");
|
||||
}
|
||||
|
||||
export async function getClashConfig() {
|
||||
return invoke<IConfigData>("get_clash_config");
|
||||
}
|
||||
|
||||
export async function forceRefreshClashConfig() {
|
||||
return invoke<IConfigData>("force_refresh_clash_config");
|
||||
}
|
||||
|
||||
export async function updateGeoData() {
|
||||
return invoke<void>("update_geo_data");
|
||||
}
|
||||
|
||||
export async function upgradeCore() {
|
||||
return invoke<void>("upgrade_clash_core");
|
||||
}
|
||||
|
||||
export async function getRules() {
|
||||
const response = await invoke<{ rules: IRuleItem[] }>("get_clash_rules");
|
||||
return response?.rules || [];
|
||||
}
|
||||
|
||||
export async function getProxyDelay(
|
||||
name: string,
|
||||
url?: string,
|
||||
timeout?: number,
|
||||
) {
|
||||
return invoke<{ delay: number }>("clash_api_get_proxy_delay", {
|
||||
name,
|
||||
url,
|
||||
timeout: timeout || 10000,
|
||||
});
|
||||
}
|
||||
|
||||
export async function updateProxy(group: string, proxy: string) {
|
||||
// const start = Date.now();
|
||||
await invoke<void>("update_proxy_choice", { group, proxy });
|
||||
// const duration = Date.now() - start;
|
||||
// console.log(`[API] updateProxy 耗时: ${duration}ms`);
|
||||
}
|
||||
|
||||
export async function syncTrayProxySelection() {
|
||||
return invoke<void>("sync_tray_proxy_selection");
|
||||
}
|
||||
|
||||
export async function updateProxyAndSync(group: string, proxy: string) {
|
||||
return invoke<void>("update_proxy_and_sync", { group, proxy });
|
||||
}
|
||||
|
||||
export async function getProxies(): Promise<{
|
||||
export async function calcuProxies(): Promise<{
|
||||
global: IProxyGroupItem;
|
||||
direct: IProxyItem;
|
||||
groups: IProxyGroupItem[];
|
||||
@@ -172,19 +121,17 @@ export async function getProxies(): Promise<{
|
||||
proxies: IProxyItem[];
|
||||
}> {
|
||||
const [proxyResponse, providerResponse] = await Promise.all([
|
||||
invoke<{ proxies: Record<string, IProxyItem> }>("get_proxies"),
|
||||
invoke<{ providers: Record<string, IProxyProviderItem> }>(
|
||||
"get_providers_proxies",
|
||||
),
|
||||
getProxies(),
|
||||
calcuProxyProviders(),
|
||||
]);
|
||||
|
||||
const proxyRecord = proxyResponse.proxies;
|
||||
const providerRecord = providerResponse.providers || {};
|
||||
const providerRecord = providerResponse;
|
||||
|
||||
// provider name map
|
||||
const providerMap = Object.fromEntries(
|
||||
Object.entries(providerRecord).flatMap(([provider, item]) =>
|
||||
item.proxies.map((p) => [p.name, { ...p, provider }]),
|
||||
item!.proxies.map((p) => [p.name, { ...p, provider }]),
|
||||
),
|
||||
);
|
||||
|
||||
@@ -209,7 +156,7 @@ export async function getProxies(): Promise<{
|
||||
let groups: IProxyGroupItem[] = Object.values(proxyRecord).reduce<
|
||||
IProxyGroupItem[]
|
||||
>((acc, each) => {
|
||||
if (each.name !== "GLOBAL" && each.all) {
|
||||
if (each?.name !== "GLOBAL" && each?.all) {
|
||||
acc.push({
|
||||
...each,
|
||||
all: each.all!.map((item) => generateItem(item)),
|
||||
@@ -242,209 +189,57 @@ export async function getProxies(): Promise<{
|
||||
|
||||
const proxies = [direct, reject].concat(
|
||||
Object.values(proxyRecord).filter(
|
||||
(p) => !p.all?.length && p.name !== "DIRECT" && p.name !== "REJECT",
|
||||
(p) => !p?.all?.length && p?.name !== "DIRECT" && p?.name !== "REJECT",
|
||||
),
|
||||
);
|
||||
|
||||
const _global: IProxyGroupItem = {
|
||||
const _global = {
|
||||
...global,
|
||||
all: global?.all?.map((item) => generateItem(item)) || [],
|
||||
};
|
||||
|
||||
return { global: _global, direct, groups, records: proxyRecord, proxies };
|
||||
return {
|
||||
global: _global as IProxyGroupItem,
|
||||
direct: direct as IProxyItem,
|
||||
groups,
|
||||
records: proxyRecord as Record<string, IProxyItem>,
|
||||
proxies: (proxies as IProxyItem[]) ?? [],
|
||||
};
|
||||
}
|
||||
|
||||
export async function getProxyProviders() {
|
||||
const response = await invoke<{
|
||||
providers: Record<string, IProxyProviderItem>;
|
||||
}>("get_providers_proxies");
|
||||
if (!response || !response.providers) {
|
||||
console.warn(
|
||||
"getProxyProviders: Invalid response structure, returning empty object",
|
||||
);
|
||||
return {};
|
||||
}
|
||||
|
||||
const providers = response.providers as Record<string, IProxyProviderItem>;
|
||||
|
||||
export async function calcuProxyProviders() {
|
||||
const providers = await getProxyProviders();
|
||||
return Object.fromEntries(
|
||||
Object.entries(providers).filter(([, item]) => {
|
||||
const type = item.vehicleType.toLowerCase();
|
||||
return type === "http" || type === "file";
|
||||
}),
|
||||
Object.entries(providers.providers)
|
||||
.sort()
|
||||
.filter(
|
||||
([_, item]) =>
|
||||
item?.vehicleType === "HTTP" || item?.vehicleType === "File",
|
||||
),
|
||||
);
|
||||
}
|
||||
|
||||
export async function getRuleProviders() {
|
||||
const response = await invoke<{
|
||||
providers: Record<string, IRuleProviderItem>;
|
||||
}>("get_rule_providers");
|
||||
|
||||
const providers = (response.providers || {}) as Record<
|
||||
string,
|
||||
IRuleProviderItem
|
||||
>;
|
||||
|
||||
return Object.fromEntries(
|
||||
Object.entries(providers).filter(([, item]) => {
|
||||
const type = item.vehicleType.toLowerCase();
|
||||
return type === "http" || type === "file";
|
||||
}),
|
||||
);
|
||||
}
|
||||
|
||||
export async function providerHealthCheck(name: string) {
|
||||
return invoke<void>("proxy_provider_health_check", { name });
|
||||
}
|
||||
|
||||
export async function proxyProviderUpdate(name: string) {
|
||||
return invoke<void>("update_proxy_provider", { name });
|
||||
}
|
||||
|
||||
export async function ruleProviderUpdate(name: string) {
|
||||
return invoke<void>("update_rule_provider", { name });
|
||||
}
|
||||
|
||||
export async function getConnections() {
|
||||
return invoke<IConnections>("get_clash_connections");
|
||||
}
|
||||
|
||||
export async function deleteConnection(id: string) {
|
||||
return invoke<void>("delete_clash_connection", { id });
|
||||
}
|
||||
|
||||
export async function closeAllConnections() {
|
||||
return invoke<void>("close_all_clash_connections");
|
||||
}
|
||||
|
||||
export async function getGroupProxyDelays(
|
||||
groupName: string,
|
||||
url?: string,
|
||||
timeout?: number,
|
||||
) {
|
||||
return invoke<Record<string, number>>("get_group_proxy_delays", {
|
||||
groupName,
|
||||
url,
|
||||
timeout,
|
||||
});
|
||||
}
|
||||
|
||||
export async function getTrafficData() {
|
||||
// console.log("[Traffic][Service] 开始调用 get_traffic_data");
|
||||
const result = await invoke<ITrafficItem>("get_traffic_data");
|
||||
// console.log("[Traffic][Service] get_traffic_data 返回结果:", result);
|
||||
return result;
|
||||
}
|
||||
|
||||
export async function getMemoryData() {
|
||||
console.log("[Memory][Service] 开始调用 get_memory_data");
|
||||
const result = await invoke<{
|
||||
inuse: number;
|
||||
oslimit?: number;
|
||||
usage_percent?: number;
|
||||
last_updated?: number;
|
||||
}>("get_memory_data");
|
||||
// console.debug("[Memory][Service] get_memory_data 返回结果:", result);
|
||||
return result;
|
||||
}
|
||||
|
||||
export async function getFormattedTrafficData() {
|
||||
console.log("[Traffic][Service] 开始调用 get_formatted_traffic_data");
|
||||
const result = await invoke<IFormattedTrafficData>(
|
||||
"get_formatted_traffic_data",
|
||||
);
|
||||
// console.debug(
|
||||
// "[Traffic][Service] get_formatted_traffic_data 返回结果:",
|
||||
// result,
|
||||
// );
|
||||
return result;
|
||||
}
|
||||
|
||||
export async function getFormattedMemoryData() {
|
||||
console.log("[Memory][Service] 开始调用 get_formatted_memory_data");
|
||||
const result = await invoke<IFormattedMemoryData>(
|
||||
"get_formatted_memory_data",
|
||||
);
|
||||
// console.debug("[Memory][Service] get_formatted_memory_data 返回结果:", result);
|
||||
return result;
|
||||
}
|
||||
|
||||
export async function getSystemMonitorOverview() {
|
||||
console.log("[Monitor][Service] 开始调用 get_system_monitor_overview");
|
||||
const result = await invoke<ISystemMonitorOverview>(
|
||||
"get_system_monitor_overview",
|
||||
);
|
||||
// console.debug(
|
||||
// "[Monitor][Service] get_system_monitor_overview 返回结果:",
|
||||
// result,
|
||||
// );
|
||||
return result;
|
||||
}
|
||||
|
||||
// 带数据验证的安全版本
|
||||
export async function getSystemMonitorOverviewSafe() {
|
||||
// console.log(
|
||||
// "[Monitor][Service] 开始调用安全版本 get_system_monitor_overview",
|
||||
// );
|
||||
try {
|
||||
const result = await invoke<any>("get_system_monitor_overview");
|
||||
// console.log("[Monitor][Service] 原始数据:", result);
|
||||
|
||||
// 导入验证器(动态导入避免循环依赖)
|
||||
const { systemMonitorValidator } = await import("@/utils/data-validator");
|
||||
|
||||
if (systemMonitorValidator.validate(result)) {
|
||||
// console.log("[Monitor][Service] 数据验证通过");
|
||||
return result as ISystemMonitorOverview;
|
||||
} else {
|
||||
// console.warn("[Monitor][Service] 数据验证失败,使用清理后的数据");
|
||||
return systemMonitorValidator.sanitize(result);
|
||||
}
|
||||
} catch {
|
||||
// console.error("[Monitor][Service] API调用失败:", error);
|
||||
// 返回安全的默认值
|
||||
const { systemMonitorValidator } = await import("@/utils/data-validator");
|
||||
return systemMonitorValidator.sanitize(null);
|
||||
}
|
||||
}
|
||||
|
||||
export async function startTrafficService() {
|
||||
console.log("[Traffic][Service] 开始调用 start_traffic_service");
|
||||
try {
|
||||
const result = await invoke<void>("start_traffic_service");
|
||||
console.log("[Traffic][Service] start_traffic_service 调用成功");
|
||||
return result;
|
||||
} catch (error) {
|
||||
console.error("[Traffic][Service] start_traffic_service 调用失败:", error);
|
||||
throw error;
|
||||
}
|
||||
}
|
||||
|
||||
export async function stopTrafficService() {
|
||||
console.log("[Traffic][Service] 开始调用 stop_traffic_service");
|
||||
const result = await invoke<void>("stop_traffic_service");
|
||||
console.log("[Traffic][Service] stop_traffic_service 调用成功");
|
||||
return result;
|
||||
}
|
||||
|
||||
export async function isDebugEnabled() {
|
||||
return invoke<boolean>("is_clash_debug_enabled");
|
||||
}
|
||||
|
||||
export async function gc() {
|
||||
return invoke<void>("clash_gc");
|
||||
}
|
||||
|
||||
export async function getClashLogs() {
|
||||
return invoke<any>("get_clash_logs");
|
||||
}
|
||||
const regex = /time="(.+?)"\s+level=(.+?)\s+msg="(.+?)"/;
|
||||
const newRegex = /(.+?)\s+(.+?)\s+(.+)/;
|
||||
const logs = await invoke<string[]>("get_clash_logs");
|
||||
|
||||
export async function startLogsMonitoring(level?: string) {
|
||||
return invoke<void>("start_logs_monitoring", { level });
|
||||
}
|
||||
return logs.reduce<ILogItem[]>((acc, log) => {
|
||||
const result = log.match(regex);
|
||||
if (result) {
|
||||
const [_, _time, type, payload] = result;
|
||||
const time = dayjs(_time).format("MM-DD HH:mm:ss");
|
||||
acc.push({ time, type, payload });
|
||||
return acc;
|
||||
}
|
||||
|
||||
export async function stopLogsMonitoring() {
|
||||
return invoke<void>("stop_logs_monitoring");
|
||||
const result2 = log.match(newRegex);
|
||||
if (result2) {
|
||||
const [_, time, type, payload] = result2;
|
||||
acc.push({ time, type, payload });
|
||||
}
|
||||
return acc;
|
||||
}, []);
|
||||
}
|
||||
|
||||
export async function clearLogs() {
|
||||
@@ -576,16 +371,6 @@ export async function cmdGetProxyDelay(
|
||||
}
|
||||
}
|
||||
|
||||
/// 用于profile切换等场景
|
||||
export async function forceRefreshProxies() {
|
||||
const start = Date.now();
|
||||
console.log("[API] 强制刷新代理缓存");
|
||||
const result = await invoke<any>("force_refresh_proxies");
|
||||
const duration = Date.now() - start;
|
||||
console.log(`[API] 代理缓存刷新完成,耗时: ${duration}ms`);
|
||||
return result;
|
||||
}
|
||||
|
||||
export async function cmdTestDelay(url: string) {
|
||||
return invoke<number>("test_delay", { url });
|
||||
}
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
import { cmdGetProxyDelay } from "@/services/cmds";
|
||||
import { delayProxyByName } from "tauri-plugin-mihomo-api";
|
||||
|
||||
const hashKey = (name: string, group: string) => `${group ?? ""}::${name}`;
|
||||
|
||||
@@ -106,7 +106,7 @@ class DelayManager {
|
||||
|
||||
// 使用Promise.race来实现超时控制
|
||||
const result = await Promise.race([
|
||||
cmdGetProxyDelay(name, timeout, url),
|
||||
delayProxyByName(name, url, timeout),
|
||||
timeoutPromise,
|
||||
]);
|
||||
|
||||
@@ -210,13 +210,14 @@ class DelayManager {
|
||||
formatDelay(delay: number, timeout = 10000) {
|
||||
if (delay === -1) return "-";
|
||||
if (delay === -2) return "testing";
|
||||
if (delay >= timeout) return "timeout";
|
||||
if (delay === 0 || (delay >= timeout && delay <= 1e5)) return "timeout";
|
||||
if (delay > 1e5) return "Error";
|
||||
return `${delay}`;
|
||||
}
|
||||
|
||||
formatDelayColor(delay: number, timeout = 10000) {
|
||||
if (delay < 0) return "";
|
||||
if (delay >= timeout) return "error.main";
|
||||
if (delay === 0 || delay >= timeout) return "error.main";
|
||||
if (delay >= 10000) return "error.main";
|
||||
if (delay >= 400) return "warning.main";
|
||||
if (delay >= 250) return "primary.main";
|
||||
|
||||
@@ -3,7 +3,6 @@ import { create } from "zustand";
|
||||
|
||||
import {
|
||||
fetchLogsViaIPC,
|
||||
startLogsStreaming,
|
||||
stopLogsStreaming,
|
||||
clearLogs as clearLogsIPC,
|
||||
} from "@/services/ipc-log-service";
|
||||
@@ -96,7 +95,7 @@ export const initGlobalLogService = (
|
||||
console.log("[GlobalLog-IPC] 启用IPC流式日志服务");
|
||||
|
||||
// 启动流式监控
|
||||
startLogsStreaming(logLevel);
|
||||
// startLogsStreaming(logLevel);
|
||||
|
||||
// 立即获取一次日志
|
||||
fetchLogsViaIPCPeriodically();
|
||||
@@ -152,7 +151,7 @@ export const changeLogLevel = (level: LogLevel) => {
|
||||
|
||||
if (enabled) {
|
||||
// IPC流式模式下重新启动监控
|
||||
startLogsStreaming(level);
|
||||
// startLogsStreaming(level);
|
||||
fetchLogsViaIPCPeriodically();
|
||||
}
|
||||
};
|
||||
@@ -180,11 +179,11 @@ export const clearGlobalLogs = () => {
|
||||
};
|
||||
|
||||
// 自定义钩子,用于获取过滤后的日志数据
|
||||
export const useGlobalLogData = (logLevel: LogLevel = "all") => {
|
||||
export const useGlobalLogData = (logLevel: LogLevel = "info") => {
|
||||
const logs = useGlobalLogStore((state) => state.logs);
|
||||
|
||||
// 根据当前选择的日志等级过滤日志
|
||||
if (logLevel === "all") {
|
||||
if (logLevel === "info") {
|
||||
return logs;
|
||||
} else {
|
||||
return logs.filter((log) => log.type.toLowerCase() === logLevel);
|
||||
|
||||
@@ -1,12 +1,6 @@
|
||||
// IPC-based log service using Tauri commands with streaming support
|
||||
import dayjs from "dayjs";
|
||||
|
||||
import {
|
||||
getClashLogs,
|
||||
startLogsMonitoring,
|
||||
stopLogsMonitoring,
|
||||
clearLogs as clearLogsCmd,
|
||||
} from "@/services/cmds";
|
||||
import { clearLogs as clearLogsCmd } from "@/services/cmds";
|
||||
|
||||
type LogLevel = "debug" | "info" | "warning" | "error" | "all";
|
||||
|
||||
@@ -21,7 +15,7 @@ interface ILogItem {
|
||||
export const startLogsStreaming = async (logLevel: LogLevel = "info") => {
|
||||
try {
|
||||
const level = logLevel === "all" ? undefined : logLevel;
|
||||
await startLogsMonitoring(level);
|
||||
// await startLogsMonitoring(level);
|
||||
console.log(
|
||||
`[IPC-LogService] Started logs monitoring with level: ${logLevel}`,
|
||||
);
|
||||
@@ -33,7 +27,7 @@ export const startLogsStreaming = async (logLevel: LogLevel = "info") => {
|
||||
// Stop logs monitoring
|
||||
export const stopLogsStreaming = async () => {
|
||||
try {
|
||||
await stopLogsMonitoring();
|
||||
// await stopLogsMonitoring();
|
||||
console.log("[IPC-LogService] Stopped logs monitoring");
|
||||
} catch (error) {
|
||||
console.error("[IPC-LogService] Failed to stop logs monitoring:", error);
|
||||
@@ -45,16 +39,16 @@ export const fetchLogsViaIPC = async (): Promise<ILogItem[]> => {
|
||||
try {
|
||||
// Server-side filtering handles the level via /logs?level={level}
|
||||
// We just fetch all cached logs regardless of the logLevel parameter
|
||||
const response = await getClashLogs();
|
||||
// const response = await getClashLogs();
|
||||
|
||||
// The response should be in the format expected by the frontend
|
||||
// Transform the logs to match the expected format
|
||||
if (Array.isArray(response)) {
|
||||
return response.map((log: any) => ({
|
||||
...log,
|
||||
time: log.time || dayjs().format("HH:mm:ss"),
|
||||
}));
|
||||
}
|
||||
// // The response should be in the format expected by the frontend
|
||||
// // Transform the logs to match the expected format
|
||||
// if (Array.isArray(response)) {
|
||||
// return response.map((log: any) => ({
|
||||
// ...log,
|
||||
// time: log.time || dayjs().format("HH:mm:ss"),
|
||||
// }));
|
||||
// }
|
||||
|
||||
return [];
|
||||
} catch (error) {
|
||||
|
||||
@@ -1,11 +1,30 @@
|
||||
import { createContextState } from "foxact/create-context-state";
|
||||
import { useLocalStorage } from "foxact/use-local-storage";
|
||||
import { LogLevel } from "tauri-plugin-mihomo-api";
|
||||
|
||||
const [ThemeModeProvider, useThemeMode, useSetThemeMode] = createContextState<
|
||||
"light" | "dark"
|
||||
>("light");
|
||||
|
||||
export const useEnableLog = () => useLocalStorage("enable-log", false);
|
||||
export type LogFilter = "all" | "debug" | "info" | "warn" | "err";
|
||||
|
||||
interface IClashLog {
|
||||
enable: boolean;
|
||||
logLevel: LogLevel;
|
||||
logFilter: LogFilter;
|
||||
}
|
||||
const defaultClashLog: IClashLog = {
|
||||
enable: true,
|
||||
logLevel: "info",
|
||||
logFilter: "all",
|
||||
};
|
||||
export const useClashLog = () =>
|
||||
useLocalStorage<IClashLog>("clash-log", defaultClashLog, {
|
||||
serializer: JSON.stringify,
|
||||
deserializer: JSON.parse,
|
||||
});
|
||||
|
||||
// export const useEnableLog = () => useLocalStorage("enable-log", false);
|
||||
|
||||
interface IConnectionSetting {
|
||||
layout: "table" | "list";
|
||||
|
||||
Reference in New Issue
Block a user