Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Dereference CNAMES at the root level #43

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions server/dns.go
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,14 @@ func answerQuestion(qtype uint16, name ...string) []dns.RR {
entry.Header().Name = name[0]
if entry.Header().Rrtype == qtype || qtype == dns.TypeANY {
answers = append(answers, entry)
} else {
// dereference root domain CNAMEs for A and AAAA lookups
if stripSubdomain(qName) == "" && entry.Header().Rrtype == dns.TypeCNAME && (qtype == dns.TypeA || qtype == dns.TypeAAAA) {
for _, t := range answerQuestion(qtype, name[0], dns.Field(entry, 1)) {
t.Header().Name = name[0]
answers = append(answers, t)
}
}
}
}

Expand Down
18 changes: 18 additions & 0 deletions server/dns_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import (
)

var nanopack = sham.Resource{Domain: "nanopack.io.", Records: []sham.Record{{Address: "127.0.0.1"}}}
var rootname = sham.Resource{Domain: "nanopack.com.", Records: []sham.Record{{Address: "nanopack.io", RType: "CNAME"}}}

func TestMain(m *testing.M) {
// manually configure
Expand All @@ -39,6 +40,12 @@ func TestDNS(t *testing.T) {
t.FailNow()
}

err = shaman.AddRecord(&rootname)
if err != nil {
t.Errorf("Failed to add root CNAME record - %v", err)
t.FailNow()
}

r, err := ResolveIt("nanopack.io", dns.TypeA)
if err != nil {
t.Errorf("Failed to get record - %v", err)
Expand All @@ -50,6 +57,17 @@ func TestDNS(t *testing.T) {
t.Errorf("Response doesn't match expected - %+q", r.Answer[0].String())
}

r, err = ResolveIt("nanopack.com", dns.TypeA)
if err != nil {
t.Errorf("Failed to get record - %v", err)
}
if len(r.Answer) == 0 {
t.Error("No record found")
}
if len(r.Answer) > 0 && r.Answer[0].String() != "nanopack.com.\t60\tIN\tA\t127.0.0.1" {
t.Errorf("Response doesn't match expected - %+q", r.Answer[0].String())
}

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