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 ( import (
"regexp" "regexp"
"strings"
C "github.com/metacubex/mihomo/constant" C "github.com/metacubex/mihomo/constant"
) )
type DomainRegex struct { type DomainRegex struct {
*Base *Base
regex string regex *regexp.Regexp
adapter string adapter string
} }
@ -19,8 +18,7 @@ func (dr *DomainRegex) RuleType() C.RuleType {
func (dr *DomainRegex) Match(metadata *C.Metadata) (bool, string) { func (dr *DomainRegex) Match(metadata *C.Metadata) (bool, string) {
domain := metadata.RuleHost() domain := metadata.RuleHost()
match, _ := regexp.MatchString(dr.regex, domain) return dr.regex.MatchString(domain), dr.adapter
return match, dr.adapter
} }
func (dr *DomainRegex) Adapter() string { func (dr *DomainRegex) Adapter() string {
@ -28,15 +26,19 @@ func (dr *DomainRegex) Adapter() string {
} }
func (dr *DomainRegex) Payload() 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{ return &DomainRegex{
Base: &Base{}, Base: &Base{},
regex: strings.ToLower(regex), regex: r,
adapter: adapter, adapter: adapter,
} }, nil
} }
//var _ C.Rule = (*DomainRegex)(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": case "DOMAIN-KEYWORD":
parsed = RC.NewDomainKeyword(payload, target) parsed = RC.NewDomainKeyword(payload, target)
case "DOMAIN-REGEX": case "DOMAIN-REGEX":
parsed = RC.NewDomainRegex(payload, target) parsed, parseErr = RC.NewDomainRegex(payload, target)
case "GEOSITE": case "GEOSITE":
parsed, parseErr = RC.NewGEOSITE(payload, target) parsed, parseErr = RC.NewGEOSITE(payload, target)
case "GEOIP": case "GEOIP":