mihomo/component/trie/domain.go

154 lines
3.1 KiB
Go
Raw Normal View History

2019-07-14 19:29:58 +08:00
package trie
import (
"errors"
"strings"
)
const (
wildcard = "*"
dotWildcard = ""
complexWildcard = "+"
domainStep = "."
2019-07-14 19:29:58 +08:00
)
2021-10-10 23:44:09 +08:00
// ErrInvalidDomain means insert domain is invalid
var ErrInvalidDomain = errors.New("invalid domain")
2019-07-14 19:29:58 +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)
type DomainTrie[T any] struct {
2022-04-06 04:25:53 +08:00
root *Node[T]
2019-07-14 19:29:58 +08:00
}
func ValidAndSplitDomain(domain string) ([]string, bool) {
if domain != "" && domain[len(domain)-1] == '.' {
return nil, false
}
domain=strings.ToLower(domain)
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
}
for _, part := range parts[1:] {
if part == "" {
return nil, false
}
}
return parts, true
}
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
// 4. .example.com
// 5. +.example.com
2022-04-06 04:25:53 +08:00
func (t *DomainTrie[T]) Insert(domain string, data T) error {
parts, valid := ValidAndSplitDomain(domain)
if !valid {
2019-07-14 19:29:58 +08:00
return ErrInvalidDomain
}
if parts[0] == complexWildcard {
t.insert(parts[1:], data)
parts[0] = dotWildcard
t.insert(parts, data)
} else {
t.insert(parts, data)
}
return nil
}
2022-04-06 04:25:53 +08:00
func (t *DomainTrie[T]) insert(parts []string, data T) {
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]
node = node.getOrNewChild(part)
2019-07-14 19:29:58 +08:00
}
node.setData(data)
2019-07-14 19:29:58 +08:00
}
// Search is the most important part of the Trie.
// Priority as:
// 1. static part
// 2. wildcard domain
// 2. dot wildcard domain
2022-04-06 04:25:53 +08:00
func (t *DomainTrie[T]) Search(domain string) *Node[T] {
parts, valid := ValidAndSplitDomain(domain)
if !valid || parts[0] == "" {
2019-07-14 19:29:58 +08:00
return nil
}
n := t.search(t.root, parts)
2019-07-14 19:29:58 +08:00
if n.isEmpty() {
return nil
}
2019-07-14 19:29:58 +08:00
return n
}
2020-04-24 23:49:19 +08:00
2022-04-06 04:25:53 +08:00
func (t *DomainTrie[T]) search(node *Node[T], parts []string) *Node[T] {
if len(parts) == 0 {
return node
}
if c := node.getChild(parts[len(parts)-1]); c != nil {
if n := t.search(c, parts[:len(parts)-1]); !n.isEmpty() {
return n
2019-07-14 19:29:58 +08:00
}
}
2019-07-14 19:29:58 +08:00
if c := node.getChild(wildcard); c != nil {
if n := t.search(c, parts[:len(parts)-1]); !n.isEmpty() {
return n
}
2019-07-14 19:29:58 +08:00
}
2021-03-24 01:00:21 +08:00
return node.getChild(dotWildcard)
2019-07-14 19:29:58 +08:00
}
func (t *DomainTrie[T]) Optimize() {
t.root.optimize()
}
func (t *DomainTrie[T]) Foreach(print func(domain string, data T)) {
for key, data := range t.root.getChildren() {
recursion([]string{key}, data, print)
}
}
func recursion[T any](items []string, node *Node[T], fn func(domain string, data T)) {
for key, data := range node.getChildren() {
newItems := append([]string{key}, items...)
if data != nil && data.inited {
domain := joinDomain(newItems)
if domain[0] == domainStepByte {
domain = complexWildcard + domain
}
fn(domain, data.Data())
}
recursion(newItems, data, fn)
}
}
func joinDomain(items []string) string {
return strings.Join(items, domainStep)
}
2019-07-14 19:29:58 +08:00
// New returns a new, empty Trie.
func New[T any]() *DomainTrie[T] {
return &DomainTrie[T]{root: newNode[T]()}
2019-07-14 19:29:58 +08:00
}