mirror of
https://github.com/AdguardTeam/AdGuardHome.git
synced 2024-11-24 22:15:45 +03:00
3045da1742
Merge in DNS/adguard-home from 2271-handle-nolint to master Closes #2271. Squashed commit of the following: commit fde5c8795ac79e1f7d02ba8c8e369b5a724a000e Merge: fc2acd898642dcd647
Author: Eugene Burkov <e.burkov@adguard.com> Date: Fri Nov 20 17:12:28 2020 +0300 Merge branch 'master' into 2271-handle-nolint commit fc2acd89871de08c39e80ace9e5bb8a7acb7afba Author: Eugene Burkov <e.burkov@adguard.com> Date: Tue Nov 17 11:55:29 2020 +0300 dnsforward: fix test output strings commit c4ebae6ea9c293bad239519c44ca5a6c576bb921 Author: Eugene Burkov <e.burkov@adguard.com> Date: Mon Nov 16 22:43:20 2020 +0300 dnsfilter: make package pass tests commit f2d98c6acabd8977f3b1b361987eaa31eb6eb9ad Author: Eugene Burkov <e.burkov@adguard.com> Date: Mon Nov 16 20:05:00 2020 +0300 querylog: make decoding pass tests commit ab5850d24c50d53b8393f2de448cc340241351d7 Merge: 6ed2066bf8a9c6e8a0
Author: Eugene Burkov <e.burkov@adguard.com> Date: Mon Nov 16 19:48:31 2020 +0300 Merge branch 'master' into 2271-handle-nolint commit 6ed2066bf567e13dd14cfa16fc7b109b59fa39ef Author: Eugene Burkov <e.burkov@adguard.com> Date: Mon Nov 16 18:13:45 2020 +0300 home: fix tests naming commit af691081fb02b7500a746b16492f01f7f9befe9a Author: Eugene Burkov <e.burkov@adguard.com> Date: Mon Nov 16 12:15:49 2020 +0300 home: impove code quality commit 2914cd3cd23ef2a1964116baab9187d89b377f86 Author: Eugene Burkov <e.burkov@adguard.com> Date: Wed Nov 11 15:46:39 2020 +0300 * querylog: remove useless check commit 9996840650e784ccc76d1f29964560435ba27dc7 Author: Eugene Burkov <e.burkov@adguard.com> Date: Wed Nov 11 13:18:34 2020 +0300 * all: fix noticed defects commit 2b15293e59337f70302fbc0db81ebb26bee0bed2 Author: Eugene Burkov <e.burkov@adguard.com> Date: Tue Nov 10 20:15:53 2020 +0300 * stats: remove last nolint directive commit b2e1ddf7b58196a2fdbf879f084edb41ca1aa1eb Author: Eugene Burkov <e.burkov@adguard.com> Date: Tue Nov 10 18:35:41 2020 +0300 * all: remove another nolint directive commit c6fc5cfcc9c95ab9e570a95ab41c3e5c0125e62e Author: Eugene Burkov <e.burkov@adguard.com> Date: Tue Nov 10 18:11:28 2020 +0300 * querylog: remove nolint directive commit 226ddbf2c92f737f085b44a4ddf6daec7b602153 Author: Eugene Burkov <e.burkov@adguard.com> Date: Tue Nov 10 16:35:26 2020 +0300 * home: remove nolint directive commit 2ea3086ad41e9003282add7e996ae722d72d878b Author: Eugene Burkov <e.burkov@adguard.com> Date: Tue Nov 10 16:13:57 2020 +0300 * home: reduce cyclomatic complexity of run function commit f479b480c48e0bb832ddef8f57586f56b8a55bab Author: Eugene Burkov <e.burkov@adguard.com> Date: Tue Nov 10 15:35:46 2020 +0300 * home: use crypto/rand instead of math/rand commit a28d4a53e3b930136b036606fc7e78404f1d208b Author: Eugene Burkov <e.burkov@adguard.com> Date: Tue Nov 10 14:11:07 2020 +0300 * dnsforward: remove gocyclo nolint directive commit 64a0a324cc2b20614ceec3ccc6505e960fe526e9 Author: Eugene Burkov <e.burkov@adguard.com> Date: Tue Nov 10 11:45:49 2020 +0300 all *: remove some nolint directives Updates #2271.
155 lines
4.6 KiB
Go
155 lines
4.6 KiB
Go
package querylog
|
|
|
|
import (
|
|
"strings"
|
|
|
|
"github.com/AdguardTeam/AdGuardHome/internal/dnsfilter"
|
|
)
|
|
|
|
type criteriaType int
|
|
|
|
const (
|
|
ctDomainOrClient criteriaType = iota // domain name or client IP address
|
|
ctFilteringStatus // filtering status
|
|
)
|
|
|
|
const (
|
|
filteringStatusAll = "all"
|
|
filteringStatusFiltered = "filtered" // all kinds of filtering
|
|
|
|
filteringStatusBlocked = "blocked" // blocked or blocked services
|
|
filteringStatusBlockedService = "blocked_services" // blocked
|
|
filteringStatusBlockedSafebrowsing = "blocked_safebrowsing" // blocked by safebrowsing
|
|
filteringStatusBlockedParental = "blocked_parental" // blocked by parental control
|
|
filteringStatusWhitelisted = "whitelisted" // whitelisted
|
|
filteringStatusRewritten = "rewritten" // all kinds of rewrites
|
|
filteringStatusSafeSearch = "safe_search" // enforced safe search
|
|
filteringStatusProcessed = "processed" // not blocked, not white-listed entries
|
|
)
|
|
|
|
// filteringStatusValues -- array with all possible filteringStatus values
|
|
var filteringStatusValues = []string{
|
|
filteringStatusAll, filteringStatusFiltered, filteringStatusBlocked,
|
|
filteringStatusBlockedService, filteringStatusBlockedSafebrowsing, filteringStatusBlockedParental,
|
|
filteringStatusWhitelisted, filteringStatusRewritten, filteringStatusSafeSearch,
|
|
filteringStatusProcessed,
|
|
}
|
|
|
|
// searchCriteria - every search request may contain a list of different search criteria
|
|
// we use each of them to match the query
|
|
type searchCriteria struct {
|
|
criteriaType criteriaType // type of the criteria
|
|
strict bool // should we strictly match (equality) or not (indexOf)
|
|
value string // search criteria value
|
|
}
|
|
|
|
// quickMatch - quickly checks if the log entry matches this search criteria
|
|
// the reason is to do it as quickly as possible without de-serializing the entry
|
|
func (c *searchCriteria) quickMatch(line string) bool {
|
|
// note that we do this only for a limited set of criteria
|
|
|
|
switch c.criteriaType {
|
|
case ctDomainOrClient:
|
|
return c.quickMatchJSONValue(line, "QH") ||
|
|
c.quickMatchJSONValue(line, "IP")
|
|
default:
|
|
return true
|
|
}
|
|
}
|
|
|
|
// quickMatchJSONValue - helper used by quickMatch
|
|
func (c *searchCriteria) quickMatchJSONValue(line string, propertyName string) bool {
|
|
val := readJSONValue(line, propertyName)
|
|
if len(val) == 0 {
|
|
return false
|
|
}
|
|
val = strings.ToLower(val)
|
|
searchVal := strings.ToLower(c.value)
|
|
|
|
if c.strict && searchVal == val {
|
|
return true
|
|
}
|
|
if !c.strict && strings.Contains(val, searchVal) {
|
|
return true
|
|
}
|
|
|
|
return false
|
|
}
|
|
|
|
// match - checks if the log entry matches this search criteria
|
|
func (c *searchCriteria) match(entry *logEntry) bool {
|
|
switch c.criteriaType {
|
|
case ctDomainOrClient:
|
|
return c.ctDomainOrClientCase(entry)
|
|
case ctFilteringStatus:
|
|
return c.ctFilteringStatusCase(entry.Result)
|
|
}
|
|
|
|
return false
|
|
}
|
|
|
|
func (c *searchCriteria) ctDomainOrClientCase(entry *logEntry) bool {
|
|
qhost := strings.ToLower(entry.QHost)
|
|
searchVal := strings.ToLower(c.value)
|
|
if c.strict && qhost == searchVal {
|
|
return true
|
|
}
|
|
if !c.strict && strings.Contains(qhost, searchVal) {
|
|
return true
|
|
}
|
|
|
|
if c.strict && entry.IP == c.value {
|
|
return true
|
|
}
|
|
if !c.strict && strings.Contains(entry.IP, c.value) {
|
|
return true
|
|
}
|
|
return false
|
|
}
|
|
|
|
func (c *searchCriteria) ctFilteringStatusCase(res dnsfilter.Result) bool {
|
|
switch c.value {
|
|
case filteringStatusAll:
|
|
return true
|
|
|
|
case filteringStatusFiltered:
|
|
return res.IsFiltered ||
|
|
res.Reason.In(
|
|
dnsfilter.NotFilteredWhiteList,
|
|
dnsfilter.ReasonRewrite,
|
|
dnsfilter.RewriteEtcHosts,
|
|
)
|
|
|
|
case filteringStatusBlocked:
|
|
return res.IsFiltered &&
|
|
res.Reason.In(dnsfilter.FilteredBlackList, dnsfilter.FilteredBlockedService)
|
|
|
|
case filteringStatusBlockedService:
|
|
return res.IsFiltered && res.Reason == dnsfilter.FilteredBlockedService
|
|
|
|
case filteringStatusBlockedParental:
|
|
return res.IsFiltered && res.Reason == dnsfilter.FilteredParental
|
|
|
|
case filteringStatusBlockedSafebrowsing:
|
|
return res.IsFiltered && res.Reason == dnsfilter.FilteredSafeBrowsing
|
|
|
|
case filteringStatusWhitelisted:
|
|
return res.Reason == dnsfilter.NotFilteredWhiteList
|
|
|
|
case filteringStatusRewritten:
|
|
return res.Reason.In(dnsfilter.ReasonRewrite, dnsfilter.RewriteEtcHosts)
|
|
|
|
case filteringStatusSafeSearch:
|
|
return res.IsFiltered && res.Reason == dnsfilter.FilteredSafeSearch
|
|
|
|
case filteringStatusProcessed:
|
|
return !res.Reason.In(
|
|
dnsfilter.FilteredBlackList,
|
|
dnsfilter.FilteredBlockedService,
|
|
dnsfilter.NotFilteredWhiteList,
|
|
)
|
|
|
|
default:
|
|
return false
|
|
}
|
|
}
|