1
0
mirror of https://github.com/v2fly/v2ray-core.git synced 2024-07-01 11:35:23 +00:00
v2fly/common/strmatcher/domain_matcher.go

63 lines
1.1 KiB
Go
Raw Normal View History

2018-08-19 19:04:15 +00:00
package strmatcher
import "strings"
func breakDomain(domain string) []string {
return strings.Split(domain, ".")
}
type node struct {
value uint32
sub map[string]*node
}
2018-08-20 13:39:58 +00:00
// DomainMatcherGroup is a IndexMatcher for a large set of Domain matchers.
// Visible for testing only.
2018-08-19 19:04:15 +00:00
type DomainMatcherGroup struct {
root *node
}
func (g *DomainMatcherGroup) Add(domain string, value uint32) {
if g.root == nil {
g.root = &node{
sub: make(map[string]*node),
}
}
current := g.root
parts := breakDomain(domain)
for i := len(parts) - 1; i >= 0; i-- {
part := parts[i]
next := current.sub[part]
if next == nil {
next = &node{sub: make(map[string]*node)}
current.sub[part] = next
}
current = next
}
current.value = value
}
2018-08-20 07:57:06 +00:00
func (g *DomainMatcherGroup) addMatcher(m domainMatcher, value uint32) {
g.Add(string(m), value)
}
2018-08-19 19:04:15 +00:00
func (g *DomainMatcherGroup) Match(domain string) uint32 {
current := g.root
if current == nil {
return 0
}
2018-08-19 19:04:15 +00:00
parts := breakDomain(domain)
for i := len(parts) - 1; i >= 0; i-- {
part := parts[i]
next := current.sub[part]
if next == nil {
break
}
current = next
}
return current.value
}