Skip to content

Commit

Permalink
Fix: domain trie search
Browse files Browse the repository at this point in the history
  • Loading branch information
Dreamacro committed Jan 26, 2022
1 parent cfe7354 commit b1a639f
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 3 deletions.
4 changes: 2 additions & 2 deletions component/trie/domain.go
Original file line number Diff line number Diff line change
Expand Up @@ -109,13 +109,13 @@ func (t *DomainTrie) search(node *Node, parts []string) *Node {
}

if c := node.getChild(parts[len(parts)-1]); c != nil {
if n := t.search(c, parts[:len(parts)-1]); n != nil {
if n := t.search(c, parts[:len(parts)-1]); n != nil && n.Data != nil {
return n
}
}

if c := node.getChild(wildcard); c != nil {
if n := t.search(c, parts[:len(parts)-1]); n != nil {
if n := t.search(c, parts[:len(parts)-1]); n != nil && n.Data != nil {
return n
}
}
Expand Down
8 changes: 8 additions & 0 deletions component/trie/domain_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -97,3 +97,11 @@ func TestTrie_Boundary(t *testing.T) {
assert.NotNil(t, tree.Insert("..dev", localIP))
assert.Nil(t, tree.Search("dev"))
}

func TestTrie_WildcardBoundary(t *testing.T) {
tree := New()
tree.Insert("+.*", localIP)
tree.Insert("stun.*.*.*", localIP)

assert.NotNil(t, tree.Search("example.com"))
}
2 changes: 1 addition & 1 deletion component/trie/node.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@ package trie

// Node is the trie's node
type Node struct {
Data interface{}
children map[string]*Node
Data interface{}
}

func (n *Node) getChild(s string) *Node {
Expand Down

0 comments on commit b1a639f

Please sign in to comment.