Log the rule which is the cause of blocking

This commit is contained in:
Thomas Anderson 2024-04-23 20:24:51 +03:00
parent 58c5069803
commit 0a9df2f181
No known key found for this signature in database
GPG Key ID: 4BFC48FBBFBB935F
2 changed files with 20 additions and 2 deletions

View File

@ -50,7 +50,11 @@ func (cache stringMap) contains(searchString string) bool {
})
if idx < searchBucketLen {
return cache[searchLen][idx*searchLen:idx*searchLen+searchLen] == strings.ToLower(normalized)
blockRule := cache[searchLen][idx*searchLen : idx*searchLen+searchLen]
if blockRule == normalized {
log.PrefixedLog("stringMap").Debugf("block rule '%s' matched with '%s'", blockRule, searchString)
return true
}
}
return false

View File

@ -1,5 +1,10 @@
package trie
import (
"github.com/0xERR0R/blocky/log"
"strings"
)
// Trie stores a set of strings and can quickly check
// if it contains an element, or one of its parents.
//
@ -108,8 +113,11 @@ func (n *parent) insert(key string, split SplitFunc) {
}
func (n *parent) hasParentOf(key string, split SplitFunc) bool {
searchString := key
rule := ""
for {
label, rest := split(key)
rule = strings.Join([]string{label, rule}, ".")
child, ok := n.children[label]
if !ok {
@ -132,7 +140,13 @@ func (n *parent) hasParentOf(key string, split SplitFunc) bool {
case terminal:
// Continue down the trie
return child.hasParentOf(rest, split)
matched := child.hasParentOf(rest, split)
if matched {
rule = strings.Join([]string{child.String(), rule}, ".")
rule = strings.Trim(rule, ".")
log.PrefixedLog("Trie").Debugf("wildcard block rule '%s' matched with '%s'", rule, searchString)
}
return matched
}
}
}