Skip to content

Commit

Permalink
fix #73: bootstrapDNS
Browse files Browse the repository at this point in the history
  • Loading branch information
machsix authored and relekang committed Jan 3, 2025
1 parent 796e5b7 commit e5171ef
Show file tree
Hide file tree
Showing 4 changed files with 58 additions and 8 deletions.
6 changes: 5 additions & 1 deletion example/Corefile
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,11 @@
debug
prometheus

blocklist https://raw.githubusercontent.com/StevenBlack/hosts/master/hosts
blocklist https://raw.githubusercontent.com/StevenBlack/hosts/master/hosts {
# if CoreDNS listens at 53, you need another DNS to bootstrap the download
bootstrap_dns 1.1.1.1:53
}

blocklist blocklist.txt {
allowlist allowlist.txt
domain_metrics
Expand Down
47 changes: 43 additions & 4 deletions list_loader.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,33 +2,72 @@ package blocklist

import (
"bufio"
"context"
"io"
"net"
"net/http"
"os"
"path/filepath"
"regexp"
"strings"
"time"

"github.com/coredns/caddy"
)

func loadList(c *caddy.Controller, location string) ([]string, error) {
func loadList(c *caddy.Controller, location string, bootStrapDNS string) ([]string, error) {
log.Infof("Loading from %s", location)
if strings.HasPrefix(location, "http://") || strings.HasPrefix(location, "https://") {
return loadListFromUrl(c, location)
return loadListFromUrl(c, location, bootStrapDNS)
}
return loadListFromFile(c, location)
}

func loadListFromUrl(c *caddy.Controller, name string) ([]string, error) {
response, err := http.Get(name)
func loadListFromUrl(c *caddy.Controller, name string, bootStrapDNS string) ([]string, error) {
client := &http.Client{}
if bootStrapDNS != "" {
client = customDNS(bootStrapDNS)
}
response, err := client.Get(name)
if err != nil {
return nil, err
}
defer response.Body.Close()
return collectDomains(response.Body, name)
}

func customDNS(bootStrapDNS string) *http.Client {
var (
dnsResolverIP = bootStrapDNS // Google DNS resolver.
dnsResolverProto = "udp" // Protocol to use for the DNS resolver
dnsResolverTimeoutMs = 5000 // Timeout (ms) for the DNS resolver (optional)
)

dialer := &net.Dialer{
Resolver: &net.Resolver{
PreferGo: true,
Dial: func(ctx context.Context, network, address string) (net.Conn, error) {
d := net.Dialer{
Timeout: time.Duration(dnsResolverTimeoutMs) * time.Millisecond,
}
return d.DialContext(ctx, dnsResolverProto, dnsResolverIP)
},
},
}
dialContext := func(ctx context.Context, network, addr string) (net.Conn, error) {
return dialer.DialContext(ctx, network, addr)
}
tr := &http.Transport{
MaxIdleConns: 10,
IdleConnTimeout: 30 * time.Second,
DisableCompression: true,
DialContext: dialContext,
}
client := &http.Client{Transport: tr}

return client
}

func loadListFromFile(c *caddy.Controller, name string) ([]string, error) {
if !filepath.IsAbs(name) {
name = filepath.Join(
Expand Down
6 changes: 5 additions & 1 deletion readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,11 @@ domain on each line. There is an example file in the example folder.
prometheus
# load from url
blocklist https://mirror1.malwaredomains.com/files/justdomains
blocklist https://mirror1.malwaredomains.com/files/justdomains {
# if CoreDNS listens at 53, you need another DNS to bootstrap the download
bootstrap_dns 1.1.1.1:53
}
# load from file, if the path is not absolute it will be relative to the Corefile
blocklist blocklist.txt
Expand Down
7 changes: 5 additions & 2 deletions setup.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ func setup(c *caddy.Controller) error {
var allowlistLocation string
var allowlist []string
var blockResponse string
var bootStrapDNS string
c.Args(&blocklistLocation)

if blocklistLocation == "" {
Expand All @@ -39,6 +40,8 @@ func setup(c *caddy.Controller) error {
log.Debugf("Setting allowlist location to %s", allowlistLocation)
case "domain_metrics":
domainMetrics = true
case "bootstrap_dns":
bootStrapDNS = c.RemainingArgs()[0]
case "block_response":
remaining := c.RemainingArgs()
if len(remaining) != 1 {
Expand All @@ -56,13 +59,13 @@ func setup(c *caddy.Controller) error {
return plugin.Error("blocklist", errors.New("To many arguments for blocklist."))
}

blocklist, err := loadList(c, blocklistLocation)
blocklist, err := loadList(c, blocklistLocation, bootStrapDNS)
if err != nil {
return plugin.Error("blocklist", err)
}

if allowlistLocation != "" {
allowlist, err = loadList(c, allowlistLocation)
allowlist, err = loadList(c, allowlistLocation, bootStrapDNS)
if err != nil {
return plugin.Error("blocklist", err)
}
Expand Down

0 comments on commit e5171ef

Please sign in to comment.