all: imp code
Some checks failed
build / test (macOS-latest) (push) Has been cancelled
build / test (ubuntu-latest) (push) Has been cancelled
build / test (windows-latest) (push) Has been cancelled
lint / go-lint (push) Has been cancelled
lint / eslint (push) Has been cancelled
build / build-release (push) Has been cancelled
build / notify (push) Has been cancelled
lint / notify (push) Has been cancelled

This commit is contained in:
Dimitry Kolyshev
2025-07-01 16:54:08 +04:00
parent de62c55be1
commit 880c024ce2
5 changed files with 29 additions and 23 deletions

View File

@@ -22,7 +22,8 @@ var serviceRules map[string][]*rules.NetworkRule
// serviceIDs contains service IDs sorted alphabetically. // serviceIDs contains service IDs sorted alphabetically.
var serviceIDs []string var serviceIDs []string
// initBlockedServices initializes package-level blocked service data. // initBlockedServices initializes package-level blocked service data. l must
// not be nil.
func initBlockedServices(ctx context.Context, l *slog.Logger) { func initBlockedServices(ctx context.Context, l *slog.Logger) {
svcLen := len(blockedServices) svcLen := len(blockedServices)
serviceIDs = make([]string, svcLen) serviceIDs = make([]string, svcLen)

View File

@@ -1029,9 +1029,9 @@ func makeResult(matchedRules []rules.Rule, reason Reason) (res Result) {
} }
} }
// InitModule manually initializes blocked services map. // InitModule manually initializes blocked services map. l must not be nil.
func InitModule(l *slog.Logger) { func InitModule(ctx context.Context, l *slog.Logger) {
initBlockedServices(context.TODO(), l) initBlockedServices(ctx, l)
} }
// New creates properly initialized DNS Filter that is ready to be used. c must // New creates properly initialized DNS Filter that is ready to be used. c must

View File

