/
config.go
269 lines (219 loc) · 6.69 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
267
268
269
package config
import (
"fmt"
"io"
"os"
"path/filepath"
"strings"
"time"
"github.com/BurntSushi/toml"
"github.com/semihalev/log"
)
// Config type
type Config struct {
Version string
BlockLists []string
BlockListDir string
RootServers []string
Root6Servers []string
RootKeys []string
FallbackServers []string
AccessList []string
LogLevel string
AccessLog string
Bind string
BindTLS string
BindDOH string
TLSCertificate string
TLSPrivateKey string
API string
Nullroute string
Nullroutev6 string
Hostsfile string
OutboundIPs []string
OutboundIP6s []string
Timeout Duration
Expire uint32
CacheSize int
Maxdepth int
RateLimit int
ClientRateLimit int
CookieSecret string
NSID string
Blocklist []string
Whitelist []string
Chaos bool
QnameMinLevel int `toml:"qname_min_level"`
sVersion string
}
// ServerVersion return current server version
func (c *Config) ServerVersion() string {
return c.sVersion
}
// Duration type
type Duration struct {
time.Duration
}
// UnmarshalText for duration type
func (d *Duration) UnmarshalText(text []byte) error {
var err error
d.Duration, err = time.ParseDuration(string(text))
return err
}
var defaultConfig = `
# Config version, config and build versions can be different.
version = "%s"
# Address to bind to for the DNS server
bind = ":53"
# Address to bind to for the DNS-over-TLS server
# bindtls = ":853"
# Address to bind to for the DNS-over-HTTPS server
# binddoh = ":8053"
# TLS certificate file
# tlscertificate = "server.crt"
# TLS private key file
# tlsprivatekey = "server.key"
# Outbound ipv4 addresses, if you set multiple, sdns can use random outbound ipv4 address by request based
outboundips = [
]
# Outbound ipv6 addresses, if you set multiple, sdns can use random outbound ipv6 address by request based
outboundip6s = [
]
# Root zone ipv4 servers
rootservers = [
"192.5.5.241:53",
"198.41.0.4:53",
"192.228.79.201:53",
"192.33.4.12:53",
"199.7.91.13:53",
"192.203.230.10:53",
"192.112.36.4:53",
"128.63.2.53:53",
"192.36.148.17:53",
"192.58.128.30:53",
"193.0.14.129:53",
"199.7.83.42:53",
"202.12.27.33:53"
]
# Root zone ipv6 servers
root6servers = [
"[2001:500:2f::f]:53",
"[2001:503:ba3e::2:30]:53",
"[2001:500:200::b]:53",
"[2001:500:2::c]:53",
"[2001:500:2d::d]:53",
"[2001:500:a8::e]:53",
"[2001:500:12::d0d]:53",
"[2001:500:1::53]:53",
"[2001:7fe::53]:53",
"[2001:503:c27::2:30]:53",
"[2001:7fd::1]:53",
"[2001:500:9f::42]:53",
"[2001:dc3::35]:53"
]
# Trusted anchors for dnssec
rootkeys = [
". 172800 IN DNSKEY 257 3 8 AwEAAaz/tAm8yTn4Mfeh5eyI96WSVexTBAvkMgJzkKTOiW1vkIbzxeF3+/4RgWOq7HrxRixHlFlExOLAJr5emLvN7SWXgnLh4+B5xQlNVz8Og8kvArMtNROxVQuCaSnIDdD5LKyWbRd2n9WGe2R8PzgCmr3EgVLrjyBxWezF0jLHwVN8efS3rCj/EWgvIWgb9tarpVUDK/b58Da+sqqls3eNbuv7pr+eoZG+SrDK6nWeL3c6H5Apxz7LjVc1uTIdsIXxuOLYA4/ilBmSVIzuDWfdRUfhHdY6+cn8HFRm+2hM8AnXGXws9555KrUB5qihylGa8subX2Nn6UwNR1AkUTV74bU="
]
# Failover resolver ipv4 or ipv6 addresses with port, left blank for disabled. Example: "1.1.1.1:53"
# fallbackservers = [
# "8.8.8.8:53",
# "8.8.4.4:53"
# ]
fallbackservers = [
]
# Address to bind to for the http API server, left blank for disabled
api = "127.0.0.1:8080"
# What kind of information should be logged, Log verbosity level [crit,error,warn,info,debug]
loglevel = "info"
# The location of access log file, left blank for disabled. SDNS uses Common Log Format by default.
# accesslog = ""
# List of remote blocklists address list. All lists will be download to blocklist folder.
# blocklists = [
# "http://mirror1.malwaredomains.com/files/justdomains",
# "https://raw.githubusercontent.com/StevenBlack/hosts/master/hosts",
# "http://sysctl.org/cameleon/hosts",
# "https://zeustracker.abuse.ch/blocklist.php?download=domainblocklist",
# "https://s3.amazonaws.com/lists.disconnect.me/simple_tracking.txt",
# "https://s3.amazonaws.com/lists.disconnect.me/simple_ad.txt",
# "https://raw.githubusercontent.com/quidsup/notrack/master/trackers.txt"
# ]
blocklists = [
]
# List of locations to recursively read blocklists from (warning, every file found is assumed to be a hosts-file or domain list)
blocklistdir = "bl"
# IPv4 address to forward blocked queries to
nullroute = "0.0.0.0"
# IPv6 address to forward blocked queries to
nullroutev6 = "::0"
# Which clients allowed to make queries
accesslist = [
"0.0.0.0/0",
"::0/0"
]
# Enables serving zone data from a hosts file, left blank for disabled
# the form of the entries in the /etc/hosts file are based on IETF RFC 952 which was updated by IETF RFC 1123.
hostsfile = ""
# Network timeout for each dns lookups in duration
timeout = "3s"
# Default error cache TTL in seconds
expire = 600
# Cache size (total records in cache)
cachesize = 256000
# Maximum iteration depth for a query
maxdepth = 30
# Query based ratelimit per second, 0 for disabled
ratelimit = 0
# Client ip address based ratelimit per minute, 0 for disabled
clientratelimit = 0
# Manual blocklist entries
blocklist = []
# Manual whitelist entries
whitelist = []
# DNS server identifier (RFC 5001), it's useful while operating multiple sdns. left blank for disabled
nsid = ""
# Enable to answer version.server, version.bind, hostname.bind, id.server chaos queries.
chaos = true
# Qname minimization level. If higher, it can be more complex and impact the response performance. If set 0, qname minimization will be disable
qname_min_level = 5
`
// Load loads the given config file
func Load(path, version string, sVersion string) (*Config, error) {
config := new(Config)
if _, err := os.Stat(path); os.IsNotExist(err) {
if path == "sdns.conf" {
// compatibility for old default conf file
if _, err := os.Stat("sdns.toml"); os.IsNotExist(err) {
if err := generateConfig(path, version); err != nil {
return nil, err
}
} else {
path = "sdns.toml"
}
}
}
log.Info("Loading config file", "path", path)
if _, err := toml.DecodeFile(path, config); err != nil {
return nil, fmt.Errorf("could not load config: %s", err)
}
if config.Version != version {
log.Warn("Config file is out of version, you can generate new one and check the changes.")
}
config.sVersion = sVersion
return config, nil
}
func generateConfig(path, version string) error {
output, err := os.Create(path)
if err != nil {
return fmt.Errorf("could not generate config: %s", err)
}
defer output.Close()
r := strings.NewReader(fmt.Sprintf(defaultConfig, version))
if _, err := io.Copy(output, r); err != nil {
return fmt.Errorf("could not copy default config: %s", err)
}
if abs, err := filepath.Abs(path); err == nil {
log.Info("Default config file generated", "config", abs)
}
return nil
}