aboutsummaryrefslogtreecommitdiffstats
path: root/config.go
blob: cc84be07e56afd183e2867f7f458737a344bae93 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
package zdns

import (
	"fmt"
	"io"
	"net"
	"net/url"
	"strings"
	"time"

	"github.com/BurntSushi/toml"
	"github.com/mpolden/zdns/hosts"
	"github.com/mpolden/zdns/sql"
)

// Config specifies is the zdns configuration parameters.
type Config struct {
	DNS      DNSOptions
	Resolver ResolverOptions
	Hosts    []Hosts
}

// DNSOptions controlers the behaviour of the DNS server.
type DNSOptions struct {
	Listen          string
	Protocol        string `toml:"protocol"`
	CacheSize       int    `toml:"cache_size"`
	CachePrefetch   bool   `toml:"cache_prefetch"`
	CachePersist    bool   `toml:"cache_persist"`
	HijackMode      string `toml:"hijack_mode"`
	hijackMode      int
	RefreshInterval string `toml:"hosts_refresh_interval"`
	refreshInterval time.Duration
	Resolvers       []string
	Database        string `toml:"database"`
	LogModeString   string `toml:"log_mode"`
	LogMode         int
	LogTTLString    string `toml:"log_ttl"`
	LogTTL          time.Duration
	ListenHTTP      string `toml:"listen_http"`
}

// ResolverOptions controls the behaviour of resolvers.
type ResolverOptions struct {
	Protocol      string `toml:"protocol"`
	TimeoutString string `toml:"timeout"`
	Timeout       time.Duration
}

// Hosts controls how a hosts file should be retrieved.
type Hosts struct {
	URL     string
	Hosts   []string `toml:"entries"`
	hosts   hosts.Hosts
	Hijack  bool
	Timeout string
	timeout time.Duration
}

func newConfig() Config {
	c := Config{}
	// Default values
	c.DNS.Listen = "127.0.0.1:53000"
	c.DNS.ListenHTTP = "127.0.0.1:8053"
	c.DNS.Protocol = "udp"
	c.DNS.CacheSize = 4096
	c.DNS.CachePrefetch = true
	c.DNS.RefreshInterval = "48h"
	c.DNS.Resolvers = []string{
		"1.1.1.1:853",
		"1.0.0.1:853",
	}
	c.DNS.LogTTLString = "168h"
	c.Resolver.TimeoutString = "2s"
	c.Resolver.Protocol = "tcp-tls"
	return c
}