@@ -18,15 +18,14 @@ import (
"github.com/stretchr/testify/require" "github.com/stretchr/testify/require"
) )
func TestMain(m *testing.M) {
testutil.DiscardLogOutput(m)
}
const ( const (
sbBlocked = "wmconvirus.narod.ru" sbBlocked = "wmconvirus.narod.ru"
pcBlocked = "pornhub.com" pcBlocked = "pornhub.com"
) )
// testLogger is the common logger for tests.
var testLogger = slogutil.NewDiscardLogger()
// Helpers. // Helpers.
func newForTest(t testing.TB, c *Config, filters []Filter) (f *DNSFilter, setts *Settings) { func newForTest(t testing.TB, c *Config, filters []Filter) (f *DNSFilter, setts *Settings) {
@@ -35,7 +34,7 @@ func newForTest(t testing.TB, c *Config, filters []Filter) (f *DNSFilter, setts
FilteringEnabled: true, FilteringEnabled: true,
} }
if c != nil { if c != nil {
c.Logger = cmp.Or(c.Logger, slogutil.NewDiscardLogger()) c.Logger = cmp.Or(c.Logger, testLogger)
c.SafeBrowsingCacheSize = 10000 c.SafeBrowsingCacheSize = 10000
c.ParentalCacheSize = 10000 c.ParentalCacheSize = 10000
c.SafeSearchCacheSize = 1000 c.SafeSearchCacheSize = 1000
@@ -46,7 +45,7 @@ func newForTest(t testing.TB, c *Config, filters []Filter) (f *DNSFilter, setts
} else { } else {
// It must not be nil. // It must not be nil.
c = &Config{ c = &Config{
Logger: slogutil.NewDiscardLogger(), Logger: testLogger,
} }
} }
f, err := New(c, filters) f, err := New(c, filters)
@@ -57,7 +56,7 @@ func newForTest(t testing.TB, c *Config, filters []Filter) (f *DNSFilter, setts
func newChecker(host string) Checker { func newChecker(host string) Checker {
return hashprefix.New(&hashprefix.Config{ return hashprefix.New(&hashprefix.Config{
Logger: slogutil.NewDiscardLogger(), Logger: testLogger,
CacheTime: 10, CacheTime: 10,
CacheSize: 100000, CacheSize: 100000,
Upstream: aghtest.NewBlockUpstream(host, true), Upstream: aghtest.NewBlockUpstream(host, true),
@@ -676,7 +675,7 @@ func TestClientSettings(t *testing.T) {
func BenchmarkSafeBrowsing(b *testing.B) { func BenchmarkSafeBrowsing(b *testing.B) {
d, setts := newForTest(b, &Config{ d, setts := newForTest(b, &Config{
Logger: slogutil.NewDiscardLogger(), Logger: testLogger,
SafeBrowsingEnabled: true, SafeBrowsingEnabled: true,
SafeBrowsingChecker: newChecker(sbBlocked), SafeBrowsingChecker: newChecker(sbBlocked),
}, nil) }, nil)
@@ -703,7 +702,7 @@ func BenchmarkSafeBrowsing(b *testing.B) {
func BenchmarkSafeBrowsing_parallel(b *testing.B) { func BenchmarkSafeBrowsing_parallel(b *testing.B) {
d, setts := newForTest(b, &Config{ d, setts := newForTest(b, &Config{
Logger: slogutil.NewDiscardLogger(), Logger: testLogger,
SafeBrowsingEnabled: true, SafeBrowsingEnabled: true,
SafeBrowsingChecker: newChecker(sbBlocked), SafeBrowsingChecker: newChecker(sbBlocked),
}, nil) }, nil)

View File

@@ -3,7 +3,6 @@ package home
import ( import (
"bytes" "bytes"
"fmt" "fmt"
"log/slog"
"net/netip" "net/netip"
"os" "os"
"path/filepath" "path/filepath"
@@ -519,7 +518,6 @@ var config = &configuration{
Name: "AdAway Default Blocklist", Name: "AdAway Default Blocklist",
}}, }},
Filtering: &filtering.Config{ Filtering: &filtering.Config{
Logger: slog.Default(),
ProtectionEnabled: true, ProtectionEnabled: true,
BlockingMode: filtering.BlockingModeDefault, BlockingMode: filtering.BlockingModeDefault,
BlockedResponseTTL: 10, // in seconds BlockedResponseTTL: 10, // in seconds

View File

@@ -357,11 +357,11 @@ func setupDNSFilteringConf(
const ( const (
dnsTimeout = 3 * time.Second dnsTimeout = 3 * time.Second
sbService = "safe browsing" sbService = "safe_browsing"
defaultSafeBrowsingServer = `https://family.adguard-dns.com/dns-query` defaultSafeBrowsingServer = `https://family.adguard-dns.com/dns-query`
sbTXTSuffix = `sb.dns.adguard.com.` sbTXTSuffix = `sb.dns.adguard.com.`
pcService = "parental control" pcService = "parental_control"
defaultParentalServer = `https://family.adguard-dns.com/dns-query` defaultParentalServer = `https://family.adguard-dns.com/dns-query`
pcTXTSuffix = `pc.dns.adguard.com.` pcTXTSuffix = `pc.dns.adguard.com.`
) )
@@ -417,7 +417,11 @@ func setupDNSFilteringConf(
// default. // default.
if conf.SafeBrowsingBlockHost == "" { if conf.SafeBrowsingBlockHost == "" {
host := defaultSafeBrowsingBlockHost host := defaultSafeBrowsingBlockHost
log.Info("%s: warning: empty blocking host; using default: %q", sbService, host) baseLogger.WarnContext(ctx,
"empty blocking host; set default",
"service", sbService,
"host", host,
)
conf.SafeBrowsingBlockHost = host conf.SafeBrowsingBlockHost = host
} }
@@ -441,7 +445,11 @@ func setupDNSFilteringConf(
// default. // default.
if conf.ParentalBlockHost == "" { if conf.ParentalBlockHost == "" {
host := defaultParentalBlockHost host := defaultParentalBlockHost
log.Info("%s: warning: empty blocking host; using default: %q", pcService, host) baseLogger.WarnContext(ctx,
"empty blocking host; set default",
"service", pcService,
"host", host,
)
conf.ParentalBlockHost = host conf.ParentalBlockHost = host
} }
@@ -616,13 +624,13 @@ func run(opts options, clientBuildFS fs.FS, done chan struct{}, sigHdlr *signalH
err = configureOS(config) err = configureOS(config)
fatalOnError(err) fatalOnError(err)
// TODO(s.chzhen): Use it for the entire initialization process.
ctx := context.Background()
// Clients package uses filtering package's static data // Clients package uses filtering package's static data
// (filtering.BlockedSvcKnown()), so we have to initialize filtering static // (filtering.BlockedSvcKnown()), so we have to initialize filtering static
// 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(slogLogger) filtering.InitModule(ctx, slogLogger)
// TODO(s.chzhen): Use it for the entire initialization process.
ctx := context.Background()
err = initContextClients(ctx, slogLogger, sigHdlr) err = initContextClients(ctx, slogLogger, sigHdlr)
fatalOnError(err) fatalOnError(err)