rule_item_process_path.go 1.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051
  1. package route
  2. import (
  3. "strings"
  4. "github.com/sagernet/sing-box/adapter"
  5. "github.com/sagernet/sing-box/common/warning"
  6. C "github.com/sagernet/sing-box/constant"
  7. )
  8. var warnProcessPathOnNonSupportedPlatform = warning.New(
  9. func() bool { return !(C.IsLinux || C.IsWindows || C.IsDarwin) },
  10. "rule item `process_path` is only supported on Linux, Windows and macOS",
  11. )
  12. var _ RuleItem = (*ProcessPathItem)(nil)
  13. type ProcessPathItem struct {
  14. processes []string
  15. processMap map[string]bool
  16. }
  17. func NewProcessPathItem(processNameList []string) *ProcessPathItem {
  18. warnProcessPathOnNonSupportedPlatform.Check()
  19. rule := &ProcessPathItem{
  20. processes: processNameList,
  21. processMap: make(map[string]bool),
  22. }
  23. for _, processName := range processNameList {
  24. rule.processMap[processName] = true
  25. }
  26. return rule
  27. }
  28. func (r *ProcessPathItem) Match(metadata *adapter.InboundContext) bool {
  29. if metadata.ProcessInfo == nil || metadata.ProcessInfo.ProcessPath == "" {
  30. return false
  31. }
  32. return r.processMap[metadata.ProcessInfo.ProcessPath]
  33. }
  34. func (r *ProcessPathItem) String() string {
  35. var description string
  36. pLen := len(r.processes)
  37. if pLen == 1 {
  38. description = "process_path=" + r.processes[0]
  39. } else {
  40. description = "process_path=[" + strings.Join(r.processes, " ") + "]"
  41. }
  42. return description
  43. }