feat: add PROCESS-NAME-REGEX and PROCESS-PATH-REGEX

This commit is contained in:
wwqgtxx 2024-05-15 10:44:56 +08:00
parent ed1e7e32c7
commit 1bc3c16b59
8 changed files with 62 additions and 25 deletions

View File

@ -48,7 +48,7 @@ type GroupBaseOption struct {
func NewGroupBase(opt GroupBaseOption) *GroupBase { func NewGroupBase(opt GroupBaseOption) *GroupBase {
var excludeFilterReg *regexp2.Regexp var excludeFilterReg *regexp2.Regexp
if opt.excludeFilter != "" { if opt.excludeFilter != "" {
excludeFilterReg = regexp2.MustCompile(opt.excludeFilter, 0) excludeFilterReg = regexp2.MustCompile(opt.excludeFilter, regexp2.None)
} }
var excludeTypeArray []string var excludeTypeArray []string
if opt.excludeType != "" { if opt.excludeType != "" {
@ -58,7 +58,7 @@ func NewGroupBase(opt GroupBaseOption) *GroupBase {
var filterRegs []*regexp2.Regexp var filterRegs []*regexp2.Regexp
if opt.filter != "" { if opt.filter != "" {
for _, filter := range strings.Split(opt.filter, "`") { for _, filter := range strings.Split(opt.filter, "`") {
filterReg := regexp2.MustCompile(filter, 0) filterReg := regexp2.MustCompile(filter, regexp2.None)
filterRegs = append(filterRegs, filterReg) filterRegs = append(filterRegs, filterReg)
} }
} }
@ -126,7 +126,7 @@ func (gb *GroupBase) GetProxies(touch bool) []C.Proxy {
for _, filterReg := range gb.filterRegs { for _, filterReg := range gb.filterRegs {
for _, p := range proxies { for _, p := range proxies {
name := p.Name() name := p.Name()
if mat, _ := filterReg.FindStringMatch(name); mat != nil { if mat, _ := filterReg.MatchString(name); mat {
if _, ok := proxiesSet[name]; !ok { if _, ok := proxiesSet[name]; !ok {
proxiesSet[name] = struct{}{} proxiesSet[name] = struct{}{}
newProxies = append(newProxies, p) newProxies = append(newProxies, p)
@ -150,7 +150,7 @@ func (gb *GroupBase) GetProxies(touch bool) []C.Proxy {
for _, filterReg := range gb.filterRegs { for _, filterReg := range gb.filterRegs {
for _, p := range proxies { for _, p := range proxies {
name := p.Name() name := p.Name()
if mat, _ := filterReg.FindStringMatch(name); mat != nil { if mat, _ := filterReg.MatchString(name); mat {
if _, ok := proxiesSet[name]; !ok { if _, ok := proxiesSet[name]; !ok {
proxiesSet[name] = struct{}{} proxiesSet[name] = struct{}{}
newProxies = append(newProxies, p) newProxies = append(newProxies, p)
@ -191,7 +191,7 @@ func (gb *GroupBase) GetProxies(touch bool) []C.Proxy {
var newProxies []C.Proxy var newProxies []C.Proxy
for _, p := range proxies { for _, p := range proxies {
name := p.Name() name := p.Name()
if mat, _ := gb.excludeFilterReg.FindStringMatch(name); mat != nil { if mat, _ := gb.excludeFilterReg.MatchString(name); mat {
continue continue
} }
newProxies = append(newProxies, p) newProxies = append(newProxies, p)

View File

@ -75,12 +75,12 @@ func ParseProxyGroup(config map[string]any, proxyMap map[string]C.Proxy, provide
if groupOption.Filter != "" { if groupOption.Filter != "" {
var filterRegs []*regexp2.Regexp var filterRegs []*regexp2.Regexp
for _, filter := range strings.Split(groupOption.Filter, "`") { for _, filter := range strings.Split(groupOption.Filter, "`") {
filterReg := regexp2.MustCompile(filter, 0) filterReg := regexp2.MustCompile(filter, regexp2.None)
filterRegs = append(filterRegs, filterReg) filterRegs = append(filterRegs, filterReg)
} }
for _, p := range AllProxies { for _, p := range AllProxies {
for _, filterReg := range filterRegs { for _, filterReg := range filterRegs {
if mat, _ := filterReg.FindStringMatch(p); mat != nil { if mat, _ := filterReg.MatchString(p); mat {
groupOption.Proxies = append(groupOption.Proxies, p) groupOption.Proxies = append(groupOption.Proxies, p)
} }
} }

View File

@ -181,14 +181,14 @@ func (hc *HealthCheck) execute(b *batch.Batch[bool], url, uid string, option *ex
filters = append(filters, filter) filters = append(filters, filter)
} }
filterReg = regexp2.MustCompile(strings.Join(filters, "|"), 0) filterReg = regexp2.MustCompile(strings.Join(filters, "|"), regexp2.None)
} }
} }
for _, proxy := range hc.proxies { for _, proxy := range hc.proxies {
// skip proxies that do not require health check // skip proxies that do not require health check
if filterReg != nil { if filterReg != nil {
if match, _ := filterReg.FindStringMatch(proxy.Name()); match == nil { if match, _ := filterReg.MatchString(proxy.Name()); !match {
continue continue
} }
} }

View File

@ -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) { 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 { if err != nil {
return nil, fmt.Errorf("invalid excludeFilter regex: %w", err) return nil, fmt.Errorf("invalid excludeFilter regex: %w", err)
} }
@ -180,7 +180,7 @@ func NewProxySetProvider(name string, interval time.Duration, filter string, exc
var filterRegs []*regexp2.Regexp var filterRegs []*regexp2.Regexp
for _, filter := range strings.Split(filter, "`") { for _, filter := range strings.Split(filter, "`") {
filterReg, err := regexp2.Compile(filter, 0) filterReg, err := regexp2.Compile(filter, regexp2.None)
if err != nil { if err != nil {
return nil, fmt.Errorf("invalid filter regex: %w", err) return nil, fmt.Errorf("invalid filter regex: %w", err)
} }
@ -356,12 +356,12 @@ func proxiesParseAndFilter(filter string, excludeFilter string, excludeTypeArray
continue continue
} }
if len(excludeFilter) > 0 { if len(excludeFilter) > 0 {
if mat, _ := excludeFilterReg.FindStringMatch(name); mat != nil { if mat, _ := excludeFilterReg.MatchString(name); mat {
continue continue
} }
} }
if len(filter) > 0 { if len(filter) > 0 {
if mat, _ := filterReg.FindStringMatch(name); mat == nil { if mat, _ := filterReg.MatchString(name); !mat {
continue continue
} }
} }

View File

@ -22,8 +22,10 @@ const (
InUser InUser
InName InName
InType InType
Process ProcessName
ProcessPath ProcessPath
ProcessNameRegex
ProcessPathRegex
RuleSet RuleSet
Network Network
Uid Uid
@ -76,10 +78,14 @@ func (rt RuleType) String() string {
return "InName" return "InName"
case InType: case InType:
return "InType" return "InType"
case Process: case ProcessName:
return "Process" return "ProcessName"
case ProcessPath: case ProcessPath:
return "ProcessPath" return "ProcessPath"
case ProcessNameRegex:
return "ProcessNameRegex"
case ProcessPathRegex:
return "ProcessPathRegex"
case MATCH: case MATCH:
return "Match" return "Match"
case RuleSet: case RuleSet:

View File

@ -1,14 +1,14 @@
package common package common
import ( import (
"regexp"
C "github.com/metacubex/mihomo/constant" C "github.com/metacubex/mihomo/constant"
"github.com/dlclark/regexp2"
) )
type DomainRegex struct { type DomainRegex struct {
*Base *Base
regex *regexp.Regexp regex *regexp2.Regexp
adapter string adapter string
} }
@ -18,7 +18,8 @@ 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()
return dr.regex.MatchString(domain), dr.adapter match, _ := dr.regex.MatchString(domain)
return match, dr.adapter
} }
func (dr *DomainRegex) Adapter() string { func (dr *DomainRegex) Adapter() string {
@ -30,7 +31,7 @@ func (dr *DomainRegex) Payload() string {
} }
func NewDomainRegex(regex string, adapter string) (*DomainRegex, error) { func NewDomainRegex(regex string, adapter string) (*DomainRegex, error) {
r, err := regexp.Compile(regex) r, err := regexp2.Compile(regex, regexp2.IgnoreCase)
if err != nil { if err != nil {
return nil, err return nil, err
} }

View File

@ -4,6 +4,8 @@ import (
"strings" "strings"
C "github.com/metacubex/mihomo/constant" C "github.com/metacubex/mihomo/constant"
"github.com/dlclark/regexp2"
) )
type Process struct { type Process struct {
@ -11,21 +13,36 @@ type Process struct {
adapter string adapter string
process string process string
nameOnly bool nameOnly bool
regexp *regexp2.Regexp
} }
func (ps *Process) RuleType() C.RuleType { func (ps *Process) RuleType() C.RuleType {
if ps.nameOnly { 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 return C.ProcessPath
} }
func (ps *Process) Match(metadata *C.Metadata) (bool, string) { func (ps *Process) Match(metadata *C.Metadata) (bool, string) {
if ps.nameOnly { 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 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 return strings.EqualFold(metadata.ProcessPath, ps.process), ps.adapter
} }
@ -41,11 +58,20 @@ func (ps *Process) ShouldFindProcess() bool {
return true 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{ return &Process{
Base: &Base{}, Base: &Base{},
adapter: adapter, adapter: adapter,
process: process, process: process,
nameOnly: nameOnly, nameOnly: nameOnly,
regexp: r,
}, nil }, nil
} }

View File

@ -50,9 +50,13 @@ func ParseRule(tp, payload, target string, params []string, subRules map[string]
case "DSCP": case "DSCP":
parsed, parseErr = RC.NewDSCP(payload, target) parsed, parseErr = RC.NewDSCP(payload, target)
case "PROCESS-NAME": case "PROCESS-NAME":
parsed, parseErr = RC.NewProcess(payload, target, true) parsed, parseErr = RC.NewProcess(payload, target, true, false)
case "PROCESS-PATH": 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": case "NETWORK":
parsed, parseErr = RC.NewNetworkType(payload, target) parsed, parseErr = RC.NewNetworkType(payload, target)
case "UID": case "UID":