feat: log the rule which is the cause of blocking (#1460)

Co-authored-by: ThinkChaos <ThinkChaos@users.noreply.github.com>
This commit is contained in:
Thomas Anderson 2024-04-24 19:58:29 +03:00 committed by GitHub
parent 58c5069803
commit e99c98b4c2
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 24 additions and 3 deletions

View File

@ -50,7 +50,12 @@ 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("string_map").Debugf("block rule '%s' matched with '%s'", blockRule, searchString)
return true
}
}
return false
@ -132,7 +137,7 @@ func (cache regexCache) elementCount() int {
func (cache regexCache) contains(searchString string) bool {
for _, regex := range cache {
if regex.MatchString(searchString) {
log.PrefixedLog("regexCache").Debugf("regex '%s' matched with '%s'", regex, searchString)
log.PrefixedLog("regex_cache").Debugf("regex '%s' matched with '%s'", regex, searchString)
return true
}

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,12 @@ 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 +141,14 @@ 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
}
}
}