diff --git a/component/trie/set_test.go b/component/trie/set_test.go index 32cc7d99..6672d885 100644 --- a/component/trie/set_test.go +++ b/component/trie/set_test.go @@ -12,9 +12,12 @@ func TestDomain(t *testing.T) { "baidu.com", "google.com", "www.google.com", + "test.a.net", + "test.a.oc", } set := trie.NewDomainSet(domainSet) assert.NotNil(t, set) + assert.True(t, set.Has("test.a.net")) assert.True(t, set.Has("google.com")) assert.False(t, set.Has("www.baidu.com")) } @@ -24,6 +27,8 @@ func TestDomainComplexWildcard(t *testing.T) { "+.baidu.com", "+.a.baidu.com", "www.baidu.com", + "test.a.net", + "test.a.oc", "www.qq.com", } set := trie.NewDomainSet(domainSet) diff --git a/component/trie/sskv.go b/component/trie/sskv.go index 7e86f539..61e92ce7 100644 --- a/component/trie/sskv.go +++ b/component/trie/sskv.go @@ -33,9 +33,8 @@ func NewDomainSet(keys []string) *DomainSet { domainTrie.Foreach(func(domain string, data struct{}) { reserveDomains = append(reserveDomains, utils.Reverse(domain)) }) - sort.Slice(reserveDomains, func(i, j int) bool { - return len(reserveDomains[i]) < len(reserveDomains[j]) - }) + // ensure that the same prefix is continuous + sort.Strings(reserveDomains) keys = reserveDomains if len(keys) == 0 { return nil @@ -77,6 +76,7 @@ func (ss *DomainSet) Has(key string) bool { if ss == nil { return false } + key = strings.TrimSpace(key) key = utils.Reverse(key) key = strings.ToLower(key) // no more labels in this node @@ -123,7 +123,9 @@ func (ss *DomainSet) Has(key string) bool { cursor.bmIdx = bmIdx cursor.index = i } else if ss.labels[bmIdx-nodeId] == c { - cursor.find = false + if ss.labels[bmIdx-nodeId] == domainStepByte { + cursor.find = false + } break } }