AdGuardHome/querylog/search_criteria.go
Artem Baskal 10f67bd383 + client, querylog: Add blocked service filter support
Close #2124

Squashed commit of the following:

commit 574726e88e4fe7df745dfc964706e1e26f8da59f
Merge: 3bc770fb dc61744d
Author: ArtemBaskal <a.baskal@adguard.com>
Date:   Wed Sep 23 19:32:38 2020 +0300

    Merge branch 'master' into feature/2124

commit 3bc770fba6d06bbd965ee181aed7b0a050175f58
Author: ArtemBaskal <a.baskal@adguard.com>
Date:   Wed Sep 23 11:22:07 2020 +0300

    minor

commit 90c0d739d1bd09dbcf5f27ff9e6c3f761bf81686
Merge: 5d1f2642 1d36abd1
Author: ArtemBaskal <a.baskal@adguard.com>
Date:   Wed Sep 23 11:17:24 2020 +0300

    Merge branch 'master' into feature/2124

commit 5d1f264212686ac7ecab30401b4f0e3c020dbee9
Author: ArtemBaskal <a.baskal@adguard.com>
Date:   Tue Sep 22 17:23:08 2020 +0300

    Display elapsed if there is no service_name in blocked service

commit af86cedc31d566238764e02c5c8e465fa41292c6
Merge: b61976a7 756f97ed
Author: ArtemBaskal <a.baskal@adguard.com>
Date:   Tue Sep 22 16:55:40 2020 +0300

    Merge branch 'master' into feature/2124

commit b61976a7f811e1d01327cad8b0925bd110c6e135
Author: ArtemBaskal <a.baskal@adguard.com>
Date:   Tue Sep 22 16:52:48 2020 +0300

    Rename params to blocked_services, update service name display on client

commit d5b8e5f7b2c4a3d6701cf8845d31b28f55c6a808
Author: ArtemBaskal <a.baskal@adguard.com>
Date:   Tue Sep 22 15:59:46 2020 +0300

    Update docs, return global blocked status

commit adc9a294f76070dea2d845155814c21c52fc6c7f
Author: ArtemBaskal <a.baskal@adguard.com>
Date:   Tue Sep 22 15:24:34 2020 +0300

    + querylog: Extract filteringStatusBlockedService

commit ee8c1dce0d7520be0d0fcfe6f798dd11f13c9262
Author: ArtemBaskal <a.baskal@adguard.com>
Date:   Tue Sep 22 13:54:16 2020 +0300

    + client: Add blocked service filter support
2020-09-23 19:47:02 +03:00

143 lines
4.5 KiB
Go

package querylog
import (
"strings"
"github.com/AdguardTeam/AdGuardHome/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
// nolint (gocyclo)
func (c *searchCriteria) match(entry *logEntry) bool {
switch c.criteriaType {
case ctDomainOrClient:
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
case ctFilteringStatus:
res := entry.Result
switch c.value {
case filteringStatusAll:
return true
case filteringStatusFiltered:
return res.IsFiltered ||
res.Reason == dnsfilter.NotFilteredWhiteList ||
res.Reason == dnsfilter.ReasonRewrite ||
res.Reason == dnsfilter.RewriteEtcHosts
case filteringStatusBlocked:
return res.IsFiltered &&
(res.Reason == dnsfilter.FilteredBlackList ||
res.Reason == 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 == dnsfilter.ReasonRewrite ||
res.Reason == dnsfilter.RewriteEtcHosts
case filteringStatusSafeSearch:
return res.IsFiltered && res.Reason == dnsfilter.FilteredSafeSearch
case filteringStatusProcessed:
return !(res.Reason == dnsfilter.FilteredBlackList ||
res.Reason == dnsfilter.FilteredBlockedService ||
res.Reason == dnsfilter.NotFilteredWhiteList)
default:
return false
}
}
return false
}