2020-05-26 15:37:37 +03:00
|
|
|
package querylog
|
|
|
|
|
|
|
|
import (
|
|
|
|
"strings"
|
|
|
|
|
2021-05-21 16:15:47 +03:00
|
|
|
"github.com/AdguardTeam/AdGuardHome/internal/filtering"
|
2021-08-13 19:20:17 +03:00
|
|
|
"github.com/AdguardTeam/golibs/stringutil"
|
2020-05-26 15:37:37 +03:00
|
|
|
)
|
|
|
|
|
2021-04-12 18:22:11 +03:00
|
|
|
type criterionType int
|
2020-05-26 15:37:37 +03:00
|
|
|
|
|
|
|
const (
|
2021-06-30 11:04:48 +03:00
|
|
|
// ctTerm is for searching by the domain name, the client's IP address,
|
|
|
|
// the client's ID or the client's name. The domain name search
|
|
|
|
// supports IDNAs.
|
2021-06-29 13:36:52 +03:00
|
|
|
ctTerm criterionType = iota
|
2021-01-27 18:32:13 +03:00
|
|
|
// ctFilteringStatus is for searching by the filtering status.
|
|
|
|
//
|
2021-04-12 18:22:11 +03:00
|
|
|
// See (*searchCriterion).ctFilteringStatusCase for details.
|
2021-01-27 18:32:13 +03:00
|
|
|
ctFilteringStatus
|
2020-05-26 15:37:37 +03:00
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
|
|
|
filteringStatusAll = "all"
|
|
|
|
filteringStatusFiltered = "filtered" // all kinds of filtering
|
|
|
|
|
2020-09-23 19:47:02 +03:00
|
|
|
filteringStatusBlocked = "blocked" // blocked or blocked services
|
|
|
|
filteringStatusBlockedService = "blocked_services" // blocked
|
2020-05-26 15:37:37 +03: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 17:14:50 +03:00
|
|
|
filteringStatusProcessed = "processed" // not blocked, not white-listed entries
|
2020-05-26 15:37:37 +03:00
|
|
|
)
|
|
|
|
|
|
|
|
// filteringStatusValues -- array with all possible filteringStatus values
|
|
|
|
var filteringStatusValues = []string{
|
|
|
|
filteringStatusAll, filteringStatusFiltered, filteringStatusBlocked,
|
2020-09-23 19:47:02 +03:00
|
|
|
filteringStatusBlockedService, filteringStatusBlockedSafebrowsing, filteringStatusBlockedParental,
|
2020-05-26 15:37:37 +03:00
|
|
|
filteringStatusWhitelisted, filteringStatusRewritten, filteringStatusSafeSearch,
|
2020-05-28 17:14:50 +03:00
|
|
|
filteringStatusProcessed,
|
2020-05-26 15:37:37 +03:00
|
|
|
}
|
|
|
|
|
2021-04-12 18:22:11 +03:00
|
|
|
// searchCriterion is a search criterion that is used to match a record.
|
|
|
|
type searchCriterion struct {
|
|
|
|
value string
|
2021-06-30 11:04:48 +03:00
|
|
|
asciiVal string
|
2021-04-12 18:22:11 +03:00
|
|
|
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 15:37:37 +03:00
|
|
|
}
|
|
|
|
|
2021-06-30 11:04:48 +03:00
|
|
|
func ctDomainOrClientCaseStrict(
|
2021-04-12 18:22:11 +03:00
|
|
|
term string,
|
2021-06-30 11:04:48 +03:00
|
|
|
asciiTerm string,
|
2021-04-12 18:22:11 +03:00
|
|
|
clientID string,
|
|
|
|
name string,
|
|
|
|
host string,
|
|
|
|
ip string,
|
|
|
|
) (ok bool) {
|
|
|
|
return strings.EqualFold(host, term) ||
|
2021-06-30 11:04:48 +03:00
|
|
|
(asciiTerm != "" && strings.EqualFold(host, asciiTerm)) ||
|
2021-04-12 18:22:11 +03:00
|
|
|
strings.EqualFold(clientID, term) ||
|
|
|
|
strings.EqualFold(ip, term) ||
|
|
|
|
strings.EqualFold(name, term)
|
|
|
|
}
|
|
|
|
|
2021-06-30 11:04:48 +03:00
|
|
|
func ctDomainOrClientCaseNonStrict(
|
2021-04-12 18:22:11 +03:00
|
|
|
term string,
|
2021-06-30 11:04:48 +03:00
|
|
|
asciiTerm string,
|
2021-04-12 18:22:11 +03:00
|
|
|
clientID string,
|
|
|
|
name string,
|
|
|
|
host string,
|
|
|
|
ip string,
|
|
|
|
) (ok bool) {
|
2021-08-13 19:20:17 +03:00
|
|
|
return stringutil.ContainsFold(clientID, term) ||
|
|
|
|
stringutil.ContainsFold(host, term) ||
|
|
|
|
(asciiTerm != "" && stringutil.ContainsFold(host, asciiTerm)) ||
|
|
|
|
stringutil.ContainsFold(ip, term) ||
|
|
|
|
stringutil.ContainsFold(name, term)
|
2021-04-12 18:22:11 +03: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
|
2021-12-27 19:40:39 +03:00
|
|
|
// optimization purposes.
|
2021-04-12 18:22:11 +03:00
|
|
|
func (c *searchCriterion) quickMatch(line string, findClient quickMatchClientFunc) (ok bool) {
|
|
|
|
switch c.criterionType {
|
2021-06-29 13:36:52 +03:00
|
|
|
case ctTerm:
|
2021-04-12 18:22:11 +03:00
|
|
|
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 {
|
2021-06-30 11:04:48 +03:00
|
|
|
return ctDomainOrClientCaseStrict(
|
|
|
|
c.value,
|
|
|
|
c.asciiVal,
|
|
|
|
clientID,
|
|
|
|
name,
|
|
|
|
host,
|
|
|
|
ip,
|
|
|
|
)
|
2021-04-12 18:22:11 +03:00
|
|
|
}
|
|
|
|
|
2021-06-30 11:04:48 +03:00
|
|
|
return ctDomainOrClientCaseNonStrict(
|
|
|
|
c.value,
|
|
|
|
c.asciiVal,
|
|
|
|
clientID,
|
|
|
|
name,
|
|
|
|
host,
|
|
|
|
ip,
|
|
|
|
)
|
2021-04-12 18:22:11 +03:00
|
|
|
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 {
|
2021-06-29 13:36:52 +03:00
|
|
|
case ctTerm:
|
2020-11-20 17:32:41 +03:00
|
|
|
return c.ctDomainOrClientCase(entry)
|
|
|
|
case ctFilteringStatus:
|
|
|
|
return c.ctFilteringStatusCase(entry.Result)
|
|
|
|
}
|
2020-05-28 17:14:50 +03:00
|
|
|
|
2020-11-20 17:32:41 +03:00
|
|
|
return false
|
|
|
|
}
|
2020-05-28 17:14:50 +03:00
|
|
|
|
2021-04-12 18:22:11 +03:00
|
|
|
func (c *searchCriterion) ctDomainOrClientCase(e *logEntry) bool {
|
2021-04-02 17:30:39 +03:00
|
|
|
clientID := e.ClientID
|
|
|
|
host := e.QHost
|
2020-05-26 15:37:37 +03:00
|
|
|
|
2021-04-02 17:30:39 +03:00
|
|
|
var name string
|
|
|
|
if e.client != nil {
|
|
|
|
name = e.client.Name
|
2020-11-20 17:32:41 +03:00
|
|
|
}
|
2021-01-20 17:27:53 +03:00
|
|
|
|
2021-04-02 17:30:39 +03:00
|
|
|
ip := e.IP.String()
|
|
|
|
if c.strict {
|
2021-06-30 11:04:48 +03:00
|
|
|
return ctDomainOrClientCaseStrict(c.value, c.asciiVal, clientID, name, host, ip)
|
2020-11-20 17:32:41 +03:00
|
|
|
}
|
2021-01-20 17:27:53 +03:00
|
|
|
|
2021-06-30 11:04:48 +03:00
|
|
|
return ctDomainOrClientCaseNonStrict(c.value, c.asciiVal, clientID, name, host, ip)
|
2020-05-26 15:37:37 +03:00
|
|
|
}
|
2020-11-20 17:32:41 +03:00
|
|
|
|
2021-05-21 16:15:47 +03:00
|
|
|
func (c *searchCriterion) ctFilteringStatusCase(res filtering.Result) bool {
|
2020-11-20 17:32:41 +03:00
|
|
|
switch c.value {
|
|
|
|
case filteringStatusAll:
|
|
|
|
return true
|
|
|
|
|
|
|
|
case filteringStatusFiltered:
|
|
|
|
return res.IsFiltered ||
|
|
|
|
res.Reason.In(
|
2021-05-21 16:15:47 +03:00
|
|
|
filtering.NotFilteredAllowList,
|
|
|
|
filtering.Rewritten,
|
|
|
|
filtering.RewrittenAutoHosts,
|
|
|
|
filtering.RewrittenRule,
|
2020-11-20 17:32:41 +03:00
|
|
|
)
|
|
|
|
|
|
|
|
case filteringStatusBlocked:
|
|
|
|
return res.IsFiltered &&
|
2021-05-21 16:15:47 +03:00
|
|
|
res.Reason.In(filtering.FilteredBlockList, filtering.FilteredBlockedService)
|
2020-11-20 17:32:41 +03:00
|
|
|
|
|
|
|
case filteringStatusBlockedService:
|
2021-05-21 16:15:47 +03:00
|
|
|
return res.IsFiltered && res.Reason == filtering.FilteredBlockedService
|
2020-11-20 17:32:41 +03:00
|
|
|
|
|
|
|
case filteringStatusBlockedParental:
|
2021-05-21 16:15:47 +03:00
|
|
|
return res.IsFiltered && res.Reason == filtering.FilteredParental
|
2020-11-20 17:32:41 +03:00
|
|
|
|
|
|
|
case filteringStatusBlockedSafebrowsing:
|
2021-05-21 16:15:47 +03:00
|
|
|
return res.IsFiltered && res.Reason == filtering.FilteredSafeBrowsing
|
2020-11-20 17:32:41 +03:00
|
|
|
|
|
|
|
case filteringStatusWhitelisted:
|
2021-05-21 16:15:47 +03:00
|
|
|
return res.Reason == filtering.NotFilteredAllowList
|
2020-11-20 17:32:41 +03:00
|
|
|
|
|
|
|
case filteringStatusRewritten:
|
2020-12-28 18:41:50 +03:00
|
|
|
return res.Reason.In(
|
2021-05-21 16:15:47 +03:00
|
|
|
filtering.Rewritten,
|
|
|
|
filtering.RewrittenAutoHosts,
|
|
|
|
filtering.RewrittenRule,
|
2020-12-28 18:41:50 +03:00
|
|
|
)
|
2020-11-20 17:32:41 +03:00
|
|
|
|
|
|
|
case filteringStatusSafeSearch:
|
2021-05-21 16:15:47 +03:00
|
|
|
return res.IsFiltered && res.Reason == filtering.FilteredSafeSearch
|
2020-11-20 17:32:41 +03:00
|
|
|
|
|
|
|
case filteringStatusProcessed:
|
|
|
|
return !res.Reason.In(
|
2021-05-21 16:15:47 +03:00
|
|
|
filtering.FilteredBlockList,
|
|
|
|
filtering.FilteredBlockedService,
|
|
|
|
filtering.NotFilteredAllowList,
|
2020-11-20 17:32:41 +03:00
|
|
|
)
|
|
|
|
|
|
|
|
default:
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
}
|