mihomo/rules/common/process.go

78 lines
1.4 KiB
Go
Raw Normal View History

package common
import (
"strings"
2023-11-03 21:01:45 +08:00
C "github.com/metacubex/mihomo/constant"
"github.com/dlclark/regexp2"
)
type Process struct {
2022-03-17 23:24:07 +08:00
*Base
adapter string
process string
nameOnly bool
regexp *regexp2.Regexp
2022-03-16 00:43:08 +08:00
}
func (ps *Process) RuleType() C.RuleType {
2022-07-06 13:44:04 +08:00
if ps.nameOnly {
if ps.regexp != nil {
return C.ProcessNameRegex
}
return C.ProcessName
2022-07-06 13:44:04 +08:00
}
if ps.regexp != nil {
return C.ProcessPathRegex
}
2022-07-06 13:44:04 +08:00
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
2021-11-17 16:03:47 +08:00
}
if ps.regexp != nil {
match, _ := ps.regexp.MatchString(metadata.ProcessPath)
return match, ps.adapter
}
return strings.EqualFold(metadata.ProcessPath, ps.process), ps.adapter
}
2021-03-24 01:00:21 +08:00
func (ps *Process) Adapter() string {
return ps.adapter
}
2021-03-24 01:00:21 +08:00
func (ps *Process) Payload() string {
return ps.process
}
func (ps *Process) ShouldFindProcess() bool {
return true
}
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{
2022-03-17 23:24:07 +08:00
Base: &Base{},
adapter: adapter,
process: process,
nameOnly: nameOnly,
regexp: r,
}, nil
}