rule_item_process_path.go 1.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354
  1. package rule
  2. import (
  3. "strings"
  4. "github.com/sagernet/sing-box/adapter"
  5. C "github.com/sagernet/sing-box/constant"
  6. )
  7. var _ RuleItem = (*ProcessPathItem)(nil)
  8. type ProcessPathItem struct {
  9. processes []string
  10. processMap map[string]bool
  11. }
  12. func NewProcessPathItem(processNameList []string) *ProcessPathItem {
  13. rule := &ProcessPathItem{
  14. processes: processNameList,
  15. processMap: make(map[string]bool),
  16. }
  17. for _, processName := range processNameList {
  18. rule.processMap[processName] = true
  19. }
  20. return rule
  21. }
  22. func (r *ProcessPathItem) Match(metadata *adapter.InboundContext) bool {
  23. if metadata.ProcessInfo == nil {
  24. return false
  25. }
  26. if metadata.ProcessInfo.ProcessPath != "" && r.processMap[metadata.ProcessInfo.ProcessPath] {
  27. return true
  28. }
  29. if C.IsAndroid {
  30. for _, packageName := range metadata.ProcessInfo.AndroidPackageNames {
  31. if r.processMap[packageName] {
  32. return true
  33. }
  34. }
  35. }
  36. return false
  37. }
  38. func (r *ProcessPathItem) String() string {
  39. var description string
  40. pLen := len(r.processes)
  41. if pLen == 1 {
  42. description = "process_path=" + r.processes[0]
  43. } else {
  44. description = "process_path=[" + strings.Join(r.processes, " ") + "]"
  45. }
  46. return description
  47. }