Skip to content

Commit

Permalink
ensure that some expressions take effect
Browse files Browse the repository at this point in the history
  • Loading branch information
wwqgtxx committed May 15, 2024
1 parent 5c37598 commit 9ce448b
Show file tree
Hide file tree
Showing 4 changed files with 57 additions and 1 deletion.
2 changes: 1 addition & 1 deletion config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -733,7 +733,7 @@ func parseRules(rulesConfig []string, proxies map[string]C.Proxy, subRules map[s

l := len(rule)

if ruleName == "NOT" || ruleName == "OR" || ruleName == "AND" || ruleName == "SUB-RULE" {
if ruleName == "NOT" || ruleName == "OR" || ruleName == "AND" || ruleName == "SUB-RULE" || ruleName == "DOMAIN-REGEX" || ruleName == "PROCESS-NAME-REGEX" || ruleName == "PROCESS-PATH-REGEX" {
target = rule[l-1]
payload = strings.Join(rule[1:l-1], ",")
} else {
Expand Down
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
GEOIP
IPCIDR
SrcIPCIDR
Expand Down Expand Up @@ -41,6 +42,8 @@ func (rt RuleType) String() string {
return "DomainSuffix"
case DomainKeyword:
return "DomainKeyword"
case DomainRegex:
return "DomainRegex"
case GEOIP:
return "GeoIP"
case IPCIDR:
Expand Down
51 changes: 51 additions & 0 deletions rule/domain_regex.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
package rules

import (
C "github.com/metacubex/mihomo/constant"

"github.com/dlclark/regexp2"
)

type DomainRegex struct {
regex *regexp2.Regexp
adapter string
}

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

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

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

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

func (dr *DomainRegex) ShouldResolveIP() bool {
return false
}

func (dr *DomainRegex) ShouldFindProcess() bool {
return false
}

func NewDomainRegex(regex string, adapter string) (*DomainRegex, error) {
r, err := regexp2.Compile(regex, regexp2.IgnoreCase)
if err != nil {
return nil, err
}
return &DomainRegex{
regex: r,
adapter: adapter,
}, nil
}

//var _ C.Rule = (*DomainRegex)(nil)
2 changes: 2 additions & 0 deletions rule/parser.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,8 @@ func ParseRule(tp, payload, target string, params []string, subRules map[string]
parsed = NewDomainSuffix(payload, target)
case "DOMAIN-KEYWORD":
parsed = NewDomainKeyword(payload, target)
case "DOMAIN-REGEX":
parsed, parseErr = NewDomainRegex(payload, target)
case "GEOIP":
noResolve := HasNoResolve(params)
parsed = NewGEOIP(payload, target, noResolve)
Expand Down

0 comments on commit 9ce448b

Please sign in to comment.