diff --git a/server/dns.go b/server/dns.go index 7510e6e..0caff6c 100644 --- a/server/dns.go +++ b/server/dns.go @@ -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) + } + } } } diff --git a/server/dns_test.go b/server/dns_test.go index 7e56c7f..89341ff 100644 --- a/server/dns_test.go +++ b/server/dns_test.go @@ -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 @@ -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) @@ -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)