From b2fcc90a359addc3f4928df87283ffd897042319 Mon Sep 17 00:00:00 2001 From: Quentin McGaw Date: Sun, 2 Jan 2022 15:13:51 +0000 Subject: [PATCH] chore(settings): internal default helpers --- internal/config/cache.go | 6 +- internal/settings/defaults/defaults.go | 86 ++++++++++++++++++++++++++ pkg/blockbuilder/settings.go | 23 ++----- pkg/cache/lru/settings.go | 6 +- pkg/check/settings.go | 37 ++++------- pkg/doh/settings.go | 24 +++---- pkg/dot/settings.go | 17 ++--- pkg/metrics/prometheus/settings.go | 11 +--- pkg/nameserver/internal.go | 10 ++- pkg/nameserver/system.go | 11 ++-- 10 files changed, 132 insertions(+), 99 deletions(-) create mode 100644 internal/settings/defaults/defaults.go diff --git a/internal/config/cache.go b/internal/config/cache.go index 1f4832fc..1af5ae13 100644 --- a/internal/config/cache.go +++ b/internal/config/cache.go @@ -4,6 +4,7 @@ import ( "errors" "fmt" + "github.com/qdm12/dns/internal/settings/defaults" "github.com/qdm12/dns/pkg/cache" "github.com/qdm12/dns/pkg/cache/lru" "github.com/qdm12/dns/pkg/cache/noop" @@ -25,10 +26,7 @@ type Cache struct { } func (c *Cache) SetDefaults() { - if c.Type == "" { - c.Type = noop.CacheType - } - + c.Type = defaults.String(c.Type, noop.CacheType) switch c.Type { case noop.CacheType: c.Noop.SetDefaults() diff --git a/internal/settings/defaults/defaults.go b/internal/settings/defaults/defaults.go new file mode 100644 index 00000000..0dab2c61 --- /dev/null +++ b/internal/settings/defaults/defaults.go @@ -0,0 +1,86 @@ +package defaults + +import ( + "net" + "net/http" + "time" + + "github.com/prometheus/client_golang/prometheus" +) + +func String(existing, defaultValue string) string { + if existing != "" { + return existing + } + return defaultValue +} + +func Int(existing, defaultValue int) int { + if existing != 0 { + return existing + } + return defaultValue +} + +func Uint16(existing, defaultValue uint16) uint16 { + if existing != 0 { + return existing + } + return defaultValue +} + +func Duration(existing time.Duration, defaultValue time.Duration) time.Duration { + if existing != 0 { + return existing + } + return defaultValue +} + +func IP(existing, defaultValue net.IP) net.IP { + if existing != nil { + return existing + } + return defaultValue +} + +func BoolPtr(existing *bool, defaultValue bool) *bool { + if existing != nil { + return existing + } + return &defaultValue +} + +func StringPtr(existing *string, defaultValue string) *string { + if existing != nil { + return existing + } + return &defaultValue +} + +func DurationPtr(existing *time.Duration, defaultValue time.Duration) *time.Duration { + if existing != nil { + return existing + } + return &defaultValue +} + +func HTTPClient(existing, defaultValue *http.Client) *http.Client { + if existing != nil { + return existing + } + return defaultValue +} + +func Resolver(existing, defaultValue *net.Resolver) *net.Resolver { + if existing != nil { + return existing + } + return defaultValue +} + +func PrometheusRegisterer(existing, defaultValue prometheus.Registerer) prometheus.Registerer { + if existing != nil { + return existing + } + return defaultValue +} diff --git a/pkg/blockbuilder/settings.go b/pkg/blockbuilder/settings.go index 90eced2a..5f868c50 100644 --- a/pkg/blockbuilder/settings.go +++ b/pkg/blockbuilder/settings.go @@ -7,6 +7,7 @@ import ( "regexp" "strings" + "github.com/qdm12/dns/internal/settings/defaults" "github.com/qdm12/gotree" "inet.af/netaddr" ) @@ -25,24 +26,10 @@ type Settings struct { } func (s *Settings) SetDefaults() { - if s.Client == nil { - s.Client = http.DefaultClient - } - - if s.BlockMalicious == nil { - t := true - s.BlockMalicious = &t - } - - if s.BlockAds == nil { - f := false - s.BlockAds = &f - } - - if s.BlockSurveillance == nil { - f := false - s.BlockSurveillance = &f - } + s.Client = defaults.HTTPClient(s.Client, http.DefaultClient) + s.BlockMalicious = defaults.BoolPtr(s.BlockMalicious, true) + s.BlockAds = defaults.BoolPtr(s.BlockAds, false) + s.BlockSurveillance = defaults.BoolPtr(s.BlockSurveillance, false) } var hostRegex = regexp.MustCompile(`^([a-zA-Z0-9]|[a-zA-Z0-9_][a-zA-Z0-9\-_]{0,61}[a-zA-Z0-9_])(\.([a-zA-Z0-9]|[a-zA-Z0-9_][a-zA-Z0-9\-_]{0,61}[a-zA-Z0-9]))*$`) //nolint:lll diff --git a/pkg/cache/lru/settings.go b/pkg/cache/lru/settings.go index d51eae5b..6a099f28 100644 --- a/pkg/cache/lru/settings.go +++ b/pkg/cache/lru/settings.go @@ -1,6 +1,7 @@ package lru import ( + "github.com/qdm12/dns/internal/settings/defaults" "github.com/qdm12/dns/pkg/cache/metrics" "github.com/qdm12/dns/pkg/cache/metrics/noop" "github.com/qdm12/gotree" @@ -16,9 +17,8 @@ type Settings struct { } func (s *Settings) SetDefaults() { - if s.MaxEntries == 0 { - s.MaxEntries = 10e4 - } + const defaultMaxEntries = 10e4 + s.MaxEntries = defaults.Int(s.MaxEntries, defaultMaxEntries) if s.Metrics == nil { s.Metrics = noop.New() diff --git a/pkg/check/settings.go b/pkg/check/settings.go index 942330e2..14bffc2f 100644 --- a/pkg/check/settings.go +++ b/pkg/check/settings.go @@ -3,6 +3,8 @@ package check import ( "net" "time" + + "github.com/qdm12/dns/internal/settings/defaults" ) type Settings struct { @@ -28,30 +30,17 @@ type Settings struct { } func (s *Settings) SetDefaults() { - if s.Resolver == nil { - s.Resolver = net.DefaultResolver - } - - if s.HostToResolve == "" { - s.HostToResolve = "github.com" - } - - if s.MaxTries == 0 { - const defaultMaxTries = 10 - s.MaxTries = defaultMaxTries - } - - if s.WaitTime == nil { - const defaultWaitTime = 300 * time.Millisecond - waitTime := defaultWaitTime - s.WaitTime = &waitTime - } - - if s.AddWaitTime == nil { - const defaultAddWaitTime = 100 * time.Millisecond - addWaitTime := defaultAddWaitTime - s.AddWaitTime = &addWaitTime - } + s.Resolver = defaults.Resolver(s.Resolver, net.DefaultResolver) + s.HostToResolve = defaults.String(s.HostToResolve, "github.com") + + const defaultMaxTries = 10 + s.MaxTries = defaults.Int(s.MaxTries, defaultMaxTries) + + const defaultWaitTime = 300 * time.Millisecond + s.WaitTime = defaults.DurationPtr(s.WaitTime, defaultWaitTime) + + const defaultAddWaitTime = 100 * time.Millisecond + s.AddWaitTime = defaults.DurationPtr(s.AddWaitTime, defaultAddWaitTime) } func (s Settings) Validate() (err error) { diff --git a/pkg/doh/settings.go b/pkg/doh/settings.go index e5a1111e..73af8f0e 100644 --- a/pkg/doh/settings.go +++ b/pkg/doh/settings.go @@ -7,6 +7,7 @@ import ( "strings" "time" + "github.com/qdm12/dns/internal/settings/defaults" "github.com/qdm12/dns/pkg/cache" cachenoop "github.com/qdm12/dns/pkg/cache/noop" "github.com/qdm12/dns/pkg/doh/metrics" @@ -64,11 +65,7 @@ type SelfDNS struct { func (s *ServerSettings) SetDefaults() { s.Resolver.SetDefaults() s.LogMiddleware.SetDefaults() - - if s.Address == "" { - const defaultAddress = ":53" - s.Address = defaultAddress - } + s.Address = defaults.String(s.Address, ":53") if s.Filter == nil { s.Filter = filternoop.New() @@ -95,10 +92,8 @@ func (s *ResolverSettings) SetDefaults() { s.DoHProviders = []string{"cloudflare"} } - if s.Timeout == 0 { - const defaultTimeout = 5 * time.Second - s.Timeout = defaultTimeout - } + const defaultTimeout = 5 * time.Second + s.Timeout = defaults.Duration(s.Timeout, defaultTimeout) if s.Warner == nil { s.Warner = lognoop.New() @@ -110,10 +105,8 @@ func (s *ResolverSettings) SetDefaults() { } func (s *SelfDNS) SetDefaults() { - if s.Timeout == 0 { - const defaultTimeout = 5 * time.Second - s.Timeout = defaultTimeout - } + const defaultTimeout = 5 * time.Second + s.Timeout = defaults.Duration(s.Timeout, defaultTimeout) if len(s.DoTProviders) == 0 { s.DoTProviders = []string{"cloudflare"} @@ -121,10 +114,7 @@ func (s *SelfDNS) SetDefaults() { // No default DNS fallback server for the internal HTTP client // to avoid leaking we are using a DoH server. - if s.IPv6 == nil { - ipv6 := false - s.IPv6 = &ipv6 - } + s.IPv6 = defaults.BoolPtr(s.IPv6, false) } var ( diff --git a/pkg/dot/settings.go b/pkg/dot/settings.go index 6e678d7f..68d9a351 100644 --- a/pkg/dot/settings.go +++ b/pkg/dot/settings.go @@ -7,6 +7,7 @@ import ( "strings" "time" + "github.com/qdm12/dns/internal/settings/defaults" "github.com/qdm12/dns/pkg/cache" cachenoop "github.com/qdm12/dns/pkg/cache/noop" "github.com/qdm12/dns/pkg/dot/metrics" @@ -61,10 +62,7 @@ func (s *ServerSettings) SetDefaults() { s.Resolver.SetDefaults() s.LogMiddleware.SetDefaults() - if s.Address == "" { - const defaultAddress = ":53" - s.Address = defaultAddress - } + s.Address = defaults.String(s.Address, ":53") if s.Filter == nil { s.Filter = filternoop.New() @@ -89,15 +87,10 @@ func (s *ResolverSettings) SetDefaults() { s.DoTProviders = []string{"cloudflare"} } - if s.Timeout == 0 { - const defaultTimeout = 5 * time.Second - s.Timeout = defaultTimeout - } + const defaultTimeout = 5 * time.Second + s.Timeout = defaults.Duration(s.Timeout, defaultTimeout) - if s.IPv6 == nil { - ipv6 := false - s.IPv6 = &ipv6 - } + s.IPv6 = defaults.BoolPtr(s.IPv6, false) if s.Warner == nil { s.Warner = lognoop.New() diff --git a/pkg/metrics/prometheus/settings.go b/pkg/metrics/prometheus/settings.go index 5e1973cc..043e1dd1 100644 --- a/pkg/metrics/prometheus/settings.go +++ b/pkg/metrics/prometheus/settings.go @@ -7,6 +7,7 @@ import ( "strings" "github.com/prometheus/client_golang/prometheus" + "github.com/qdm12/dns/internal/settings/defaults" ) type Settings struct { @@ -20,14 +21,8 @@ type Settings struct { } func (s *Settings) SetDefaults() { - if s.Prefix == nil { - prefix := "" - s.Prefix = &prefix - } - - if s.Registry == nil { - s.Registry = prometheus.DefaultRegisterer - } + s.Prefix = defaults.StringPtr(s.Prefix, "") + s.Registry = defaults.PrometheusRegisterer(s.Registry, prometheus.DefaultRegisterer) } var ( diff --git a/pkg/nameserver/internal.go b/pkg/nameserver/internal.go index 2abaae77..762949c3 100644 --- a/pkg/nameserver/internal.go +++ b/pkg/nameserver/internal.go @@ -5,6 +5,8 @@ import ( "fmt" "net" "time" + + "github.com/qdm12/dns/internal/settings/defaults" ) type SettingsInternalDNS struct { @@ -20,12 +22,8 @@ type SettingsInternalDNS struct { } func (s *SettingsInternalDNS) SetDefaults() { - if s.IP == nil { - s.IP = net.IPv4(127, 0, 0, 1) //nolint:gomnd - } - if s.Port == 0 { - s.Port = 53 - } + s.IP = defaults.IP(s.IP, net.IPv4(127, 0, 0, 1)) //nolint:gomnd + s.Port = defaults.Uint16(s.Port, 53) } func (s SettingsInternalDNS) Validate() (err error) { diff --git a/pkg/nameserver/system.go b/pkg/nameserver/system.go index 137a7b4f..4012c27c 100644 --- a/pkg/nameserver/system.go +++ b/pkg/nameserver/system.go @@ -5,6 +5,8 @@ import ( "net" "os" "strings" + + "github.com/qdm12/dns/internal/settings/defaults" ) type SettingsSystemDNS struct { @@ -20,13 +22,8 @@ type SettingsSystemDNS struct { } func (s *SettingsSystemDNS) SetDefaults() { - if s.IP == nil { - s.IP = net.IPv4(127, 0, 0, 1) //nolint:gomnd - } - - if s.ResolvPath == "" { - s.ResolvPath = "/etc/resolv.conf" - } + s.IP = defaults.IP(s.IP, net.IPv4(127, 0, 0, 1)) //nolint:gomnd + s.ResolvPath = defaults.String(s.ResolvPath, "/etc/resolv.conf") } func (s *SettingsSystemDNS) Validate() (err error) {