Skip to content

Commit

Permalink
feat: add DOMAIN-REGEX rule
Browse files Browse the repository at this point in the history
  • Loading branch information
xishang0128 committed Mar 7, 2024
1 parent fad1a08 commit 234a4bf
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 1 deletion.
3 changes: 3 additions & 0 deletions constant/rule.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ const (
Domain RuleType = iota
DomainSuffix
DomainKeyword
DomainRegex
GEOSITE
GEOIP
IPCIDR
Expand Down Expand Up @@ -40,6 +41,8 @@ func (rt RuleType) String() string {
return "DomainSuffix"
case DomainKeyword:
return "DomainKeyword"
case DomainRegex:
return "DomainRegex"
case GEOSITE:
return "GeoSite"
case GEOIP:
Expand Down
42 changes: 42 additions & 0 deletions rules/common/domain_regex.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
package common

import (
"regexp"
"strings"

C "github.com/metacubex/mihomo/constant"
)

type DomainRegex struct {
*Base
regex string
adapter string
}

func (dr *DomainRegex) RuleType() C.RuleType {
return C.DomainRegex
}

func (dr *DomainRegex) Match(metadata *C.Metadata) (bool, string) {
domain := metadata.RuleHost()
match, _ := regexp.MatchString(dr.regex, domain)
return match, dr.adapter
}

func (dr *DomainRegex) Adapter() string {
return dr.adapter
}

func (dr *DomainRegex) Payload() string {
return dr.regex
}

func NewDomainRegex(regex string, adapter string) *DomainRegex {
return &DomainRegex{
Base: &Base{},
regex: strings.ToLower(regex),
adapter: adapter,
}
}

//var _ C.Rule = (*DomainRegex)(nil)
4 changes: 3 additions & 1 deletion rules/parser.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ package rules

import (
"fmt"

C "github.com/metacubex/mihomo/constant"
RC "github.com/metacubex/mihomo/rules/common"
"github.com/metacubex/mihomo/rules/logic"
Expand All @@ -17,6 +17,8 @@ func ParseRule(tp, payload, target string, params []string, subRules map[string]
parsed = RC.NewDomainSuffix(payload, target)
case "DOMAIN-KEYWORD":
parsed = RC.NewDomainKeyword(payload, target)
case "DOMAIN-REGEX":
parsed = RC.NewDomainRegex(payload, target)
case "GEOSITE":
parsed, parseErr = RC.NewGEOSITE(payload, target)
case "GEOIP":
Expand Down

0 comments on commit 234a4bf

Please sign in to comment.