optimize port setting exit and save mechanism

This commit is contained in:
Ahao
2025-05-21 15:27:43 +08:00
parent 250dcf569c
commit 1dbfc96ebf
3 changed files with 105 additions and 35 deletions

View File

@@ -4,8 +4,16 @@ import { useVerge } from "@/hooks/use-verge";
import { showNotice } from "@/services/noticeService"; import { showNotice } from "@/services/noticeService";
import getSystem from "@/utils/get-system"; import getSystem from "@/utils/get-system";
import { Shuffle } from "@mui/icons-material"; import { Shuffle } from "@mui/icons-material";
import { IconButton, List, ListItem, ListItemText, TextField } from "@mui/material"; import {
import { useLockFn } from "ahooks"; CircularProgress,
IconButton,
List,
ListItem,
ListItemText,
Stack,
TextField
} from "@mui/material";
import { useLockFn, useRequest } from "ahooks";
import { forwardRef, useImperativeHandle, useState } from "react"; import { forwardRef, useImperativeHandle, useState } from "react";
import { useTranslation } from "react-i18next"; import { useTranslation } from "react-i18next";
@@ -42,6 +50,30 @@ export const ClashPortViewer = forwardRef<ClashPortViewerRef, ClashPortViewerPro
const [tproxyPort, setTproxyPort] = useState(verge?.verge_tproxy_port ?? 7896); const [tproxyPort, setTproxyPort] = useState(verge?.verge_tproxy_port ?? 7896);
const [tproxyEnabled, setTproxyEnabled] = useState(verge?.verge_tproxy_enabled ?? false); const [tproxyEnabled, setTproxyEnabled] = useState(verge?.verge_tproxy_enabled ?? false);
// 添加保存请求防止GUI卡死
const { loading, run: saveSettings } = useRequest(
async (params: {
clashConfig: any;
vergeConfig: any;
}) => {
const { clashConfig, vergeConfig } = params;
await Promise.all([
patchInfo(clashConfig),
patchVerge(vergeConfig)
]);
},
{
manual: true,
onSuccess: () => {
setOpen(false);
showNotice("success", t("Port settings saved")); // 调用提示函数
},
onError: () => {
showNotice("error", t("Failed to save settings")); // 调用提示函数
}
}
);
useImperativeHandle(ref, () => ({ useImperativeHandle(ref, () => ({
open: () => { open: () => {
setMixedPort(verge?.verge_mixed_port ?? clashInfo?.mixed_port ?? 7897); setMixedPort(verge?.verge_mixed_port ?? clashInfo?.mixed_port ?? 7897);
@@ -69,22 +101,33 @@ export const ClashPortViewer = forwardRef<ClashPortViewerRef, ClashPortViewerPro
].filter(p => p !== -1); ].filter(p => p !== -1);
if (new Set(portList).size !== portList.length) { if (new Set(portList).size !== portList.length) {
showNotice("error", t("Port Conflict"));
return; return;
} }
try { // 验证端口范围
// 更新Clash配置 const isValidPort = (port: number) => port >= 1 && port <= 65535;
await patchInfo({ const allPortsValid = [
mixedPort,
socksEnabled ? socksPort : 0,
httpEnabled ? httpPort : 0,
redirEnabled ? redirPort : 0,
tproxyEnabled ? tproxyPort : 0
].every(port => port === 0 || isValidPort(port));
if (!allPortsValid) {
return;
}
// 准备配置数据
const clashConfig = {
"mixed-port": mixedPort, "mixed-port": mixedPort,
"socks-port": socksPort, "socks-port": socksPort,
port: httpPort, port: httpPort,
"redir-port": redirPort, "redir-port": redirPort,
"tproxy-port": tproxyPort "tproxy-port": tproxyPort
} as any); };
// 更新Verge配置 const vergeConfig = {
await patchVerge({
verge_mixed_port: mixedPort, verge_mixed_port: mixedPort,
verge_socks_port: socksPort, verge_socks_port: socksPort,
verge_socks_enabled: socksEnabled, verge_socks_enabled: socksEnabled,
@@ -94,23 +137,44 @@ export const ClashPortViewer = forwardRef<ClashPortViewerRef, ClashPortViewerPro
verge_redir_enabled: redirEnabled, verge_redir_enabled: redirEnabled,
verge_tproxy_port: tproxyPort, verge_tproxy_port: tproxyPort,
verge_tproxy_enabled: tproxyEnabled verge_tproxy_enabled: tproxyEnabled
};
// 提交保存请求
await saveSettings({ clashConfig, vergeConfig });
}); });
setOpen(false); // 优化的数字输入处理
showNotice("success", t("Port settings saved")); const handleNumericChange = (setter: (value: number) => void) => (e: React.ChangeEvent<HTMLInputElement>) => {
} catch (err) { const value = e.target.value.replace(/\D+/, '');
showNotice("error", t("Failed to save settings")); if (value === '') {
setter(0);
return;
} }
});
const num = parseInt(value, 10);
if (!isNaN(num) && num >= 0 && num <= 65535) {
setter(num);
}
};
return ( return (
<BaseDialog <BaseDialog
open={open} open={open}
title={t("Port Configuration")} title={t("Port Configuration")}
contentSx={{ width: 360, padding: "8px" }} contentSx={{
okBtn={t("Save")} width: 400
}}
okBtn={
loading ? (
<Stack direction="row" alignItems="center" spacing={1}>
<CircularProgress size={20} />
{t("Saving...")}
</Stack>
) : t("Save")
}
cancelBtn={t("Cancel")} cancelBtn={t("Cancel")}
onClose={() => setOpen(false)} onClose={() => setOpen(false)}
onCancel={() => setOpen(false)}
onOk={onSave} onOk={onSave}
> >
<List sx={{ width: "100%" }}> <List sx={{ width: "100%" }}>

View File

@@ -628,5 +628,8 @@
"Configuration saved successfully": "Configuration saved successfully", "Configuration saved successfully": "Configuration saved successfully",
"Last generated": "Last generated", "Last generated": "Last generated",
"External Controller Config": "External Controller Config", "External Controller Config": "External Controller Config",
"Enable one-click random API port and key. Click to randomize the port and key": "Enable one-click random API port and key. Click to randomize the port and key" "Enable one-click random API port and key. Click to randomize the port and key": "Enable one-click random API port and key. Click to randomize the port and key",
"Saving...": "Saving...",
"Port settings saved": "Port settings saved",
"Failed to save settings": "Failed to save settings"
} }

View File

@@ -274,7 +274,7 @@
"Unified Delay Info": "开启统一延迟时,会进行两次延迟测试,以消除连接握手等带来的不同类型节点的延迟差异", "Unified Delay Info": "开启统一延迟时,会进行两次延迟测试,以消除连接握手等带来的不同类型节点的延迟差异",
"Log Level": "日志等级", "Log Level": "日志等级",
"Log Level Info": "仅对日志目录 Service 文件夹下的内核日志文件生效", "Log Level Info": "仅对日志目录 Service 文件夹下的内核日志文件生效",
"Port Configuration": "端口设置", "Port Configuration": "端口配置设置",
"Random Port": "随机端口", "Random Port": "随机端口",
"Mixed Port": "混合代理端口", "Mixed Port": "混合代理端口",
"Socks Port": "SOCKS 代理端口", "Socks Port": "SOCKS 代理端口",
@@ -628,5 +628,8 @@
"Configuration saved successfully": "随机配置,保存完成", "Configuration saved successfully": "随机配置,保存完成",
"Last generated": "记录生成", "Last generated": "记录生成",
"External Controller Config": "API配置", "External Controller Config": "API配置",
"Enable one-click random API port and key. Click to randomize the port and key": "开启一键随机API端口和密钥点进去就可以随机端口和密钥" "Enable one-click random API port and key. Click to randomize the port and key": "开启一键随机API端口和密钥点进去就可以随机端口和密钥",
"Saving...": "保存中...",
"Port settings saved": "端口设置已保存",
"Failed to save settings": "端口设置失败"
} }