-
Notifications
You must be signed in to change notification settings - Fork 68
/
config.go
266 lines (223 loc) · 6.76 KB
/
config.go
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
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
package main
import (
cTls "crypto/tls"
"errors"
"fmt"
"github.com/cottand/leng/tls"
"github.com/jonboulle/clockwork"
"github.com/pelletier/go-toml/v2"
"log"
"os"
"path/filepath"
"strings"
)
// BuildVersion returns the build version of leng, this should be incremented every new release
var BuildVersion = "1.6.0"
// ConfigVersion returns the version of leng, this should be incremented every time the config changes so leng presents a warning
var ConfigVersion = "1.6.0"
// Config holds the configuration parameters
type Config struct {
Version string
LogConfig string
Bind string
API string
Interval int
Timeout int
QuestionCacheCap int
TTL uint32
CustomDNSRecords []string
APIDebug bool
Blocking Blocking
Upstream Upstream
Metrics Metrics `toml:"metrics"`
DnsOverHttpServer DnsOverHttpServer
FollowCnameDepth uint32
}
type Blocking struct {
Sources []string
SourcesStore string
SourceDirs []string
Blocklist []string
Whitelist []string
NXDomain bool
Nullroute string
Nullroutev6 string
}
type Upstream struct {
DoH string
Nameservers []string
TimeoutS int `toml:"timeout_s"`
Expire uint32
Maxcount int
}
type Metrics struct {
Enabled bool
Path string
HighCardinalityEnabled bool
ResetPeriodMinutes int64
HistogramsEnabled bool
}
type DnsOverHttpServer struct {
Enabled bool
Bind string
TimeoutMs int64
TLS TlsConfig
parsedTls *cTls.Config
}
type TlsConfig struct {
certPath, keyPath, caPath string
enabled bool
}
func (c TlsConfig) parsedConfig() (*cTls.Config, error) {
if !c.enabled {
return nil, nil
}
return tls.NewTLSConfig(c.certPath, c.keyPath, c.caPath)
}
var defaultConfig = `
# version this config was generated from
version = "%s"
# log configuration
# format: comma separated list of options, where options is one of
# file:<filename>@<loglevel>
# stderr>@<loglevel>
# syslog@<loglevel>
# loglevel: 0 = errors and important operations, 1 = dns queries, 2 = debug
# e.g. logconfig = "file:leng.log@2,syslog@1,stderr@2"
logconfig = "stderr@2"
# apidebug enables the debug mode of the http api library
apidebug = false
# address to bind to for the DNS server
bind = "0.0.0.0:53"
# address to bind to for the API server
api = "127.0.0.1:8080"
# concurrency interval for lookups in milliseconds
interval = 200
# question cache capacity, 0 for infinite but not recommended (this is used for storing logs)
questioncachecap = 5000
# timeout for upstream DNS queries, in ms
timeout = 5000
# manual whitelist entries - comments for reference
whitelist = [
# "getsentry.com",
# "www.getsentry.com"
]
# manual custom dns entries - comments for reference
customdnsrecords = [
# "example.mywebsite.tld IN A 10.0.0.1"
# "example.other.tld IN CNAME wikipedia.org"
]
# How deep to follow chains of CNAME records
# set to 0 to disable CNAME-following entirely
# (anything more than 10 should be more than plenty)
# see https://github.com/Cottand/leng/wiki/CNAME%E2%80%90following-DNS
followCnameDepth = 12
[Blocking]
# response to blocked queries with a NXDOMAIN
nxdomain = false
# ipv4 address to forward blocked queries to
nullroute = "0.0.0.0"
# ipv6 address to forward blocked queries to
nullroutev6 = "0:0:0:0:0:0:0:0"
# manual blocklist entries
blocklist = []
# list of sources to pull blocklists from, stores them in ./sources
sources = [
"https://raw.githubusercontent.com/StevenBlack/hosts/master/hosts",
"https://sysctl.org/cameleon/hosts",
"https://s3.amazonaws.com/lists.disconnect.me/simple_tracking.txt",
"https://s3.amazonaws.com/lists.disconnect.me/simple_ad.txt",
"https://gitlab.com/quidsup/notrack-blocklists/raw/master/notrack-blocklist.txt"
]
# list of locations to recursively read blocklists from (warning, every file found is assumed to be a hosts-file or domain list)
sourcedirs = ["./sources"]
sourcesStore = "./sources"
[Upstream]
# Dns over HTTPS provider to use.
DoH = "https://cloudflare-dns.com/dns-query"
# nameservers to forward queries to
nameservers = ["1.1.1.1:53", "1.0.0.1:53"]
# query timeout for dns lookups in seconds
timeout_s = 5
# cache entry lifespan in seconds
expire = 600
# cache capacity, 0 for infinite
maxcount = 0
# Prometheus metrics - disabled by default
[Metrics]
enabled = false
path = "/metrics"
# see https://cottand.github.io/leng/Prometheus-Metrics.html
highCardinalityEnabled = false
histogramsEnabled = false
resetPeriodMinutes = 60
[DnsOverHttpServer]
enabled = false
bind = "0.0.0.0:80"
timeoutMs = 5000
# TLS config is not required for DoH if you have some proxy (ie, caddy, nginx, traefik...) manage HTTPS for you
[DnsOverHttpServer.TLS]
enabled = false
certPath = ""
keyPath = ""
# if empty, system CAs will be used
caPath = ""
`
func parseDefaultConfig() Config {
var config Config
err := toml.Unmarshal([]byte(defaultConfig), &config)
if err != nil {
logger.Fatalf("There was an error parsing the default config: %v", err)
}
config.Version = ConfigVersion
return config
}
// WallClock is the wall clock
var WallClock = clockwork.NewRealClock()
func contextualisedParsingErrorFrom(err error) error {
errString := strings.Builder{}
var derr *toml.DecodeError
_, _ = fmt.Fprint(&errString, "could not load config:", err)
if errors.As(err, &derr) {
errString.WriteByte('\n')
_, _ = fmt.Fprintln(&errString, derr.String())
row, col := derr.Position()
_, _ = fmt.Fprintln(&errString, "error occurred at row", row, "column", col)
}
return errors.New(errString.String())
}
// LoadConfig loads the given config file
func LoadConfig(path string) (*Config, error) {
var config = parseDefaultConfig()
if _, err := os.Stat(path); os.IsNotExist(err) {
log.Printf("warning, config not found - using defaults")
return &config, nil
}
path = filepath.Clean(path)
file, err := os.Open(path)
if err != nil {
log.Printf("warning, failed to open config (%v) - using defaults", err)
return &config, nil
}
defer func(file *os.File) {
_ = file.Close()
}(file)
d := toml.NewDecoder(file)
if err := d.Decode(&config); err != nil {
return nil, contextualisedParsingErrorFrom(err)
}
dohTls, err := config.DnsOverHttpServer.TLS.parsedConfig()
if err != nil {
return nil, fmt.Errorf("could not load TLS config: %s", err)
}
config.DnsOverHttpServer.parsedTls = dohTls
if config.Version != ConfigVersion {
if config.Version == "" {
config.Version = "none"
}
log.Printf("warning, leng.toml is out of date!\nconfig v%s\nleng config v%s\nleng v%s\nplease update your config\n", config.Version, ConfigVersion, BuildVersion)
} else {
log.Printf("leng v%s\n", BuildVersion)
}
return &config, nil
}