1
0
mirror of https://github.com/v2fly/v2ray-core.git synced 2024-09-29 15:26:29 -04:00
v2fly/common/strmatcher/strmatcher.go

161 lines
2.5 KiB
Go
Raw Normal View History

2018-06-26 15:57:41 -04:00
package strmatcher
2018-08-19 15:04:15 -04:00
import (
"regexp"
"sync"
"time"
"v2ray.com/core/common/task"
)
2018-06-26 15:57:41 -04:00
type Matcher interface {
Match(string) bool
}
type Type byte
const (
Full Type = iota
Substr
Domain
Regex
)
func (t Type) New(pattern string) (Matcher, error) {
switch t {
case Full:
return fullMatcher(pattern), nil
case Substr:
return substrMatcher(pattern), nil
case Domain:
return domainMatcher(pattern), nil
case Regex:
r, err := regexp.Compile(pattern)
if err != nil {
return nil, err
}
return &regexMatcher{
pattern: r,
}, nil
default:
panic("Unknown type")
}
}
2018-08-19 15:04:15 -04:00
type IndexMatcher interface {
Match(pattern string) uint32
}
2018-06-26 15:57:41 -04:00
type matcherEntry struct {
m Matcher
id uint32
}
type MatcherGroup struct {
count uint32
2018-08-20 03:57:06 -04:00
fullMatcher FullMatcherGroup
2018-08-19 15:04:15 -04:00
domainMatcher DomainMatcherGroup
2018-06-26 15:57:41 -04:00
otherMatchers []matcherEntry
}
func NewMatcherGroup() *MatcherGroup {
2018-08-20 03:57:06 -04:00
return &MatcherGroup{}
2018-06-26 15:57:41 -04:00
}
func (g *MatcherGroup) Add(m Matcher) uint32 {
g.count++
2018-08-20 03:57:06 -04:00
c := g.count
2018-06-26 15:57:41 -04:00
2018-08-19 15:04:15 -04:00
switch tm := m.(type) {
case fullMatcher:
2018-08-20 03:57:06 -04:00
g.fullMatcher.addMatcher(tm, c)
2018-08-19 15:04:15 -04:00
case domainMatcher:
2018-08-20 03:57:06 -04:00
g.domainMatcher.addMatcher(tm, c)
2018-08-19 15:04:15 -04:00
default:
2018-06-26 15:57:41 -04:00
g.otherMatchers = append(g.otherMatchers, matcherEntry{
m: m,
id: c,
})
}
return c
}
func (g *MatcherGroup) Match(pattern string) uint32 {
2018-08-20 03:57:06 -04:00
if c := g.fullMatcher.Match(pattern); c > 0 {
2018-06-26 15:57:41 -04:00
return c
}
2018-08-19 15:07:31 -04:00
if c := g.domainMatcher.Match(pattern); c > 0 {
return c
}
2018-06-26 15:57:41 -04:00
for _, e := range g.otherMatchers {
if e.m.Match(pattern) {
return e.id
}
}
return 0
}
func (g *MatcherGroup) Size() uint32 {
return g.count
}
2018-08-19 15:04:15 -04:00
type cacheEntry struct {
timestamp time.Time
result uint32
}
type CachedMatcherGroup struct {
2018-08-19 16:16:06 -04:00
sync.RWMutex
2018-08-19 15:04:15 -04:00
group *MatcherGroup
cache map[string]cacheEntry
cleanup *task.Periodic
}
func NewCachedMatcherGroup(g *MatcherGroup) *CachedMatcherGroup {
r := &CachedMatcherGroup{
group: g,
cache: make(map[string]cacheEntry),
}
r.cleanup = &task.Periodic{
Interval: time.Second * 30,
Execute: func() error {
r.Lock()
defer r.Unlock()
2018-08-19 16:16:06 -04:00
expire := time.Now().Add(-1 * time.Second * 120)
2018-08-19 15:04:15 -04:00
for p, e := range r.cache {
if e.timestamp.Before(expire) {
delete(r.cache, p)
}
}
return nil
},
}
return r
}
func (g *CachedMatcherGroup) Match(pattern string) uint32 {
2018-08-19 16:16:06 -04:00
g.RLock()
2018-08-19 15:04:15 -04:00
r, f := g.cache[pattern]
2018-08-19 16:16:06 -04:00
g.RUnlock()
2018-08-19 15:04:15 -04:00
if f {
return r.result
}
mr := g.group.Match(pattern)
2018-08-19 16:16:06 -04:00
g.Lock()
2018-08-19 15:04:15 -04:00
g.cache[pattern] = cacheEntry{
result: mr,
timestamp: time.Now(),
}
2018-08-19 16:16:06 -04:00
g.Unlock()
2018-08-19 15:04:15 -04:00
return mr
}