2020-05-26 12:37:37 +00:00
|
|
|
package querylog
|
|
|
|
|
|
|
|
import (
|
|
|
|
"strings"
|
|
|
|
|
2020-10-30 10:32:02 +00:00
|
|
|
"github.com/AdguardTeam/AdGuardHome/internal/dnsfilter"
|
2020-05-26 12:37:37 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
type criteriaType int
|
|
|
|
|
|
|
|
const (
|
2021-01-27 15:32:13 +00:00
|
|
|
// ctDomainOrClient is for searching by the domain name, the client's IP
|
|
|
|
// address, or the clinet's ID.
|
|
|
|
ctDomainOrClient criteriaType = iota
|
|
|
|
// ctFilteringStatus is for searching by the filtering status.
|
|
|
|
//
|
|
|
|
// See (*searchCriteria).ctFilteringStatusCase for details.
|
|
|
|
ctFilteringStatus
|
2020-05-26 12:37:37 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
|
|
|
filteringStatusAll = "all"
|
|
|
|
filteringStatusFiltered = "filtered" // all kinds of filtering
|
|
|
|
|
2020-09-23 16:47:02 +00:00
|
|
|
filteringStatusBlocked = "blocked" // blocked or blocked services
|
|
|
|
filteringStatusBlockedService = "blocked_services" // blocked
|
2020-05-26 12:37:37 +00: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 14:14:50 +00:00
|
|
|
filteringStatusProcessed = "processed" // not blocked, not white-listed entries
|
2020-05-26 12:37:37 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
// filteringStatusValues -- array with all possible filteringStatus values
|
|
|
|
var filteringStatusValues = []string{
|
|
|
|
filteringStatusAll, filteringStatusFiltered, filteringStatusBlocked,
|
2020-09-23 16:47:02 +00:00
|
|
|
filteringStatusBlockedService, filteringStatusBlockedSafebrowsing, filteringStatusBlockedParental,
|
2020-05-26 12:37:37 +00:00
|
|
|
filteringStatusWhitelisted, filteringStatusRewritten, filteringStatusSafeSearch,
|
2020-05-28 14:14:50 +00:00
|
|
|
filteringStatusProcessed,
|
2020-05-26 12:37:37 +00: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 {
|
2021-01-27 15:32:13 +00:00
|
|
|
value string // search criteria value
|
2020-05-26 12:37:37 +00:00
|
|
|
criteriaType criteriaType // type of the criteria
|
|
|
|
strict bool // should we strictly match (equality) or not (indexOf)
|
|
|
|
}
|
|
|
|
|
|
|
|
// match - checks if the log entry matches this search criteria
|
|
|
|
func (c *searchCriteria) match(entry *logEntry) bool {
|
|
|
|
switch c.criteriaType {
|
2020-05-28 14:14:50 +00:00
|
|
|
case ctDomainOrClient:
|
2020-11-20 14:32:41 +00:00
|
|
|
return c.ctDomainOrClientCase(entry)
|
|
|
|
case ctFilteringStatus:
|
|
|
|
return c.ctFilteringStatusCase(entry.Result)
|
|
|
|
}
|
2020-05-28 14:14:50 +00:00
|
|
|
|
2020-11-20 14:32:41 +00:00
|
|
|
return false
|
|
|
|
}
|
2020-05-28 14:14:50 +00:00
|
|
|
|
2021-04-02 14:30:39 +00:00
|
|
|
func (c *searchCriteria) ctDomainOrClientCaseStrict(term, clientID, name, host, ip string) bool {
|
|
|
|
return strings.EqualFold(host, term) ||
|
|
|
|
strings.EqualFold(clientID, term) ||
|
|
|
|
strings.EqualFold(ip, term) ||
|
|
|
|
strings.EqualFold(name, term)
|
|
|
|
}
|
2021-01-20 14:27:53 +00:00
|
|
|
|
2021-04-02 14:30:39 +00:00
|
|
|
func (c *searchCriteria) ctDomainOrClientCase(e *logEntry) bool {
|
|
|
|
clientID := e.ClientID
|
|
|
|
host := e.QHost
|
2020-05-26 12:37:37 +00:00
|
|
|
|
2021-04-02 14:30:39 +00:00
|
|
|
var name string
|
|
|
|
if e.client != nil {
|
|
|
|
name = e.client.Name
|
2020-11-20 14:32:41 +00:00
|
|
|
}
|
2021-01-20 14:27:53 +00:00
|
|
|
|
2021-04-02 14:30:39 +00:00
|
|
|
ip := e.IP.String()
|
|
|
|
term := strings.ToLower(c.value)
|
|
|
|
if c.strict {
|
|
|
|
return c.ctDomainOrClientCaseStrict(term, clientID, name, host, ip)
|
2020-11-20 14:32:41 +00:00
|
|
|
}
|
2021-01-20 14:27:53 +00:00
|
|
|
|
2021-04-02 14:30:39 +00:00
|
|
|
// TODO(a.garipov): Write a case-insensitive version of strings.Contains
|
|
|
|
// instead of generating garbage. Or, perhaps in the future, use
|
|
|
|
// a locale-appropriate matcher from golang.org/x/text.
|
|
|
|
clientID = strings.ToLower(clientID)
|
|
|
|
host = strings.ToLower(host)
|
|
|
|
ip = strings.ToLower(ip)
|
|
|
|
name = strings.ToLower(name)
|
|
|
|
term = strings.ToLower(term)
|
|
|
|
|
|
|
|
return strings.Contains(clientID, term) ||
|
|
|
|
strings.Contains(host, term) ||
|
|
|
|
strings.Contains(ip, term) ||
|
|
|
|
strings.Contains(name, term)
|
2020-05-26 12:37:37 +00:00
|
|
|
}
|
2020-11-20 14:32:41 +00: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 14:48:07 +00:00
|
|
|
dnsfilter.NotFilteredAllowList,
|
2020-12-28 15:41:50 +00:00
|
|
|
dnsfilter.Rewritten,
|
|
|
|
dnsfilter.RewrittenAutoHosts,
|
|
|
|
dnsfilter.RewrittenRule,
|
2020-11-20 14:32:41 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
case filteringStatusBlocked:
|
|
|
|
return res.IsFiltered &&
|
2020-12-21 14:48:07 +00:00
|
|
|
res.Reason.In(dnsfilter.FilteredBlockList, dnsfilter.FilteredBlockedService)
|
2020-11-20 14:32:41 +00: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 14:48:07 +00:00
|
|
|
return res.Reason == dnsfilter.NotFilteredAllowList
|
2020-11-20 14:32:41 +00:00
|
|
|
|
|
|
|
case filteringStatusRewritten:
|
2020-12-28 15:41:50 +00:00
|
|
|
return res.Reason.In(
|
|
|
|
dnsfilter.Rewritten,
|
|
|
|
dnsfilter.RewrittenAutoHosts,
|
|
|
|
dnsfilter.RewrittenRule,
|
|
|
|
)
|
2020-11-20 14:32:41 +00:00
|
|
|
|
|
|
|
case filteringStatusSafeSearch:
|
|
|
|
return res.IsFiltered && res.Reason == dnsfilter.FilteredSafeSearch
|
|
|
|
|
|
|
|
case filteringStatusProcessed:
|
|
|
|
return !res.Reason.In(
|
2020-12-21 14:48:07 +00:00
|
|
|
dnsfilter.FilteredBlockList,
|
2020-11-20 14:32:41 +00:00
|
|
|
dnsfilter.FilteredBlockedService,
|
2020-12-21 14:48:07 +00:00
|
|
|
dnsfilter.NotFilteredAllowList,
|
2020-11-20 14:32:41 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
default:
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
}
|