Use dns.IsSubDomain (#112)

For the match function use the proper thing from go dns. Fix all
callers and tests to use this.

Fixes: #107
This commit is contained in:
Miek Gieben 2016-04-12 22:34:44 +01:00
parent 842953f179
commit 25cf16af0e
8 changed files with 22 additions and 22 deletions

View file

@ -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

View file

@ -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)
}
}

View file

@ -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)

View file

@ -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.

View file

@ -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
}

View file

@ -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)
}
}
}

View file

@ -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)
}

View file

@ -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
}