all: imp code

This commit is contained in:
Stanislav Chzhen 2024-10-03 20:24:48 +03:00
parent 792975e248
commit 038bae59d5
7 changed files with 36 additions and 22 deletions

View file

@ -516,7 +516,7 @@ func TestSafeSearch(t *testing.T) {
ctx := testutil.ContextWithTimeout(t, testTimeout)
safeSearch, err := safesearch.NewDefault(ctx, &safesearch.DefaultConfig{
BaseLogger: slogutil.NewDiscardLogger(),
Logger: slogutil.NewDiscardLogger(),
ServicesConfig: safeSearchConf,
CacheSize: filterConf.SafeSearchCacheSize,
CacheTTL: time.Minute * time.Duration(filterConf.CacheTime),

View file

@ -24,6 +24,12 @@ import (
"github.com/miekg/dns"
)
// Attribute keys and values for logging.
const (
LogPrefix = "safesearch"
LogKey = "client"
)
// Service is a enum with service names used as search providers.
type Service string
@ -62,8 +68,8 @@ func isServiceProtected(s filtering.SafeSearchConfig, service Service) (ok bool)
// DefaultConfig is the configuration structure for [Default].
type DefaultConfig struct {
// BaseLogger is used to create logger for [Default].
BaseLogger *slog.Logger
// Logger is used for logging the operation of the safe search filter.
Logger *slog.Logger
// ClientName is the name of the persistent client associated with the safe
// search filter, if there is one.
@ -93,20 +99,17 @@ type Default struct {
// engine may be nil, which means that this safe search filter is disabled.
engine *urlfilter.DNSEngine
// cache stores safe search filtering results.
cache cache.Cache
// cacheTTL is the Time to Live duration for cached items.
cacheTTL time.Duration
}
// NewDefault returns an initialized default safe search filter. name is used
// for logging.
// NewDefault returns an initialized default safe search filter.
func NewDefault(ctx context.Context, conf *DefaultConfig) (ss *Default, err error) {
logger := conf.BaseLogger.With(slogutil.KeyPrefix, "safesearch")
if conf.ClientName != "" {
logger = logger.With("client", conf.ClientName)
}
ss = &Default{
logger: logger,
logger: conf.Logger,
mu: &sync.RWMutex{},
cache: cache.New(cache.Config{
EnableLRU: true,
@ -132,7 +135,7 @@ func (ss *Default) resetEngine(
conf filtering.SafeSearchConfig,
) (err error) {
if !conf.Enabled {
ss.logger.InfoContext(ctx, "disabled")
ss.logger.DebugContext(ctx, "disabled")
return nil
}

View file

@ -41,7 +41,7 @@ var yandexIP = netip.AddrFrom4([4]byte{213, 180, 193, 56})
func newForTest(t testing.TB, ssConf filtering.SafeSearchConfig) (ss *Default) {
ss, err := NewDefault(testutil.ContextWithTimeout(t, testTimeout), &DefaultConfig{
BaseLogger: slogutil.NewDiscardLogger(),
Logger: slogutil.NewDiscardLogger(),
ServicesConfig: ssConf,
CacheSize: testCacheSize,
CacheTTL: testCacheTTL,

View file

@ -49,7 +49,7 @@ func TestDefault_CheckHost_yandex(t *testing.T) {
conf := testConf
ctx := testutil.ContextWithTimeout(t, testTimeout)
ss, err := safesearch.NewDefault(ctx, &safesearch.DefaultConfig{
BaseLogger: slogutil.NewDiscardLogger(),
Logger: slogutil.NewDiscardLogger(),
ServicesConfig: conf,
CacheSize: testCacheSize,
CacheTTL: testCacheTTL,
@ -111,7 +111,7 @@ func TestDefault_CheckHost_yandex(t *testing.T) {
func TestDefault_CheckHost_google(t *testing.T) {
ctx := testutil.ContextWithTimeout(t, testTimeout)
ss, err := safesearch.NewDefault(ctx, &safesearch.DefaultConfig{
BaseLogger: slogutil.NewDiscardLogger(),
Logger: slogutil.NewDiscardLogger(),
ServicesConfig: testConf,
CacheSize: testCacheSize,
CacheTTL: testCacheTTL,
@ -163,7 +163,7 @@ func (r *testResolver) LookupIP(
func TestDefault_CheckHost_duckduckgoAAAA(t *testing.T) {
ctx := testutil.ContextWithTimeout(t, testTimeout)
ss, err := safesearch.NewDefault(ctx, &safesearch.DefaultConfig{
BaseLogger: slogutil.NewDiscardLogger(),
Logger: slogutil.NewDiscardLogger(),
ServicesConfig: testConf,
CacheSize: testCacheSize,
CacheTTL: testCacheTTL,
@ -186,7 +186,7 @@ func TestDefault_Update(t *testing.T) {
conf := testConf
ctx := testutil.ContextWithTimeout(t, testTimeout)
ss, err := safesearch.NewDefault(ctx, &safesearch.DefaultConfig{
BaseLogger: slogutil.NewDiscardLogger(),
Logger: slogutil.NewDiscardLogger(),
ServicesConfig: conf,
CacheSize: testCacheSize,
CacheTTL: testCacheTTL,

View file

@ -21,6 +21,7 @@ import (
"github.com/AdguardTeam/dnsproxy/proxy"
"github.com/AdguardTeam/dnsproxy/upstream"
"github.com/AdguardTeam/golibs/errors"
"github.com/AdguardTeam/golibs/logutil/slogutil"
"github.com/AdguardTeam/golibs/stringutil"
)
@ -213,9 +214,13 @@ func (o *clientObject) toPersistent(
}
if o.SafeSearchConf.Enabled {
logger := baseLogger.With(
slogutil.KeyPrefix, safesearch.LogPrefix,
safesearch.LogKey, cli.Name,
)
var ss *safesearch.Default
ss, err = safesearch.NewDefault(ctx, &safesearch.DefaultConfig{
BaseLogger: baseLogger,
Logger: logger,
ServicesConfig: o.SafeSearchConf,
ClientName: cli.Name,
CacheSize: safeSearchCacheSize,

View file

@ -14,6 +14,7 @@ import (
"github.com/AdguardTeam/AdGuardHome/internal/filtering/safesearch"
"github.com/AdguardTeam/AdGuardHome/internal/schedule"
"github.com/AdguardTeam/AdGuardHome/internal/whois"
"github.com/AdguardTeam/golibs/logutil/slogutil"
)
// clientJSON is a common structure used by several handlers to deal with
@ -210,9 +211,13 @@ func (clients *clientsContainer) jsonToClient(
c.UseOwnBlockedServices = !cj.UseGlobalBlockedServices
if c.SafeSearchConf.Enabled {
logger := clients.baseLogger.With(
slogutil.KeyPrefix, safesearch.LogPrefix,
safesearch.LogKey, c.Name,
)
var ss *safesearch.Default
ss, err = safesearch.NewDefault(ctx, &safesearch.DefaultConfig{
BaseLogger: clients.baseLogger,
Logger: logger,
ServicesConfig: c.SafeSearchConf,
ClientName: c.Name,
CacheSize: clients.safeSearchCacheSize,

View file

@ -452,8 +452,9 @@ func setupDNSFilteringConf(
conf.ParentalBlockHost = host
}
logger := baseLogger.With(slogutil.KeyPrefix, safesearch.LogPrefix)
conf.SafeSearch, err = safesearch.NewDefault(ctx, &safesearch.DefaultConfig{
BaseLogger: baseLogger,
Logger: logger,
ServicesConfig: conf.SafeSearchConf,
CacheSize: conf.SafeSearchCacheSize,
CacheTTL: cacheTime,
@ -590,7 +591,7 @@ func run(opts options, clientBuildFS fs.FS, done chan struct{}) {
// data first, but also to avoid relying on automatic Go init() function.
filtering.InitModule()
// TODO(s.chzhen): Use it.
// TODO(s.chzhen): Use it for the entire initialization process.
ctx := context.Background()
err = initContextClients(ctx, slogLogger)