Feature: domain trie support wildcard alias

This commit is contained in:
gVisor bot 2020-05-28 12:13:05 +08:00
parent fff62644f0
commit 759b7aa934
9 changed files with 40 additions and 22 deletions

View file

@ -119,12 +119,14 @@ experimental:
# - "user1:pass1" # - "user1:pass1"
# - "user2:pass2" # - "user2:pass2"
# # experimental hosts, support wildcard (e.g. *.clash.dev Even *.foo.*.example.com) # # hosts, support wildcard (e.g. *.clash.dev Even *.foo.*.example.com)
# # static domain has a higher priority than wildcard domain (foo.example.com > *.example.com > .example.com) # # static domain has a higher priority than wildcard domain (foo.example.com > *.example.com > .example.com)
# # +.foo.com equal .foo.com and foo.com
# hosts: # hosts:
# '*.clash.dev': 127.0.0.1 # '*.clash.dev': 127.0.0.1
# '.dev': 127.0.0.1 # '.dev': 127.0.0.1
# 'alpha.clash.dev': '::1' # 'alpha.clash.dev': '::1'
# '+.foo.dev': 127.0.0.1
# dns: # dns:
# enable: true # set true to enable dns (default is false) # enable: true # set true to enable dns (default is false)

View file

@ -6,7 +6,7 @@ import (
"sync" "sync"
"github.com/Dreamacro/clash/common/cache" "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 // Pool is a implementation about fake ip generator without storage
@ -16,7 +16,7 @@ type Pool struct {
gateway uint32 gateway uint32
offset uint32 offset uint32
mux sync.Mutex mux sync.Mutex
host *trie.Trie host *trie.DomainTrie
cache *cache.LruCache cache *cache.LruCache
} }
@ -120,7 +120,7 @@ func uintToIP(v uint32) net.IP {
} }
// New return Pool instance // 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 min := ipToUint(ipnet.IP) + 2
ones, bits := ipnet.Mask.Size() ones, bits := ipnet.Mask.Size()

View file

