Skip to content

Commit

Permalink
Use the configured resolvers exclusively (caddyserver#131) (fix caddy…
Browse files Browse the repository at this point in the history
…server#132)

* feature: add optional !important suffix

if !important is added to any of the resolvers, then all are considered
exclusive and no other fallbacks will be added.

* fix: !important can be on it's own

* simplify recursiveNameservers

- use custom OR default nameservers
- add testing

* removed print line

* tests: fixed defaults when resolv.conf is found
  • Loading branch information
kmpm authored Jun 8, 2021
1 parent a1d0012 commit b668c8b
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 7 deletions.
14 changes: 10 additions & 4 deletions dnsutil.go
Original file line number Diff line number Diff line change
Expand Up @@ -313,11 +313,17 @@ func updateDomainWithCName(r *dns.Msg, fqdn string) string {
}

// recursiveNameservers are used to pre-check DNS propagation. It
// prepends user-configured nameservers (custom) to the defaults
// obtained from resolv.conf and defaultNameservers and ensures
// that all server addresses have a port value.
// picks user-configured nameservers (custom) OR the defaults
// obtained from resolv.conf and defaultNameservers if none is
// configured and ensures that all server addresses have a port value.
func recursiveNameservers(custom []string) []string {
servers := append(custom, systemOrDefaultNameservers(defaultResolvConf, defaultNameservers)...)
var servers []string
if len(custom) == 0 {
servers = systemOrDefaultNameservers(defaultResolvConf, defaultNameservers)
} else {
servers = make([]string, len(custom))
copy(servers, custom)
}
populateNameserverPorts(servers)
return servers
}
Expand Down
36 changes: 33 additions & 3 deletions dnsutil_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ package certmagic
// It has been modified.

import (
"net"
"reflect"
"sort"
"strings"
Expand Down Expand Up @@ -206,19 +207,48 @@ func TestResolveConfServers(t *testing.T) {
}

func TestRecursiveNameserversAddsPort(t *testing.T) {
custom := []string{"127.0.0.1"}
type want struct {
port string
}
custom := []string{"127.0.0.1", "ns1.google.com:43"}
expectations := []want{{port: "53"}, {port: "43"}}
results := recursiveNameservers(custom)

if !reflect.DeepEqual(custom, []string{"127.0.0.1", "ns1.google.com:43"}) {
t.Errorf("Expected custom nameservers to be unmodified. got %v", custom)
}

if len(results) != len(expectations) {
t.Errorf("%v wrong results length. got %d, want %d", results, len(results), len(expectations))
}

var hasCustom bool
for i, res := range results {
hasCustom = hasCustom || strings.HasPrefix(res, custom[0])
if !strings.HasSuffix(res, ":53") {
t.Errorf("%v Expected all results to have a port, but result %d doesn't: %s", results, i, res)
if _, port, err := net.SplitHostPort(res); err != nil {
t.Errorf("%v Error splitting result %d into host and port: %v", results, i, err)
} else {
if port != expectations[i].port {
t.Errorf("%v Expected result %d to have port %s but got %s", results, i, expectations[i].port, port)
}
}
}
if !hasCustom {
t.Errorf("%v Expected custom resolvers to be included, but they weren't: %v", results, custom)
}

}

func TestRecursiveNameserversDefaults(t *testing.T) {
results := recursiveNameservers(nil)
if len(results) < 1 {
t.Errorf("%v Expected at least 1 records as default when nil custom", results)
}

results = recursiveNameservers([]string{})
if len(results) < 1 {
t.Errorf("%v Expected at least 1 records as default when empty custom", results)
}
}

func clearFqdnCache() {
Expand Down

0 comments on commit b668c8b

Please sign in to comment.