perf: domain rule

This commit is contained in:
putyy
2025-12-27 11:31:07 +08:00
committed by putyy
parent 6b18e7fba1
commit 86378b9fba

View File

@@ -11,6 +11,7 @@ type Rule struct {
raw string raw string
isNeg bool // 是否否定规则(以 ! 开头) isNeg bool // 是否否定规则(以 ! 开头)
isWildcard bool // 是否为 *.domain 形式 isWildcard bool // 是否为 *.domain 形式
isAll bool
domain string // 域名部分,不含 "*." domain string // 域名部分,不含 "*."
} }
@@ -51,6 +52,15 @@ func (r *RuleSet) Load(rs string) error {
} }
} }
if line == "*" {
rules = append(rules, Rule{
raw: "*",
isAll: true,
isNeg: isNeg,
})
continue
}
isWildcard := false isWildcard := false
domain := line domain := line
if strings.HasPrefix(line, "*.") { if strings.HasPrefix(line, "*.") {
@@ -96,22 +106,20 @@ func (r *RuleSet) shouldMitm(host string) bool {
action := false action := false
for _, rule := range r.rules { for _, rule := range r.rules {
if rule.isAll {
action = !rule.isNeg
continue
}
if rule.isWildcard { if rule.isWildcard {
if h == rule.domain || strings.HasSuffix(h, "."+rule.domain) { if h == rule.domain || strings.HasSuffix(h, "."+rule.domain) {
if rule.isNeg { action = !rule.isNeg
action = false
} else {
action = true
} }
continue
} }
} else {
if h == rule.domain { if h == rule.domain {
if rule.isNeg { action = !rule.isNeg
action = false
} else {
action = true
}
}
} }
} }
return action return action