Skip to content

Commit

Permalink
Search upper level domains if domain not found
Browse files Browse the repository at this point in the history
  • Loading branch information
glinton committed May 13, 2016
1 parent 0a9a6b2 commit 67d5d59
Show file tree
Hide file tree
Showing 4 changed files with 43 additions and 8 deletions.
1 change: 0 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@ shaman add -d nanopack.io -A 127.0.0.1
dig @localhost nanopack.io +short
# 127.0.0.1


# Congratulations!
```

Expand Down
2 changes: 1 addition & 1 deletion core/common/common.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ func SanitizeDomain(domain *string) {
}
}

// UnsanitizeDomain ensures the domain ends with a `.`
// UnsanitizeDomain ensures the domain does not end with a `.`
func UnsanitizeDomain(domain *string) {
t := []byte(*domain)
if len(t) > 0 && t[len(t)-1] == '.' {
Expand Down
46 changes: 41 additions & 5 deletions server/dns.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package server

import (
"fmt"
"strings"

"github.com/miekg/dns"

Expand Down Expand Up @@ -45,13 +46,18 @@ func handlerFunc(res dns.ResponseWriter, req *dns.Msg) {
// answerQuestion returns resource record answers for the domain in question
func answerQuestion(question dns.Question) []dns.RR {
answers := make([]dns.RR, 0)
r, _ := shaman.GetRecord(question.Name)
records := r.StringSlice()
// fmt.Printf("Records received - %+q\n", records)
for _, record := range records {

// get the resource (check memory, cache, and (todo:) upstream)
r, err := shaman.GetRecord(question.Name)
if err != nil {
config.Log.Trace("Failed to get records for '%s' - %v", question.Name, err)
}

// validate the records and append correct type to answers[]
for _, record := range r.StringSlice() {
entry, err := dns.NewRR(record)
if err != nil {
config.Log.Trace("Failed to create RR from record - %v", err)
config.Log.Debug("Failed to create RR from record - %v", err)
continue
}
entry.Header().Name = question.Name
Expand All @@ -60,5 +66,35 @@ func answerQuestion(question dns.Question) []dns.RR {
}
}

// todo: should `shaman.GetRecord` be wildcard aware (*.domain.com) or is this ok
// recursively resolve if no records found
if len(answers) == 0 {
question.Name = stripSubdomain(question.Name)
if len(question.Name) > 0 {
config.Log.Trace("Checking again with '%v'", question.Name)
return answerQuestion(question)
}
}

return answers
}

// stripSubdomain strips off the subbest domain, returning the domain (won't return TLD)
func stripSubdomain(name string) string {
words := 3 // assume rooted domain (end with '.')
// handle edge case of unrooted domain
t := []byte(name)
if len(t) > 0 && t[len(t)-1] != '.' {
words = 2
}

config.Log.Trace("Stripping subdomain from '%v'", name)
names := strings.Split(name, ".")

// prevent searching for just 'com.' (["domain", "com", ""])
if len(names) > words {
return strings.Join(names[1:], ".")
} else {
return ""
}
}
2 changes: 1 addition & 1 deletion server/dns_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ func TestDNS(t *testing.T) {
t.Errorf("Response doesn't match expected - %+q", r.Answer[0].String())
}

r, err = ResolveIt("nanobox.io", dns.TypeA)
r, err = ResolveIt("a.b.nanobox.io", dns.TypeA)
if err != nil {
t.Errorf("Failed to get record - %v", err)
}
Expand Down

0 comments on commit 67d5d59

Please sign in to comment.