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) ctx := testutil.ContextWithTimeout(t, testTimeout)
safeSearch, err := safesearch.NewDefault(ctx, &safesearch.DefaultConfig{ safeSearch, err := safesearch.NewDefault(ctx, &safesearch.DefaultConfig{
BaseLogger: slogutil.NewDiscardLogger(), Logger: slogutil.NewDiscardLogger(),
ServicesConfig: safeSearchConf, ServicesConfig: safeSearchConf,
CacheSize: filterConf.SafeSearchCacheSize, CacheSize: filterConf.SafeSearchCacheSize,
CacheTTL: time.Minute * time.Duration(filterConf.CacheTime), CacheTTL: time.Minute * time.Duration(filterConf.CacheTime),

View File

@ -24,6 +24,12 @@ import (
"github.com/miekg/dns" "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. // Service is a enum with service names used as search providers.
type Service string type Service string
@ -62,8 +68,8 @@ func isServiceProtected(s filtering.SafeSearchConfig, service Service) (ok bool)
// DefaultConfig is the configuration structure for [Default]. // DefaultConfig is the configuration structure for [Default].
type DefaultConfig struct { type DefaultConfig struct {
// BaseLogger is used to create logger for [Default]. // Logger is used for logging the operation of the safe search filter.
BaseLogger *slog.Logger Logger *slog.Logger
// ClientName is the name of the persistent client associated with the safe // ClientName is the name of the persistent client associated with the safe
// search filter, if there is one. // 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 may be nil, which means that this safe search filter is disabled.
engine *urlfilter.DNSEngine engine *urlfilter.DNSEngine
// cache stores safe search filtering results.
cache cache.Cache cache cache.Cache
// cacheTTL is the Time to Live duration for cached items.
cacheTTL time.Duration cacheTTL time.Duration
} }
// NewDefault returns an initialized default safe search filter. name is used // NewDefault returns an initialized default safe search filter.
// for logging.
func NewDefault(ctx context.Context, conf *DefaultConfig) (ss *Default, err error) { 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{ ss = &Default{
logger: logger, logger: conf.Logger,
mu: &sync.RWMutex{}, mu: &sync.RWMutex{},
cache: cache.New(cache.Config{ cache: cache.New(cache.Config{
EnableLRU: true, EnableLRU: true,
@ -132,7 +135,7 @@ func (ss *Default) resetEngine(
conf filtering.SafeSearchConfig, conf filtering.SafeSearchConfig,
) (err error) { ) (err error) {
if !conf.Enabled { if !conf.Enabled {
ss.logger.InfoContext(ctx, "disabled") ss.logger.DebugContext(ctx, "disabled")
return nil 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) { func newForTest(t testing.TB, ssConf filtering.SafeSearchConfig) (ss *Default) {
ss, err := NewDefault(testutil.ContextWithTimeout(t, testTimeout), &DefaultConfig{ ss, err := NewDefault(testutil.ContextWithTimeout(t, testTimeout), &DefaultConfig{
BaseLogger: slogutil.NewDiscardLogger(), Logger: slogutil.NewDiscardLogger(),
ServicesConfig: ssConf, ServicesConfig: ssConf,
CacheSize: testCacheSize, CacheSize: testCacheSize,
CacheTTL: testCacheTTL, CacheTTL: testCacheTTL,

View File

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

View File

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

View File

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

View File

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