fix: count error

This commit is contained in:
Skyxim 2022-04-28 08:54:33 +08:00
parent ffb49ba4c5
commit 30eaa8add6
3 changed files with 9 additions and 3 deletions

View file

@ -32,6 +32,7 @@ func (c *classicalStrategy) ShouldResolveIP() bool {
func (c *classicalStrategy) OnUpdate(rules []string) {
var classicalRules []C.Rule
shouldResolveIP := false
count := 0
for _, rawRule := range rules {
ruleType, rule, params := ruleParse(rawRule)
r, err := parseRule(ruleType, rule, "", params)
@ -43,11 +44,12 @@ func (c *classicalStrategy) OnUpdate(rules []string) {
}
classicalRules = append(classicalRules, r)
c.count++
count++
}
}
c.rules = classicalRules
c.count = count
}
func NewClassicalStrategy() *classicalStrategy {

View file

@ -27,16 +27,18 @@ func (d *domainStrategy) ShouldResolveIP() bool {
func (d *domainStrategy) OnUpdate(rules []string) {
domainTrie := trie.New[bool]()
count := 0
for _, rule := range rules {
err := domainTrie.Insert(rule, true)
if err != nil {
log.Warnln("invalid domain:[%s]", rule)
} else {
d.count++
count++
}
}
d.domainRules = domainTrie
d.count = count
}
func ruleParse(ruleRaw string) (string, string, []string) {

View file

@ -26,16 +26,18 @@ func (i *ipcidrStrategy) ShouldResolveIP() bool {
func (i *ipcidrStrategy) OnUpdate(rules []string) {
ipCidrTrie := trie.NewIpCidrTrie()
count := 0
for _, rule := range rules {
err := ipCidrTrie.AddIpCidrForString(rule)
if err != nil {
log.Warnln("invalid Ipcidr:[%s]", rule)
} else {
i.count++
count++
}
}
i.trie = ipCidrTrie
i.count = count
i.shouldResolveIP = i.count > 0
}