2021-05-21 16:15:47 +03:00
|
|
|
package filtering
|
2018-08-30 17:25:33 +03:00
|
|
|
|
2023-04-06 14:12:50 +03:00
|
|
|
import "github.com/miekg/dns"
|
2020-12-07 15:38:05 +03:00
|
|
|
|
2023-03-07 17:52:03 +03:00
|
|
|
// SafeSearch interface describes a service for search engines hosts rewrites.
|
|
|
|
type SafeSearch interface {
|
2023-04-06 14:12:50 +03:00
|
|
|
// CheckHost checks host with safe search filter. CheckHost must be safe
|
|
|
|
// for concurrent use. qtype must be either [dns.TypeA] or [dns.TypeAAAA].
|
2023-03-07 17:52:03 +03:00
|
|
|
CheckHost(host string, qtype uint16) (res Result, err error)
|
2023-04-06 14:12:50 +03:00
|
|
|
|
|
|
|
// Update updates the configuration of the safe search filter. Update must
|
|
|
|
// be safe for concurrent use. An implementation of Update may ignore some
|
|
|
|
// fields, but it must document which.
|
|
|
|
Update(conf SafeSearchConfig) (err error)
|
2023-03-07 17:52:03 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// SafeSearchConfig is a struct with safe search related settings.
|
|
|
|
type SafeSearchConfig struct {
|
|
|
|
// CustomResolver is the resolver used by safe search.
|
2023-03-23 15:25:58 +03:00
|
|
|
CustomResolver Resolver `yaml:"-" json:"-"`
|
2023-03-07 17:52:03 +03:00
|
|
|
|
|
|
|
// Enabled indicates if safe search is enabled entirely.
|
|
|
|
Enabled bool `yaml:"enabled" json:"enabled"`
|
|
|
|
|
|
|
|
// Services flags. Each flag indicates if the corresponding service is
|
|
|
|
// enabled or disabled.
|
|
|
|
|
|
|
|
Bing bool `yaml:"bing" json:"bing"`
|
|
|
|
DuckDuckGo bool `yaml:"duckduckgo" json:"duckduckgo"`
|
|
|
|
Google bool `yaml:"google" json:"google"`
|
|
|
|
Pixabay bool `yaml:"pixabay" json:"pixabay"`
|
|
|
|
Yandex bool `yaml:"yandex" json:"yandex"`
|
|
|
|
YouTube bool `yaml:"youtube" json:"youtube"`
|
|
|
|
}
|
|
|
|
|
2023-03-15 14:31:07 +03:00
|
|
|
// checkSafeSearch checks host with safe search engine. Matches
|
|
|
|
// [hostChecker.check].
|
2021-03-25 20:30:30 +03:00
|
|
|
func (d *DNSFilter) checkSafeSearch(
|
|
|
|
host string,
|
2023-04-06 14:12:50 +03:00
|
|
|
qtype uint16,
|
2021-05-21 16:15:47 +03:00
|
|
|
setts *Settings,
|
2021-03-25 20:30:30 +03:00
|
|
|
) (res Result, err error) {
|
2023-04-06 14:12:50 +03:00
|
|
|
if !setts.ProtectionEnabled ||
|
|
|
|
!setts.SafeSearchEnabled ||
|
|
|
|
(qtype != dns.TypeA && qtype != dns.TypeAAAA) {
|
2021-03-25 20:30:30 +03:00
|
|
|
return Result{}, nil
|
|
|
|
}
|
|
|
|
|
2023-03-15 14:31:07 +03:00
|
|
|
if d.safeSearch == nil {
|
2020-12-07 15:38:05 +03:00
|
|
|
return Result{}, nil
|
|
|
|
}
|
|
|
|
|
2023-03-15 14:31:07 +03:00
|
|
|
clientSafeSearch := setts.ClientSafeSearch
|
|
|
|
if clientSafeSearch != nil {
|
2023-04-06 14:12:50 +03:00
|
|
|
return clientSafeSearch.CheckHost(host, qtype)
|
2020-12-07 15:38:05 +03:00
|
|
|
}
|
|
|
|
|
2023-04-06 14:12:50 +03:00
|
|
|
return d.safeSearch.CheckHost(host, qtype)
|
2018-08-30 17:25:33 +03:00
|
|
|
}
|