sing-box/route/rule_process_path.go
dyhkwong 374743d022
Add process_path rule item (#51)
* process matching supports full path
* Remove strings.ToLower
2022-08-30 10:44:40 +08:00

52 lines
1.3 KiB
Go

package route
import (
"strings"
"github.com/sagernet/sing-box/adapter"
"github.com/sagernet/sing-box/common/warning"
C "github.com/sagernet/sing-box/constant"
)
var warnProcessPathOnNonSupportedPlatform = warning.New(
func() bool { return !(C.IsLinux || C.IsWindows || C.IsDarwin) },
"rule item `process_path` is only supported on Linux, Windows and macOS",
)
var _ RuleItem = (*ProcessPathItem)(nil)
type ProcessPathItem struct {
processes []string
processMap map[string]bool
}
func NewProcessPathItem(processNameList []string) *ProcessPathItem {
warnProcessPathOnNonSupportedPlatform.Check()
rule := &ProcessPathItem{
processes: processNameList,
processMap: make(map[string]bool),
}
for _, processName := range processNameList {
rule.processMap[processName] = true
}
return rule
}
func (r *ProcessPathItem) Match(metadata *adapter.InboundContext) bool {
if metadata.ProcessInfo == nil || metadata.ProcessInfo.ProcessPath == "" {
return false
}
return r.processMap[metadata.ProcessInfo.ProcessPath]
}
func (r *ProcessPathItem) String() string {
var description string
pLen := len(r.processes)
if pLen == 1 {
description = "process_path=" + r.processes[0]
} else {
description = "process_path=[" + strings.Join(r.processes, " ") + "]"
}
return description
}