diff --git a/go.mod b/go.mod index 76111230..e6dd6b29 100644 --- a/go.mod +++ b/go.mod @@ -6,7 +6,7 @@ require ( github.com/fatih/color v1.10.0 github.com/golang/mock v1.4.4 github.com/kyokomi/emoji v2.2.4+incompatible - github.com/qdm12/dns v1.4.0-rc5 + github.com/qdm12/dns v1.4.0 github.com/qdm12/golibs v0.0.0-20210124192933-79a950eaf217 github.com/qdm12/ss-server v0.1.0 github.com/qdm12/updated v0.0.0-20210102005021-dd457d77f94a diff --git a/go.sum b/go.sum index 1b78859a..8fc7e900 100644 --- a/go.sum +++ b/go.sum @@ -92,8 +92,8 @@ github.com/pkg/errors v0.8.1 h1:iURUrRGxPUNPdy5/HRSm+Yj6okJ6UtLINN0Q9M4+h3I= github.com/pkg/errors v0.8.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0= github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM= github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4= -github.com/qdm12/dns v1.4.0-rc5 h1:XXjYaFI3pDY1U4YFH5t5AI5IEKlIALmnE34VFhgkdQE= -github.com/qdm12/dns v1.4.0-rc5/go.mod h1:WUY4/U8Z2O8888DPrahrIBv8GdYeoIcEy4aUDecZ+UM= +github.com/qdm12/dns v1.4.0 h1:P8kVMGo7yIEZSk18fA9XQh9faL1CW20aHosWP064MAA= +github.com/qdm12/dns v1.4.0/go.mod h1:WUY4/U8Z2O8888DPrahrIBv8GdYeoIcEy4aUDecZ+UM= github.com/qdm12/golibs v0.0.0-20201227203847-2fd99ffdfdba/go.mod h1:pikkTN7g7zRuuAnERwqW1yAFq6pYmxrxpjiwGvb0Ysc= github.com/qdm12/golibs v0.0.0-20210124192933-79a950eaf217 h1:/eMBq0vbc/KmVPXbwLfssp547pp6APRS1x/JNmPvm0s= github.com/qdm12/golibs v0.0.0-20210124192933-79a950eaf217/go.mod h1:pikkTN7g7zRuuAnERwqW1yAFq6pYmxrxpjiwGvb0Ysc= diff --git a/internal/settings/dns.go b/internal/settings/dns.go index 53145912..3d5cc781 100644 --- a/internal/settings/dns.go +++ b/internal/settings/dns.go @@ -27,54 +27,56 @@ func (d *DNS) String() string { return strings.Join(d.lines(), "\n") } +const ( + subIndent = " |--" + indent = " " // used if lines already contain the subIndent +) + func (d *DNS) lines() (lines []string) { - if !d.Enabled { - return []string{"DNS over TLS disabled, using plaintext DNS " + d.PlaintextAddress.String()} + lines = append(lines, subIndent+"DNS:") + if d.PlaintextAddress != nil { + lines = append(lines, indent+subIndent+"Plaintext address: "+d.PlaintextAddress.String()) } + keepNameserver := "no" + if d.KeepNameserver { + keepNameserver = "yes" + } + lines = append(lines, + indent+subIndent+"Keep nameserver (disabled blocking): "+keepNameserver) + if !d.Enabled { + lines = append(lines, indent+subIndent+"DNS over TLS: disabled") + return lines + } + lines = append(lines, indent+subIndent+"DNS over TLS:") - const prefix = " |--" - - lines = append(lines, "DNS settings:") - - lines = append(lines, prefix+"Unbound:") + lines = append(lines, indent+indent+subIndent+"Unbound:") for _, line := range d.Unbound.Lines() { - indent := " " + prefix - if strings.HasPrefix(line, prefix) { - indent = " " - } - lines = append(lines, indent+line) + lines = append(lines, indent+indent+indent+line) } blockMalicious := disabled if d.BlockMalicious { blockMalicious = enabled } - lines = append(lines, prefix+"Block malicious: "+blockMalicious) + lines = append(lines, indent+indent+subIndent+"Block malicious: "+blockMalicious) blockAds := disabled if d.BlockAds { blockAds = enabled } - lines = append(lines, prefix+"Block ads: "+blockAds) + lines = append(lines, indent+indent+subIndent+"Block ads: "+blockAds) blockSurveillance := disabled if d.BlockSurveillance { blockSurveillance = enabled } - lines = append(lines, prefix+"Block surveillance: "+blockSurveillance) + lines = append(lines, indent+indent+subIndent+"Block surveillance: "+blockSurveillance) update := "deactivated" if d.UpdatePeriod > 0 { update = "every " + d.UpdatePeriod.String() } - lines = append(lines, prefix+"Update: "+update) - - keepNameserver := "no" - if d.KeepNameserver { - keepNameserver = "yes" - } - lines = append(lines, - prefix+"Keep nameserver (disabled blocking): "+keepNameserver) + lines = append(lines, indent+indent+subIndent+"Update: "+update) return lines } diff --git a/internal/settings/dns_test.go b/internal/settings/dns_test.go index 412b996d..5f6384bc 100644 --- a/internal/settings/dns_test.go +++ b/internal/settings/dns_test.go @@ -3,7 +3,9 @@ package settings import ( "net" "testing" + "time" + "github.com/qdm12/dns/pkg/models" "github.com/stretchr/testify/assert" ) @@ -18,34 +20,47 @@ func Test_DNS_Lines(t *testing.T) { PlaintextAddress: net.IP{1, 1, 1, 1}, }, lines: []string{ - "DNS over TLS disabled, using plaintext DNS 1.1.1.1", + " |--DNS:", + " |--Plaintext address: 1.1.1.1", + " |--Keep nameserver (disabled blocking): no", + " |--DNS over TLS: disabled", }, }, "enabled DOT": { settings: DNS{ - Enabled: true, + Enabled: true, + KeepNameserver: true, + Unbound: models.Settings{ + Providers: []string{"cloudflare"}, + }, + BlockMalicious: true, + BlockAds: true, + BlockSurveillance: true, + UpdatePeriod: time.Hour, }, 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", + " |--DNS:", + " |--Keep nameserver (disabled blocking): yes", + " |--DNS over TLS:", + " |--Unbound:", + " |--DNS over TLS providers:", + " |--cloudflare", + " |--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: enabled", + " |--Block ads: enabled", + " |--Block surveillance: enabled", + " |--Update: every 1h0m0s", }, }, }