Add process_path_regex rule type
This commit is contained in:
@@ -179,6 +179,14 @@ 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.ProcessPathRegex) > 0 {
|
||||
item, err := NewProcessPathRegexItem(options.ProcessPathRegex)
|
||||
if err != nil {
|
||||
return nil, E.Cause(err, "process_path_regex")
|
||||
}
|
||||
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)
|
||||
|
||||
@@ -183,6 +183,14 @@ func NewDefaultDNSRule(router adapter.Router, logger log.ContextLogger, options
|
||||
rule.items = append(rule.items, item)
|
||||
rule.allItems = append(rule.allItems, item)
|
||||
}
|
||||
if len(options.ProcessPathRegex) > 0 {
|
||||
item, err := NewProcessPathRegexItem(options.ProcessPathRegex)
|
||||
if err != nil {
|
||||
return nil, E.Cause(err, "process_path_regex")
|
||||
}
|
||||
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)
|
||||
|
||||
@@ -123,6 +123,14 @@ func NewDefaultHeadlessRule(router adapter.Router, options option.DefaultHeadles
|
||||
rule.items = append(rule.items, item)
|
||||
rule.allItems = append(rule.allItems, item)
|
||||
}
|
||||
if len(options.ProcessPathRegex) > 0 {
|
||||
item, err := NewProcessPathRegexItem(options.ProcessPathRegex)
|
||||
if err != nil {
|
||||
return nil, E.Cause(err, "process_path_regex")
|
||||
}
|
||||
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)
|
||||
|
||||
54
route/rule_item_process_path_regex.go
Normal file
54
route/rule_item_process_path_regex.go
Normal file
@@ -0,0 +1,54 @@
|
||||
package route
|
||||
|
||||
import (
|
||||
"regexp"
|
||||
"strings"
|
||||
|
||||
"github.com/sagernet/sing-box/adapter"
|
||||
E "github.com/sagernet/sing/common/exceptions"
|
||||
F "github.com/sagernet/sing/common/format"
|
||||
)
|
||||
|
||||
var _ RuleItem = (*ProcessPathRegexItem)(nil)
|
||||
|
||||
type ProcessPathRegexItem struct {
|
||||
matchers []*regexp.Regexp
|
||||
description string
|
||||
}
|
||||
|
||||
func NewProcessPathRegexItem(expressions []string) (*ProcessPathRegexItem, error) {
|
||||
matchers := make([]*regexp.Regexp, 0, len(expressions))
|
||||
for i, regex := range expressions {
|
||||
matcher, err := regexp.Compile(regex)
|
||||
if err != nil {
|
||||
return nil, E.Cause(err, "parse expression ", i)
|
||||
}
|
||||
matchers = append(matchers, matcher)
|
||||
}
|
||||
description := "process_path_regex="
|
||||
eLen := len(expressions)
|
||||
if eLen == 1 {
|
||||
description += expressions[0]
|
||||
} else if eLen > 3 {
|
||||
description += F.ToString("[", strings.Join(expressions[:3], " "), "]")
|
||||
} else {
|
||||
description += F.ToString("[", strings.Join(expressions, " "), "]")
|
||||
}
|
||||
return &ProcessPathRegexItem{matchers, description}, nil
|
||||
}
|
||||
|
||||
func (r *ProcessPathRegexItem) Match(metadata *adapter.InboundContext) bool {
|
||||
if metadata.ProcessInfo == nil || metadata.ProcessInfo.ProcessPath == "" {
|
||||
return false
|
||||
}
|
||||
for _, matcher := range r.matchers {
|
||||
if matcher.MatchString(metadata.ProcessInfo.ProcessPath) {
|
||||
return true
|
||||
}
|
||||
}
|
||||
return false
|
||||
}
|
||||
|
||||
func (r *ProcessPathRegexItem) String() string {
|
||||
return r.description
|
||||
}
|
||||
Reference in New Issue
Block a user