mirror of
https://github.com/XTLS/Xray-core.git
synced 2026-05-08 14:13:22 +00:00
Xray-core: Refactor geodata (#5814)
https://github.com/XTLS/Xray-core/issues/4422#issuecomment-3533007890 Breaking changes https://github.com/XTLS/Xray-core/pull/5569 Reverts https://github.com/XTLS/Xray-core/pull/5505 Closes https://github.com/XTLS/Xray-core/pull/643
This commit is contained in:
85
common/geodata/strmatcher/valuematcher_linear.go
Normal file
85
common/geodata/strmatcher/valuematcher_linear.go
Normal file
@@ -0,0 +1,85 @@
|
||||
package strmatcher
|
||||
|
||||
// LinearValueMatcher is an implementation of ValueMatcher.
|
||||
type LinearValueMatcher struct {
|
||||
full *FullMatcherGroup
|
||||
domain *DomainMatcherGroup
|
||||
substr *SubstrMatcherGroup
|
||||
regex *SimpleMatcherGroup
|
||||
}
|
||||
|
||||
func NewLinearValueMatcher() *LinearValueMatcher {
|
||||
return new(LinearValueMatcher)
|
||||
}
|
||||
|
||||
// Add implements ValueMatcher.Add.
|
||||
func (g *LinearValueMatcher) Add(matcher Matcher, value uint32) {
|
||||
switch matcher := matcher.(type) {
|
||||
case FullMatcher:
|
||||
if g.full == nil {
|
||||
g.full = NewFullMatcherGroup()
|
||||
}
|
||||
g.full.AddFullMatcher(matcher, value)
|
||||
case DomainMatcher:
|
||||
if g.domain == nil {
|
||||
g.domain = NewDomainMatcherGroup()
|
||||
}
|
||||
g.domain.AddDomainMatcher(matcher, value)
|
||||
case SubstrMatcher:
|
||||
if g.substr == nil {
|
||||
g.substr = new(SubstrMatcherGroup)
|
||||
}
|
||||
g.substr.AddSubstrMatcher(matcher, value)
|
||||
default:
|
||||
if g.regex == nil {
|
||||
g.regex = new(SimpleMatcherGroup)
|
||||
}
|
||||
g.regex.AddMatcher(matcher, value)
|
||||
}
|
||||
}
|
||||
|
||||
// Build implements ValueMatcher.Build.
|
||||
func (*LinearValueMatcher) Build() error {
|
||||
return nil
|
||||
}
|
||||
|
||||
// Match implements ValueMatcher.Match.
|
||||
func (g *LinearValueMatcher) Match(input string) []uint32 {
|
||||
// Allocate capacity to prevent matches escaping to heap
|
||||
result := make([][]uint32, 0, 5)
|
||||
if g.full != nil {
|
||||
if matches := g.full.Match(input); len(matches) > 0 {
|
||||
result = append(result, matches)
|
||||
}
|
||||
}
|
||||
if g.domain != nil {
|
||||
if matches := g.domain.Match(input); len(matches) > 0 {
|
||||
result = append(result, matches)
|
||||
}
|
||||
}
|
||||
if g.substr != nil {
|
||||
if matches := g.substr.Match(input); len(matches) > 0 {
|
||||
result = append(result, matches)
|
||||
}
|
||||
}
|
||||
if g.regex != nil {
|
||||
if matches := g.regex.Match(input); len(matches) > 0 {
|
||||
result = append(result, matches)
|
||||
}
|
||||
}
|
||||
return CompositeMatches(result)
|
||||
}
|
||||
|
||||
// MatchAny implements ValueMatcher.MatchAny.
|
||||
func (g *LinearValueMatcher) MatchAny(input string) bool {
|
||||
if g.full != nil && g.full.MatchAny(input) {
|
||||
return true
|
||||
}
|
||||
if g.domain != nil && g.domain.MatchAny(input) {
|
||||
return true
|
||||
}
|
||||
if g.substr != nil && g.substr.MatchAny(input) {
|
||||
return true
|
||||
}
|
||||
return g.regex != nil && g.regex.MatchAny(input)
|
||||
}
|
||||
Reference in New Issue
Block a user