forked from Lucapaulo/dnsproxy
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
610 lines (500 loc) · 20.4 KB
/
main.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
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
package main
import (
"crypto/tls"
"fmt"
"io/ioutil"
"net"
"os"
"os/signal"
"strings"
"syscall"
"time"
"github.com/lucas-clemente/quic-go"
"github.com/AdguardTeam/dnsproxy/proxy"
"github.com/AdguardTeam/dnsproxy/upstream"
"github.com/AdguardTeam/golibs/log"
"github.com/ameshkov/dnscrypt/v2"
goFlags "github.com/jessevdk/go-flags"
"gopkg.in/yaml.v3"
)
// Options represents console arguments. For further additions, please do not
// use the default option since it will cause some problems when config files
// are used.
type Options struct {
// Configuration file path (yaml), the config path should be read without
// using goFlags in order not to have default values overriding yaml
// options.
ConfigPath string `long:"config-path" description:"yaml configuration file. Minimal working configuration in config.yaml.dist. Options passed through command line will override the ones from this file." default:""`
// Log settings
// --
// Should we write
Verbose bool `yaml:"verbose" short:"v" long:"verbose" description:"Verbose output (optional)" optional:"yes" optional-value:"true"`
// Path to a log file
LogOutput string `yaml:"output" short:"o" long:"output" description:"Path to the log file. If not set, write to stdout."`
// Listen addrs
// --
// Server listen address
ListenAddrs []string `yaml:"listen-addrs" short:"l" long:"listen" description:"Listening addresses"`
// Server listen ports
ListenPorts []int `yaml:"listen-ports" short:"p" long:"port" description:"Listening ports. Zero value disables TCP and UDP listeners"`
// HTTPS listen ports
HTTPSListenPorts []int `yaml:"https-port" short:"s" long:"https-port" description:"Listening ports for DNS-over-HTTPS"`
// TLS listen ports
TLSListenPorts []int `yaml:"tls-port" short:"t" long:"tls-port" description:"Listening ports for DNS-over-TLS"`
// QUIC listen ports
QUICListenPorts []int `yaml:"quic-port" short:"q" long:"quic-port" description:"Listening ports for DNS-over-QUIC"`
// DNSCrypt listen ports
DNSCryptListenPorts []int `yaml:"dnscrypt-port" short:"y" long:"dnscrypt-port" description:"Listening ports for DNSCrypt"`
// Encryption config
// --
// Path to the .crt with the certificate chain
TLSCertPath string `yaml:"tls-crt" short:"c" long:"tls-crt" description:"Path to a file with the certificate chain"`
// Path to the file with the private key
TLSKeyPath string `yaml:"tls-key" short:"k" long:"tls-key" description:"Path to a file with the private key"`
// Minimum TLS version
TLSMinVersion float32 `yaml:"tls-min-version" long:"tls-min-version" description:"Minimum TLS version, for example 1.0" optional:"yes"`
// Minimum TLS version
TLSMaxVersion float32 `yaml:"tls-max-version" long:"tls-max-version" description:"Maximum TLS version, for example 1.3" optional:"yes"`
// Disable TLS certificate verification
Insecure bool `yaml:"insecure" long:"insecure" description:"Disable secure TLS certificate validation" optional:"yes" optional-value:"false"`
// Path to the DNSCrypt configuration file
DNSCryptConfigPath string `yaml:"dnscrypt-config" short:"g" long:"dnscrypt-config" description:"Path to a file with DNSCrypt configuration. You can generate one using https://github.com/ameshkov/dnscrypt"`
// Upstream DNS servers settings
// --
// DNS upstreams
Upstreams []string `yaml:"upstream" short:"u" long:"upstream" description:"An upstream to be used (can be specified multiple times). You can also specify path to a file with the list of servers" optional:"false"`
// Bootstrap DNS
BootstrapDNS []string `yaml:"bootstrap" short:"b" long:"bootstrap" description:"Bootstrap DNS for DoH and DoT, can be specified multiple times (default: 8.8.8.8:53)"`
// Fallback DNS resolver
Fallbacks []string `yaml:"fallback" short:"f" long:"fallback" description:"Fallback resolvers to use when regular ones are unavailable, can be specified multiple times. You can also specify path to a file with the list of servers"`
// If true, parallel queries to all configured upstream servers
AllServers bool `yaml:"all-servers" long:"all-servers" description:"If specified, parallel queries to all configured upstream servers are enabled" optional:"yes" optional-value:"true"`
// Respond to A or AAAA requests only with the fastest IP address
// detected by ICMP response time or TCP connection time
FastestAddress bool `yaml:"fastest-addr" long:"fastest-addr" description:"Respond to A or AAAA requests only with the fastest IP address" optional:"yes" optional-value:"true"`
// Cache settings
// --
// If true, DNS cache is enabled
Cache bool `yaml:"cache" long:"cache" description:"If specified, DNS cache is enabled" optional:"yes" optional-value:"true"`
// Cache size value
CacheSizeBytes int `yaml:"cache-size" long:"cache-size" description:"Cache size (in bytes). Default: 64k"`
// DNS cache minimum TTL value - overrides record value
CacheMinTTL uint32 `yaml:"cache-min-ttl" long:"cache-min-ttl" description:"Minimum TTL value for DNS entries, in seconds. Capped at 3600. Artificially extending TTLs should only be done with careful consideration."`
// DNS cache maximum TTL value - overrides record value
CacheMaxTTL uint32 `yaml:"cache-max-ttl" long:"cache-max-ttl" description:"Maximum TTL value for DNS entries, in seconds."`
// CacheOptimistic, if set to true, enables the optimistic DNS cache. That means that cached results will be served even if their cache TTL has already expired.
CacheOptimistic bool `yaml:"cache-optimistic" long:"cache-optimistic" description:"If specified, optimistic DNS cache is enabled" optional:"yes" optional-value:"true"`
// Anti-DNS amplification measures
// --
// Ratelimit value
Ratelimit int `yaml:"ratelimit" short:"r" long:"ratelimit" description:"Ratelimit (requests per second)"`
// If true, refuse ANY requests
RefuseAny bool `yaml:"refuse-any" long:"refuse-any" description:"If specified, refuse ANY requests" optional:"yes" optional-value:"true"`
// ECS settings
// --
// Use EDNS Client Subnet extension
EnableEDNSSubnet bool `yaml:"edns" long:"edns" description:"Use EDNS Client Subnet extension" optional:"yes" optional-value:"true"`
// Use Custom EDNS Client Address
EDNSAddr string `yaml:"edns-addr" long:"edns-addr" description:"Send EDNS Client Address"`
// DNS64 settings
// --
// Defines whether DNS64 functionality is enabled or not
DNS64 bool `yaml:"dns64" long:"dns64" description:"If specified, dnsproxy will act as a DNS64 server" optional:"yes" optional-value:"true"`
// DNS64Prefix defines the DNS64 prefix that dnsproxy should use when it acts as a DNS64 server
DNS64Prefix string `yaml:"dns64-prefix" long:"dns64-prefix" description:"If specified, this is the DNS64 prefix dnsproxy will be using when it works as a DNS64 server. If not specified, dnsproxy uses the 'Well-Known Prefix' 64:ff9b::" required:"false"`
// Other settings and options
// --
// If true, all AAAA requests will be replied with NoError RCode and empty answer
IPv6Disabled bool `yaml:"ipv6-disabled" long:"ipv6-disabled" description:"If specified, all AAAA requests will be replied with NoError RCode and empty answer" optional:"yes" optional-value:"true"`
// Transform responses that contain at least one of the given IP addresses into NXDOMAIN
BogusNXDomain []string `yaml:"bogus-nxdomain" long:"bogus-nxdomain" description:"Transform responses that contain at least one of the given IP addresses into NXDOMAIN. Can be specified multiple times."`
// UDP buffer size value
UDPBufferSize int `yaml:"udp-buf-size" long:"udp-buf-size" description:"Set the size of the UDP buffer in bytes. A value <= 0 will use the system default."`
// The maximum number of go routines
MaxGoRoutines int `yaml:"max-go-routines" long:"max-go-routines" description:"Set the maximum number of go routines. A value <= 0 will not not set a maximum."`
// Print DNSProxy version (just for the help)
Version bool `yaml:"version" long:"version" description:"Prints the program version"`
}
// VersionString will be set through ldflags, contains current version
var VersionString = "undefined" // nolint:gochecknoglobals
const defaultTimeout = 10 * time.Second
// defaultDNS64Prefix is a so-called "Well-Known Prefix" for DNS64.
// if dnsproxy operates as a DNS64 server, we'll be using it.
const defaultDNS64Prefix = "64:ff9b::/96"
func main() {
options := &Options{}
for _, arg := range os.Args {
if arg == "--version" {
fmt.Printf("dnsproxy version: %s\n", VersionString)
os.Exit(0)
}
// TODO(e.burkov, a.garipov): Use flag package and remove the manual
// options parsing.
//
// See https://github.com/AdguardTeam/dnsproxy/issues/182.
if len(arg) > 13 {
if arg[:13] == "--config-path" {
fmt.Printf("Path: %s\n", arg[14:])
b, err := ioutil.ReadFile(arg[14:])
if err != nil {
log.Fatalf("failed to read the config file %s: %v", arg[14:], err)
}
err = yaml.Unmarshal(b, options)
if err != nil {
log.Fatalf("failed to unmarshal the config file %s: %v", arg[14:], err)
}
}
}
}
parser := goFlags.NewParser(options, goFlags.Default)
_, err := parser.Parse()
if err != nil {
if flagsErr, ok := err.(*goFlags.Error); ok && flagsErr.Type == goFlags.ErrHelp {
os.Exit(0)
} else {
os.Exit(1)
}
}
run(options)
}
func run(options *Options) {
if options.Verbose {
log.SetLevel(log.DEBUG)
}
if options.LogOutput != "" {
file, err := os.OpenFile(options.LogOutput, os.O_WRONLY|os.O_CREATE|os.O_APPEND, 0644)
if err != nil {
log.Fatalf("cannot create a log file: %s", err)
}
defer file.Close() //nolint
log.SetOutput(file)
}
// Prepare the proxy server
tokenStore := quic.NewLRUTokenStore(5, 50)
clientSessionCache := tls.NewLRUClientSessionCache(100)
config := createProxyConfig(options, tokenStore, clientSessionCache)
dnsProxy := &proxy.Proxy{Config: config}
// Init DNS64 if needed
initDNS64(dnsProxy, options)
// Add extra handler if needed
if options.IPv6Disabled {
ipv6Configuration := ipv6Configuration{ipv6Disabled: options.IPv6Disabled}
dnsProxy.RequestHandler = ipv6Configuration.handleDNSRequest
}
// Start the proxy
err := dnsProxy.Start()
if err != nil {
log.Fatalf("cannot start the DNS proxy due to %s", err)
}
upstreamZero := config.UpstreamConfig.Upstreams[0]
signalChannel := make(chan os.Signal, 1)
signal.Notify(signalChannel, syscall.SIGINT, syscall.SIGTERM, syscall.SIGUSR1)
infinite:
for {
sig := <-signalChannel
if sig == syscall.SIGUSR1 {
log.Info("Creating new session")
upstreamZero.Reset()
} else {
break infinite
}
}
// Stopping the proxy
err = dnsProxy.Stop()
if err != nil {
log.Fatalf("cannot stop the DNS proxy due to %s", err)
}
}
// createProxyConfig creates proxy.Config from the command line arguments
func createProxyConfig(options *Options, tokenStore quic.TokenStore, clientSessionCache tls.ClientSessionCache) proxy.Config {
// Create the config
config := proxy.Config{
Ratelimit: options.Ratelimit,
CacheEnabled: options.Cache,
CacheSizeBytes: options.CacheSizeBytes,
CacheMinTTL: options.CacheMinTTL,
CacheMaxTTL: options.CacheMaxTTL,
CacheOptimistic: options.CacheOptimistic,
RefuseAny: options.RefuseAny,
// TODO(e.burkov): The following CIDRs are aimed to match any
// address. This is not quite proper approach to be used by
// default so think about configuring it.
TrustedProxies: []string{"0.0.0.0/0", "::0/0"},
EnableEDNSClientSubnet: options.EnableEDNSSubnet,
UDPBufferSize: options.UDPBufferSize,
MaxGoroutines: options.MaxGoRoutines,
}
initUpstreams(&config, options, tokenStore, clientSessionCache)
initEDNS(&config, options)
initBogusNXDomain(&config, options)
initTLSConfig(&config, options)
initDNSCryptConfig(&config, options)
initListenAddrs(&config, options)
return config
}
// initUpstreams inits upstream-related config
func initUpstreams(config *proxy.Config, options *Options, tokenStore quic.TokenStore, clientSessionCache tls.ClientSessionCache) {
// Init upstreams
upstreams := loadServersList(options.Upstreams)
upsOpts := &upstream.Options{
InsecureSkipVerify: options.Insecure,
Bootstrap: options.BootstrapDNS,
Timeout: defaultTimeout,
TokenStore: tokenStore,
ClientSessionCache: clientSessionCache,
}
upstreamConfig, err := proxy.ParseUpstreamsConfig(upstreams, upsOpts)
if err != nil {
log.Fatalf("error while parsing upstreams configuration: %s", err)
}
config.UpstreamConfig = upstreamConfig
if options.AllServers {
config.UpstreamMode = proxy.UModeParallel
} else if options.FastestAddress {
config.UpstreamMode = proxy.UModeFastestAddr
} else {
config.UpstreamMode = proxy.UModeLoadBalance
}
if options.Fallbacks != nil {
fallbacks := []upstream.Upstream{}
for i, f := range loadServersList(options.Fallbacks) {
// Use the same options for fallback servers as for
// upstream servers until it is possible to configure it
// separately.
//
// See https://github.com/AdguardTeam/dnsproxy/issues/161.
fallback, err := upstream.AddressToUpstream(f, upsOpts)
if err != nil {
log.Fatalf("cannot parse the fallback %s (%s): %s", f, options.BootstrapDNS, err)
}
log.Printf("Fallback %d is %s", i, fallback.Address())
fallbacks = append(fallbacks, fallback)
}
config.Fallbacks = fallbacks
}
}
// initEDNS inits EDNS-related config
func initEDNS(config *proxy.Config, options *Options) {
if options.EDNSAddr != "" {
if options.EnableEDNSSubnet {
ednsIP := net.ParseIP(options.EDNSAddr)
if ednsIP == nil {
log.Fatalf("cannot parse %s", options.EDNSAddr)
}
config.EDNSAddr = ednsIP
} else {
log.Printf("--edns-addr=%s need --edns to work", options.EDNSAddr)
}
}
}
// initBogusNXDomain inits BogusNXDomain structure
func initBogusNXDomain(config *proxy.Config, options *Options) {
if len(options.BogusNXDomain) > 0 {
bogusIP := []net.IP{}
for _, s := range options.BogusNXDomain {
ip := net.ParseIP(s)
if ip == nil {
log.Error("Invalid IP: %s", s)
} else {
bogusIP = append(bogusIP, ip)
}
}
config.BogusNXDomain = bogusIP
}
}
// initTLSConfig inits the TLS config
func initTLSConfig(config *proxy.Config, options *Options) {
if options.TLSCertPath != "" && options.TLSKeyPath != "" {
tlsConfig, err := newTLSConfig(options)
if err != nil {
log.Fatalf("failed to load TLS config: %s", err)
}
config.TLSConfig = tlsConfig
}
}
// initDNSCryptConfig inits the DNSCrypt config
func initDNSCryptConfig(config *proxy.Config, options *Options) {
if options.DNSCryptConfigPath == "" {
return
}
b, err := ioutil.ReadFile(options.DNSCryptConfigPath)
if err != nil {
log.Fatalf("failed to read DNSCrypt config %s: %v", options.DNSCryptConfigPath, err)
}
rc := &dnscrypt.ResolverConfig{}
err = yaml.Unmarshal(b, rc)
if err != nil {
log.Fatalf("failed to unmarshal DNSCrypt config: %v", err)
}
cert, err := rc.CreateCert()
if err != nil {
log.Fatalf("failed to create DNSCrypt certificate: %v", err)
}
config.DNSCryptResolverCert = cert
config.DNSCryptProviderName = rc.ProviderName
}
// initListenAddrs inits listen addrs
func initListenAddrs(config *proxy.Config, options *Options) {
listenIPs := []net.IP{}
if len(options.ListenAddrs) == 0 {
// If ListenAddrs has not been parsed through config file nor command
// line we set it to "0.0.0.0".
options.ListenAddrs = []string{"0.0.0.0"}
}
if len(options.ListenPorts) == 0 {
// If ListenPorts has not been parsed through config file nor command
// line we set it to 53.
options.ListenPorts = []int{53}
}
for _, a := range options.ListenAddrs {
ip := net.ParseIP(a)
if ip == nil {
log.Fatalf("cannot parse %s", a)
}
listenIPs = append(listenIPs, ip)
}
if len(options.ListenPorts) != 0 && options.ListenPorts[0] != 0 {
for _, port := range options.ListenPorts {
for _, ip := range listenIPs {
ua := &net.UDPAddr{Port: port, IP: ip}
config.UDPListenAddr = append(config.UDPListenAddr, ua)
ta := &net.TCPAddr{Port: port, IP: ip}
config.TCPListenAddr = append(config.TCPListenAddr, ta)
}
}
}
if config.TLSConfig != nil {
for _, port := range options.TLSListenPorts {
for _, ip := range listenIPs {
a := &net.TCPAddr{Port: port, IP: ip}
config.TLSListenAddr = append(config.TLSListenAddr, a)
}
}
for _, port := range options.HTTPSListenPorts {
for _, ip := range listenIPs {
a := &net.TCPAddr{Port: port, IP: ip}
config.HTTPSListenAddr = append(config.HTTPSListenAddr, a)
}
}
for _, port := range options.QUICListenPorts {
for _, ip := range listenIPs {
a := &net.UDPAddr{Port: port, IP: ip}
config.QUICListenAddr = append(config.QUICListenAddr, a)
}
}
}
if config.DNSCryptResolverCert != nil && config.DNSCryptProviderName != "" {
for _, port := range options.DNSCryptListenPorts {
for _, ip := range listenIPs {
tcp := &net.TCPAddr{Port: port, IP: ip}
config.DNSCryptTCPListenAddr = append(config.DNSCryptTCPListenAddr, tcp)
udp := &net.UDPAddr{Port: port, IP: ip}
config.DNSCryptUDPListenAddr = append(config.DNSCryptUDPListenAddr, udp)
}
}
}
}
// initDNS64 inits the DNS64 configuration for dnsproxy
func initDNS64(p *proxy.Proxy, options *Options) {
if !options.DNS64 {
return
}
dns64Prefix := options.DNS64Prefix
if dns64Prefix == "" {
dns64Prefix = defaultDNS64Prefix
}
// DNS64 prefix may be specified as a CIDR: "64:ff9b::/96"
ip, _, err := net.ParseCIDR(dns64Prefix)
if err != nil {
// Or it could be specified as an IP address: "64:ff9b::"
ip = net.ParseIP(dns64Prefix)
}
if ip == nil || len(ip) < net.IPv6len {
log.Fatalf("Invalid DNS64 prefix: %s", dns64Prefix)
return
}
p.SetNAT64Prefix(ip[:proxy.NAT64PrefixLength])
}
// IPv6 configuration
type ipv6Configuration struct {
ipv6Disabled bool // If true, all AAAA requests will be replied with NoError RCode and empty answer
}
// handleDNSRequest checks IPv6 configuration for current session before resolve
func (c *ipv6Configuration) handleDNSRequest(p *proxy.Proxy, ctx *proxy.DNSContext) error {
if proxy.CheckDisabledAAAARequest(ctx, c.ipv6Disabled) {
return nil
}
return p.Resolve(ctx)
}
// NewTLSConfig returns a TLS config that includes a certificate
// Use for server TLS config or when using a client certificate
// If caPath is empty, system CAs will be used
func newTLSConfig(options *Options) (*tls.Config, error) {
// Set default TLS min/max versions
tlsMinVersion := tls.VersionTLS10 // Default for crypto/tls
tlsMaxVersion := tls.VersionTLS13 // Default for crypto/tls
switch options.TLSMinVersion {
case 1.1:
tlsMinVersion = tls.VersionTLS11
case 1.2:
tlsMinVersion = tls.VersionTLS12
case 1.3:
tlsMinVersion = tls.VersionTLS13
}
switch options.TLSMaxVersion {
case 1.0:
tlsMaxVersion = tls.VersionTLS10
case 1.1:
tlsMaxVersion = tls.VersionTLS11
case 1.2:
tlsMaxVersion = tls.VersionTLS12
}
cert, err := loadX509KeyPair(options.TLSCertPath, options.TLSKeyPath)
if err != nil {
return nil, fmt.Errorf("could not load TLS cert: %s", err)
}
return &tls.Config{Certificates: []tls.Certificate{cert}, MinVersion: uint16(tlsMinVersion), MaxVersion: uint16(tlsMaxVersion)}, nil
}
// loadX509KeyPair reads and parses a public/private key pair from a pair
// of files. The files must contain PEM encoded data. The certificate file
// may contain intermediate certificates following the leaf certificate to
// form a certificate chain. On successful return, Certificate.Leaf will
// be nil because the parsed form of the certificate is not retained.
func loadX509KeyPair(certFile, keyFile string) (tls.Certificate, error) {
certPEMBlock, err := ioutil.ReadFile(certFile)
if err != nil {
return tls.Certificate{}, err
}
keyPEMBlock, err := ioutil.ReadFile(keyFile)
if err != nil {
return tls.Certificate{}, err
}
return tls.X509KeyPair(certPEMBlock, keyPEMBlock)
}
// loadServersList loads a list of DNS servers from the specified list.
// the thing is that the user may specify either a server address
// or path to a file with a list of addresses. This method takes care of it,
// reads the file, loads servers from it if needed.
func loadServersList(sources []string) []string {
var servers []string
for _, source := range sources {
data, err := ioutil.ReadFile(source)
if err != nil {
// Ignore errors, just consider it a server address
// and not a file
servers = append(servers, source)
}
lines := strings.Split(string(data), "\n")
for _, line := range lines {
line = strings.TrimSpace(line)
// Ignore comments in the file
if line == "" ||
strings.HasPrefix(line, "!") ||
strings.HasPrefix(line, "#") {
continue
}
servers = append(servers, line)
}
}
return servers
}