From b632575e39c5da5fceca85084fa91869a7daebd4 Mon Sep 17 00:00:00 2001 From: H1JK Date: Sat, 23 Dec 2023 00:05:07 +0800 Subject: [PATCH] chore: Cleanup unused GeoSite matchers --- component/geodata/router/condition.go | 18 +--- .../strmatcher/ac_automaton_matcher.go | 2 +- .../geodata/strmatcher/domain_matcher.go | 98 ------------------- component/geodata/strmatcher/full_matcher.go | 25 ----- component/geodata/strmatcher/mph_matcher.go | 28 +++--- component/geodata/strmatcher/strmatcher.go | 47 --------- component/geodata/utils.go | 7 +- 7 files changed, 17 insertions(+), 208 deletions(-) delete mode 100644 component/geodata/strmatcher/domain_matcher.go delete mode 100644 component/geodata/strmatcher/full_matcher.go diff --git a/component/geodata/router/condition.go b/component/geodata/router/condition.go index 82684d93e..73bc88d4f 100644 --- a/component/geodata/router/condition.go +++ b/component/geodata/router/condition.go @@ -42,7 +42,7 @@ type succinctDomainMatcher struct { not bool } -func (m succinctDomainMatcher) ApplyDomain(domain string) bool { +func (m *succinctDomainMatcher) ApplyDomain(domain string) bool { isMatched := m.set.Has(domain) if !isMatched { for _, matcher := range m.otherMatchers { @@ -113,22 +113,6 @@ func NewMphMatcherGroup(domains []*Domain, not bool) (DomainMatcher, error) { }, nil } -func NewDomainMatcher(domains []*Domain, not bool) (DomainMatcher, error) { - g := new(strmatcher.MatcherGroup) - for _, d := range domains { - m, err := domainToMatcher(d) - if err != nil { - return nil, err - } - g.Add(m) - } - - return &v2rayDomainMatcher{ - matchers: g, - not: not, - }, nil -} - func (m *v2rayDomainMatcher) ApplyDomain(domain string) bool { isMatched := len(m.matchers.Match(strings.ToLower(domain))) > 0 if m.not { diff --git a/component/geodata/strmatcher/ac_automaton_matcher.go b/component/geodata/strmatcher/ac_automaton_matcher.go index a21a83a22..fd02d5117 100644 --- a/component/geodata/strmatcher/ac_automaton_matcher.go +++ b/component/geodata/strmatcher/ac_automaton_matcher.go @@ -39,7 +39,7 @@ func newNode() [validCharCount]Edge { return s } -var char2Index = []int{ +var char2Index = [...]int{ 'A': 0, 'a': 0, 'B': 1, diff --git a/component/geodata/strmatcher/domain_matcher.go b/component/geodata/strmatcher/domain_matcher.go deleted file mode 100644 index ae8e65bc2..000000000 --- a/component/geodata/strmatcher/domain_matcher.go +++ /dev/null @@ -1,98 +0,0 @@ -package strmatcher - -import "strings" - -func breakDomain(domain string) []string { - return strings.Split(domain, ".") -} - -type node struct { - values []uint32 - sub map[string]*node -} - -// DomainMatcherGroup is a IndexMatcher for a large set of Domain matchers. -// Visible for testing only. -type DomainMatcherGroup struct { - root *node -} - -func (g *DomainMatcherGroup) Add(domain string, value uint32) { - if g.root == nil { - g.root = new(node) - } - - current := g.root - parts := breakDomain(domain) - for i := len(parts) - 1; i >= 0; i-- { - part := parts[i] - if current.sub == nil { - current.sub = make(map[string]*node) - } - next := current.sub[part] - if next == nil { - next = new(node) - current.sub[part] = next - } - current = next - } - - current.values = append(current.values, value) -} - -func (g *DomainMatcherGroup) addMatcher(m domainMatcher, value uint32) { - g.Add(string(m), value) -} - -func (g *DomainMatcherGroup) Match(domain string) []uint32 { - if domain == "" { - return nil - } - - current := g.root - if current == nil { - return nil - } - - nextPart := func(idx int) int { - for i := idx - 1; i >= 0; i-- { - if domain[i] == '.' { - return i - } - } - return -1 - } - - matches := [][]uint32{} - idx := len(domain) - for { - if idx == -1 || current.sub == nil { - break - } - - nidx := nextPart(idx) - part := domain[nidx+1 : idx] - next := current.sub[part] - if next == nil { - break - } - current = next - idx = nidx - if len(current.values) > 0 { - matches = append(matches, current.values) - } - } - switch len(matches) { - case 0: - return nil - case 1: - return matches[0] - default: - result := []uint32{} - for idx := range matches { - // Insert reversely, the subdomain that matches further ranks higher - result = append(result, matches[len(matches)-1-idx]...) - } - return result - } -} diff --git a/component/geodata/strmatcher/full_matcher.go b/component/geodata/strmatcher/full_matcher.go deleted file mode 100644 index e00d02aa9..000000000 --- a/component/geodata/strmatcher/full_matcher.go +++ /dev/null @@ -1,25 +0,0 @@ -package strmatcher - -type FullMatcherGroup struct { - matchers map[string][]uint32 -} - -func (g *FullMatcherGroup) Add(domain string, value uint32) { - if g.matchers == nil { - g.matchers = make(map[string][]uint32) - } - - g.matchers[domain] = append(g.matchers[domain], value) -} - -func (g *FullMatcherGroup) addMatcher(m fullMatcher, value uint32) { - g.Add(string(m), value) -} - -func (g *FullMatcherGroup) Match(str string) []uint32 { - if g.matchers == nil { - return nil - } - - return g.matchers[str] -} diff --git a/component/geodata/strmatcher/mph_matcher.go b/component/geodata/strmatcher/mph_matcher.go index 8d8b05089..7c1b4062c 100644 --- a/component/geodata/strmatcher/mph_matcher.go +++ b/component/geodata/strmatcher/mph_matcher.go @@ -236,25 +236,25 @@ tail: h ^= uint64(*(*byte)(p)) h ^= uint64(*(*byte)(unsafe.Add(p, s>>1))) << 8 h ^= uint64(*(*byte)(unsafe.Add(p, s-1))) << 16 - h = rotl31(h*m1) * m2 + h = bits.RotateLeft64(h*m1, 31) * m2 case s <= 8: h ^= uint64(readUnaligned32(p)) h ^= uint64(readUnaligned32(unsafe.Add(p, s-4))) << 32 - h = rotl31(h*m1) * m2 + h = bits.RotateLeft64(h*m1, 31) * m2 case s <= 16: h ^= readUnaligned64(p) - h = rotl31(h*m1) * m2 + h = bits.RotateLeft64(h*m1, 31) * m2 h ^= readUnaligned64(unsafe.Add(p, s-8)) - h = rotl31(h*m1) * m2 + h = bits.RotateLeft64(h*m1, 31) * m2 case s <= 32: h ^= readUnaligned64(p) - h = rotl31(h*m1) * m2 + h = bits.RotateLeft64(h*m1, 31) * m2 h ^= readUnaligned64(unsafe.Add(p, 8)) - h = rotl31(h*m1) * m2 + h = bits.RotateLeft64(h*m1, 31) * m2 h ^= readUnaligned64(unsafe.Add(p, s-16)) - h = rotl31(h*m1) * m2 + h = bits.RotateLeft64(h*m1, 31) * m2 h ^= readUnaligned64(unsafe.Add(p, s-8)) - h = rotl31(h*m1) * m2 + h = bits.RotateLeft64(h*m1, 31) * m2 default: v1 := h v2 := uint64(seed * hashkey[1]) @@ -262,16 +262,16 @@ tail: v4 := uint64(seed * hashkey[3]) for s >= 32 { v1 ^= readUnaligned64(p) - v1 = rotl31(v1*m1) * m2 + v1 = bits.RotateLeft64(v1*m1, 31) * m2 p = unsafe.Add(p, 8) v2 ^= readUnaligned64(p) - v2 = rotl31(v2*m2) * m3 + v2 = bits.RotateLeft64(v2*m2, 31) * m3 p = unsafe.Add(p, 8) v3 ^= readUnaligned64(p) - v3 = rotl31(v3*m3) * m4 + v3 = bits.RotateLeft64(v3*m3, 31) * m4 p = unsafe.Add(p, 8) v4 ^= readUnaligned64(p) - v4 = rotl31(v4*m4) * m1 + v4 = bits.RotateLeft64(v4*m4, 31) * m1 p = unsafe.Add(p, 8) s -= 32 } @@ -290,10 +290,6 @@ func readUnaligned32(p unsafe.Pointer) uint32 { return uint32(q[0]) | uint32(q[1])<<8 | uint32(q[2])<<16 | uint32(q[3])<<24 } -func rotl31(x uint64) uint64 { - return (x << 31) | (x >> (64 - 31)) -} - func readUnaligned64(p unsafe.Pointer) uint64 { q := (*[8]byte)(p) return uint64(q[0]) | uint64(q[1])<<8 | uint64(q[2])<<16 | uint64(q[3])<<24 | uint64(q[4])<<32 | uint64(q[5])<<40 | uint64(q[6])<<48 | uint64(q[7])<<56 diff --git a/component/geodata/strmatcher/strmatcher.go b/component/geodata/strmatcher/strmatcher.go index 294e6e73b..6bdb8b97d 100644 --- a/component/geodata/strmatcher/strmatcher.go +++ b/component/geodata/strmatcher/strmatcher.go @@ -58,50 +58,3 @@ type matcherEntry struct { m Matcher id uint32 } - -// MatcherGroup is an implementation of IndexMatcher. -// Empty initialization works. -type MatcherGroup struct { - count uint32 - fullMatcher FullMatcherGroup - domainMatcher DomainMatcherGroup - otherMatchers []matcherEntry -} - -// Add adds a new Matcher into the MatcherGroup, and returns its index. The index will never be 0. -func (g *MatcherGroup) Add(m Matcher) uint32 { - g.count++ - c := g.count - - switch tm := m.(type) { - case fullMatcher: - g.fullMatcher.addMatcher(tm, c) - case domainMatcher: - g.domainMatcher.addMatcher(tm, c) - default: - g.otherMatchers = append(g.otherMatchers, matcherEntry{ - m: m, - id: c, - }) - } - - return c -} - -// 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)...) - for _, e := range g.otherMatchers { - if e.m.Match(pattern) { - result = append(result, e.id) - } - } - return result -} - -// Size returns the number of matchers in the MatcherGroup. -func (g *MatcherGroup) Size() uint32 { - return g.count -} diff --git a/component/geodata/utils.go b/component/geodata/utils.go index 33c12e68c..a4002aeb3 100644 --- a/component/geodata/utils.go +++ b/component/geodata/utils.go @@ -33,12 +33,11 @@ func SetLoader(newLoader string) { func SetSiteMatcher(newMatcher string) { switch newMatcher { - case "hybrid": - newMatcher = "mph" + case "mph", "hybrid": + geoSiteMatcher = "mph" default: - newMatcher = "succinct" + geoSiteMatcher = "succinct" } - geoSiteMatcher = newMatcher } func Verify(name string) error {