From 4ccb17dde65274aad83ee17ffc167f5030248206 Mon Sep 17 00:00:00 2001 From: Dyna <108321411+Ahaohaohao@users.noreply.github.com> Date: Tue, 8 Jul 2025 17:05:17 +0800 Subject: [PATCH] fix: the problem that the DNS override nameserver-policy field cannot correctly recognize multiple writing methods (#4011) * fix: the problem that the DNS override nameserver-policy field cannot correctly recognize multiple writing methods * update logs * fix-dns-viewer.tsx --- UPDATELOG.md | 3 +- src/components/setting/mods/dns-viewer.tsx | 67 +++++++++++----------- 2 files changed, 34 insertions(+), 36 deletions(-) diff --git a/UPDATELOG.md b/UPDATELOG.md index 363a16f9..a345e072 100644 --- a/UPDATELOG.md +++ b/UPDATELOG.md @@ -10,7 +10,8 @@ - 修复导入订阅时非 http 协议链接被错误尝试导入 - 修复切换节点后页面长时间 loading 及缓存过期导致的数据不同步问题 - 修复将快捷键名称更名为 `Clash Verge`之后无法删除图标和无法删除注册表 -- 修复`DNS`覆写服务器支持默认留空 +- 修复`DNS`覆写 `fallback` `proxy server` `nameserver` `direct Nameserver` 字段支持留空 +- 修复`DNS`覆写 `nameserver-policy` 字段无法正确识别 `geo` 库 ### ✨ 新增功能 diff --git a/src/components/setting/mods/dns-viewer.tsx b/src/components/setting/mods/dns-viewer.tsx index 10e475e8..55c9270b 100644 --- a/src/components/setting/mods/dns-viewer.tsx +++ b/src/components/setting/mods/dns-viewer.tsx @@ -332,54 +332,51 @@ export const DnsViewer = forwardRef((props, ref) => { } }; - // 格式化nameserver-policy为字符串 - const formatNameserverPolicy = (policy: any): string => { - if (!policy) return ""; - - let result: string[] = []; - - Object.entries(policy).forEach(([domain, servers]) => { - if (Array.isArray(servers)) { - // 处理数组格式的服务器 - const serversStr = servers.join(";"); - result.push(`${domain}=${serversStr}`); - } else { - // 处理单个服务器 - result.push(`${domain}=${servers}`); - } - }); - - return result.join(", "); - }; - // 解析nameserver-policy为对象 const parseNameserverPolicy = (str: string): Record => { const result: Record = {}; if (!str) return result; - str.split(",").forEach((item) => { - const parts = item.trim().split("="); - if (parts.length < 2) return; + // 处理geosite:xxx,yyy格式 + const ruleRegex = /\s*([^=]+?)\s*=\s*([^,]+)(?:,|$)/g; + let match; - const domain = parts[0].trim(); - const serversStr = parts.slice(1).join("=").trim(); + while ((match = ruleRegex.exec(str)) !== null) { + const [, domainsPart, serversPart] = match; - // 检查是否包含多个分号分隔的服务器 - if (serversStr.includes(";")) { - // 多个服务器,作为数组处理 - result[domain] = serversStr - .split(";") - .map((s) => s.trim()) - .filter(Boolean); + // 处理域名部分 + let domains; + if (domainsPart.startsWith("geosite:")) { + domains = [domainsPart.trim()]; } else { - // 单个服务器 - result[domain] = serversStr; + domains = [domainsPart.trim()]; } - }); + + // 处理服务器部分 + const servers = serversPart.split(";").map((s) => s.trim()); + + // 为每个域名组分配相同的服务器列表 + domains.forEach((domain) => { + result[domain] = servers; + }); + } return result; }; + // 格式化nameserver-policy为字符串 + const formatNameserverPolicy = (policy: any): string => { + if (!policy || typeof policy !== "object") return ""; + + // 直接将对象转换为字符串格式 + return Object.entries(policy) + .map(([domain, servers]) => { + const serversStr = Array.isArray(servers) ? servers.join(";") : servers; + return `${domain}=${serversStr}`; + }) + .join(", "); + }; + // 格式化hosts为字符串 const formatHosts = (hosts: any): string => { if (!hosts || typeof hosts !== "object") return "";