Skip to content

Commit

Permalink
feat: add PROCESS-NAME-REGEX and PROCESS-PATH-REGEX
Browse files Browse the repository at this point in the history
  • Loading branch information
wwqgtxx committed May 15, 2024
1 parent ed1e7e3 commit 1bc3c16
Show file tree
Hide file tree
Showing 8 changed files with 62 additions and 25 deletions.
10 changes: 5 additions & 5 deletions adapter/outboundgroup/groupbase.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ type GroupBaseOption struct {
func NewGroupBase(opt GroupBaseOption) *GroupBase {
var excludeFilterReg *regexp2.Regexp
if opt.excludeFilter != "" {
excludeFilterReg = regexp2.MustCompile(opt.excludeFilter, 0)
excludeFilterReg = regexp2.MustCompile(opt.excludeFilter, regexp2.None)
}
var excludeTypeArray []string
if opt.excludeType != "" {
Expand All @@ -58,7 +58,7 @@ func NewGroupBase(opt GroupBaseOption) *GroupBase {
var filterRegs []*regexp2.Regexp
if opt.filter != "" {
for _, filter := range strings.Split(opt.filter, "`") {
filterReg := regexp2.MustCompile(filter, 0)
filterReg := regexp2.MustCompile(filter, regexp2.None)
filterRegs = append(filterRegs, filterReg)
}
}
Expand Down Expand Up @@ -126,7 +126,7 @@ func (gb *GroupBase) GetProxies(touch bool) []C.Proxy {
for _, filterReg := range gb.filterRegs {
for _, p := range proxies {
name := p.Name()
if mat, _ := filterReg.FindStringMatch(name); mat != nil {
if mat, _ := filterReg.MatchString(name); mat {
if _, ok := proxiesSet[name]; !ok {
proxiesSet[name] = struct{}{}
newProxies = append(newProxies, p)
Expand All @@ -150,7 +150,7 @@ func (gb *GroupBase) GetProxies(touch bool) []C.Proxy {
for _, filterReg := range gb.filterRegs {
for _, p := range proxies {
name := p.Name()
if mat, _ := filterReg.FindStringMatch(name); mat != nil {
if mat, _ := filterReg.MatchString(name); mat {
if _, ok := proxiesSet[name]; !ok {
proxiesSet[name] = struct{}{}
newProxies = append(newProxies, p)
Expand Down Expand Up @@ -191,7 +191,7 @@ func (gb *GroupBase) GetProxies(touch bool) []C.Proxy {
var newProxies []C.Proxy
for _, p := range proxies {
name := p.Name()
if mat, _ := gb.excludeFilterReg.FindStringMatch(name); mat != nil {
if mat, _ := gb.excludeFilterReg.MatchString(name); mat {
continue
}
newProxies = append(newProxies, p)
Expand Down
4 changes: 2 additions & 2 deletions adapter/outboundgroup/parser.go
Original file line number Diff line number Diff line change
Expand Up @@ -75,12 +75,12 @@ func ParseProxyGroup(config map[string]any, proxyMap map[string]C.Proxy, provide
if groupOption.Filter != "" {
var filterRegs []*regexp2.Regexp
for _, filter := range strings.Split(groupOption.Filter, "`") {
filterReg := regexp2.MustCompile(filter, 0)
filterReg := regexp2.MustCompile(filter, regexp2.None)
filterRegs = append(filterRegs, filterReg)
}
for _, p := range AllProxies {
for _, filterReg := range filterRegs {
if mat, _ := filterReg.FindStringMatch(p); mat != nil {
if mat, _ := filterReg.MatchString(p); mat {
groupOption.Proxies = append(groupOption.Proxies, p)
}
}
Expand Down
4 changes: 2 additions & 2 deletions adapter/provider/healthcheck.go
Original file line number Diff line number Diff line change
Expand Up @@ -181,14 +181,14 @@ func (hc *HealthCheck) execute(b *batch.Batch[bool], url, uid string, option *ex
filters = append(filters, filter)
}

filterReg = regexp2.MustCompile(strings.Join(filters, "|"), 0)
filterReg = regexp2.MustCompile(strings.Join(filters, "|"), regexp2.None)
}
}

for _, proxy := range hc.proxies {
// skip proxies that do not require health check
if filterReg != nil {
if match, _ := filterReg.FindStringMatch(proxy.Name()); match == nil {
if match, _ := filterReg.MatchString(proxy.Name()); !match {
continue
}
}
Expand Down
8 changes: 4 additions & 4 deletions adapter/provider/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -169,7 +169,7 @@ func stopProxyProvider(pd *ProxySetProvider) {
}

func NewProxySetProvider(name string, interval time.Duration, filter string, excludeFilter string, excludeType string, dialerProxy string, override OverrideSchema, vehicle types.Vehicle, hc *HealthCheck) (*ProxySetProvider, error) {
excludeFilterReg, err := regexp2.Compile(excludeFilter, 0)
excludeFilterReg, err := regexp2.Compile(excludeFilter, regexp2.None)
if err != nil {
return nil, fmt.Errorf("invalid excludeFilter regex: %w", err)
}
Expand All @@ -180,7 +180,7 @@ func NewProxySetProvider(name string, interval time.Duration, filter string, exc

var filterRegs []*regexp2.Regexp
for _, filter := range strings.Split(filter, "`") {
filterReg, err := regexp2.Compile(filter, 0)
filterReg, err := regexp2.Compile(filter, regexp2.None)
if err != nil {
return nil, fmt.Errorf("invalid filter regex: %w", err)
}
Expand Down Expand Up @@ -356,12 +356,12 @@ func proxiesParseAndFilter(filter string, excludeFilter string, excludeTypeArray
continue
}
if len(excludeFilter) > 0 {
if mat, _ := excludeFilterReg.FindStringMatch(name); mat != nil {
if mat, _ := excludeFilterReg.MatchString(name); mat {
continue
}
}
if len(filter) > 0 {
if mat, _ := filterReg.FindStringMatch(name); mat == nil {
if mat, _ := filterReg.MatchString(name); !mat {
continue
}
}
Expand Down
12 changes: 9 additions & 3 deletions constant/rule.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,10 @@ const (
InUser
InName
InType
Process
ProcessName
ProcessPath
ProcessNameRegex
ProcessPathRegex
RuleSet
Network
Uid
Expand Down Expand Up @@ -76,10 +78,14 @@ func (rt RuleType) String() string {
return "InName"
case InType:
return "InType"
case Process:
return "Process"
case ProcessName:
return "ProcessName"
case ProcessPath:
return "ProcessPath"
case ProcessNameRegex:
return "ProcessNameRegex"
case ProcessPathRegex:
return "ProcessPathRegex"
case MATCH:
return "Match"
case RuleSet:
Expand Down
11 changes: 6 additions & 5 deletions rules/common/domain_regex.go
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
package common

import (
"regexp"

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

"github.com/dlclark/regexp2"
)

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

Expand All @@ -18,7 +18,8 @@ func (dr *DomainRegex) RuleType() C.RuleType {

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

func (dr *DomainRegex) Adapter() string {
Expand All @@ -30,7 +31,7 @@ func (dr *DomainRegex) Payload() string {
}

func NewDomainRegex(regex string, adapter string) (*DomainRegex, error) {
r, err := regexp.Compile(regex)
r, err := regexp2.Compile(regex, regexp2.IgnoreCase)
if err != nil {
return nil, err
}
Expand Down
30 changes: 28 additions & 2 deletions rules/common/process.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,28 +4,45 @@ import (
"strings"

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

"github.com/dlclark/regexp2"
)

type Process struct {
*Base
adapter string
process string
nameOnly bool
regexp *regexp2.Regexp
}

func (ps *Process) RuleType() C.RuleType {
if ps.nameOnly {
return C.Process
if ps.regexp != nil {
return C.ProcessNameRegex
}
return C.ProcessName
}

if ps.regexp != nil {
return C.ProcessPathRegex
}
return C.ProcessPath
}

func (ps *Process) Match(metadata *C.Metadata) (bool, string) {
if ps.nameOnly {
if ps.regexp != nil {
match, _ := ps.regexp.MatchString(metadata.Process)
return match, ps.adapter
}
return strings.EqualFold(metadata.Process, ps.process), ps.adapter
}

if ps.regexp != nil {
match, _ := ps.regexp.MatchString(metadata.ProcessPath)
return match, ps.adapter
}
return strings.EqualFold(metadata.ProcessPath, ps.process), ps.adapter
}

Expand All @@ -41,11 +58,20 @@ func (ps *Process) ShouldFindProcess() bool {
return true
}

func NewProcess(process string, adapter string, nameOnly bool) (*Process, error) {
func NewProcess(process string, adapter string, nameOnly bool, regex bool) (*Process, error) {
var r *regexp2.Regexp
var err error
if regex {
r, err = regexp2.Compile(process, regexp2.IgnoreCase)
if err != nil {
return nil, err
}
}
return &Process{
Base: &Base{},
adapter: adapter,
process: process,
nameOnly: nameOnly,
regexp: r,
}, nil
}
8 changes: 6 additions & 2 deletions rules/parser.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,9 +50,13 @@ func ParseRule(tp, payload, target string, params []string, subRules map[string]
case "DSCP":
parsed, parseErr = RC.NewDSCP(payload, target)
case "PROCESS-NAME":
parsed, parseErr = RC.NewProcess(payload, target, true)
parsed, parseErr = RC.NewProcess(payload, target, true, false)
case "PROCESS-PATH":
parsed, parseErr = RC.NewProcess(payload, target, false)
parsed, parseErr = RC.NewProcess(payload, target, false, false)
case "PROCESS-NAME-REGEX":
parsed, parseErr = RC.NewProcess(payload, target, true, true)
case "PROCESS-PATH-REGEX":
parsed, parseErr = RC.NewProcess(payload, target, false, true)
case "NETWORK":
parsed, parseErr = RC.NewNetworkType(payload, target)
case "UID":
Expand Down

0 comments on commit 1bc3c16

Please sign in to comment.