chore: Cleanup unused GeoSite matchers

This commit is contained in:
H1JK 2023-12-23 00:05:07 +08:00
parent 59ab4fe745
commit b632575e39
7 changed files with 17 additions and 208 deletions

View File

@ -42,7 +42,7 @@ type succinctDomainMatcher struct {
not bool not bool
} }
func (m succinctDomainMatcher) ApplyDomain(domain string) bool { func (m *succinctDomainMatcher) ApplyDomain(domain string) bool {
isMatched := m.set.Has(domain) isMatched := m.set.Has(domain)
if !isMatched { if !isMatched {
for _, matcher := range m.otherMatchers { for _, matcher := range m.otherMatchers {
@ -113,22 +113,6 @@ func NewMphMatcherGroup(domains []*Domain, not bool) (DomainMatcher, error) {
}, nil }, 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 { func (m *v2rayDomainMatcher) ApplyDomain(domain string) bool {
isMatched := len(m.matchers.Match(strings.ToLower(domain))) > 0 isMatched := len(m.matchers.Match(strings.ToLower(domain))) > 0
if m.not { if m.not {

View File

@ -39,7 +39,7 @@ func newNode() [validCharCount]Edge {
return s return s
} }
var char2Index = []int{ var char2Index = [...]int{
'A': 0, 'A': 0,
'a': 0, 'a': 0,
'B': 1, 'B': 1,

View File

@ -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
}
}

View File

@ -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]
}

View File

@ -236,25 +236,25 @@ tail:
h ^= uint64(*(*byte)(p)) h ^= uint64(*(*byte)(p))
h ^= uint64(*(*byte)(unsafe.Add(p, s>>1))) << 8 h ^= uint64(*(*byte)(unsafe.Add(p, s>>1))) << 8
h ^= uint64(*(*byte)(unsafe.Add(p, s-1))) << 16 h ^= uint64(*(*byte)(unsafe.Add(p, s-1))) << 16
h = rotl31(h*m1) * m2 h = bits.RotateLeft64(h*m1, 31) * m2
case s <= 8: case s <= 8:
h ^= uint64(readUnaligned32(p)) h ^= uint64(readUnaligned32(p))
h ^= uint64(readUnaligned32(unsafe.Add(p, s-4))) << 32 h ^= uint64(readUnaligned32(unsafe.Add(p, s-4))) << 32
h = rotl31(h*m1) * m2 h = bits.RotateLeft64(h*m1, 31) * m2
case s <= 16: case s <= 16:
h ^= readUnaligned64(p) h ^= readUnaligned64(p)
h = rotl31(h*m1) * m2 h = bits.RotateLeft64(h*m1, 31) * m2
h ^= readUnaligned64(unsafe.Add(p, s-8)) h ^= readUnaligned64(unsafe.Add(p, s-8))
h = rotl31(h*m1) * m2 h = bits.RotateLeft64(h*m1, 31) * m2
case s <= 32: case s <= 32:
h ^= readUnaligned64(p) h ^= readUnaligned64(p)
h = rotl31(h*m1) * m2 h = bits.RotateLeft64(h*m1, 31) * m2
h ^= readUnaligned64(unsafe.Add(p, 8)) 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 ^= 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 ^= readUnaligned64(unsafe.Add(p, s-8))
h = rotl31(h*m1) * m2 h = bits.RotateLeft64(h*m1, 31) * m2
default: default:
v1 := h v1 := h
v2 := uint64(seed * hashkey[1]) v2 := uint64(seed * hashkey[1])
@ -262,16 +262,16 @@ tail:
v4 := uint64(seed * hashkey[3]) v4 := uint64(seed * hashkey[3])
for s >= 32 { for s >= 32 {
v1 ^= readUnaligned64(p) v1 ^= readUnaligned64(p)
v1 = rotl31(v1*m1) * m2 v1 = bits.RotateLeft64(v1*m1, 31) * m2
p = unsafe.Add(p, 8) p = unsafe.Add(p, 8)
v2 ^= readUnaligned64(p) v2 ^= readUnaligned64(p)
v2 = rotl31(v2*m2) * m3 v2 = bits.RotateLeft64(v2*m2, 31) * m3
p = unsafe.Add(p, 8) p = unsafe.Add(p, 8)
v3 ^= readUnaligned64(p) v3 ^= readUnaligned64(p)
v3 = rotl31(v3*m3) * m4 v3 = bits.RotateLeft64(v3*m3, 31) * m4
p = unsafe.Add(p, 8) p = unsafe.Add(p, 8)
v4 ^= readUnaligned64(p) v4 ^= readUnaligned64(p)
v4 = rotl31(v4*m4) * m1 v4 = bits.RotateLeft64(v4*m4, 31) * m1
p = unsafe.Add(p, 8) p = unsafe.Add(p, 8)
s -= 32 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 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 { func readUnaligned64(p unsafe.Pointer) uint64 {
q := (*[8]byte)(p) 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 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

View File

@ -58,50 +58,3 @@ type matcherEntry struct {
m Matcher m Matcher
id uint32 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
}

View File

@ -33,12 +33,11 @@ func SetLoader(newLoader string) {
func SetSiteMatcher(newMatcher string) { func SetSiteMatcher(newMatcher string) {
switch newMatcher { switch newMatcher {
case "hybrid": case "mph", "hybrid":
newMatcher = "mph" geoSiteMatcher = "mph"
default: default:
newMatcher = "succinct" geoSiteMatcher = "succinct"
} }
geoSiteMatcher = newMatcher
} }
func Verify(name string) error { func Verify(name string) error {