strmatcher.go 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  1. package strmatcher
  2. import (
  3. "regexp"
  4. "sync"
  5. "time"
  6. "v2ray.com/core/common/task"
  7. )
  8. // Matcher is the interface to determine a string matches a pattern.
  9. type Matcher interface {
  10. // Match returns true if the given string matches a predefined pattern.
  11. Match(string) bool
  12. }
  13. // Type is the type of the matcher.
  14. type Type byte
  15. const (
  16. // Full is the type of matcher that the input string must exactly equal to the pattern.
  17. Full Type = iota
  18. // Substr is the type of matcher that the input string must contain the pattern as a sub-string.
  19. Substr
  20. // Domain is the type of matcher that the input string must be a sub-domain or itself of the pattern.
  21. Domain
  22. // Regex is the type of matcher that the input string must matches the regular-expression pattern.
  23. Regex
  24. )
  25. // New creates a new Matcher based on the given pattern.
  26. func (t Type) New(pattern string) (Matcher, error) {
  27. switch t {
  28. case Full:
  29. return fullMatcher(pattern), nil
  30. case Substr:
  31. return substrMatcher(pattern), nil
  32. case Domain:
  33. return domainMatcher(pattern), nil
  34. case Regex:
  35. r, err := regexp.Compile(pattern)
  36. if err != nil {
  37. return nil, err
  38. }
  39. return &regexMatcher{
  40. pattern: r,
  41. }, nil
  42. default:
  43. panic("Unknown type")
  44. }
  45. }
  46. // IndexMatcher is the interface for matching with a group of matchers.
  47. type IndexMatcher interface {
  48. // Match returns the the index of a matcher that matches the input. It returns 0 if no such matcher exists.
  49. Match(input string) uint32
  50. }
  51. type matcherEntry struct {
  52. m Matcher
  53. id uint32
  54. }
  55. // MatcherGroup is an implementation of IndexMatcher.
  56. // Empty initialization works.
  57. type MatcherGroup struct {
  58. count uint32
  59. fullMatcher FullMatcherGroup
  60. domainMatcher DomainMatcherGroup
  61. otherMatchers []matcherEntry
  62. }
  63. // Add adds a new Matcher into the MatcherGroup, and returns its index. The index will never be 0.
  64. func (g *MatcherGroup) Add(m Matcher) uint32 {
  65. g.count++
  66. c := g.count
  67. switch tm := m.(type) {
  68. case fullMatcher:
  69. g.fullMatcher.addMatcher(tm, c)
  70. case domainMatcher:
  71. g.domainMatcher.addMatcher(tm, c)
  72. default:
  73. g.otherMatchers = append(g.otherMatchers, matcherEntry{
  74. m: m,
  75. id: c,
  76. })
  77. }
  78. return c
  79. }
  80. // Match implements IndexMatcher.Match.
  81. func (g *MatcherGroup) Match(pattern string) uint32 {
  82. if c := g.fullMatcher.Match(pattern); c > 0 {
  83. return c
  84. }
  85. if c := g.domainMatcher.Match(pattern); c > 0 {
  86. return c
  87. }
  88. for _, e := range g.otherMatchers {
  89. if e.m.Match(pattern) {
  90. return e.id
  91. }
  92. }
  93. return 0
  94. }
  95. // Size returns the number of matchers in the MatcherGroup.
  96. func (g *MatcherGroup) Size() uint32 {
  97. return g.count
  98. }
  99. type cacheEntry struct {
  100. timestamp time.Time
  101. result uint32
  102. }
  103. // CachedMatcherGroup is a IndexMatcher with cachable results.
  104. type CachedMatcherGroup struct {
  105. sync.RWMutex
  106. group *MatcherGroup
  107. cache map[string]cacheEntry
  108. cleanup *task.Periodic
  109. }
  110. // NewCachedMatcherGroup creats a new CachedMatcherGroup.
  111. func NewCachedMatcherGroup(g *MatcherGroup) *CachedMatcherGroup {
  112. r := &CachedMatcherGroup{
  113. group: g,
  114. cache: make(map[string]cacheEntry),
  115. }
  116. r.cleanup = &task.Periodic{
  117. Interval: time.Second * 30,
  118. Execute: func() error {
  119. r.Lock()
  120. defer r.Unlock()
  121. expire := time.Now().Add(-1 * time.Second * 120)
  122. for p, e := range r.cache {
  123. if e.timestamp.Before(expire) {
  124. delete(r.cache, p)
  125. }
  126. }
  127. return nil
  128. },
  129. }
  130. return r
  131. }
  132. // Match implements IndexMatcher.Match.
  133. func (g *CachedMatcherGroup) Match(pattern string) uint32 {
  134. g.RLock()
  135. r, f := g.cache[pattern]
  136. g.RUnlock()
  137. if f {
  138. return r.result
  139. }
  140. mr := g.group.Match(pattern)
  141. g.Lock()
  142. g.cache[pattern] = cacheEntry{
  143. result: mr,
  144. timestamp: time.Now(),
  145. }
  146. g.Unlock()
  147. return mr
  148. }