handle wildcard CNAME

This commit is contained in:
Niklas 2021-02-02 01:15:57 +01:00
parent f0f4fa5376
commit 0fe5d73853
2 changed files with 21 additions and 3 deletions

View File

@ -6,6 +6,8 @@ import (
"github.com/miekg/dns" "github.com/miekg/dns"
) )
type rrMap map[uint16]map[string][]dns.RR
// All record types to send when a ANY request is send // All record types to send when a ANY request is send
var anyRecordTypes = []uint16{ var anyRecordTypes = []uint16{
dns.TypeSOA, dns.TypeSOA,
@ -43,8 +45,9 @@ func handleRequest(w dns.ResponseWriter, r *dns.Msg, zone zoneView) {
// Handle any other type // Handle any other type
m.Answer = append(m.Answer, rrs[q.Name]...) m.Answer = append(m.Answer, rrs[q.Name]...)
// Check for wildcard // if no rr found yet
if len(m.Answer) == 0 { if len(m.Answer) == 0 {
// Check for wildcard
parts := dns.SplitDomainName(q.Name)[1:] parts := dns.SplitDomainName(q.Name)[1:]
searchDomain := "*." + dns.Fqdn(strings.Join(parts, ".")) searchDomain := "*." + dns.Fqdn(strings.Join(parts, "."))
foundDomain := rrs[searchDomain] foundDomain := rrs[searchDomain]
@ -80,6 +83,23 @@ func handleRequest(w dns.ResponseWriter, r *dns.Msg, zone zoneView) {
m.Answer = append(m.Answer, zone.rr[q.Qtype][t.Target]...) m.Answer = append(m.Answer, zone.rr[q.Qtype][t.Target]...)
} }
} }
} else {
// No direct A/AAAA or CNAME found. Check for CNAME wildcard
parts := dns.SplitDomainName(q.Name)[1:]
searchDomain := "*." + dns.Fqdn(strings.Join(parts, "."))
foundDomain := zone.rr[dns.TypeCNAME][searchDomain]
for _, rr := range foundDomain {
// Add CNAME to answer section
newRR := rr
newRR.Header().Name = q.Name
m.Answer = append(m.Answer, newRR)
// Add resolved CNAME to *also* to the answer section (bind does the same soo)
if t, ok := rr.(*dns.CNAME); ok {
m.Answer = append(m.Answer, zone.rr[dns.TypeA][t.Target]...)
m.Answer = append(m.Answer, zone.rr[dns.TypeAAAA][t.Target]...)
}
}
} }
} }
case dns.TypeNS: case dns.TypeNS:

View File

@ -16,8 +16,6 @@ type zoneView struct {
type zoneMap map[string][]zoneView type zoneMap map[string][]zoneView
type rrMap map[uint16]map[string][]dns.RR
// Start starts cooldns // Start starts cooldns
func Start(configPath string) { func Start(configPath string) {
config, err := loadConfig(configPath) config, err := loadConfig(configPath)