2020-05-26 15:37:37 +03:00
|
|
|
package querylog
|
|
|
|
|
|
|
|
import (
|
2024-11-21 20:19:39 +03:00
|
|
|
"context"
|
2023-02-27 17:18:56 +03:00
|
|
|
"fmt"
|
2024-11-21 20:19:39 +03:00
|
|
|
"log/slog"
|
2020-05-26 15:37:37 +03:00
|
|
|
"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.
|
2024-11-21 20:19:39 +03:00
|
|
|
func (c *searchCriterion) quickMatch(
|
|
|
|
ctx context.Context,
|
|
|
|
logger *slog.Logger,
|
|
|
|
line string,
|
|
|
|
findClient quickMatchClientFunc,
|
|
|
|
) (ok bool) {
|
2021-04-12 18:22:11 +03:00
|
|
|
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
|
2024-11-21 20:19:39 +03:00
|
|
|
if cli := findClient(ctx, logger, clientID, ip); cli != nil {
|
2021-04-12 18:22:11 +03:00
|
|
|
name = cli.Name
|
|
|
|
}
|
|
|
|
|
|
|
|
if c.strict {
|
2022-03-03 17:52:11 +03:00
|
|
|
return ctDomainOrClientCaseStrict(c.value, c.asciiVal, clientID, name, host, ip)
|
2021-04-12 18:22:11 +03:00
|
|
|
}
|
|
|
|
|
2022-03-03 17:52:11 +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:
|
2023-02-27 17:18:56 +03:00
|
|
|
return c.ctFilteringStatusCase(entry.Result.Reason, entry.Result.IsFiltered)
|
2020-11-20 17:32:41 +03:00
|
|
|
}
|
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
|
|
|
|
2023-02-27 17:18:56 +03:00
|
|
|
// ctFilteringStatusCase returns true if the result matches the value.
|
|
|
|
func (c *searchCriterion) ctFilteringStatusCase(
|
|
|
|
reason filtering.Reason,
|
|
|
|
isFiltered bool,
|
|
|
|
) (matched bool) {
|
2020-11-20 17:32:41 +03:00
|
|
|
switch c.value {
|
|
|
|
case filteringStatusAll:
|
|
|
|
return true
|
2023-03-26 14:19:19 +03:00
|
|
|
case filteringStatusFiltered:
|
|
|
|
return isFiltered || reason.In(
|
|
|
|
filtering.NotFilteredAllowList,
|
|
|
|
filtering.Rewritten,
|
|
|
|
filtering.RewrittenAutoHosts,
|
|
|
|
filtering.RewrittenRule,
|
|
|
|
)
|
2023-02-27 17:18:56 +03:00
|
|
|
case
|
|
|
|
filteringStatusBlocked,
|
|
|
|
filteringStatusBlockedParental,
|
|
|
|
filteringStatusBlockedSafebrowsing,
|
|
|
|
filteringStatusBlockedService,
|
|
|
|
filteringStatusSafeSearch:
|
|
|
|
return isFiltered && c.isFilteredWithReason(reason)
|
2020-11-20 17:32:41 +03:00
|
|
|
case filteringStatusWhitelisted:
|
2023-02-27 17:18:56 +03:00
|
|
|
return reason == filtering.NotFilteredAllowList
|
2020-11-20 17:32:41 +03:00
|
|
|
case filteringStatusRewritten:
|
2023-02-27 17:18:56 +03:00
|
|
|
return 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 filteringStatusProcessed:
|
2023-02-27 17:18:56 +03:00
|
|
|
return !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
|
|
|
|
}
|
|
|
|
}
|
2023-02-27 17:18:56 +03:00
|
|
|
|
|
|
|
// isFilteredWithReason returns true if reason matches the criterion value.
|
|
|
|
// c.value must be one of:
|
|
|
|
//
|
|
|
|
// - filteringStatusBlocked
|
|
|
|
// - filteringStatusBlockedParental
|
|
|
|
// - filteringStatusBlockedSafebrowsing
|
|
|
|
// - filteringStatusBlockedService
|
|
|
|
// - filteringStatusSafeSearch
|
|
|
|
func (c *searchCriterion) isFilteredWithReason(reason filtering.Reason) (matched bool) {
|
|
|
|
switch c.value {
|
|
|
|
case filteringStatusBlocked:
|
|
|
|
return reason.In(filtering.FilteredBlockList, filtering.FilteredBlockedService)
|
|
|
|
case filteringStatusBlockedParental:
|
|
|
|
return reason == filtering.FilteredParental
|
|
|
|
case filteringStatusBlockedSafebrowsing:
|
|
|
|
return reason == filtering.FilteredSafeBrowsing
|
|
|
|
case filteringStatusBlockedService:
|
|
|
|
return reason == filtering.FilteredBlockedService
|
|
|
|
case filteringStatusSafeSearch:
|
|
|
|
return reason == filtering.FilteredSafeSearch
|
|
|
|
default:
|
|
|
|
panic(fmt.Errorf("unexpected value %q", c.value))
|
|
|
|
}
|
|
|
|
}
|