From 3fc6d550037d812cb60d98a7dc83d071a42c7a6a Mon Sep 17 00:00:00 2001 From: Dreamacro <305009791@qq.com> Date: Fri, 24 Apr 2020 23:49:19 +0800 Subject: [PATCH] Fix: domain wildcard behavior --- component/domain-trie/tire.go | 15 +++++++++++---- component/domain-trie/trie_test.go | 2 ++ 2 files changed, 13 insertions(+), 4 deletions(-) diff --git a/component/domain-trie/tire.go b/component/domain-trie/tire.go index b4f2bce4..e7322e5d 100644 --- a/component/domain-trie/tire.go +++ b/component/domain-trie/tire.go @@ -81,6 +81,7 @@ func (t *Trie) Search(domain string) *Node { n := t.root var dotWildcardNode *Node + var wildcardNode *Node for i := len(parts) - 1; i >= 0; i-- { part := parts[i] @@ -88,10 +89,16 @@ func (t *Trie) Search(domain string) *Node { dotWildcardNode = node } - if n.hasChild(part) { - n = n.getChild(part) - } else { - n = n.getChild(wildcard) + child := n.getChild(part) + if child == nil && wildcardNode != nil { + child = wildcardNode.getChild(part) + } + wildcardNode = n.getChild(wildcard) + + n = child + if n == nil { + n = wildcardNode + wildcardNode = nil } if n == nil { diff --git a/component/domain-trie/trie_test.go b/component/domain-trie/trie_test.go index 927e4341..fa0f88f7 100644 --- a/component/domain-trie/trie_test.go +++ b/component/domain-trie/trie_test.go @@ -34,6 +34,7 @@ func TestTrie_Wildcard(t *testing.T) { "*.dev", ".org", ".example.net", + ".apple.*", } for _, domain := range domains { @@ -44,6 +45,7 @@ func TestTrie_Wildcard(t *testing.T) { assert.NotNil(t, tree.Search("sub.foo.example.com")) assert.NotNil(t, tree.Search("test.org")) assert.NotNil(t, tree.Search("test.example.net")) + assert.NotNil(t, tree.Search("test.apple.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"))