diff --git a/core/assets/path.go b/core/assets/path.go index 46b883b1c..abe4638d0 100644 --- a/core/assets/path.go +++ b/core/assets/path.go @@ -8,9 +8,9 @@ import ( // Path returns the path to the folder // where the application may store data. This -// currently resolves to ~/.caddy +// currently resolves to ~/.coredns func Path() string { - return filepath.Join(userHomeDir(), ".caddy") + return filepath.Join(userHomeDir(), ".coredns") } // userHomeDir returns the user's home directory according to diff --git a/core/assets/path_test.go b/core/assets/path_test.go index 374f813af..50e8841d9 100644 --- a/core/assets/path_test.go +++ b/core/assets/path_test.go @@ -6,7 +6,7 @@ import ( ) func TestPath(t *testing.T) { - if actual := Path(); !strings.HasSuffix(actual, ".caddy") { + if actual := Path(); !strings.HasSuffix(actual, ".coredns") { t.Errorf("Expected path to be a .caddy folder, got: %v", actual) } } diff --git a/middleware/log/log.go b/middleware/log/log.go index 645914c92..d949ea7b0 100644 --- a/middleware/log/log.go +++ b/middleware/log/log.go @@ -22,7 +22,7 @@ type Logger struct { func (l Logger) ServeDNS(ctx context.Context, w dns.ResponseWriter, r *dns.Msg) (int, error) { state := middleware.State{W: w, Req: r} for _, rule := range l.Rules { - if middleware.Name(state.Name()).Matches(rule.NameScope) { + if middleware.Name(rule.NameScope).Matches(state.Name()) { responseRecorder := middleware.NewResponseRecorder(w) rcode, err := l.Next.ServeDNS(ctx, responseRecorder, r) diff --git a/middleware/name.go b/middleware/name.go index ab9772b37..616ae68bd 100644 --- a/middleware/name.go +++ b/middleware/name.go @@ -9,13 +9,14 @@ import ( // Name represents a domain name. type Name string -// Matches checks to see if other matches n. -// -// Name matching will probably not always be a direct -// comparison; this method assures that names can be -// easily and consistently matched. -func (n Name) Matches(other string) bool { - return strings.HasSuffix(string(n), other) +// Matches checks to see if other is a subdomain (or the same domain) of n. +// This method assures that names can be easily and consistently matched. +func (n Name) Matches(child string) bool { + if dns.Name(n) == dns.Name(child) { + return true + } + + return dns.IsSubDomain(string(n), child) } // Normalize lowercases and makes n fully qualified. diff --git a/middleware/proxy/upstream.go b/middleware/proxy/upstream.go index 52375323e..7f339c01f 100644 --- a/middleware/proxy/upstream.go +++ b/middleware/proxy/upstream.go @@ -238,6 +238,9 @@ func (u *staticUpstream) Select() *UpstreamHost { func (u *staticUpstream) IsAllowedPath(name string) bool { for _, ignoredSubDomain := range u.IgnoredSubDomains { + if dns.Name(name) == dns.Name(u.From()) { + return true + } if middleware.Name(name).Matches(ignoredSubDomain + u.From()) { return false } diff --git a/middleware/proxy/upstream_test.go b/middleware/proxy/upstream_test.go index 6f96d7ce2..3d69ca8fb 100644 --- a/middleware/proxy/upstream_test.go +++ b/middleware/proxy/upstream_test.go @@ -70,7 +70,7 @@ func TestAllowedPaths(t *testing.T) { for i, test := range tests { isAllowed := upstream.IsAllowedPath(test.name) if test.expected != isAllowed { - t.Errorf("Test %d: expected %v found %v", i+1, test.expected, isAllowed) + t.Errorf("Test %d: expected %v found %v for %s", i+1, test.expected, isAllowed, test.name) } } } diff --git a/middleware/rewrite/condition.go b/middleware/rewrite/condition.go index ddd4c38b1..3ccde21f9 100644 --- a/middleware/rewrite/condition.go +++ b/middleware/rewrite/condition.go @@ -76,6 +76,7 @@ func startsWithFunc(a, b string) bool { // endsWithFunc is condition for EndsWith operator. // It checks if b is a suffix of a. func endsWithFunc(a, b string) bool { + // TODO(miek): IsSubDomain return strings.HasSuffix(a, b) } diff --git a/middleware/zone.go b/middleware/zone.go index 46c17b62d..0f3cd0edc 100644 --- a/middleware/zone.go +++ b/middleware/zone.go @@ -1,21 +1,16 @@ package middleware -import ( - "strings" - - "github.com/miekg/dns" -) +import "github.com/miekg/dns" type Zones []string -// Matches checks to see if other matches p. The match will return the most -// specific zones that matches other. The empty string signals a not found -// condition. +// Matches checks is qname is a subdomain of any of the zones in z. The match +// will return the most specific zones that matches other. The empty string +// signals a not found condition. func (z Zones) Matches(qname string) string { zone := "" - // TODO(miek): use IsSubDomain here? for _, zname := range z { - if strings.HasSuffix(qname, zname) { + if dns.IsSubDomain(zname, qname) { if len(zname) > len(zone) { zone = zname }