@ -5,7 +5,7 @@ import (
"net" "net"
"strings" "strings"
trie "github.com/Dreamacro/clash/component/domain-trie" "github.com/Dreamacro/clash/component/trie"
) )
var ( var (

View file

@ -6,9 +6,10 @@ import (
) )
const ( const (
wildcard = "*" wildcard = "*"
dotWildcard = "" dotWildcard = ""
domainStep = "." complexWildcard = "+"
domainStep = "."
) )
var ( var (
@ -16,9 +17,9 @@ var (
ErrInvalidDomain = errors.New("invalid domain") 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) // support wildcard domain (e.g *.google.com)
type Trie struct { type DomainTrie struct {
root *Node root *Node
} }
@ -47,12 +48,25 @@ func validAndSplitDomain(domain string) ([]string, bool) {
// 2. *.example.com // 2. *.example.com
// 3. subdomain.*.example.com // 3. subdomain.*.example.com
// 4. .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) parts, valid := validAndSplitDomain(domain)
if !valid { if !valid {
return ErrInvalidDomain 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 node := t.root
// reverse storage domain part to save space // reverse storage domain part to save space
for i := len(parts) - 1; i >= 0; i-- { for i := len(parts) - 1; i >= 0; i-- {
@ -65,7 +79,6 @@ func (t *Trie) Insert(domain string, data interface{}) error {
} }
node.Data = data node.Data = data
return nil
} }
// Search is the most important part of the Trie. // 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 // 1. static part
// 2. wildcard domain // 2. wildcard domain
// 2. dot wildcard domain // 2. dot wildcard domain
func (t *Trie) Search(domain string) *Node { func (t *DomainTrie) Search(domain string) *Node {
parts, valid := validAndSplitDomain(domain) parts, valid := validAndSplitDomain(domain)
if !valid || parts[0] == "" { if !valid || parts[0] == "" {
return nil return nil
@ -121,6 +134,6 @@ func (t *Trie) Search(domain string) *Node {
} }
// New returns a new, empty Trie. // New returns a new, empty Trie.
func New() *Trie { func New() *DomainTrie {
return &Trie{root: newNode(nil)} return &DomainTrie{root: newNode(nil)}
} }

View file

@ -35,6 +35,7 @@ func TestTrie_Wildcard(t *testing.T) {
".org", ".org",
".example.net", ".example.net",
".apple.*", ".apple.*",
"+.foo.com",
} }
for _, domain := range domains { 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.org"))
assert.NotNil(t, tree.Search("test.example.net")) assert.NotNil(t, tree.Search("test.example.net"))
assert.NotNil(t, tree.Search("test.apple.com")) 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.sub.example.com"))
assert.Nil(t, tree.Search("foo.example.dev")) assert.Nil(t, tree.Search("foo.example.dev"))
assert.Nil(t, tree.Search("example.com")) assert.Nil(t, tree.Search("example.com"))

View file

@ -12,8 +12,8 @@ import (
"github.com/Dreamacro/clash/adapters/outboundgroup" "github.com/Dreamacro/clash/adapters/outboundgroup"
"github.com/Dreamacro/clash/adapters/provider" "github.com/Dreamacro/clash/adapters/provider"
"github.com/Dreamacro/clash/component/auth" "github.com/Dreamacro/clash/component/auth"
trie "github.com/Dreamacro/clash/component/domain-trie"
"github.com/Dreamacro/clash/component/fakeip" "github.com/Dreamacro/clash/component/fakeip"
"github.com/Dreamacro/clash/component/trie"
C "github.com/Dreamacro/clash/constant" C "github.com/Dreamacro/clash/constant"
"github.com/Dreamacro/clash/dns" "github.com/Dreamacro/clash/dns"
"github.com/Dreamacro/clash/log" "github.com/Dreamacro/clash/log"
@ -69,7 +69,7 @@ type Config struct {
General *General General *General
DNS *DNS DNS *DNS
Experimental *Experimental Experimental *Experimental
Hosts *trie.Trie Hosts *trie.DomainTrie
Rules []C.Rule Rules []C.Rule
Users []auth.AuthUser Users []auth.AuthUser
Proxies map[string]C.Proxy Proxies map[string]C.Proxy
@ -450,7 +450,7 @@ func parseRules(cfg *RawConfig, proxies map[string]C.Proxy) ([]C.Rule, error) {
return rules, nil return rules, nil
} }
func parseHosts(cfg *RawConfig) (*trie.Trie, error) { func parseHosts(cfg *RawConfig) (*trie.DomainTrie, error) {
tree := trie.New() tree := trie.New()
if len(cfg.Hosts) != 0 { if len(cfg.Hosts) != 0 {
for domain, ipStr := range cfg.Hosts { for domain, ipStr := range cfg.Hosts {
@ -586,7 +586,7 @@ func parseDNS(cfg RawDNS) (*DNS, error) {
return nil, err return nil, err
} }
var host *trie.Trie var host *trie.DomainTrie
// fake ip skip host filter // fake ip skip host filter
if len(cfg.FakeIPFilter) != 0 { if len(cfg.FakeIPFilter) != 0 {
host = trie.New() host = trie.New()

View file

@ -9,8 +9,8 @@ import (
"github.com/Dreamacro/clash/adapters/provider" "github.com/Dreamacro/clash/adapters/provider"
"github.com/Dreamacro/clash/component/auth" "github.com/Dreamacro/clash/component/auth"
"github.com/Dreamacro/clash/component/dialer" "github.com/Dreamacro/clash/component/dialer"
trie "github.com/Dreamacro/clash/component/domain-trie"
"github.com/Dreamacro/clash/component/resolver" "github.com/Dreamacro/clash/component/resolver"
"github.com/Dreamacro/clash/component/trie"
"github.com/Dreamacro/clash/config" "github.com/Dreamacro/clash/config"
C "github.com/Dreamacro/clash/constant" C "github.com/Dreamacro/clash/constant"
"github.com/Dreamacro/clash/dns" "github.com/Dreamacro/clash/dns"
@ -154,7 +154,7 @@ func updateDNS(c *config.DNS) {
} }
} }
func updateHosts(tree *trie.Trie) { func updateHosts(tree *trie.DomainTrie) {
resolver.DefaultHosts = tree resolver.DefaultHosts = tree
} }

View file

@ -1,9 +1,9 @@
package hub package hub
import ( import (
"github.com/Dreamacro/clash/config"
"github.com/Dreamacro/clash/hub/executor" "github.com/Dreamacro/clash/hub/executor"
"github.com/Dreamacro/clash/hub/route" "github.com/Dreamacro/clash/hub/route"
"github.com/Dreamacro/clash/config"
) )
type Option func(*config.Config) type Option func(*config.Config)