Move duplicate code to a function

Signed-off-by: xenolf <xenolf@users.noreply.github.com>
This commit is contained in:
xenolf 2016-04-08 01:00:29 +02:00
parent 239a7005ef
commit 44d92633c6
No known key found for this signature in database
GPG key ID: C9C13317912FAB20

View file

@ -214,14 +214,7 @@ func FindZoneByFqdn(fqdn, nameserver string) (string, error) {
// We have a success, so one of the answers has to be a SOA RR
for _, ans := range in.Answer {
if soa, ok := ans.(*dns.SOA); ok {
zone := soa.Hdr.Name
// If we ended up on one of the TLDs, it means the domain did not exist.
publicsuffix, _ := publicsuffix.PublicSuffix(UnFqdn(zone))
if publicsuffix == UnFqdn(zone) {
return "", fmt.Errorf("Could not determine zone authoritatively")
}
fqdnToZone[fqdn] = zone
return zone, nil
return checkIfTLD(fqdn, soa)
}
}
// Or it is NODATA, fall through to NXDOMAIN
@ -229,6 +222,13 @@ func FindZoneByFqdn(fqdn, nameserver string) (string, error) {
// Search the authority section for our precious SOA RR
for _, ns := range in.Ns {
if soa, ok := ns.(*dns.SOA); ok {
return checkIfTLD(fqdn, soa)
}
}
return "", fmt.Errorf("NS %s did not return the expected SOA record in the authority section", nameserver)
}
func checkIfTLD(fqdn string, soa *dns.SOA) (string, error) {
zone := soa.Hdr.Name
// If we ended up on one of the TLDs, it means the domain did not exist.
publicsuffix, _ := publicsuffix.PublicSuffix(UnFqdn(zone))
@ -237,9 +237,6 @@ func FindZoneByFqdn(fqdn, nameserver string) (string, error) {
}
fqdnToZone[fqdn] = zone
return zone, nil
}
}
return "", fmt.Errorf("NS %s did not return the expected SOA record in the authority section", nameserver)
}
// ClearFqdnCache clears the cache of fqdn to zone mappings. Primarily used in testing.