2019-07-14 19:29:58 +08:00
|
|
|
package trie
|
|
|
|
|
|
|
|
import (
|
|
|
|
"errors"
|
|
|
|
"strings"
|
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
2020-05-28 12:13:05 +08:00
|
|
|
wildcard = "*"
|
|
|
|
dotWildcard = ""
|
|
|
|
complexWildcard = "+"
|
|
|
|
domainStep = "."
|
2019-07-14 19:29:58 +08:00
|
|
|
)
|
|
|
|
|
|
|
|
var (
|
|
|
|
// ErrInvalidDomain means insert domain is invalid
|
|
|
|
ErrInvalidDomain = errors.New("invalid domain")
|
|
|
|
)
|
|
|
|
|
2020-05-28 12:13:05 +08:00
|
|
|
// DomainTrie contains the main logic for adding and searching nodes for domain segments.
|
2019-07-14 19:29:58 +08:00
|
|
|
// support wildcard domain (e.g *.google.com)
|
2020-05-28 12:13:05 +08:00
|
|
|
type DomainTrie struct {
|
2019-07-14 19:29:58 +08:00
|
|
|
root *Node
|
|
|
|
}
|
|
|
|
|
2020-04-08 15:45:59 +08:00
|
|
|
func validAndSplitDomain(domain string) ([]string, bool) {
|
|
|
|
if domain != "" && domain[len(domain)-1] == '.' {
|
|
|
|
return nil, false
|
|
|
|
}
|
|
|
|
|
|
|
|
parts := strings.Split(domain, domainStep)
|
|
|
|
if len(parts) == 1 {
|
2020-06-07 17:25:51 +08:00
|
|
|
if parts[0] == "" {
|
|
|
|
return nil, false
|
|
|
|
}
|
|
|
|
|
|
|
|
return parts, true
|
2020-04-08 15:45:59 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
for _, part := range parts[1:] {
|
|
|
|
if part == "" {
|
|
|
|
return nil, false
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return parts, true
|
2019-07-16 00:57:08 +08:00
|
|
|
}
|
|
|
|
|
2019-07-14 19:29:58 +08:00
|
|
|
// Insert adds a node to the trie.
|
|
|
|
// Support
|
|
|
|
// 1. www.example.com
|
|
|
|
// 2. *.example.com
|
|
|
|
// 3. subdomain.*.example.com
|
2020-04-08 15:45:59 +08:00
|
|
|
// 4. .example.com
|
2020-05-28 12:13:05 +08:00
|
|
|
// 5. +.example.com
|
|
|
|
func (t *DomainTrie) Insert(domain string, data interface{}) error {
|
2020-04-08 15:45:59 +08:00
|
|
|
parts, valid := validAndSplitDomain(domain)
|
|
|
|
if !valid {
|
2019-07-14 19:29:58 +08:00
|
|
|
return ErrInvalidDomain
|
|
|
|
}
|
|
|
|
|
2020-05-28 12:13:05 +08:00
|
|
|
if parts[0] == complexWildcard {
|
|
|
|
t.insert(parts[1:], data)
|
|
|
|
parts[0] = dotWildcard
|
|
|
|
t.insert(parts, data)
|
|
|
|
} else {
|
|
|
|
t.insert(parts, data)
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (t *DomainTrie) insert(parts []string, data interface{}) {
|
2019-07-14 19:29:58 +08:00
|
|
|
node := t.root
|
|
|
|
// reverse storage domain part to save space
|
|
|
|
for i := len(parts) - 1; i >= 0; i-- {
|
|
|
|
part := parts[i]
|
|
|
|
if !node.hasChild(part) {
|
|
|
|
node.addChild(part, newNode(nil))
|
|
|
|
}
|
|
|
|
|
|
|
|
node = node.getChild(part)
|
|
|
|
}
|
|
|
|
|
|
|
|
node.Data = data
|
|
|
|
}
|
|
|
|
|
|
|
|
// Search is the most important part of the Trie.
|
|
|
|
// Priority as:
|
|
|
|
// 1. static part
|
|
|
|
// 2. wildcard domain
|
2020-04-08 15:45:59 +08:00
|
|
|
// 2. dot wildcard domain
|
2020-05-28 12:13:05 +08:00
|
|
|
func (t *DomainTrie) Search(domain string) *Node {
|
2020-04-08 15:45:59 +08:00
|
|
|
parts, valid := validAndSplitDomain(domain)
|
|
|
|
if !valid || parts[0] == "" {
|
2019-07-14 19:29:58 +08:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2020-06-24 18:41:23 +08:00
|
|
|
n := t.search(t.root, parts)
|
2019-07-14 19:29:58 +08:00
|
|
|
|
2020-06-24 18:41:23 +08:00
|
|
|
if n.Data == nil {
|
|
|
|
return nil
|
|
|
|
}
|
2019-07-14 19:29:58 +08:00
|
|
|
|
2020-06-24 18:41:23 +08:00
|
|
|
return n
|
|
|
|
}
|
2020-04-24 23:49:19 +08:00
|
|
|
|
2020-06-24 18:41:23 +08:00
|
|
|
func (t *DomainTrie) search(node *Node, parts []string) *Node {
|
|
|
|
if len(parts) == 0 {
|
|
|
|
return node
|
|
|
|
}
|
2020-04-08 15:45:59 +08:00
|
|
|
|
2020-06-24 18:41:23 +08:00
|
|
|
if c := node.getChild(parts[len(parts)-1]); c != nil {
|
|
|
|
if n := t.search(c, parts[:len(parts)-1]); n != nil {
|
|
|
|
return n
|
2019-07-14 19:29:58 +08:00
|
|
|
}
|
2020-04-08 15:45:59 +08:00
|
|
|
}
|
2019-07-14 19:29:58 +08:00
|
|
|
|
2020-06-24 18:41:23 +08:00
|
|
|
if c := node.getChild(wildcard); c != nil {
|
|
|
|
if n := t.search(c, parts[:len(parts)-1]); n != nil {
|
|
|
|
return n
|
2020-04-08 15:45:59 +08:00
|
|
|
}
|
2019-07-14 19:29:58 +08:00
|
|
|
}
|
|
|
|
|
2020-06-24 18:41:23 +08:00
|
|
|
if c := node.getChild(dotWildcard); c != nil {
|
|
|
|
return c
|
2019-07-15 18:00:51 +08:00
|
|
|
}
|
|
|
|
|
2020-06-24 18:41:23 +08:00
|
|
|
return nil
|
2019-07-14 19:29:58 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// New returns a new, empty Trie.
|
2020-05-28 12:13:05 +08:00
|
|
|
func New() *DomainTrie {
|
|
|
|
return &DomainTrie{root: newNode(nil)}
|
2019-07-14 19:29:58 +08:00
|
|
|
}
|