修复正则匹配字符串不生效的问题

This commit is contained in:
Alex Yang
2025-11-29 00:23:20 +08:00
parent 3207510c91
commit 0468f52050
2 changed files with 7 additions and 8 deletions

View File

@@ -21,7 +21,7 @@
"name": "AdGuard DNS filter", "name": "AdGuard DNS filter",
"url": "https://gitea.amazehome.xyz/AMAZEHOME/hosts-and-filters/raw/branch/main/filter.txt", "url": "https://gitea.amazehome.xyz/AMAZEHOME/hosts-and-filters/raw/branch/main/filter.txt",
"enabled": true, "enabled": true,
"lastUpdateTime": "2025-11-28T15:45:11.073Z" "lastUpdateTime": "2025-11-28T16:13:03.564Z"
}, },
{ {
"name": "Adaway Default Blocklist", "name": "Adaway Default Blocklist",
@@ -39,7 +39,7 @@
"name": "My GitHub Rules", "name": "My GitHub Rules",
"url": "https://gitea.amazehome.xyz/AMAZEHOME/hosts-and-filters/raw/branch/main/rules/costomize.txt", "url": "https://gitea.amazehome.xyz/AMAZEHOME/hosts-and-filters/raw/branch/main/rules/costomize.txt",
"enabled": true, "enabled": true,
"lastUpdateTime": "2025-11-28T14:42:09.271Z" "lastUpdateTime": "2025-11-28T16:13:05.960Z"
}, },
{ {
"name": "CNList", "name": "CNList",

View File

@@ -374,12 +374,11 @@ func (m *ShieldManager) parseRule(line string, isLocal bool, source string) {
} }
case strings.HasPrefix(line, "/") && strings.HasSuffix(line, "/"): case strings.HasPrefix(line, "/") && strings.HasSuffix(line, "/"):
// 关键字匹配规则:/keyword/ 格式,不区分大小写,字面量匹配特殊字符 // 正则表达式匹配规则:/regex/ 格式,不区分大小写
keyword := strings.TrimPrefix(strings.TrimSuffix(line, "/"), "/") pattern := strings.TrimPrefix(strings.TrimSuffix(line, "/"), "/")
// 转义特殊字符,确保字面量匹配 // 编译为不区分大小写的正则表达式,确保能匹配域名中任意位置
quotedKeyword := regexp.QuoteMeta(keyword) // 对于像 /domain/ 这样的规则,应该匹配包含 domain 字符串的任何域名
// 编译为不区分大小写的正则表达式,匹配域名中任意位置 if re, err := regexp.Compile("(?i).*" + regexp.QuoteMeta(pattern) + ".*"); err == nil {
if re, err := regexp.Compile("(?i)" + quotedKeyword); err == nil {
// 保存原始规则字符串 // 保存原始规则字符串
m.addRegexRule(re, line, !isException, isLocal, source) m.addRegexRule(re, line, !isException, isLocal, source)
} }