Phase 4.1: Error handling, edge cases, and robustness (#11)
CI / test (push) Failing after 2m36s
CI / test (push) Failing after 2m36s
This commit was merged in pull request #11.
This commit is contained in:
@@ -8,6 +8,7 @@ import (
|
||||
|
||||
"github.com/hits/ExploreDNS/internal/dns"
|
||||
miekgdns "github.com/miekg/dns"
|
||||
"golang.org/x/net/idna"
|
||||
)
|
||||
|
||||
type ResolutionState int
|
||||
@@ -46,12 +47,35 @@ type Referral struct {
|
||||
Prob float64
|
||||
}
|
||||
|
||||
// idnaLookup is the IDN lookup profile used to convert internationalised domain
|
||||
// names (unicode labels) to their ACE/punycode equivalents before querying.
|
||||
var idnaLookup = idna.New(
|
||||
idna.MapForLookup(),
|
||||
idna.BidiRule(),
|
||||
idna.StrictDomainName(false),
|
||||
)
|
||||
|
||||
// toASCII converts a domain name that may contain unicode labels to its
|
||||
// punycode (ACE) representation. Pure-ASCII names are returned unchanged.
|
||||
// On conversion errors the original name is returned so the caller can still
|
||||
// attempt a query (the server will reject it if truly invalid).
|
||||
func toASCII(name string) string {
|
||||
if name == "" || name == "." {
|
||||
return name
|
||||
}
|
||||
ascii, err := idnaLookup.ToASCII(name)
|
||||
if err != nil {
|
||||
return name
|
||||
}
|
||||
return ascii
|
||||
}
|
||||
|
||||
func NewReferral(name string, qtype uint16, bailiwick string, depth int, prob float64, parent *Referral) *Referral {
|
||||
return &Referral{
|
||||
Name: miekgdns.Fqdn(strings.ToLower(name)),
|
||||
Name: miekgdns.Fqdn(strings.ToLower(toASCII(name))),
|
||||
Qtype: qtype,
|
||||
Qclass: miekgdns.ClassINET,
|
||||
Bailiwick: miekgdns.Fqdn(strings.ToLower(bailiwick)),
|
||||
Bailiwick: miekgdns.Fqdn(strings.ToLower(toASCII(bailiwick))),
|
||||
Depth: depth,
|
||||
Prob: prob,
|
||||
Parent: parent,
|
||||
@@ -236,3 +260,17 @@ func getVisitedNames(visited map[string]bool) []string {
|
||||
}
|
||||
return names
|
||||
}
|
||||
|
||||
// IsNameInChain reports whether name appears anywhere in this referral's ancestor
|
||||
// chain, including this referral itself. Used for CNAME loop detection.
|
||||
func (r *Referral) IsNameInChain(name string) bool {
|
||||
n := miekgdns.Fqdn(strings.ToLower(name))
|
||||
curr := r
|
||||
for curr != nil {
|
||||
if curr.Name == n {
|
||||
return true
|
||||
}
|
||||
curr = curr.Parent
|
||||
}
|
||||
return false
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user