diff --git a/UPDATELOG.md b/UPDATELOG.md index fae1af7e..473576d5 100644 --- a/UPDATELOG.md +++ b/UPDATELOG.md @@ -15,7 +15,8 @@ - 使用 tauri window-state 管理窗口,尝试解决各种窗口异常 #### 新增了: - - 允许代理主机地址设置为非 127.0.0.1。对 WSL 代理友好。 + - 允许代理主机地址设置为非 127.0.0.1 对 WSL 代理友好 + - 关闭系统代理时关闭已建立的网络连接 #### 优化了: - 系统代理 Bypass 设置 diff --git a/src-tauri/src/feat/proxy.rs b/src-tauri/src/feat/proxy.rs index a7f22f09..feef34b7 100644 --- a/src-tauri/src/feat/proxy.rs +++ b/src-tauri/src/feat/proxy.rs @@ -10,8 +10,22 @@ use tauri_plugin_clipboard_manager::ClipboardExt; pub fn toggle_system_proxy() { let enable = Config::verge().draft().enable_system_proxy; let enable = enable.unwrap_or(false); + let auto_close_connection = Config::verge() + .data() + .auto_close_connection + .unwrap_or(false); AsyncHandler::spawn(move || async move { + // 如果当前系统代理即将关闭,且自动关闭连接设置为true,则关闭所有连接 + if enable && auto_close_connection { + if let Err(err) = crate::module::mihomo::MihomoManager::global() + .close_all_connections() + .await + { + log::error!(target: "app", "Failed to close all connections: {}", err); + } + } + match super::patch_verge( IVerge { enable_system_proxy: Some(!enable), diff --git a/src-tauri/src_crates/crate_mihomo_api/src/lib.rs b/src-tauri/src_crates/crate_mihomo_api/src/lib.rs index 61a2d3c2..dfa87a80 100644 --- a/src-tauri/src_crates/crate_mihomo_api/src/lib.rs +++ b/src-tauri/src_crates/crate_mihomo_api/src/lib.rs @@ -95,6 +95,19 @@ impl MihomoManager { self.update_providers_proxies(providers_proxies); Ok(self) } + + pub async fn close_all_connections(&self) -> Result<(), String> { + let url = format!("{}/connections", self.mihomo_server); + let response = self.send_request(Method::DELETE, url, None).await?; + if response["code"] == 204 { + Ok(()) + } else { + Err(response["message"] + .as_str() + .unwrap_or("unknown error") + .to_string()) + } + } } impl MihomoManager { diff --git a/src/components/setting/setting-system.tsx b/src/components/setting/setting-system.tsx index 72ffc112..bb5d332a 100644 --- a/src/components/setting/setting-system.tsx +++ b/src/components/setting/setting-system.tsx @@ -24,6 +24,7 @@ import { import { useLockFn } from "ahooks"; import { Button, Tooltip } from "@mui/material"; import { useSystemState } from "@/hooks/use-system-state"; +import { closeAllConnections } from "@/services/api"; interface Props { onError?: (err: Error) => void; @@ -182,6 +183,9 @@ const SettingSystem = ({ onError }: Props) => { onFormat={onSwitchFormat} onChange={(e) => onChangeData({ enable_system_proxy: e })} onGuard={async (e) => { + if (!e && verge?.auto_close_connection) { + closeAllConnections(); + } await patchVerge({ enable_system_proxy: e }); await updateProxyStatus(); }} diff --git a/src/components/shared/ProxyControlSwitches.tsx b/src/components/shared/ProxyControlSwitches.tsx index 8586d0d3..d00d45f1 100644 --- a/src/components/shared/ProxyControlSwitches.tsx +++ b/src/components/shared/ProxyControlSwitches.tsx @@ -1,4 +1,4 @@ -import { useRef, useEffect } from "react"; +import { useRef } from "react"; import { useTranslation } from "react-i18next"; import useSWR, { mutate } from "swr"; import { @@ -16,7 +16,6 @@ import { useTheme, } from "@mui/material"; import { DialogRef, Notice, Switch } from "@/components/base"; -import { TooltipIcon } from "@/components/base/base-tooltip-icon"; import { GuardState } from "@/components/setting/mods/guard-state"; import { SysproxyViewer } from "@/components/setting/mods/sysproxy-viewer"; import { TunViewer } from "@/components/setting/mods/tun-viewer"; @@ -28,7 +27,7 @@ import { installService, } from "@/services/cmds"; import { useLockFn } from "ahooks"; -import { SettingItem } from "@/components/setting/mods/setting-comp"; +import { closeAllConnections } from "@/services/api"; interface ProxySwitchProps { label?: string; @@ -168,6 +167,9 @@ const ProxyControlSwitches = ({ label, onError }: ProxySwitchProps) => { onFormat={onSwitchFormat} onChange={(e) => onChangeData({ enable_system_proxy: e })} onGuard={async (e) => { + if (!e && verge?.auto_close_connection) { + closeAllConnections(); + } await patchVerge({ enable_system_proxy: e }); await updateProxyStatus(); }} diff --git a/src/pages/proxies.tsx b/src/pages/proxies.tsx index 59b32a21..2bc6b590 100644 --- a/src/pages/proxies.tsx +++ b/src/pages/proxies.tsx @@ -4,7 +4,7 @@ import { useLockFn } from "ahooks"; import { useTranslation } from "react-i18next"; import { Box, Button, ButtonGroup } from "@mui/material"; import { closeAllConnections, getClashConfig } from "@/services/api"; -import { patchClashConfig, patchClashMode } from "@/services/cmds"; +import { patchClashMode } from "@/services/cmds"; import { useVerge } from "@/hooks/use-verge"; import { BasePage } from "@/components/base"; import { ProxyGroups } from "@/components/proxy/proxy-groups"; diff --git a/src/providers/app-data-provider.tsx b/src/providers/app-data-provider.tsx index fa3413dc..0b854996 100644 --- a/src/providers/app-data-provider.tsx +++ b/src/providers/app-data-provider.tsx @@ -1,7 +1,7 @@ import { createContext, useContext, useMemo } from "react"; import useSWR from "swr"; import useSWRSubscription from "swr/subscription"; -import { getProxies, getConnections, getRules, getClashConfig, getProxyProviders, getRuleProviders } from "@/services/api"; +import { getProxies, getRules, getClashConfig, getProxyProviders, getRuleProviders } from "@/services/api"; import { getSystemProxy, getRunningMode, getAppUptime } from "@/services/cmds"; import { useClashInfo } from "@/hooks/use-clash"; import { createAuthSockette } from "@/utils/websocket";