Skip to content

Commit

Permalink
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 49ff966 commit 6d42bf6
Show file tree
Hide file tree
Showing 5 changed files with 46 additions and 10 deletions.
2 changes: 1 addition & 1 deletion adapter/outboundgroup/common.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ func getProvidersProxies(providers []provider.ProxyProvider, touch bool, filter
for _, filterReg := range 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{}{}
matchedProxies = append(matchedProxies, p)
Expand Down
4 changes: 2 additions & 2 deletions adapter/provider/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -161,12 +161,12 @@ func NewProxySetProvider(name string, interval time.Duration, filter string, exc
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 @@ -14,8 +14,10 @@ const (
InUser
InName
InType
Process
ProcessName
ProcessPath
ProcessNameRegex
ProcessPathRegex
Network
Type_
IPSet
Expand Down Expand Up @@ -61,10 +63,14 @@ func (rt RuleType) String() string {
return "Network"
case Type_:
return "Type"
case Process:
return "Process"
case ProcessName:
return "ProcessName"
case ProcessPath:
return "ProcessPath"
case ProcessNameRegex:
return "ProcessNameRegex"
case ProcessPathRegex:
return "ProcessPathRegex"
case IPSet:
return "IPSet"
case MATCH:
Expand Down
8 changes: 6 additions & 2 deletions rule/parser.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,9 +42,13 @@ func ParseRule(tp, payload, target string, params []string, subRules map[string]
case "IN-PORT":
parsed, parseErr = NewPort(payload, target, C.InPort)
case "PROCESS-NAME":
parsed, parseErr = NewProcess(payload, target, true)
parsed, parseErr = NewProcess(payload, target, true, false)
case "PROCESS-PATH":
parsed, parseErr = NewProcess(payload, target, false)
parsed, parseErr = NewProcess(payload, target, false, false)
case "PROCESS-NAME-REGEX":
parsed, parseErr = NewProcess(payload, target, true, true)
case "PROCESS-PATH-REGEX":
parsed, parseErr = NewProcess(payload, target, false, true)
case "NETWORK":
parsed, parseErr = NewNetwork(payload, target)
case "IN-TYPE":
Expand Down
30 changes: 28 additions & 2 deletions rule/process.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,27 +4,44 @@ import (
"strings"

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

"github.com/dlclark/regexp2"
)

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
}

Expand All @@ -44,10 +61,19 @@ 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{
adapter: adapter,
process: process,
nameOnly: nameOnly,
regexp: r,
}, nil
}

0 comments on commit 6d42bf6

Please sign in to comment.