Skip to content

Commit

Permalink
all: imp code
Browse files Browse the repository at this point in the history
  • Loading branch information
Mizzick committed Jul 9, 2024
1 parent 7e8d3b5 commit 4db7cdc
Show file tree
Hide file tree
Showing 5 changed files with 37 additions and 17 deletions.
12 changes: 5 additions & 7 deletions internal/dnsforward/dnsforward.go
Original file line number Diff line number Diff line change
Expand Up @@ -205,7 +205,10 @@ type DNSCreateParams struct {
PrivateNets netutil.SubnetSet
Anonymizer *aghnet.IPMut
EtcHosts *aghnet.HostsContainer
Logger *slog.Logger

// Logger is used as a base logger. It must not be nil.
Logger *slog.Logger

LocalDomain string
}

Expand Down Expand Up @@ -236,18 +239,13 @@ func NewServer(p DNSCreateParams) (s *Server, err error) {
etcHosts = upstream.NewHostsResolver(p.EtcHosts)
}

l := p.Logger
if l == nil {
l = slog.Default()
}

s = &Server{
dnsFilter: p.DNSFilter,
dhcpServer: p.DHCPServer,
stats: p.Stats,
queryLog: p.QueryLog,
privateNets: p.PrivateNets,
logger: l.With(slogutil.KeyPrefix, "dnsforward"),
logger: p.Logger.With(slogutil.KeyPrefix, "dnsforward"),
// TODO(e.burkov): Use some case-insensitive string comparison.
localDomainSuffix: strings.ToLower(localDomainSuffix),
etcHosts: etcHosts,
Expand Down
29 changes: 24 additions & 5 deletions internal/dnsforward/dnsforward_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ import (
"github.com/AdguardTeam/AdGuardHome/internal/filtering/safesearch"
"github.com/AdguardTeam/dnsproxy/proxy"
"github.com/AdguardTeam/dnsproxy/upstream"
"github.com/AdguardTeam/golibs/logutil/slogutil"
"github.com/AdguardTeam/golibs/netutil"
"github.com/AdguardTeam/golibs/testutil"
"github.com/AdguardTeam/golibs/timeutil"
Expand Down Expand Up @@ -99,6 +100,7 @@ func createTestServer(
DHCPServer: dhcp,
DNSFilter: f,
PrivateNets: netutil.SubnetSetFunc(netutil.IsLocallyServed),
Logger: slogutil.NewDiscardLogger(),
})
require.NoError(t, err)

Expand Down Expand Up @@ -339,7 +341,10 @@ func TestServer_timeout(t *testing.T) {
ServePlainDNS: true,
}

s, err := NewServer(DNSCreateParams{DNSFilter: createTestDNSFilter(t)})
s, err := NewServer(DNSCreateParams{
DNSFilter: createTestDNSFilter(t),
Logger: slogutil.NewDiscardLogger(),
})
require.NoError(t, err)

err = s.Prepare(srvConf)
Expand All @@ -349,7 +354,10 @@ func TestServer_timeout(t *testing.T) {
})

t.Run("default", func(t *testing.T) {
s, err := NewServer(DNSCreateParams{DNSFilter: createTestDNSFilter(t)})
s, err := NewServer(DNSCreateParams{
DNSFilter: createTestDNSFilter(t),
Logger: slogutil.NewDiscardLogger(),
})
require.NoError(t, err)

s.conf.Config.UpstreamMode = UpstreamModeLoadBalance
Expand All @@ -376,7 +384,9 @@ func TestServer_Prepare_fallbacks(t *testing.T) {
ServePlainDNS: true,
}

s, err := NewServer(DNSCreateParams{})
s, err := NewServer(DNSCreateParams{
Logger: slogutil.NewDiscardLogger(),
})
require.NoError(t, err)

err = s.Prepare(srvConf)
Expand Down Expand Up @@ -962,6 +972,7 @@ func TestBlockedCustomIP(t *testing.T) {
DHCPServer: dhcp,
DNSFilter: f,
PrivateNets: netutil.SubnetSetFunc(netutil.IsLocallyServed),
Logger: slogutil.NewDiscardLogger(),
})
require.NoError(t, err)

Expand Down Expand Up @@ -1127,6 +1138,7 @@ func TestRewrite(t *testing.T) {
DHCPServer: dhcp,
DNSFilter: f,
PrivateNets: netutil.SubnetSetFunc(netutil.IsLocallyServed),
Logger: slogutil.NewDiscardLogger(),
})
require.NoError(t, err)

