Skip to content

Commit

Permalink
Fix for cnames see arvancloud#4
Browse files Browse the repository at this point in the history
Changes taken from issue author

Signed-off-by: Jo De Boeck <[email protected]>
  • Loading branch information
grimpy committed Oct 3, 2019
1 parent 8a6e3c4 commit 7789699
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 5 deletions.
28 changes: 26 additions & 2 deletions handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ package redis

import (
"fmt"
// "fmt"
"strings"
"time"

"github.com/coredns/coredns/plugin"
Expand Down Expand Up @@ -77,7 +77,17 @@ func (redis *Redis) ServeDNS(ctx context.Context, w dns.ResponseWriter, r *dns.M

switch qtype {
case "A":
answers, extras = redis.A(qname, z, record)
// Having a CNAME excludes A records, add cnames when querying for A records
if len(record.CNAME) > 0 {
//println("We have a cname in the record")
answers2 := make([]dns.RR, 0, 10)
extras2 := make([]dns.RR, 0, 10)
answers2, extras2 = redis.CNAME(qname, z, record)
answers = append(answers, answers2...)
extras = append(extras, extras2...)
} else {
answers, extras = redis.A(qname, z, record)
}
case "AAAA":
answers, extras = redis.AAAA(qname, z, record)
case "CNAME":
Expand Down Expand Up @@ -106,6 +116,20 @@ func (redis *Redis) ServeDNS(ctx context.Context, w dns.ResponseWriter, r *dns.M
m.Answer = append(m.Answer, answers...)
m.Extra = append(m.Extra, extras...)

// If there is a CNAME RR in the answers, solve the alias
for _, CNAMERecord := range record.CNAME {
println(CNAMERecord.Host)
var query = strings.TrimSuffix(CNAMERecord.Host, "."+z.Name)
records := redis.get(query, z)

answersN := make([]dns.RR, 0, 10)
extrasN := make([]dns.RR, 0, 10)
answersN, extrasN = redis.A(CNAMERecord.Host, z, records)
m.Answer = append(m.Answer, answersN...)
m.Extra = append(m.Extra, extrasN...)

}

state.SizeAndDo(m)
m = state.Scrub(m)
_ = w.WriteMsg(m)
Expand Down
6 changes: 3 additions & 3 deletions lookup_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,10 @@ var zones = []string {
"example.com.", "example.net.",
}

var lookupEntries = [][][]string {
var lookupEntries = [][][]string{
{
{"@",
"{\"soa\":{\"ttl\":300, \"minttl\":100, \"mbox\":\"hostmaster.example.com.\",\"ns\":\"ns1.example.com.\",\"refresh\":44,\"retry\":55,\"expire\":66}}",
"{\"a\":[{\"ttl\":300, \"ip\":\"5.5.5.5\"}], \"soa\":{\"ttl\":300, \"minttl\":100, \"mbox\":\"hostmaster.example.com.\",\"ns\":\"ns1.example.com.\",\"refresh\":44,\"retry\":55,\"expire\":66}}",
},
{"x",
"{\"a\":[{\"ttl\":300, \"ip\":\"1.2.3.4\"},{\"ttl\":300, \"ip\":\"5.6.7.8\"}]," +
Expand All @@ -28,7 +28,7 @@ var lookupEntries = [][][]string {
"\"mx\":[{\"ttl\":300, \"host\":\"mx1.example.com.\", \"preference\":10},{\"ttl\":300, \"host\":\"mx2.example.com.\", \"preference\":10}]}",
},
{"y",
"{\"cname\":[{\"ttl\":300, \"host\":\"x.example.com.\"}]}",
"{\"cname\":[{\"ttl\":300, \"host\":\"example.com.\"}]}",
},
{"ns1",
"{\"a\":[{\"ttl\":300, \"ip\":\"2.2.2.2\"}]}",
Expand Down

0 comments on commit 7789699

Please sign in to comment.