mirror of
https://github.com/AdguardTeam/AdGuardHome.git
synced 2024-12-14 18:51:34 +03:00
21dfb5ffe8
Close #1828
Squashed commit of the following:
commit 347aff1f8c5293bd06ecc0693970fe447b6e2c75
Merge: f07c8dc0 9df7aba9
Author: Simon Zolin <s.zolin@adguard.com>
Date: Fri Jul 3 12:10:02 2020 +0300
Merge remote-tracking branch 'origin/master' into 1828-qlog-case-insens
commit f07c8dc0665585ce00c415293c47c5a8c9724881
Author: Simon Zolin <s.zolin@adguard.com>
Date: Fri Jul 3 12:09:49 2020 +0300
minor
commit 512c7acd48f649a0d11b4fef66a1351fcd19635b
Author: Simon Zolin <s.zolin@adguard.com>
Date: Thu Jul 2 19:01:43 2020 +0300
minor
commit 9f5700af56340c38a2d892101e32959702816205
Author: Simon Zolin <s.zolin@adguard.com>
Date: Thu Jul 2 16:53:56 2020 +0300
* querylog: case-insensitive search by domain name
140 lines
4.3 KiB
Go
140 lines
4.3 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 service
|
|
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,
|
|
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 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
|
|
}
|