chore: Check regex rule expression when initializing

This commit is contained in:
H1JK 2024-03-08 22:42:48 +08:00 committed by wwqgtxx
parent 5702d28cda
commit 90d0ef033b
2 changed files with 11 additions and 9 deletions

View File

@ -2,14 +2,13 @@ package common
import (
"regexp"
"strings"
C "github.com/metacubex/mihomo/constant"
)
type DomainRegex struct {
*Base
regex string
regex *regexp.Regexp
adapter string
}
@ -19,8 +18,7 @@ func (dr *DomainRegex) RuleType() C.RuleType {
func (dr *DomainRegex) Match(metadata *C.Metadata) (bool, string) {
domain := metadata.RuleHost()
match, _ := regexp.MatchString(dr.regex, domain)
return match, dr.adapter
return dr.regex.MatchString(domain), dr.adapter
}
func (dr *DomainRegex) Adapter() string {
@ -28,15 +26,19 @@ func (dr *DomainRegex) Adapter() string {
}
func (dr *DomainRegex) Payload() string {
return dr.regex
return dr.regex.String()
}
func NewDomainRegex(regex string, adapter string) *DomainRegex {
func NewDomainRegex(regex string, adapter string) (*DomainRegex, error) {
r, err := regexp.Compile(regex)
if err != nil {
return nil, err
}
return &DomainRegex{
Base: &Base{},
regex: strings.ToLower(regex),
regex: r,
adapter: adapter,
}
}, nil
}
//var _ C.Rule = (*DomainRegex)(nil)

View File

@ -18,7 +18,7 @@ func ParseRule(tp, payload, target string, params []string, subRules map[string]
case "DOMAIN-KEYWORD":
parsed = RC.NewDomainKeyword(payload, target)
case "DOMAIN-REGEX":
parsed = RC.NewDomainRegex(payload, target)
parsed, parseErr = RC.NewDomainRegex(payload, target)
case "GEOSITE":
parsed, parseErr = RC.NewGEOSITE(payload, target)
case "GEOIP":