Maintenance: improve DNS settings log

This commit is contained in:
Quentin McGaw
2021-02-01 01:22:46 +00:00
parent dd5a9c6067
commit 81556ec2e1
4 changed files with 65 additions and 48 deletions

2
go.mod
View File

@@ -6,7 +6,7 @@ require (
github.com/fatih/color v1.10.0 github.com/fatih/color v1.10.0
github.com/golang/mock v1.4.4 github.com/golang/mock v1.4.4
github.com/kyokomi/emoji v2.2.4+incompatible 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/golibs v0.0.0-20210124192933-79a950eaf217
github.com/qdm12/ss-server v0.1.0 github.com/qdm12/ss-server v0.1.0
github.com/qdm12/updated v0.0.0-20210102005021-dd457d77f94a github.com/qdm12/updated v0.0.0-20210102005021-dd457d77f94a

4
go.sum
View File

@@ -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/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 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4= 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 h1:P8kVMGo7yIEZSk18fA9XQh9faL1CW20aHosWP064MAA=
github.com/qdm12/dns v1.4.0-rc5/go.mod h1:WUY4/U8Z2O8888DPrahrIBv8GdYeoIcEy4aUDecZ+UM= 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-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 h1:/eMBq0vbc/KmVPXbwLfssp547pp6APRS1x/JNmPvm0s=
github.com/qdm12/golibs v0.0.0-20210124192933-79a950eaf217/go.mod h1:pikkTN7g7zRuuAnERwqW1yAFq6pYmxrxpjiwGvb0Ysc= github.com/qdm12/golibs v0.0.0-20210124192933-79a950eaf217/go.mod h1:pikkTN7g7zRuuAnERwqW1yAFq6pYmxrxpjiwGvb0Ysc=

View File

@@ -27,54 +27,56 @@ func (d *DNS) String() string {
return strings.Join(d.lines(), "\n") return strings.Join(d.lines(), "\n")
} }
const (
subIndent = " |--"
indent = " " // used if lines already contain the subIndent
)
func (d *DNS) lines() (lines []string) { func (d *DNS) lines() (lines []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 { if !d.Enabled {
return []string{"DNS over TLS disabled, using plaintext DNS " + d.PlaintextAddress.String()} lines = append(lines, indent+subIndent+"DNS over TLS: disabled")
return lines
} }
lines = append(lines, indent+subIndent+"DNS over TLS:")
const prefix = " |--" lines = append(lines, indent+indent+subIndent+"Unbound:")
lines = append(lines, "DNS settings:")
lines = append(lines, prefix+"Unbound:")
for _, line := range d.Unbound.Lines() { for _, line := range d.Unbound.Lines() {
indent := " " + prefix lines = append(lines, indent+indent+indent+line)
if strings.HasPrefix(line, prefix) {
indent = " "
}
lines = append(lines, indent+line)
} }
blockMalicious := disabled blockMalicious := disabled
if d.BlockMalicious { if d.BlockMalicious {
blockMalicious = enabled blockMalicious = enabled
} }
lines = append(lines, prefix+"Block malicious: "+blockMalicious) lines = append(lines, indent+indent+subIndent+"Block malicious: "+blockMalicious)
blockAds := disabled blockAds := disabled
if d.BlockAds { if d.BlockAds {
blockAds = enabled blockAds = enabled
} }
lines = append(lines, prefix+"Block ads: "+blockAds) lines = append(lines, indent+indent+subIndent+"Block ads: "+blockAds)
blockSurveillance := disabled blockSurveillance := disabled
if d.BlockSurveillance { if d.BlockSurveillance {
blockSurveillance = enabled blockSurveillance = enabled
} }
lines = append(lines, prefix+"Block surveillance: "+blockSurveillance) lines = append(lines, indent+indent+subIndent+"Block surveillance: "+blockSurveillance)
update := "deactivated" update := "deactivated"
if d.UpdatePeriod > 0 { if d.UpdatePeriod > 0 {
update = "every " + d.UpdatePeriod.String() update = "every " + d.UpdatePeriod.String()
} }
lines = append(lines, prefix+"Update: "+update) lines = append(lines, indent+indent+subIndent+"Update: "+update)
keepNameserver := "no"
if d.KeepNameserver {
keepNameserver = "yes"
}
lines = append(lines,
prefix+"Keep nameserver (disabled blocking): "+keepNameserver)
return lines return lines
} }

View File

@@ -3,7 +3,9 @@ package settings
import ( import (
"net" "net"
"testing" "testing"
"time"
"github.com/qdm12/dns/pkg/models"
"github.com/stretchr/testify/assert" "github.com/stretchr/testify/assert"
) )
@@ -18,17 +20,31 @@ func Test_DNS_Lines(t *testing.T) {
PlaintextAddress: net.IP{1, 1, 1, 1}, PlaintextAddress: net.IP{1, 1, 1, 1},
}, },
lines: []string{ 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": { "enabled DOT": {
settings: DNS{ 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{ lines: []string{
"DNS settings:", " |--DNS:",
" |--Keep nameserver (disabled blocking): yes",
" |--DNS over TLS:",
" |--Unbound:", " |--Unbound:",
" |--DNS over TLS provider:", " |--DNS over TLS providers:",
" |--cloudflare",
" |--Listening port: 0", " |--Listening port: 0",
" |--Access control:", " |--Access control:",
" |--Allowed:", " |--Allowed:",
@@ -41,11 +57,10 @@ func Test_DNS_Lines(t *testing.T) {
" |--Blocked hostnames:", " |--Blocked hostnames:",
" |--Blocked IP addresses:", " |--Blocked IP addresses:",
" |--Allowed hostnames:", " |--Allowed hostnames:",
" |--Block malicious: disabled", " |--Block malicious: enabled",
" |--Block ads: disabled", " |--Block ads: enabled",
" |--Block surveillance: disabled", " |--Block surveillance: enabled",
" |--Update: deactivated", " |--Update: every 1h0m0s",
" |--Keep nameserver (disabled blocking): no",
}, },
}, },
} }