2020-05-26 05:37:37 -07:00
|
|
|
package querylog
|
|
|
|
|
|
|
|
import (
|
|
|
|
"strings"
|
|
|
|
|
2020-10-30 03:32:02 -07:00
|
|
|
"github.com/AdguardTeam/AdGuardHome/internal/dnsfilter"
|
2020-05-26 05:37:37 -07:00
|
|
|
)
|
|
|
|
|
|
|
|
type criteriaType int
|
|
|
|
|
|
|
|
const (
|
2020-05-28 07:14:50 -07:00
|
|
|
ctDomainOrClient criteriaType = iota // domain name or client IP address
|
2020-05-26 05:37:37 -07:00
|
|
|
ctFilteringStatus // filtering status
|
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
|
|
|
filteringStatusAll = "all"
|
|
|
|
filteringStatusFiltered = "filtered" // all kinds of filtering
|
|
|
|
|
2020-09-23 09:47:02 -07:00
|
|
|
filteringStatusBlocked = "blocked" // blocked or blocked services
|
|
|
|
filteringStatusBlockedService = "blocked_services" // blocked
|
2020-05-26 05:37:37 -07:00
|
|
|
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
|
2020-05-28 07:14:50 -07:00
|
|
|
filteringStatusProcessed = "processed" // not blocked, not white-listed entries
|
2020-05-26 05:37:37 -07:00
|
|
|
)
|
|
|
|
|
|
|
|
// filteringStatusValues -- array with all possible filteringStatus values
|
|
|
|
var filteringStatusValues = []string{
|
|
|
|
filteringStatusAll, filteringStatusFiltered, filteringStatusBlocked,
|
2020-09-23 09:47:02 -07:00
|
|
|
filteringStatusBlockedService, filteringStatusBlockedSafebrowsing, filteringStatusBlockedParental,
|
2020-05-26 05:37:37 -07:00
|
|
|
filteringStatusWhitelisted, filteringStatusRewritten, filteringStatusSafeSearch,
|
2020-05-28 07:14:50 -07:00
|
|
|
filteringStatusProcessed,
|
2020-05-26 05:37:37 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
// 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 {
|
2020-05-28 07:14:50 -07:00
|
|
|
case ctDomainOrClient:
|
|
|
|
return c.quickMatchJSONValue(line, "QH") ||
|
|
|
|
c.quickMatchJSONValue(line, "IP")
|
2020-05-26 05:37:37 -07:00
|
|
|
default:
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// quickMatchJSONValue - helper used by quickMatch
|
2020-11-30 03:32:58 -07:00
|
|
|
func (c *searchCriteria) quickMatchJSONValue(line, propertyName string) bool {
|
2020-05-26 05:37:37 -07:00
|
|
|
val := readJSONValue(line, propertyName)
|
|
|
|
if len(val) == 0 {
|
|
|
|
return false
|
|
|
|
}
|
2020-07-03 02:21:09 -07:00
|
|
|
val = strings.ToLower(val)
|
|
|
|
searchVal := strings.ToLower(c.value)
|
2020-05-26 05:37:37 -07:00
|
|
|
|
2020-07-03 02:21:09 -07:00
|
|
|
if c.strict && searchVal == val {
|
2020-05-26 05:37:37 -07:00
|
|
|
return true
|
|
|
|
}
|
2020-07-03 02:21:09 -07:00
|
|
|
if !c.strict && strings.Contains(val, searchVal) {
|
2020-05-26 05:37:37 -07:00
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
// match - checks if the log entry matches this search criteria
|
|
|
|
func (c *searchCriteria) match(entry *logEntry) bool {
|
|
|
|
switch c.criteriaType {
|
2020-05-28 07:14:50 -07:00
|
|
|
case ctDomainOrClient:
|
2020-11-20 07:32:41 -07:00
|
|
|
return c.ctDomainOrClientCase(entry)
|
|
|
|
case ctFilteringStatus:
|
|
|
|
return c.ctFilteringStatusCase(entry.Result)
|
|
|
|
}
|
2020-05-28 07:14:50 -07:00
|
|
|
|
2020-11-20 07:32:41 -07:00
|
|
|
return false
|
|
|
|
}
|
2020-05-28 07:14:50 -07:00
|
|
|
|
2020-11-20 07:32:41 -07:00
|
|
|
func (c *searchCriteria) ctDomainOrClientCase(entry *logEntry) bool {
|
|
|
|
qhost := strings.ToLower(entry.QHost)
|
|
|
|
searchVal := strings.ToLower(c.value)
|
|
|
|
if c.strict && qhost == searchVal {
|
|
|
|
return true
|
|
|
|
}
|
2021-01-20 07:27:53 -07:00
|
|
|
|
2020-11-20 07:32:41 -07:00
|
|
|
if !c.strict && strings.Contains(qhost, searchVal) {
|
|
|
|
return true
|
2020-05-26 05:37:37 -07:00
|
|
|
}
|
|
|
|
|
2021-01-20 07:27:53 -07:00
|
|
|
ipStr := entry.IP.String()
|
|
|
|
if c.strict && ipStr == c.value {
|
2020-11-20 07:32:41 -07:00
|
|
|
return true
|
|
|
|
}
|
2021-01-20 07:27:53 -07:00
|
|
|
|
|
|
|
if !c.strict && strings.Contains(ipStr, c.value) {
|
2020-11-20 07:32:41 -07:00
|
|
|
return true
|
|
|
|
}
|
2021-01-20 07:27:53 -07:00
|
|
|
|
2020-05-26 05:37:37 -07:00
|
|
|
return false
|
|
|
|
}
|
2020-11-20 07:32:41 -07:00
|
|
|
|
|
|
|
func (c *searchCriteria) ctFilteringStatusCase(res dnsfilter.Result) bool {
|
|
|
|
switch c.value {
|
|
|
|
case filteringStatusAll:
|
|
|
|
return true
|
|
|
|
|
|
|
|
case filteringStatusFiltered:
|
|
|
|
return res.IsFiltered ||
|
|
|
|
res.Reason.In(
|
2020-12-21 07:48:07 -07:00
|
|
|
dnsfilter.NotFilteredAllowList,
|
2020-12-28 08:41:50 -07:00
|
|
|
dnsfilter.Rewritten,
|
|
|
|
dnsfilter.RewrittenAutoHosts,
|
|
|
|
dnsfilter.RewrittenRule,
|
2020-11-20 07:32:41 -07:00
|
|
|
)
|
|
|
|
|
|
|
|
case filteringStatusBlocked:
|
|
|
|
return res.IsFiltered &&
|
2020-12-21 07:48:07 -07:00
|
|
|
res.Reason.In(dnsfilter.FilteredBlockList, dnsfilter.FilteredBlockedService)
|
2020-11-20 07:32:41 -07:00
|
|
|
|
|
|
|
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:
|
2020-12-21 07:48:07 -07:00
|
|
|
return res.Reason == dnsfilter.NotFilteredAllowList
|
2020-11-20 07:32:41 -07:00
|
|
|
|
|
|
|
case filteringStatusRewritten:
|
2020-12-28 08:41:50 -07:00
|
|
|
return res.Reason.In(
|
|
|
|
dnsfilter.Rewritten,
|
|
|
|
dnsfilter.RewrittenAutoHosts,
|
|
|
|
dnsfilter.RewrittenRule,
|
|
|
|
)
|
2020-11-20 07:32:41 -07:00
|
|
|
|
|
|
|
case filteringStatusSafeSearch:
|
|
|
|
return res.IsFiltered && res.Reason == dnsfilter.FilteredSafeSearch
|
|
|
|
|
|
|
|
case filteringStatusProcessed:
|
|
|
|
return !res.Reason.In(
|
2020-12-21 07:48:07 -07:00
|
|
|
dnsfilter.FilteredBlockList,
|
2020-11-20 07:32:41 -07:00
|
|
|
dnsfilter.FilteredBlockedService,
|
2020-12-21 07:48:07 -07:00
|
|
|
dnsfilter.NotFilteredAllowList,
|
2020-11-20 07:32:41 -07:00
|
|
|
)
|
|
|
|
|
|
|
|
default:
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
}
|