2020-05-26 05:37:37 -07:00
|
|
|
package querylog
|
|
|
|
|
|
|
|
import (
|
|
|
|
"strings"
|
2021-05-20 03:42:35 -07:00
|
|
|
"unicode"
|
|
|
|
"unicode/utf8"
|
2020-05-26 05:37:37 -07:00
|
|
|
|
2021-05-21 06:15:47 -07:00
|
|
|
"github.com/AdguardTeam/AdGuardHome/internal/filtering"
|
2020-05-26 05:37:37 -07:00
|
|
|
)
|
|
|
|
|
2021-04-12 08:22:11 -07:00
|
|
|
type criterionType int
|
2020-05-26 05:37:37 -07:00
|
|
|
|
|
|
|
const (
|
2021-01-27 08:32:13 -07:00
|
|
|
// ctDomainOrClient is for searching by the domain name, the client's IP
|
|
|
|
// address, or the clinet's ID.
|
2021-04-12 08:22:11 -07:00
|
|
|
ctDomainOrClient criterionType = iota
|
2021-01-27 08:32:13 -07:00
|
|
|
// ctFilteringStatus is for searching by the filtering status.
|
|
|
|
//
|
2021-04-12 08:22:11 -07:00
|
|
|
// See (*searchCriterion).ctFilteringStatusCase for details.
|
2021-01-27 08:32:13 -07:00
|
|
|
ctFilteringStatus
|
2020-05-26 05:37:37 -07:00
|
|
|
)
|
|
|
|
|
|
|
|
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
|
|
|
}
|
|
|
|
|
2021-04-12 08:22:11 -07:00
|
|
|
// searchCriterion is a search criterion that is used to match a record.
|
|
|
|
type searchCriterion struct {
|
|
|
|
value string
|
|
|
|
criterionType criterionType
|
|
|
|
// strict, if true, means that the criterion must be applied to the
|
|
|
|
// whole value rather than the part of it. That is, equality and not
|
|
|
|
// containment.
|
|
|
|
strict bool
|
2020-05-26 05:37:37 -07:00
|
|
|
}
|
|
|
|
|
2021-04-12 08:22:11 -07:00
|
|
|
func (c *searchCriterion) ctDomainOrClientCaseStrict(
|
|
|
|
term string,
|
|
|
|
clientID string,
|
|
|
|
name string,
|
|
|
|
host string,
|
|
|
|
ip string,
|
|
|
|
) (ok bool) {
|
|
|
|
return strings.EqualFold(host, term) ||
|
|
|
|
strings.EqualFold(clientID, term) ||
|
|
|
|
strings.EqualFold(ip, term) ||
|
|
|
|
strings.EqualFold(name, term)
|
|
|
|
}
|
|
|
|
|
2021-05-20 03:42:35 -07:00
|
|
|
// containsFold reports whehter s contains, ignoring letter case, substr.
|
|
|
|
//
|
|
|
|
// TODO(a.garipov): Move to aghstrings if needed elsewhere.
|
|
|
|
func containsFold(s, substr string) (ok bool) {
|
|
|
|
sLen, substrLen := len(s), len(substr)
|
|
|
|
if sLen < substrLen {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
if sLen == substrLen {
|
|
|
|
return strings.EqualFold(s, substr)
|
|
|
|
}
|
|
|
|
|
|
|
|
first, _ := utf8.DecodeRuneInString(substr)
|
|
|
|
firstFolded := unicode.SimpleFold(first)
|
|
|
|
|
|
|
|
for i := 0; i != -1 && len(s) >= len(substr); {
|
|
|
|
if strings.EqualFold(s[:substrLen], substr) {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
|
|
|
i = strings.IndexFunc(s[1:], func(r rune) (eq bool) {
|
|
|
|
return r == first || r == firstFolded
|
|
|
|
})
|
|
|
|
|
|
|
|
s = s[1+i:]
|
|
|
|
}
|
|
|
|
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
2021-04-12 08:22:11 -07:00
|
|
|
func (c *searchCriterion) ctDomainOrClientCaseNonStrict(
|
|
|
|
term string,
|
|
|
|
clientID string,
|
|
|
|
name string,
|
|
|
|
host string,
|
|
|
|
ip string,
|
|
|
|
) (ok bool) {
|
2021-05-20 03:42:35 -07:00
|
|
|
return containsFold(clientID, term) ||
|
|
|
|
containsFold(host, term) ||
|
|
|
|
containsFold(ip, term) ||
|
|
|
|
containsFold(name, term)
|
2021-04-12 08:22:11 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
// quickMatch quickly checks if the line matches the given search criterion.
|
|
|
|
// It returns false if the like doesn't match. This method is only here for
|
|
|
|
// optimisation purposes.
|
|
|
|
func (c *searchCriterion) quickMatch(line string, findClient quickMatchClientFunc) (ok bool) {
|
|
|
|
switch c.criterionType {
|
|
|
|
case ctDomainOrClient:
|
|
|
|
host := readJSONValue(line, `"QH":"`)
|
|
|
|
ip := readJSONValue(line, `"IP":"`)
|
|
|
|
clientID := readJSONValue(line, `"CID":"`)
|
|
|
|
|
|
|
|
var name string
|
|
|
|
if cli := findClient(clientID, ip); cli != nil {
|
|
|
|
name = cli.Name
|
|
|
|
}
|
|
|
|
|
|
|
|
if c.strict {
|
|
|
|
return c.ctDomainOrClientCaseStrict(c.value, clientID, name, host, ip)
|
|
|
|
}
|
|
|
|
|
|
|
|
return c.ctDomainOrClientCaseNonStrict(c.value, clientID, name, host, ip)
|
|
|
|
case ctFilteringStatus:
|
|
|
|
// Go on, as we currently don't do quick matches against
|
|
|
|
// filtering statuses.
|
|
|
|
return true
|
|
|
|
default:
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// match checks if the log entry matches this search criterion.
|
|
|
|
func (c *searchCriterion) match(entry *logEntry) bool {
|
|
|
|
switch c.criterionType {
|
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
|
|
|
|
2021-04-12 08:22:11 -07:00
|
|
|
func (c *searchCriterion) ctDomainOrClientCase(e *logEntry) bool {
|
2021-04-02 07:30:39 -07:00
|
|
|
clientID := e.ClientID
|
|
|
|
host := e.QHost
|
2020-05-26 05:37:37 -07:00
|
|
|
|
2021-04-02 07:30:39 -07:00
|
|
|
var name string
|
|
|
|
if e.client != nil {
|
|
|
|
name = e.client.Name
|
2020-11-20 07:32:41 -07:00
|
|
|
}
|
2021-01-20 07:27:53 -07:00
|
|
|
|
2021-04-02 07:30:39 -07:00
|
|
|
ip := e.IP.String()
|
|
|
|
term := strings.ToLower(c.value)
|
|
|
|
if c.strict {
|
|
|
|
return c.ctDomainOrClientCaseStrict(term, clientID, name, host, ip)
|
2020-11-20 07:32:41 -07:00
|
|
|
}
|
2021-01-20 07:27:53 -07:00
|
|
|
|
2021-04-12 08:22:11 -07:00
|
|
|
return c.ctDomainOrClientCaseNonStrict(term, clientID, name, host, ip)
|
2020-05-26 05:37:37 -07:00
|
|
|
}
|
2020-11-20 07:32:41 -07:00
|
|
|
|
2021-05-21 06:15:47 -07:00
|
|
|
func (c *searchCriterion) ctFilteringStatusCase(res filtering.Result) bool {
|
2020-11-20 07:32:41 -07:00
|
|
|
switch c.value {
|
|
|
|
case filteringStatusAll:
|
|
|
|
return true
|
|
|
|
|
|
|
|
case filteringStatusFiltered:
|
|
|
|
return res.IsFiltered ||
|
|
|
|
res.Reason.In(
|
2021-05-21 06:15:47 -07:00
|
|
|
filtering.NotFilteredAllowList,
|
|
|
|
filtering.Rewritten,
|
|
|
|
filtering.RewrittenAutoHosts,
|
|
|
|
filtering.RewrittenRule,
|
2020-11-20 07:32:41 -07:00
|
|
|
)
|
|
|
|
|
|
|
|
case filteringStatusBlocked:
|
|
|
|
return res.IsFiltered &&
|
2021-05-21 06:15:47 -07:00
|
|
|
res.Reason.In(filtering.FilteredBlockList, filtering.FilteredBlockedService)
|
2020-11-20 07:32:41 -07:00
|
|
|
|
|
|
|
case filteringStatusBlockedService:
|
2021-05-21 06:15:47 -07:00
|
|
|
return res.IsFiltered && res.Reason == filtering.FilteredBlockedService
|
2020-11-20 07:32:41 -07:00
|
|
|
|
|
|
|
case filteringStatusBlockedParental:
|
2021-05-21 06:15:47 -07:00
|
|
|
return res.IsFiltered && res.Reason == filtering.FilteredParental
|
2020-11-20 07:32:41 -07:00
|
|
|
|
|
|
|
case filteringStatusBlockedSafebrowsing:
|
2021-05-21 06:15:47 -07:00
|
|
|
return res.IsFiltered && res.Reason == filtering.FilteredSafeBrowsing
|
2020-11-20 07:32:41 -07:00
|
|
|
|
|
|
|
case filteringStatusWhitelisted:
|
2021-05-21 06:15:47 -07:00
|
|
|
return res.Reason == filtering.NotFilteredAllowList
|
2020-11-20 07:32:41 -07:00
|
|
|
|
|
|
|
case filteringStatusRewritten:
|
2020-12-28 08:41:50 -07:00
|
|
|
return res.Reason.In(
|
2021-05-21 06:15:47 -07:00
|
|
|
filtering.Rewritten,
|
|
|
|
filtering.RewrittenAutoHosts,
|
|
|
|
filtering.RewrittenRule,
|
2020-12-28 08:41:50 -07:00
|
|
|
)
|
2020-11-20 07:32:41 -07:00
|
|
|
|
|
|
|
case filteringStatusSafeSearch:
|
2021-05-21 06:15:47 -07:00
|
|
|
return res.IsFiltered && res.Reason == filtering.FilteredSafeSearch
|
2020-11-20 07:32:41 -07:00
|
|
|
|
|
|
|
case filteringStatusProcessed:
|
|
|
|
return !res.Reason.In(
|
2021-05-21 06:15:47 -07:00
|
|
|
filtering.FilteredBlockList,
|
|
|
|
filtering.FilteredBlockedService,
|
|
|
|
filtering.NotFilteredAllowList,
|
2020-11-20 07:32:41 -07:00
|
|
|
)
|
|
|
|
|
|
|
|
default:
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
}
|