2018-08-19 15:04:15 -04:00
|
|
|
package strmatcher
|
|
|
|
|
|
|
|
import "strings"
|
|
|
|
|
|
|
|
func breakDomain(domain string) []string {
|
|
|
|
return strings.Split(domain, ".")
|
|
|
|
}
|
|
|
|
|
|
|
|
type node struct {
|
2020-08-11 01:31:04 -04:00
|
|
|
values []uint32
|
|
|
|
sub map[string]*node
|
2018-08-19 15:04:15 -04:00
|
|
|
}
|
|
|
|
|
2021-10-31 06:01:13 -04:00
|
|
|
// DomainMatcherGroup is an implementation of MatcherGroup.
|
|
|
|
// It uses trie to optimize both memory consumption and lookup speed. Trie node is domain label based.
|
2018-08-19 15:04:15 -04:00
|
|
|
type DomainMatcherGroup struct {
|
|
|
|
root *node
|
|
|
|
}
|
|
|
|
|
2021-10-31 06:01:13 -04:00
|
|
|
// AddDomainMatcher implements MatcherGroupForDomain.AddDomainMatcher.
|
|
|
|
func (g *DomainMatcherGroup) AddDomainMatcher(matcher DomainMatcher, value uint32) {
|
2018-08-19 15:04:15 -04:00
|
|
|
if g.root == nil {
|
2018-08-22 16:49:02 -04:00
|
|
|
g.root = new(node)
|
2018-08-19 15:04:15 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
current := g.root
|
2021-10-31 06:01:13 -04:00
|
|
|
parts := breakDomain(matcher.Pattern())
|
2018-08-19 15:04:15 -04:00
|
|
|
for i := len(parts) - 1; i >= 0; i-- {
|
|
|
|
part := parts[i]
|
2018-08-22 16:49:02 -04:00
|
|
|
if current.sub == nil {
|
|
|
|
current.sub = make(map[string]*node)
|
|
|
|
}
|
2018-08-19 15:04:15 -04:00
|
|
|
next := current.sub[part]
|
|
|
|
if next == nil {
|
2018-08-22 16:49:02 -04:00
|
|
|
next = new(node)
|
2018-08-19 15:04:15 -04:00
|
|
|
current.sub[part] = next
|
|
|
|
}
|
|
|
|
current = next
|
|
|
|
}
|
|
|
|
|
2020-08-11 01:31:04 -04:00
|
|
|
current.values = append(current.values, value)
|
2018-08-19 15:04:15 -04:00
|
|
|
}
|
|
|
|
|
2021-10-31 06:01:13 -04:00
|
|
|
// Match implements MatcherGroup.Match.
|
2020-08-11 01:31:04 -04:00
|
|
|
func (g *DomainMatcherGroup) Match(domain string) []uint32 {
|
2019-06-14 09:47:28 -04:00
|
|
|
if domain == "" {
|
2020-08-11 01:31:04 -04:00
|
|
|
return nil
|
2018-11-02 07:14:41 -04:00
|
|
|
}
|
|
|
|
|
2018-08-19 15:04:15 -04:00
|
|
|
current := g.root
|
2018-08-20 03:47:18 -04:00
|
|
|
if current == nil {
|
2020-08-11 01:31:04 -04:00
|
|
|
return nil
|
2018-08-20 03:47:18 -04:00
|
|
|
}
|
|
|
|
|
2018-11-02 07:14:41 -04:00
|
|
|
nextPart := func(idx int) int {
|
|
|
|
for i := idx - 1; i >= 0; i-- {
|
|
|
|
if domain[i] == '.' {
|
|
|
|
return i
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return -1
|
|
|
|
}
|
|
|
|
|
2020-08-13 03:26:30 -04:00
|
|
|
matches := [][]uint32{}
|
2018-11-02 07:14:41 -04:00
|
|
|
idx := len(domain)
|
|
|
|
for {
|
|
|
|
if idx == -1 || current.sub == nil {
|
2018-08-22 16:49:02 -04:00
|
|
|
break
|
|
|
|
}
|
2018-11-02 07:14:41 -04:00
|
|
|
|
|
|
|
nidx := nextPart(idx)
|
|
|
|
part := domain[nidx+1 : idx]
|
2018-08-19 15:04:15 -04:00
|
|
|
next := current.sub[part]
|
|
|
|
if next == nil {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
current = next
|
2018-11-02 07:14:41 -04:00
|
|
|
idx = nidx
|
2020-08-13 03:26:30 -04:00
|
|
|
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
|
2018-08-19 15:04:15 -04:00
|
|
|
}
|
|
|
|
}
|
2021-10-31 06:01:13 -04:00
|
|
|
|
|
|
|
// MatchAny implements MatcherGroup.MatchAny.
|
|
|
|
func (g *DomainMatcherGroup) MatchAny(domain string) bool {
|
|
|
|
return len(g.Match(domain)) > 0
|
|
|
|
}
|