Skip to content

Commit

Permalink
chore(settings): internal default helpers
Browse files Browse the repository at this point in the history
  • Loading branch information
qdm12 committed Jan 2, 2022
1 parent e001980 commit 6c3bec6
Show file tree
Hide file tree
Showing 10 changed files with 133 additions and 99 deletions.
6 changes: 2 additions & 4 deletions internal/config/cache.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -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()
Expand Down
86 changes: 86 additions & 0 deletions internal/settings/defaults/defaults.go
Original file line number Diff line number Diff line change
@@ -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
}
23 changes: 5 additions & 18 deletions pkg/blockbuilder/settings.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"regexp"
"strings"

"github.com/qdm12/dns/internal/settings/defaults"
"github.com/qdm12/gotree"
"inet.af/netaddr"
)
Expand All @@ -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
Expand Down
6 changes: 3 additions & 3 deletions pkg/cache/lru/settings.go
Original file line number Diff line number Diff line change
@@ -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"
Expand All @@ -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()
Expand Down
37 changes: 13 additions & 24 deletions pkg/check/settings.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ package check
import (
"net"
"time"

"github.com/qdm12/dns/internal/settings/defaults"
)

type Settings struct {
Expand All @@ -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) {
Expand Down
24 changes: 7 additions & 17 deletions pkg/doh/settings.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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()
Expand All @@ -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()
Expand All @@ -110,21 +105,16 @@ 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"}
}
// 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 (
Expand Down
17 changes: 5 additions & 12 deletions pkg/dot/settings.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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()
Expand All @@ -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()
Expand Down
11 changes: 3 additions & 8 deletions pkg/metrics/prometheus/settings.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"strings"

"github.com/prometheus/client_golang/prometheus"
"github.com/qdm12/dns/internal/settings/defaults"
)

type Settings struct {
Expand All @@ -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 (
Expand Down
11 changes: 5 additions & 6 deletions pkg/nameserver/internal.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ import (
"fmt"
"net"
"time"

"github.com/qdm12/dns/internal/settings/defaults"
)

type SettingsInternalDNS struct {
Expand All @@ -20,12 +22,9 @@ 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
const defaultPort = 53
s.Port = defaults.Uint16(s.Port, defaultPort)
}

func (s SettingsInternalDNS) Validate() (err error) {
Expand Down
11 changes: 4 additions & 7 deletions pkg/nameserver/system.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ import (
"net"
"os"
"strings"

"github.com/qdm12/dns/internal/settings/defaults"
)

type SettingsSystemDNS struct {
Expand All @@ -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) {
Expand Down

0 comments on commit 6c3bec6

Please sign in to comment.