Skip to content

Commit

Permalink
fix: don't panic with unknown DNS question type. (#4)
Browse files Browse the repository at this point in the history
* fix: don't panic with unknown DNS question type.
* fix: typos.
* refactor: use dns.Fqdn
  • Loading branch information
ldez authored and cpu committed Jan 4, 2019
1 parent 17a3d10 commit 37390bc
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 31 deletions.
4 changes: 2 additions & 2 deletions challenge-servers.go
Original file line number Diff line number Diff line change
Expand Up @@ -60,15 +60,15 @@ type ChallSrv struct {
redirects map[string]string
}

// mockDNSData holds mock respones for DNS A, AAAA, and CAA lookups.
// mockDNSData holds mock responses for DNS A, AAAA, and CAA lookups.
type mockDNSData struct {
// The IPv4 address used for all A record responses that don't match a host in
// aRecords.
defaultIPv4 string
// The IPv6 address used for all AAAA record responses that don't match a host
// in aaaaRecords.
defaultIPv6 string
// A map of host to IPv4 addressess in string form for A record responses.
// A map of host to IPv4 addresses in string form for A record responses.
aRecords map[string][]string
// A map of host to IPv6 addresses in string form for AAAA record responses.
aaaaRecords map[string][]string
Expand Down
8 changes: 8 additions & 0 deletions dns.go
Original file line number Diff line number Diff line change
Expand Up @@ -145,6 +145,7 @@ func (s *ChallSrv) dnsHandler(w dns.ResponseWriter, r *dns.Msg) {
s.AddRequestEvent(DNSRequestEvent{
Question: q,
})

var answerFunc dnsAnswerFunc
switch q.Qtype {
case dns.TypeTXT:
Expand All @@ -155,7 +156,14 @@ func (s *ChallSrv) dnsHandler(w dns.ResponseWriter, r *dns.Msg) {
answerFunc = s.aaaaAnswers
case dns.TypeCAA:
answerFunc = s.caaAnswers
default:
m.SetRcode(r, dns.RcodeNotImplemented)
}

if answerFunc == nil {
break
}

if records := answerFunc(q); len(records) > 0 {
m.Answer = append(m.Answer, records...)
}
Expand Down
2 changes: 1 addition & 1 deletion httpone.go
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ func (s *ChallSrv) AddHTTPRedirect(path, targetURL string) {
s.redirects[path] = targetURL
}

// DeletedHTTPRedirect deletes a redirect for the given path.
// DeleteHTTPRedirect deletes a redirect for the given path.
func (s *ChallSrv) DeleteHTTPRedirect(path string) {
s.challMu.Lock()
defer s.challMu.Unlock()
Expand Down
38 changes: 10 additions & 28 deletions mockdns.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package challtestsrv

import (
"strings"
"github.com/miekg/dns"
)

// SetDefaultDNSIPv4 sets the default IPv4 address used for A query responses
Expand Down Expand Up @@ -43,9 +43,7 @@ func (s *ChallSrv) GetDefaultDNSIPv6() string {
func (s *ChallSrv) AddDNSARecord(host string, addresses []string) {
s.challMu.Lock()
defer s.challMu.Unlock()
if !strings.HasSuffix(host, ".") {
host = host + "."
}
host = dns.Fqdn(host)
s.dnsMocks.aRecords[host] = append(s.dnsMocks.aRecords[host], addresses...)
}

Expand All @@ -54,19 +52,15 @@ func (s *ChallSrv) AddDNSARecord(host string, addresses []string) {
func (s *ChallSrv) DeleteDNSARecord(host string) {
s.challMu.Lock()
defer s.challMu.Unlock()
if !strings.HasSuffix(host, ".") {
host = host + "."
}
host = dns.Fqdn(host)
delete(s.dnsMocks.aRecords, host)
}

// GetDNSARecord returns a slice of IPv4 addresses (in string form) that will be
// returned when querying for A records for the given host.
func (s *ChallSrv) GetDNSARecord(host string) []string {
s.challMu.RLock()
if !strings.HasSuffix(host, ".") {
host = host + "."
}
host = dns.Fqdn(host)
defer s.challMu.RUnlock()
return s.dnsMocks.aRecords[host]
}
Expand All @@ -76,9 +70,7 @@ func (s *ChallSrv) GetDNSARecord(host string) []string {
func (s *ChallSrv) AddDNSAAAARecord(host string, addresses []string) {
s.challMu.Lock()
defer s.challMu.Unlock()
if !strings.HasSuffix(host, ".") {
host = host + "."
}
host = dns.Fqdn(host)
s.dnsMocks.aaaaRecords[host] = append(s.dnsMocks.aaaaRecords[host], addresses...)
}

Expand All @@ -87,9 +79,7 @@ func (s *ChallSrv) AddDNSAAAARecord(host string, addresses []string) {
func (s *ChallSrv) DeleteDNSAAAARecord(host string) {
s.challMu.Lock()
defer s.challMu.Unlock()
if !strings.HasSuffix(host, ".") {
host = host + "."
}
host = dns.Fqdn(host)
delete(s.dnsMocks.aaaaRecords, host)
}

Expand All @@ -98,9 +88,7 @@ func (s *ChallSrv) DeleteDNSAAAARecord(host string) {
func (s *ChallSrv) GetDNSAAAARecord(host string) []string {
s.challMu.RLock()
defer s.challMu.RUnlock()
if !strings.HasSuffix(host, ".") {
host = host + "."
}
host = dns.Fqdn(host)
return s.dnsMocks.aaaaRecords[host]
}

Expand All @@ -109,9 +97,7 @@ func (s *ChallSrv) GetDNSAAAARecord(host string) []string {
func (s *ChallSrv) AddDNSCAARecord(host string, policies []MockCAAPolicy) {
s.challMu.Lock()
defer s.challMu.Unlock()
if !strings.HasSuffix(host, ".") {
host = host + "."
}
host = dns.Fqdn(host)
s.dnsMocks.caaRecords[host] = append(s.dnsMocks.caaRecords[host], policies...)
}

Expand All @@ -120,9 +106,7 @@ func (s *ChallSrv) AddDNSCAARecord(host string, policies []MockCAAPolicy) {
func (s *ChallSrv) DeleteDNSCAARecord(host string) {
s.challMu.Lock()
defer s.challMu.Unlock()
if !strings.HasSuffix(host, ".") {
host = host + "."
}
host = dns.Fqdn(host)
delete(s.dnsMocks.caaRecords, host)
}

Expand All @@ -131,8 +115,6 @@ func (s *ChallSrv) DeleteDNSCAARecord(host string) {
func (s *ChallSrv) GetDNSCAARecord(host string) []MockCAAPolicy {
s.challMu.RLock()
defer s.challMu.RUnlock()
if !strings.HasSuffix(host, ".") {
host = host + "."
}
host = dns.Fqdn(host)
return s.dnsMocks.caaRecords[host]
}

0 comments on commit 37390bc

Please sign in to comment.