func (c *Config) load() error {
	var err error
	if c.DNS.Listen == "" {
		return fmt.Errorf("invalid listening address: %s", c.DNS.Listen)
	}
	if c.DNS.Protocol == "" {
		c.DNS.Protocol = "udp"
	}
	if c.DNS.Protocol != "udp" {
		return fmt.Errorf("unsupported protocol: %s", c.DNS.Protocol)
	}
	if c.DNS.CacheSize < 0 {
		return fmt.Errorf("cache size must be >= 0")
	}
	if c.DNS.CachePersist && c.DNS.Database == "" {
		return fmt.Errorf("cache_persist = %t requires 'database' to be set", c.DNS.CachePersist)
	}
	switch c.DNS.HijackMode {
	case "", "zero":
		c.DNS.hijackMode = HijackZero
	case "empty":
		c.DNS.hijackMode = HijackEmpty
	case "hosts":
		c.DNS.hijackMode = HijackHosts
	default:
		return fmt.Errorf("invalid hijack mode: %s", c.DNS.HijackMode)
	}
	if c.DNS.RefreshInterval == "" {
		c.DNS.RefreshInterval = "0"
	}
	c.DNS.refreshInterval, err = time.ParseDuration(c.DNS.RefreshInterval)
	if err != nil {
		return fmt.Errorf("invalid refresh interval: %w", err)
	}
	if c.DNS.refreshInterval < 0 {
		return fmt.Errorf("refresh interval must be >= 0")
	}
	for i, hs := range c.Hosts {
		if (hs.URL == "") == (hs.Hosts == nil) {
			return fmt.Errorf("exactly one of url or hosts must be set")
		}
		if hs.URL != "" {
			url, err := url.Parse(hs.URL)
			if err != nil {
				return fmt.Errorf("%s: invalid url: %w", hs.URL, err)
			}
			switch url.Scheme {
			case "file", "http", "https":
			default:
				return fmt.Errorf("%s: unsupported scheme: %s", hs.URL, url.Scheme)
			}
			if url.Scheme == "file" && hs.Timeout != "" {
				return fmt.Errorf("%s: timeout cannot be set for %s url", hs.URL, url.Scheme)
			}
			if c.Hosts[i].Timeout == "" {
				c.Hosts[i].Timeout = "0"
			}
			c.Hosts[i].timeout, err = time.ParseDuration(c.Hosts[i].Timeout)
			if err != nil {
				return fmt.Errorf("%s: invalid timeout: %s", hs.URL, hs.Timeout)
			}
		}
		if hs.Hosts != nil {
			if hs.Timeout != "" {
				return fmt.Errorf("%s: timeout cannot be set for inline hosts", hs.Hosts)
			}
			var err error
			r := strings.NewReader(strings.Join(hs.Hosts, "\n"))
			c.Hosts[i].hosts, err = hosts.Parse(r)
			if err != nil {
				return err
			}
		}
	}
	for _, r := range c.DNS.Resolvers {
		if c.Resolver.Protocol == "https" {
			u, err := url.Parse(r)
			if err != nil {
				return fmt.Errorf("invalid resolver %s: %w", r, err)
			}
			if u.Scheme != "https" {
				return fmt.Errorf("protocol %s requires https scheme for resolver %s", c.Resolver.Protocol, r)
			}
		} else {
			if _, _, err := net.SplitHostPort(r); err != nil {
				return fmt.Errorf("invalid resolver: %w", err)
			}
		}
	}
	if c.Resolver.Protocol == "udp" {
		c.Resolver.Protocol = "" // Empty means UDP when passed to dns.ListenAndServe
	}
	switch c.Resolver.Protocol {
	case "", "tcp", "tcp-tls", "https":
	default:
		return fmt.Errorf("invalid resolver protocol: %s", c.Resolver.Protocol)
	}
	c.Resolver.Timeout, err = time.ParseDuration(c.Resolver.TimeoutString)
	if err != nil {
		return fmt.Errorf("invalid resolver timeout: %s", c.Resolver.TimeoutString)
	}
	if c.Resolver.Timeout < 0 {
		return fmt.Errorf("resolver timeout must be >= 0")
	}
	if c.Resolver.Timeout == 0 {
		c.Resolver.Timeout = 5 * time.Second
	}
	switch c.DNS.LogModeString {
	case "":
		c.DNS.LogMode = sql.LogDiscard
	case "all":
		c.DNS.LogMode = sql.LogAll
	case "hijacked":
		c.DNS.LogMode = sql.LogHijacked
	default:
		return fmt.Errorf("invalid log mode: %s", c.DNS.LogModeString)
	}
	if c.DNS.LogModeString != "" && c.DNS.Database == "" {
		return fmt.Errorf("log_mode = %q requires 'database' to be set", c.DNS.LogModeString)
	}
	if c.DNS.LogTTLString == "" {
		c.DNS.LogTTLString = "0"
	}
	c.DNS.LogTTL, err = time.ParseDuration(c.DNS.LogTTLString)
	if err != nil {
		return fmt.Errorf("invalid log TTL: %s", c.DNS.LogTTLString)
	}
	return nil
}

// ReadConfig reads a zdns configuration from reader r.
func ReadConfig(r io.Reader) (Config, error) {
	conf := newConfig()
	_, err := toml.NewDecoder(r).Decode(&conf)
	if err != nil {
		return Config{}, err
	}
	return conf, conf.load()
}