Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Log the rule which is the cause of blocking #1460

Merged
merged 2 commits into from Apr 24, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
9 changes: 7 additions & 2 deletions cache/stringcache/string_caches.go
Expand Up @@ -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
zc-devs marked this conversation as resolved.
Show resolved Hide resolved
}
}

return false
Expand Down Expand Up @@ -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
}
Expand Down
18 changes: 17 additions & 1 deletion trie/trie.go
@@ -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.
//
Expand Down Expand Up @@ -108,8 +113,12 @@ func (n *parent) insert(key string, split SplitFunc) {
}

func (n *parent) hasParentOf(key string, split SplitFunc) bool {
searchString := key
rule := ""
zc-devs marked this conversation as resolved.
Show resolved Hide resolved

for {
label, rest := split(key)
rule = strings.Join([]string{label, rule}, ".")

child, ok := n.children[label]
if !ok {
Expand All @@ -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
zc-devs marked this conversation as resolved.
Show resolved Hide resolved
}
}
}
Expand Down