Feature: domain trie support wildcard alias
This commit is contained in:
@ -6,7 +6,7 @@ import (
|
||||
"sync"
|
||||
|
||||
"github.com/Dreamacro/clash/common/cache"
|
||||
trie "github.com/Dreamacro/clash/component/domain-trie"
|
||||
"github.com/Dreamacro/clash/component/trie"
|
||||
)
|
||||
|
||||
// Pool is a implementation about fake ip generator without storage
|
||||
@ -16,7 +16,7 @@ type Pool struct {
|
||||
gateway uint32
|
||||
offset uint32
|
||||
mux sync.Mutex
|
||||
host *trie.Trie
|
||||
host *trie.DomainTrie
|
||||
cache *cache.LruCache
|
||||
}
|
||||
|
||||
@ -120,7 +120,7 @@ func uintToIP(v uint32) net.IP {
|
||||
}
|
||||
|
||||
// New return Pool instance
|
||||
func New(ipnet *net.IPNet, size int, host *trie.Trie) (*Pool, error) {
|
||||
func New(ipnet *net.IPNet, size int, host *trie.DomainTrie) (*Pool, error) {
|
||||
min := ipToUint(ipnet.IP) + 2
|
||||
|
||||
ones, bits := ipnet.Mask.Size()
|
||||
|
@ -5,7 +5,7 @@ import (
|
||||
"net"
|
||||
"strings"
|
||||
|
||||
trie "github.com/Dreamacro/clash/component/domain-trie"
|
||||
"github.com/Dreamacro/clash/component/trie"
|
||||
)
|
||||
|
||||
var (
|
||||
|
@ -6,9 +6,10 @@ import (
|
||||
)
|
||||
|
||||
const (
|
||||
wildcard = "*"
|
||||
dotWildcard = ""
|
||||
domainStep = "."
|
||||
wildcard = "*"
|
||||
dotWildcard = ""
|
||||
complexWildcard = "+"
|
||||
domainStep = "."
|
||||
)
|
||||
|
||||
var (
|
||||
@ -16,9 +17,9 @@ var (
|
||||
ErrInvalidDomain = errors.New("invalid domain")
|
||||
)
|
||||
|
||||
// Trie contains the main logic for adding and searching nodes for domain segments.
|
||||
// DomainTrie contains the main logic for adding and searching nodes for domain segments.
|
||||
// support wildcard domain (e.g *.google.com)
|
||||
type Trie struct {
|
||||
type DomainTrie struct {
|
||||
root *Node
|
||||
}
|
||||
|
||||
@ -47,12 +48,25 @@ func validAndSplitDomain(domain string) ([]string, bool) {
|
||||
// 2. *.example.com
|
||||
// 3. subdomain.*.example.com
|
||||
// 4. .example.com
|
||||
func (t *Trie) Insert(domain string, data interface{}) error {
|
||||
// 5. +.example.com
|
||||
func (t *DomainTrie) Insert(domain string, data interface{}) error {
|
||||
parts, valid := validAndSplitDomain(domain)
|
||||
if !valid {
|
||||
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
|
||||
}
|
||||
|
||||
func (t *DomainTrie) insert(parts []string, data interface{}) {
|
||||
node := t.root
|
||||
// reverse storage domain part to save space
|
||||
for i := len(parts) - 1; i >= 0; i-- {
|
||||
@ -65,7 +79,6 @@ func (t *Trie) Insert(domain string, data interface{}) error {
|
||||
}
|
||||
|
||||
node.Data = data
|
||||
return nil
|
||||
}
|
||||
|
||||
// Search is the most important part of the Trie.
|
||||
@ -73,7 +86,7 @@ func (t *Trie) Insert(domain string, data interface{}) error {
|
||||
// 1. static part
|
||||
// 2. wildcard domain
|
||||
// 2. dot wildcard domain
|
||||
func (t *Trie) Search(domain string) *Node {
|
||||
func (t *DomainTrie) Search(domain string) *Node {
|
||||
parts, valid := validAndSplitDomain(domain)
|
||||
if !valid || parts[0] == "" {
|
||||
return nil
|
||||
@ -121,6 +134,6 @@ func (t *Trie) Search(domain string) *Node {
|
||||
}
|
||||
|
||||
// New returns a new, empty Trie.
|
||||
func New() *Trie {
|
||||
return &Trie{root: newNode(nil)}
|
||||
func New() *DomainTrie {
|
||||
return &DomainTrie{root: newNode(nil)}
|
||||
}
|
@ -35,6 +35,7 @@ func TestTrie_Wildcard(t *testing.T) {
|
||||
".org",
|
||||
".example.net",
|
||||
".apple.*",
|
||||
"+.foo.com",
|
||||
}
|
||||
|
||||
for _, domain := range domains {
|
||||
@ -46,6 +47,8 @@ func TestTrie_Wildcard(t *testing.T) {
|
||||
assert.NotNil(t, tree.Search("test.org"))
|
||||
assert.NotNil(t, tree.Search("test.example.net"))
|
||||
assert.NotNil(t, tree.Search("test.apple.com"))
|
||||
assert.NotNil(t, tree.Search("test.foo.com"))
|
||||
assert.NotNil(t, tree.Search("foo.com"))
|
||||
assert.Nil(t, tree.Search("foo.sub.example.com"))
|
||||
assert.Nil(t, tree.Search("foo.example.dev"))
|
||||
assert.Nil(t, tree.Search("example.com"))
|
Reference in New Issue
Block a user