From 45532406018044b542cfcf81282512987319e707 Mon Sep 17 00:00:00 2001 From: Quentin McGaw Date: Sun, 3 Jan 2021 00:51:47 +0000 Subject: [PATCH] Feature: Improve DNS settings start log --- internal/settings/dns.go | 54 +++++++++++++++++++++---------- internal/settings/dns_test.go | 60 +++++++++++++++++++++++++++++++++++ 2 files changed, 98 insertions(+), 16 deletions(-) create mode 100644 internal/settings/dns_test.go diff --git a/internal/settings/dns.go b/internal/settings/dns.go index d4c100ca..53145912 100644 --- a/internal/settings/dns.go +++ b/internal/settings/dns.go @@ -24,37 +24,59 @@ type DNS struct { //nolint:maligned } func (d *DNS) String() string { + return strings.Join(d.lines(), "\n") +} + +func (d *DNS) lines() (lines []string) { if !d.Enabled { - return fmt.Sprintf("DNS over TLS disabled, using plaintext DNS %s", d.PlaintextAddress) + return []string{"DNS over TLS disabled, using plaintext DNS " + d.PlaintextAddress.String()} } - blockMalicious, blockSurveillance, blockAds := disabled, disabled, disabled + + const prefix = " |--" + + lines = append(lines, "DNS settings:") + + lines = append(lines, prefix+"Unbound:") + for _, line := range d.Unbound.Lines() { + indent := " " + prefix + if strings.HasPrefix(line, prefix) { + indent = " " + } + lines = append(lines, indent+line) + } + + blockMalicious := disabled if d.BlockMalicious { blockMalicious = enabled } - if d.BlockSurveillance { - blockSurveillance = enabled - } + lines = append(lines, prefix+"Block malicious: "+blockMalicious) + + blockAds := disabled if d.BlockAds { blockAds = enabled } + lines = append(lines, prefix+"Block ads: "+blockAds) + + blockSurveillance := disabled + if d.BlockSurveillance { + blockSurveillance = enabled + } + lines = append(lines, prefix+"Block surveillance: "+blockSurveillance) + update := "deactivated" if d.UpdatePeriod > 0 { - update = fmt.Sprintf("every %s", d.UpdatePeriod) + update = "every " + d.UpdatePeriod.String() } + lines = append(lines, prefix+"Update: "+update) + keepNameserver := "no" if d.KeepNameserver { keepNameserver = "yes" } - settingsList := []string{ - "DNS settings:", - "Block malicious: " + blockMalicious, - "Block surveillance: " + blockSurveillance, - "Block ads: " + blockAds, - "Update: " + update, - "Keep nameserver (disabled blocking): " + keepNameserver, - "Unbound settings: " + "\n |--" + strings.Join(d.Unbound.Lines(), "\n |--"), - } - return strings.Join(settingsList, "\n |--") + lines = append(lines, + prefix+"Keep nameserver (disabled blocking): "+keepNameserver) + + return lines } // GetDNSSettings obtains DNS over TLS settings from environment variables using the params package. diff --git a/internal/settings/dns_test.go b/internal/settings/dns_test.go new file mode 100644 index 00000000..412b996d --- /dev/null +++ b/internal/settings/dns_test.go @@ -0,0 +1,60 @@ +package settings + +import ( + "net" + "testing" + + "github.com/stretchr/testify/assert" +) + +func Test_DNS_Lines(t *testing.T) { + t.Parallel() + testCases := map[string]struct { + settings DNS + lines []string + }{ + "disabled DOT": { + settings: DNS{ + PlaintextAddress: net.IP{1, 1, 1, 1}, + }, + lines: []string{ + "DNS over TLS disabled, using plaintext DNS 1.1.1.1", + }, + }, + "enabled DOT": { + settings: DNS{ + Enabled: true, + }, + lines: []string{ + "DNS settings:", + " |--Unbound:", + " |--DNS over TLS provider:", + " |--Listening port: 0", + " |--Access control:", + " |--Allowed:", + " |--Caching: disabled", + " |--IPv4 resolution: disabled", + " |--IPv6 resolution: disabled", + " |--Verbosity level: 0/5", + " |--Verbosity details level: 0/4", + " |--Validation log level: 0/2", + " |--Blocked hostnames:", + " |--Blocked IP addresses:", + " |--Allowed hostnames:", + " |--Block malicious: disabled", + " |--Block ads: disabled", + " |--Block surveillance: disabled", + " |--Update: deactivated", + " |--Keep nameserver (disabled blocking): no", + }, + }, + } + for name, testCase := range testCases { + testCase := testCase + t.Run(name, func(t *testing.T) { + t.Parallel() + lines := testCase.settings.lines() + assert.Equal(t, testCase.lines, lines) + }) + } +}