From 09a39cce03f69b1f9801e66d763d7b5208411336 Mon Sep 17 00:00:00 2001 From: Eugene Bujak Date: Tue, 25 Sep 2018 19:26:26 +0300 Subject: [PATCH] Allow disabling of filtering but keeping querylog, safebrowsing, safesearch and parental working. --- config.go | 10 ++--- coredns_plugin/coredns_plugin.go | 55 ++++++++++++++------------- coredns_plugin/coredns_plugin_test.go | 3 +- 3 files changed, 36 insertions(+), 32 deletions(-) diff --git a/config.go b/config.go index ee3d91df..e44345b4 100644 --- a/config.go +++ b/config.go @@ -160,16 +160,16 @@ func writeAllConfigs() error { } const coreDNSConfigTemplate = `. { - {{if .FilteringEnabled}}dnsfilter {{.FilterFile}} { + dnsfilter {{if .FilteringEnabled}}{{.FilterFile}}{{end}} { {{if .SafeBrowsingEnabled}}safebrowsing{{end}} {{if .ParentalEnabled}}parental {{.ParentalSensitivity}}{{end}} {{if .SafeSearchEnabled}}safesearch{{end}} {{if .QueryLogEnabled}}querylog{{end}} - }{{end}} + } {{.Pprof}} - hosts { - fallthrough - } + hosts { + fallthrough + } {{if .UpstreamDNS}}forward . {{range .UpstreamDNS}}{{.}} {{end}}{{end}} {{.Cache}} {{.Prometheus}} diff --git a/coredns_plugin/coredns_plugin.go b/coredns_plugin/coredns_plugin.go index 0000156c..09b64a9e 100644 --- a/coredns_plugin/coredns_plugin.go +++ b/coredns_plugin/coredns_plugin.go @@ -92,14 +92,12 @@ func setupPlugin(c *caddy.Controller) (*plug, error) { p.d = dnsfilter.New() p.hosts = make(map[string]net.IP) - var filterFileName string + filterFileNames := []string{} for c.Next() { args := c.RemainingArgs() - if len(args) == 0 { - // must have at least one argument - return nil, c.ArgErr() + if len(args) > 0 { + filterFileNames = append(filterFileNames, args...) } - filterFileName = args[0] for c.NextBlock() { switch c.Val() { case "safebrowsing": @@ -139,34 +137,39 @@ func setupPlugin(c *caddy.Controller) (*plug, error) { } } - file, err := os.Open(filterFileName) - if err != nil { - return nil, err - } - defer file.Close() + log.Printf("filterFileNames = %+v", filterFileNames) - count := 0 - scanner := bufio.NewScanner(file) - for scanner.Scan() { - text := scanner.Text() - if p.parseEtcHosts(text) { - continue - } - err = p.d.AddRule(text, 0) - if err == dnsfilter.ErrInvalidSyntax { - continue - } + for i, filterFileName := range filterFileNames { + file, err := os.Open(filterFileName) if err != nil { return nil, err } - count++ - } - log.Printf("Added %d rules from %s", count, filterFileName) + defer file.Close() - if err = scanner.Err(); err != nil { - return nil, err + count := 0 + scanner := bufio.NewScanner(file) + for scanner.Scan() { + text := scanner.Text() + if p.parseEtcHosts(text) { + continue + } + err = p.d.AddRule(text, uint32(i)) + if err == dnsfilter.ErrInvalidSyntax { + continue + } + if err != nil { + return nil, err + } + count++ + } + log.Printf("Added %d rules from %s", count, filterFileName) + + if err = scanner.Err(); err != nil { + return nil, err + } } + var err error p.upstream, err = upstream.New(nil) if err != nil { return nil, err diff --git a/coredns_plugin/coredns_plugin_test.go b/coredns_plugin/coredns_plugin_test.go index 5c309269..17210b63 100644 --- a/coredns_plugin/coredns_plugin_test.go +++ b/coredns_plugin/coredns_plugin_test.go @@ -20,7 +20,8 @@ func TestSetup(t *testing.T) { config string failing bool }{ - {`dnsfilter`, true}, + {`dnsfilter`, false}, + {`dnsfilter /dev/nonexistent/abcdef`, true}, {`dnsfilter ../tests/dns.txt`, false}, {`dnsfilter ../tests/dns.txt { safebrowsing }`, false}, {`dnsfilter ../tests/dns.txt { parental }`, true},