process.go 1.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  1. package common
  2. import (
  3. "strings"
  4. C "github.com/metacubex/mihomo/constant"
  5. "github.com/dlclark/regexp2"
  6. )
  7. type Process struct {
  8. *Base
  9. adapter string
  10. process string
  11. nameOnly bool
  12. regexp *regexp2.Regexp
  13. }
  14. func (ps *Process) RuleType() C.RuleType {
  15. if ps.nameOnly {
  16. if ps.regexp != nil {
  17. return C.ProcessNameRegex
  18. }
  19. return C.ProcessName
  20. }
  21. if ps.regexp != nil {
  22. return C.ProcessPathRegex
  23. }
  24. return C.ProcessPath
  25. }
  26. func (ps *Process) Match(metadata *C.Metadata) (bool, string) {
  27. if ps.nameOnly {
  28. if ps.regexp != nil {
  29. match, _ := ps.regexp.MatchString(metadata.Process)
  30. return match, ps.adapter
  31. }
  32. return strings.EqualFold(metadata.Process, ps.process), ps.adapter
  33. }
  34. if ps.regexp != nil {
  35. match, _ := ps.regexp.MatchString(metadata.ProcessPath)
  36. return match, ps.adapter
  37. }
  38. return strings.EqualFold(metadata.ProcessPath, ps.process), ps.adapter
  39. }
  40. func (ps *Process) Adapter() string {
  41. return ps.adapter
  42. }
  43. func (ps *Process) Payload() string {
  44. return ps.process
  45. }
  46. func (ps *Process) ShouldFindProcess() bool {
  47. return true
  48. }
  49. func NewProcess(process string, adapter string, nameOnly bool, regex bool) (*Process, error) {
  50. var r *regexp2.Regexp
  51. var err error
  52. if regex {
  53. r, err = regexp2.Compile(process, regexp2.IgnoreCase)
  54. if err != nil {
  55. return nil, err
  56. }
  57. }
  58. return &Process{
  59. Base: &Base{},
  60. adapter: adapter,
  61. process: process,
  62. nameOnly: nameOnly,
  63. regexp: r,
  64. }, nil
  65. }