Expand Down Expand Up @@ -1256,6 +1268,7 @@ func TestPTRResponseFromDHCPLeases(t *testing.T) {
},
},
PrivateNets: netutil.SubnetSetFunc(netutil.IsLocallyServed),
Logger: slogutil.NewDiscardLogger(),
LocalDomain: localDomain,
})
require.NoError(t, err)
Expand Down Expand Up @@ -1341,6 +1354,7 @@ func TestPTRResponseFromHosts(t *testing.T) {
DHCPServer: dhcp,
DNSFilter: flt,
PrivateNets: netutil.SubnetSetFunc(netutil.IsLocallyServed),
Logger: slogutil.NewDiscardLogger(),
})
require.NoError(t, err)

Expand Down Expand Up @@ -1392,24 +1406,29 @@ func TestNewServer(t *testing.T) {
in DNSCreateParams
wantErrMsg string
}{{
name: "success",
in: DNSCreateParams{},
name: "success",
in: DNSCreateParams{
Logger: slogutil.NewDiscardLogger(),
},
wantErrMsg: "",
}, {
name: "success_local_tld",
in: DNSCreateParams{
Logger: slogutil.NewDiscardLogger(),
LocalDomain: "mynet",
},
wantErrMsg: "",
}, {
name: "success_local_domain",
in: DNSCreateParams{
Logger: slogutil.NewDiscardLogger(),
LocalDomain: "my.local.net",
},
wantErrMsg: "",
}, {
name: "bad_local_domain",
in: DNSCreateParams{
Logger: slogutil.NewDiscardLogger(),
LocalDomain: "!!!",
},
wantErrMsg: `local domain: bad domain name "!!!": ` +
Expand Down
3 changes: 3 additions & 0 deletions internal/dnsforward/filter_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (
"github.com/AdguardTeam/AdGuardHome/internal/filtering"
"github.com/AdguardTeam/dnsproxy/proxy"
"github.com/AdguardTeam/dnsproxy/upstream"
"github.com/AdguardTeam/golibs/logutil/slogutil"
"github.com/AdguardTeam/golibs/netutil"
"github.com/miekg/dns"
"github.com/stretchr/testify/assert"
Expand Down Expand Up @@ -57,6 +58,7 @@ func TestHandleDNSRequest_handleDNSRequest(t *testing.T) {
},
DNSFilter: f,
PrivateNets: netutil.SubnetSetFunc(netutil.IsLocallyServed),
Logger: slogutil.NewDiscardLogger(),
})
require.NoError(t, err)

Expand Down Expand Up @@ -229,6 +231,7 @@ func TestHandleDNSRequest_filterDNSResponse(t *testing.T) {
DHCPServer: &testDHCP{},
DNSFilter: f,
PrivateNets: netutil.SubnetSetFunc(netutil.IsLocallyServed),
Logger: slogutil.NewDiscardLogger(),
})
require.NoError(t, err)

Expand Down
4 changes: 2 additions & 2 deletions internal/home/home.go
Original file line number Diff line number Diff line change
Expand Up @@ -559,7 +559,7 @@ func run(opts options, clientBuildFS fs.FS, done chan struct{}) {
fatalOnError(err)

// TODO(a.garipov): Use slog everywhere.
l := initLogger(ls)
slogLogger := newSlogLogger(ls)

// Print the first message after logger is configured.
log.Info(version.Full())
Expand All @@ -568,7 +568,7 @@ func run(opts options, clientBuildFS fs.FS, done chan struct{}) {
log.Info("AdGuard Home is running as a service")
}

err = setupContext(opts, l)
err = setupContext(opts, slogLogger)
fatalOnError(err)

err = configureOS(config)
Expand Down
6 changes: 3 additions & 3 deletions internal/home/log.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,8 @@ import (
// for logger output.
const configSyslog = "syslog"

// initLogger returns new [*slog.Logger] configured with the given settings.
func initLogger(ls *logSettings) (l *slog.Logger) {
// newSlogLogger returns new [*slog.Logger] configured with the given settings.
func newSlogLogger(ls *logSettings) (l *slog.Logger) {
if !ls.Enabled {
return slogutil.NewDiscardLogger()
}
Expand All @@ -46,7 +46,7 @@ func configureLogger(ls *logSettings) (err error) {

// Write logs to stdout by default.
if ls.File == "" {
return err
return nil
}

if ls.File == configSyslog {
Expand Down

0 comments on commit 4db7cdc

Please sign in to comment.