From e99c98b4c2eb7a61de1d910eb8f23e7bf7b758b2 Mon Sep 17 00:00:00 2001 From: Thomas Anderson <127358482+zc-devs@users.noreply.github.com> Date: Wed, 24 Apr 2024 19:58:29 +0300 Subject: [PATCH] feat: log the rule which is the cause of blocking (#1460) Co-authored-by: ThinkChaos --- cache/stringcache/string_caches.go | 9 +++++++-- trie/trie.go | 18 +++++++++++++++++- 2 files changed, 24 insertions(+), 3 deletions(-) diff --git a/cache/stringcache/string_caches.go b/cache/stringcache/string_caches.go index 45c7120f..001118cd 100644 --- a/cache/stringcache/string_caches.go +++ b/cache/stringcache/string_caches.go @@ -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 } diff --git a/trie/trie.go b/trie/trie.go index 55028ff1..1fc315d3 100644 --- a/trie/trie.go +++ b/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. // @@ -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 } } }