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

@@ -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
}
@@ -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 {
@@ -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
}

View File

@@ -4,6 +4,8 @@ import (
"strings"
C "github.com/metacubex/mihomo/constant"
"github.com/dlclark/regexp2"
)
type Process struct {
@@ -11,21 +13,36 @@ type Process struct {
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
}
@@ -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
}