Skip to content

Commit 6c3bec6

Browse files
committed
chore(settings): internal default helpers
1 parent e001980 commit 6c3bec6

10 files changed

Lines changed: 133 additions & 99 deletions

File tree

internal/config/cache.go

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import (
44
"errors"
55
"fmt"
66

7+
"github.com/qdm12/dns/internal/settings/defaults"
78
"github.com/qdm12/dns/pkg/cache"
89
"github.com/qdm12/dns/pkg/cache/lru"
910
"github.com/qdm12/dns/pkg/cache/noop"
@@ -25,10 +26,7 @@ type Cache struct {
2526
}
2627

2728
func (c *Cache) SetDefaults() {
28-
if c.Type == "" {
29-
c.Type = noop.CacheType
30-
}
31-
29+
c.Type = defaults.String(c.Type, noop.CacheType)
3230
switch c.Type {
3331
case noop.CacheType:
3432
c.Noop.SetDefaults()
Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
1+
package defaults
2+
3+
import (
4+
"net"
5+
"net/http"
6+
"time"
7+
8+
"github.com/prometheus/client_golang/prometheus"
9+
)
10+
11+
func String(existing, defaultValue string) string {
12+
if existing != "" {
13+
return existing
14+
}
15+
return defaultValue
16+
}
17+
18+
func Int(existing, defaultValue int) int {
19+
if existing != 0 {
20+
return existing
21+
}
22+
return defaultValue
23+
}
24+
25+
func Uint16(existing, defaultValue uint16) uint16 {
26+
if existing != 0 {
27+
return existing
28+
}
29+
return defaultValue
30+
}
31+
32+
func Duration(existing time.Duration, defaultValue time.Duration) time.Duration {
33+
if existing != 0 {
34+
return existing
35+
}
36+
return defaultValue
37+
}
38+
39+
func IP(existing, defaultValue net.IP) net.IP {
40+
if existing != nil {
41+
return existing
42+
}
43+
return defaultValue
44+
}
45+
46+
func BoolPtr(existing *bool, defaultValue bool) *bool {
47+
if existing != nil {
48+
return existing
49+
}
50+
return &defaultValue
51+
}
52+
53+
func StringPtr(existing *string, defaultValue string) *string {
54+
if existing != nil {
55+
return existing
56+
}
57+
return &defaultValue
58+
}
59+
60+
func DurationPtr(existing *time.Duration, defaultValue time.Duration) *time.Duration {
61+
if existing != nil {
62+
return existing
63+
}
64+
return &defaultValue
65+
}
66+
67+
func HTTPClient(existing, defaultValue *http.Client) *http.Client {
68+
if existing != nil {
69+
return existing
70+
}
71+
return defaultValue
72+
}
73+
74+
func Resolver(existing, defaultValue *net.Resolver) *net.Resolver {
75+
if existing != nil {
76+
return existing
77+
}
78+
return defaultValue
79+
}
80+
81+
func PrometheusRegisterer(existing, defaultValue prometheus.Registerer) prometheus.Registerer {
82+
if existing != nil {
83+
return existing
84+
}
85+
return defaultValue
86+
}

pkg/blockbuilder/settings.go

Lines changed: 5 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import (
77
"regexp"
88
"strings"
99

10+
"github.com/qdm12/dns/internal/settings/defaults"
1011
"github.com/qdm12/gotree"
1112
"inet.af/netaddr"
1213
)
@@ -25,24 +26,10 @@ type Settings struct {
2526
}
2627

2728
func (s *Settings) SetDefaults() {
28-
if s.Client == nil {
29-
s.Client = http.DefaultClient
30-
}
31-
32-
if s.BlockMalicious == nil {
33-
t := true
34-
s.BlockMalicious = &t
35-
}
36-
37-
if s.BlockAds == nil {
38-
f := false
39-
s.BlockAds = &f
40-
}
41-
42-
if s.BlockSurveillance == nil {
43-
f := false
44-
s.BlockSurveillance = &f
45-
}
29+
s.Client = defaults.HTTPClient(s.Client, http.DefaultClient)
30+
s.BlockMalicious = defaults.BoolPtr(s.BlockMalicious, true)
31+
s.BlockAds = defaults.BoolPtr(s.BlockAds, false)
32+
s.BlockSurveillance = defaults.BoolPtr(s.BlockSurveillance, false)
4633
}
4734

4835
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

pkg/cache/lru/settings.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package lru
22

33
import (
4+
"github.com/qdm12/dns/internal/settings/defaults"
45
"github.com/qdm12/dns/pkg/cache/metrics"
56
"github.com/qdm12/dns/pkg/cache/metrics/noop"
67
"github.com/qdm12/gotree"
@@ -16,9 +17,8 @@ type Settings struct {
1617
}
1718

1819
func (s *Settings) SetDefaults() {
19-
if s.MaxEntries == 0 {
20-
s.MaxEntries = 10e4
21-
}
20+
const defaultMaxEntries = 10e4
21+
s.MaxEntries = defaults.Int(s.MaxEntries, defaultMaxEntries)
2222

2323
if s.Metrics == nil {
2424
s.Metrics = noop.New()

pkg/check/settings.go

Lines changed: 13 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@ package check
33
import (
44
"net"
55
"time"
6+
7+
"github.com/qdm12/dns/internal/settings/defaults"
68
)
79

810
type Settings struct {
@@ -28,30 +30,17 @@ type Settings struct {
2830
}
2931

3032
func (s *Settings) SetDefaults() {
31-
if s.Resolver == nil {
32-
s.Resolver = net.DefaultResolver
33-
}
34-
35-
if s.HostToResolve == "" {
36-
s.HostToResolve = "github.com"
37-
}
38-
39-
if s.MaxTries == 0 {
40-
const defaultMaxTries = 10
41-
s.MaxTries = defaultMaxTries
42-
}
43-
44-
if s.WaitTime == nil {
45-
const defaultWaitTime = 300 * time.Millisecond
46-
waitTime := defaultWaitTime
47-
s.WaitTime = &waitTime
48-
}
49-
50-
if s.AddWaitTime == nil {
51-
const defaultAddWaitTime = 100 * time.Millisecond
52-
addWaitTime := defaultAddWaitTime
53-
s.AddWaitTime = &addWaitTime
54-
}
33+
s.Resolver = defaults.Resolver(s.Resolver, net.DefaultResolver)
34+
s.HostToResolve = defaults.String(s.HostToResolve, "github.com")
35+
36+
const defaultMaxTries = 10
37+
s.MaxTries = defaults.Int(s.MaxTries, defaultMaxTries)
38+
39+
const defaultWaitTime = 300 * time.Millisecond
40+
s.WaitTime = defaults.DurationPtr(s.WaitTime, defaultWaitTime)
41+
42+
const defaultAddWaitTime = 100 * time.Millisecond
43+
s.AddWaitTime = defaults.DurationPtr(s.AddWaitTime, defaultAddWaitTime)
5544
}
5645

5746
func (s Settings) Validate() (err error) {

pkg/doh/settings.go

Lines changed: 7 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import (
77
"strings"
88
"time"
99

10+
"github.com/qdm12/dns/internal/settings/defaults"
1011
"github.com/qdm12/dns/pkg/cache"
1112
cachenoop "github.com/qdm12/dns/pkg/cache/noop"
1213
"github.com/qdm12/dns/pkg/doh/metrics"
@@ -64,11 +65,7 @@ type SelfDNS struct {
6465
func (s *ServerSettings) SetDefaults() {
6566
s.Resolver.SetDefaults()
6667
s.LogMiddleware.SetDefaults()
67-
68-
if s.Address == "" {
69-
const defaultAddress = ":53"
70-
s.Address = defaultAddress
71-
}
68+
s.Address = defaults.String(s.Address, ":53")
7269

7370
if s.Filter == nil {
7471
s.Filter = filternoop.New()
@@ -95,10 +92,8 @@ func (s *ResolverSettings) SetDefaults() {
9592
s.DoHProviders = []string{"cloudflare"}
9693
}
9794

98-
if s.Timeout == 0 {
99-
const defaultTimeout = 5 * time.Second
100-
s.Timeout = defaultTimeout
101-
}
95+
const defaultTimeout = 5 * time.Second
96+
s.Timeout = defaults.Duration(s.Timeout, defaultTimeout)
10297

10398
if s.Warner == nil {
10499
s.Warner = lognoop.New()
@@ -110,21 +105,16 @@ func (s *ResolverSettings) SetDefaults() {
110105
}
111106

112107
func (s *SelfDNS) SetDefaults() {
113-
if s.Timeout == 0 {
114-
const defaultTimeout = 5 * time.Second
115-
s.Timeout = defaultTimeout
116-
}
108+
const defaultTimeout = 5 * time.Second
109+
s.Timeout = defaults.Duration(s.Timeout, defaultTimeout)
117110

118111
if len(s.DoTProviders) == 0 {
119112
s.DoTProviders = []string{"cloudflare"}
120113
}
121114
// No default DNS fallback server for the internal HTTP client
122115
// to avoid leaking we are using a DoH server.
123116

124-
if s.IPv6 == nil {
125-
ipv6 := false
126-
s.IPv6 = &ipv6
127-
}
117+
s.IPv6 = defaults.BoolPtr(s.IPv6, false)
128118
}
129119

130120
var (

pkg/dot/settings.go

Lines changed: 5 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import (
77
"strings"
88
"time"
99

10+
"github.com/qdm12/dns/internal/settings/defaults"
1011
"github.com/qdm12/dns/pkg/cache"
1112
cachenoop "github.com/qdm12/dns/pkg/cache/noop"
1213
"github.com/qdm12/dns/pkg/dot/metrics"
@@ -61,10 +62,7 @@ func (s *ServerSettings) SetDefaults() {
6162
s.Resolver.SetDefaults()
6263
s.LogMiddleware.SetDefaults()
6364

64-
if s.Address == "" {
65-
const defaultAddress = ":53"
66-
s.Address = defaultAddress
67-
}
65+
s.Address = defaults.String(s.Address, ":53")
6866

6967
if s.Filter == nil {
7068
s.Filter = filternoop.New()
@@ -89,15 +87,10 @@ func (s *ResolverSettings) SetDefaults() {
8987
s.DoTProviders = []string{"cloudflare"}
9088
}
9189

92-
if s.Timeout == 0 {
93-
const defaultTimeout = 5 * time.Second
94-
s.Timeout = defaultTimeout
95-
}
90+
const defaultTimeout = 5 * time.Second
91+
s.Timeout = defaults.Duration(s.Timeout, defaultTimeout)
9692

97-
if s.IPv6 == nil {
98-
ipv6 := false
99-
s.IPv6 = &ipv6
100-
}
93+
s.IPv6 = defaults.BoolPtr(s.IPv6, false)
10194

10295
if s.Warner == nil {
10396
s.Warner = lognoop.New()

pkg/metrics/prometheus/settings.go

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import (
77
"strings"
88

99
"github.com/prometheus/client_golang/prometheus"
10+
"github.com/qdm12/dns/internal/settings/defaults"
1011
)
1112

1213
type Settings struct {
@@ -20,14 +21,8 @@ type Settings struct {
2021
}
2122

2223
func (s *Settings) SetDefaults() {
23-
if s.Prefix == nil {
24-
prefix := ""
25-
s.Prefix = &prefix
26-
}
27-
28-
if s.Registry == nil {
29-
s.Registry = prometheus.DefaultRegisterer
30-
}
24+
s.Prefix = defaults.StringPtr(s.Prefix, "")
25+
s.Registry = defaults.PrometheusRegisterer(s.Registry, prometheus.DefaultRegisterer)
3126
}
3227

3328
var (

pkg/nameserver/internal.go

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@ import (
55
"fmt"
66
"net"
77
"time"
8+
9+
"github.com/qdm12/dns/internal/settings/defaults"
810
)
911

1012
type SettingsInternalDNS struct {
@@ -20,12 +22,9 @@ type SettingsInternalDNS struct {
2022
}
2123

2224
func (s *SettingsInternalDNS) SetDefaults() {
23-
if s.IP == nil {
24-
s.IP = net.IPv4(127, 0, 0, 1) //nolint:gomnd
25-
}
26-
if s.Port == 0 {
27-
s.Port = 53
28-
}
25+
s.IP = defaults.IP(s.IP, net.IPv4(127, 0, 0, 1)) //nolint:gomnd
26+
const defaultPort = 53
27+
s.Port = defaults.Uint16(s.Port, defaultPort)
2928
}
3029

3130
func (s SettingsInternalDNS) Validate() (err error) {

pkg/nameserver/system.go

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@ import (
55
"net"
66
"os"
77
"strings"
8+
9+
"github.com/qdm12/dns/internal/settings/defaults"
810
)
911

1012
type SettingsSystemDNS struct {
@@ -20,13 +22,8 @@ type SettingsSystemDNS struct {
2022
}
2123

2224
func (s *SettingsSystemDNS) SetDefaults() {
23-
if s.IP == nil {
24-
s.IP = net.IPv4(127, 0, 0, 1) //nolint:gomnd
25-
}
26-
27-
if s.ResolvPath == "" {
28-
s.ResolvPath = "/etc/resolv.conf"
29-
}
25+
s.IP = defaults.IP(s.IP, net.IPv4(127, 0, 0, 1)) //nolint:gomnd
26+
s.ResolvPath = defaults.String(s.ResolvPath, "/etc/resolv.conf")
3027
}
3128

3229
func (s *SettingsSystemDNS) Validate() (err error) {

0 commit comments

Comments
 (0)