Add process_path rule item (#51)

* process matching supports full path
* Remove strings.ToLower
This commit is contained in:
dyhkwong 2022-08-30 10:44:40 +08:00 committed by GitHub
parent cd98ea5008
commit 374743d022
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
7 changed files with 67 additions and 4 deletions

View file

@ -94,6 +94,7 @@ type DefaultDNSRule struct {
Port Listable[uint16] `json:"port,omitempty"` Port Listable[uint16] `json:"port,omitempty"`
PortRange Listable[string] `json:"port_range,omitempty"` PortRange Listable[string] `json:"port_range,omitempty"`
ProcessName Listable[string] `json:"process_name,omitempty"` ProcessName Listable[string] `json:"process_name,omitempty"`
ProcessPath Listable[string] `json:"process_path,omitempty"`
PackageName Listable[string] `json:"package_name,omitempty"` PackageName Listable[string] `json:"package_name,omitempty"`
User Listable[string] `json:"user,omitempty"` User Listable[string] `json:"user,omitempty"`
UserID Listable[int32] `json:"user_id,omitempty"` UserID Listable[int32] `json:"user_id,omitempty"`

View file

@ -96,6 +96,7 @@ type DefaultRule struct {
Port Listable[uint16] `json:"port,omitempty"` Port Listable[uint16] `json:"port,omitempty"`
PortRange Listable[string] `json:"port_range,omitempty"` PortRange Listable[string] `json:"port_range,omitempty"`
ProcessName Listable[string] `json:"process_name,omitempty"` ProcessName Listable[string] `json:"process_name,omitempty"`
ProcessPath Listable[string] `json:"process_path,omitempty"`
PackageName Listable[string] `json:"package_name,omitempty"` PackageName Listable[string] `json:"package_name,omitempty"`
User Listable[string] `json:"user,omitempty"` User Listable[string] `json:"user,omitempty"`
UserID Listable[int32] `json:"user_id,omitempty"` UserID Listable[int32] `json:"user_id,omitempty"`

View file

@ -789,11 +789,11 @@ func isGeositeDNSRule(rule option.DefaultDNSRule) bool {
} }
func isProcessRule(rule option.DefaultRule) bool { func isProcessRule(rule option.DefaultRule) bool {
return len(rule.ProcessName) > 0 || len(rule.PackageName) > 0 || len(rule.User) > 0 || len(rule.UserID) > 0 return len(rule.ProcessName) > 0 || len(rule.ProcessPath) > 0 || len(rule.PackageName) > 0 || len(rule.User) > 0 || len(rule.UserID) > 0
} }
func isProcessDNSRule(rule option.DefaultDNSRule) bool { func isProcessDNSRule(rule option.DefaultDNSRule) bool {
return len(rule.ProcessName) > 0 || len(rule.PackageName) > 0 || len(rule.User) > 0 || len(rule.UserID) > 0 return len(rule.ProcessName) > 0 || len(rule.ProcessPath) > 0 || len(rule.PackageName) > 0 || len(rule.User) > 0 || len(rule.UserID) > 0
} }
func notPrivateNode(code string) bool { func notPrivateNode(code string) bool {

View file

@ -172,6 +172,11 @@ func NewDefaultRule(router adapter.Router, logger log.ContextLogger, options opt
rule.items = append(rule.items, item) rule.items = append(rule.items, item)
rule.allItems = append(rule.allItems, item) rule.allItems = append(rule.allItems, item)
} }
if len(options.ProcessPath) > 0 {
item := NewProcessPathItem(options.ProcessPath)
rule.items = append(rule.items, item)
rule.allItems = append(rule.allItems, item)
}
if len(options.PackageName) > 0 { if len(options.PackageName) > 0 {
item := NewPackageNameItem(options.PackageName) item := NewPackageNameItem(options.PackageName)
rule.items = append(rule.items, item) rule.items = append(rule.items, item)

View file

@ -155,6 +155,11 @@ func NewDefaultDNSRule(router adapter.Router, logger log.ContextLogger, options
rule.items = append(rule.items, item) rule.items = append(rule.items, item)
rule.allItems = append(rule.allItems, item) rule.allItems = append(rule.allItems, item)
} }
if len(options.ProcessPath) > 0 {
item := NewProcessPathItem(options.ProcessPath)
rule.items = append(rule.items, item)
rule.allItems = append(rule.allItems, item)
}
if len(options.PackageName) > 0 { if len(options.PackageName) > 0 {
item := NewPackageNameItem(options.PackageName) item := NewPackageNameItem(options.PackageName)
rule.items = append(rule.items, item) rule.items = append(rule.items, item)

View file

@ -11,7 +11,7 @@ import (
var warnProcessNameOnNonSupportedPlatform = warning.New( var warnProcessNameOnNonSupportedPlatform = warning.New(
func() bool { return !(C.IsLinux || C.IsWindows || C.IsDarwin) }, func() bool { return !(C.IsLinux || C.IsWindows || C.IsDarwin) },
"rule item `process_item` is only supported on Linux, Windows, and macOS", "rule item `process_name` is only supported on Linux, Windows and macOS",
) )
var _ RuleItem = (*ProcessItem)(nil) var _ RuleItem = (*ProcessItem)(nil)
@ -37,7 +37,7 @@ func (r *ProcessItem) Match(metadata *adapter.InboundContext) bool {
if metadata.ProcessInfo == nil || metadata.ProcessInfo.ProcessPath == "" { if metadata.ProcessInfo == nil || metadata.ProcessInfo.ProcessPath == "" {
return false return false
} }
return r.processMap[strings.ToLower(filepath.Base(metadata.ProcessInfo.ProcessPath))] return r.processMap[filepath.Base(metadata.ProcessInfo.ProcessPath)]
} }
func (r *ProcessItem) String() string { func (r *ProcessItem) String() string {

View file

@ -0,0 +1,51 @@
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
}