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 1beeb49
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 7 deletions.
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 1beeb49

Please sign in to comment.