From bdfc1fe35cf198a0e67e168a642d83e6df4d2595 Mon Sep 17 00:00:00 2001 From: dyhkwong <50692134+dyhkwong@users.noreply.github.com> Date: Fri, 26 Aug 2022 23:46:33 +0800 Subject: [PATCH 1/3] process matching supports full path --- option/dns.go | 1 + option/route.go | 1 + route/router.go | 4 +-- route/rule.go | 5 ++++ route/rule_dns.go | 5 ++++ route/rule_process.go | 2 +- route/rule_process_path.go | 51 ++++++++++++++++++++++++++++++++++++++ 7 files changed, 66 insertions(+), 3 deletions(-) create mode 100644 route/rule_process_path.go diff --git a/option/dns.go b/option/dns.go index aac75ea82a..d8da0ff11a 100644 --- a/option/dns.go +++ b/option/dns.go @@ -94,6 +94,7 @@ type DefaultDNSRule struct { Port Listable[uint16] `json:"port,omitempty"` PortRange Listable[string] `json:"port_range,omitempty"` ProcessName Listable[string] `json:"process_name,omitempty"` + ProcessPath Listable[string] `json:"process_path,omitempty"` PackageName Listable[string] `json:"package_name,omitempty"` User Listable[string] `json:"user,omitempty"` UserID Listable[int32] `json:"user_id,omitempty"` diff --git a/option/route.go b/option/route.go index 6f54b7df65..86f31d235e 100644 --- a/option/route.go +++ b/option/route.go @@ -96,6 +96,7 @@ type DefaultRule struct { Port Listable[uint16] `json:"port,omitempty"` PortRange Listable[string] `json:"port_range,omitempty"` ProcessName Listable[string] `json:"process_name,omitempty"` + ProcessPath Listable[string] `json:"process_path,omitempty"` PackageName Listable[string] `json:"package_name,omitempty"` User Listable[string] `json:"user,omitempty"` UserID Listable[int32] `json:"user_id,omitempty"` diff --git a/route/router.go b/route/router.go index 42c3fadf25..fe81dc243a 100644 --- a/route/router.go +++ b/route/router.go @@ -789,11 +789,11 @@ func isGeositeDNSRule(rule option.DefaultDNSRule) 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.PackageName) > 0 || len(rule.User) > 0 || len(rule.UserID) > 0 || len(rule.ProcessPath) > 0 } 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.PackageName) > 0 || len(rule.User) > 0 || len(rule.UserID) > 0 || len(rule.ProcessPath) > 0 } func notPrivateNode(code string) bool { diff --git a/route/rule.go b/route/rule.go index 35d7d24fa2..12186e4b4c 100644 --- a/route/rule.go +++ b/route/rule.go @@ -172,6 +172,11 @@ func NewDefaultRule(router adapter.Router, logger log.ContextLogger, options opt rule.items = append(rule.items, 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 { item := NewPackageNameItem(options.PackageName) rule.items = append(rule.items, item) diff --git a/route/rule_dns.go b/route/rule_dns.go index 053bf7773f..6364f54857 100644 --- a/route/rule_dns.go +++ b/route/rule_dns.go @@ -155,6 +155,11 @@ func NewDefaultDNSRule(router adapter.Router, logger log.ContextLogger, options rule.items = append(rule.items, 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 { item := NewPackageNameItem(options.PackageName) rule.items = append(rule.items, item) diff --git a/route/rule_process.go b/route/rule_process.go index d09d3eb4b3..223a16ef1a 100644 --- a/route/rule_process.go +++ b/route/rule_process.go @@ -11,7 +11,7 @@ import ( var warnProcessNameOnNonSupportedPlatform = warning.New( 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) diff --git a/route/rule_process_path.go b/route/rule_process_path.go new file mode 100644 index 0000000000..fc9e9ea341 --- /dev/null +++ b/route/rule_process_path.go @@ -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[strings.ToLower(processName)] = true + } + return rule +} + +func (r *ProcessPathItem) Match(metadata *adapter.InboundContext) bool { + if metadata.ProcessInfo == nil || metadata.ProcessInfo.ProcessPath == "" { + return false + } + return r.processMap[strings.ToLower(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 +} From 18bbee82f0d9d092a8d6e757029b35a2c368f352 Mon Sep 17 00:00:00 2001 From: dyhkwong <50692134+dyhkwong@users.noreply.github.com> Date: Mon, 29 Aug 2022 23:03:15 +0800 Subject: [PATCH 2/3] Remove strings.ToLower --- route/rule_process_path.go | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/route/rule_process_path.go b/route/rule_process_path.go index fc9e9ea341..4398f61426 100644 --- a/route/rule_process_path.go +++ b/route/rule_process_path.go @@ -27,7 +27,7 @@ func NewProcessPathItem(processNameList []string) *ProcessPathItem { processMap: make(map[string]bool), } for _, processName := range processNameList { - rule.processMap[strings.ToLower(processName)] = true + rule.processMap[processName] = true } return rule } @@ -36,7 +36,7 @@ func (r *ProcessPathItem) Match(metadata *adapter.InboundContext) bool { if metadata.ProcessInfo == nil || metadata.ProcessInfo.ProcessPath == "" { return false } - return r.processMap[strings.ToLower(metadata.ProcessInfo.ProcessPath)] + return r.processMap[metadata.ProcessInfo.ProcessPath] } func (r *ProcessPathItem) String() string { From 0920ee12f8ad4ad798ede00f6ee3ff5f269c4e9b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E4=B8=96=E7=95=8C?= Date: Tue, 30 Aug 2022 10:43:42 +0800 Subject: [PATCH 3/3] Minor fixes --- route/router.go | 4 ++-- route/{rule_process.go => rule_process_name.go} | 2 +- 2 files changed, 3 insertions(+), 3 deletions(-) rename route/{rule_process.go => rule_process_name.go} (93%) diff --git a/route/router.go b/route/router.go index fe81dc243a..a95ed1f19c 100644 --- a/route/router.go +++ b/route/router.go @@ -789,11 +789,11 @@ func isGeositeDNSRule(rule option.DefaultDNSRule) bool { } func isProcessRule(rule option.DefaultRule) bool { - return len(rule.ProcessName) > 0 || len(rule.PackageName) > 0 || len(rule.User) > 0 || len(rule.UserID) > 0 || len(rule.ProcessPath) > 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 { - return len(rule.ProcessName) > 0 || len(rule.PackageName) > 0 || len(rule.User) > 0 || len(rule.UserID) > 0 || len(rule.ProcessPath) > 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 { diff --git a/route/rule_process.go b/route/rule_process_name.go similarity index 93% rename from route/rule_process.go rename to route/rule_process_name.go index 223a16ef1a..b0a151a170 100644 --- a/route/rule_process.go +++ b/route/rule_process_name.go @@ -37,7 +37,7 @@ func (r *ProcessItem) Match(metadata *adapter.InboundContext) bool { if metadata.ProcessInfo == nil || metadata.ProcessInfo.ProcessPath == "" { 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 {