/
config.go
237 lines (193 loc) · 5.7 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
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
ConnectTimeout Duration
Expire uint32
CacheSize int
Maxdepth int
RateLimit int
ClientRateLimit int
CookieSecret string
Blocklist []string
Whitelist []string
}
// 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
outboundips = [
]
# outbound ipv6 addresses, if you set multiple, sdns can use random outbound ipv6 address
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"
]
# root keys 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. Leave blank for disable.
fallbackservers = [
]
# address to bind to for the http API server, leave blank for disable
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, leave blank for disable. SDNS uses Common Log Format by default.
# accesslog = ""
# list of remote blocklists
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"
]
# 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, leave blank for disable
# the form of the entries in the /etc/hosts file are based on IETF RFC 952 which was updated by IETF RFC 1123.
hostsfile = ""
# query timeout for dns lookups in duration
timeout = "2s"
# connect timeout for dns lookups in duration
connecttimeout = "2s"
# default cache TTL in seconds
expire = 600
# cache size (total records in cache)
cachesize = 256000
# maximum recursion depth for nameservers
maxdepth = 30
# query based ratelimit per second, 0 for disable
ratelimit = 0
# client ip address based ratelimit per minute, 0 for disable
clientratelimit = 0
# manual blocklist entries
blocklist = []
# manual whitelist entries
whitelist = []
`
// Load loads the given config file
func Load(path, version string) (*Config, error) {
config := new(Config)
if _, err := os.Stat(path); os.IsNotExist(err) {
if err := generateConfig(path, version); err != nil {
return nil, err
}
}
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 date, you can generate new one and check the changes.")
}
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
}