rule_item_process_path.go 964 B

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