1
0
mirror of https://github.com/v2fly/v2ray-core.git synced 2024-06-28 10:15:23 +00:00
v2fly/common/strmatcher/strmatcher.go

108 lines
2.6 KiB
Go
Raw Normal View History

2018-06-26 19:57:41 +00:00
package strmatcher
2018-08-19 19:04:15 +00:00
import (
"regexp"
)
2018-06-26 19:57:41 +00:00
2018-08-20 13:39:58 +00:00
// Matcher is the interface to determine a string matches a pattern.
2018-06-26 19:57:41 +00:00
type Matcher interface {
2018-08-20 13:39:58 +00:00
// Match returns true if the given string matches a predefined pattern.
2018-06-26 19:57:41 +00:00
Match(string) bool
String() string
2018-06-26 19:57:41 +00:00
}
2018-08-20 13:39:58 +00:00
// Type is the type of the matcher.
2018-06-26 19:57:41 +00:00
type Type byte
const (
2018-08-20 13:39:58 +00:00
// Full is the type of matcher that the input string must exactly equal to the pattern.
2018-06-26 19:57:41 +00:00
Full Type = iota
2018-08-20 13:39:58 +00:00
// Substr is the type of matcher that the input string must contain the pattern as a sub-string.
2018-06-26 19:57:41 +00:00
Substr
2018-08-20 13:39:58 +00:00
// Domain is the type of matcher that the input string must be a sub-domain or itself of the pattern.
2018-06-26 19:57:41 +00:00
Domain
2018-08-20 13:39:58 +00:00
// Regex is the type of matcher that the input string must matches the regular-expression pattern.
2018-06-26 19:57:41 +00:00
Regex
)
2018-08-20 13:39:58 +00:00
// New creates a new Matcher based on the given pattern.
2018-06-26 19:57:41 +00:00
func (t Type) New(pattern string) (Matcher, error) {
// 1. regex matching is case-sensitive
2018-06-26 19:57:41 +00:00
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-20 13:39:58 +00:00
// IndexMatcher is the interface for matching with a group of matchers.
2018-08-19 19:04:15 +00:00
type IndexMatcher interface {
2020-08-31 09:53:16 +00:00
// Match returns the index of a matcher that matches the input. It returns empty array if no such matcher exists.
Match(input string) []uint32
2018-08-19 19:04:15 +00:00
}
2018-06-26 19:57:41 +00:00
type matcherEntry struct {
m Matcher
id uint32
}
2018-08-20 13:39:58 +00:00
// MatcherGroup is an implementation of IndexMatcher.
// Empty initialization works.
2018-06-26 19:57:41 +00:00
type MatcherGroup struct {
count uint32
2018-08-20 07:57:06 +00:00
fullMatcher FullMatcherGroup
2018-08-19 19:04:15 +00:00
domainMatcher DomainMatcherGroup
2018-06-26 19:57:41 +00:00
otherMatchers []matcherEntry
}
2018-08-20 13:39:58 +00:00
// Add adds a new Matcher into the MatcherGroup, and returns its index. The index will never be 0.
2018-06-26 19:57:41 +00:00
func (g *MatcherGroup) Add(m Matcher) uint32 {
g.count++
2018-08-20 07:57:06 +00:00
c := g.count
2018-06-26 19:57:41 +00:00
2018-08-19 19:04:15 +00:00
switch tm := m.(type) {
case fullMatcher:
2018-08-20 07:57:06 +00:00
g.fullMatcher.addMatcher(tm, c)
2018-08-19 19:04:15 +00:00
case domainMatcher:
2018-08-20 07:57:06 +00:00
g.domainMatcher.addMatcher(tm, c)
2018-08-19 19:04:15 +00:00
default:
2018-06-26 19:57:41 +00:00
g.otherMatchers = append(g.otherMatchers, matcherEntry{
m: m,
id: c,
})
}
return c
}
2018-08-20 13:39:58 +00:00
// Match implements IndexMatcher.Match.
func (g *MatcherGroup) Match(pattern string) []uint32 {
result := []uint32{}
result = append(result, g.fullMatcher.Match(pattern)...)
result = append(result, g.domainMatcher.Match(pattern)...)
2018-06-26 19:57:41 +00:00
for _, e := range g.otherMatchers {
if e.m.Match(pattern) {
result = append(result, e.id)
2018-06-26 19:57:41 +00:00
}
}
return result
2018-06-26 19:57:41 +00:00
}
2018-08-20 13:39:58 +00:00
// Size returns the number of matchers in the MatcherGroup.
2018-06-26 19:57:41 +00:00
func (g *MatcherGroup) Size() uint32 {
return g.count
}