1
0
Fork 0
mirror of https://github.com/Luzifer/rootzone.git synced 2024-12-20 11:21:19 +00:00
rootzone/iana.go
Knut Ahlers 213e724e1b
Add retries to all DNS calls
Signed-off-by: Knut Ahlers <knut@ahlers.me>
2019-06-22 20:03:17 +02:00

79 lines
1.5 KiB
Go

package main
import (
"bufio"
"net"
"net/http"
"strings"
"github.com/miekg/dns"
"github.com/pkg/errors"
log "github.com/sirupsen/logrus"
)
func getIANATLDs() ([]string, error) {
tlds := []string{}
resp, err := http.Get(cfg.IANATldList)
if err != nil {
return nil, errors.Wrap(err, "Failed to retrieve IANA TLD list")
}
defer resp.Body.Close()
scanner := bufio.NewScanner(resp.Body)
for scanner.Scan() {
if b := scanner.Bytes(); b[0] == '#' || len(b) == 0 {
continue
}
tlds = append(tlds, strings.ToLower(scanner.Text())+".")
}
return tlds, nil
}
func getIANAZoneMasters(tld string) ([]string, error) {
log.WithField("tld", tld).Trace("Getting zone masters")
masters := []string{}
c := new(dns.Client)
c.Net = "tcp"
m := new(dns.Msg)
m.SetQuestion(tld, dns.TypeNS)
var (
err error
r *dns.Msg
)
if err = retry(func() error {
r, _, err = c.Exchange(m, getRandomInternicRoot())
return errors.Wrap(err, "Could not query nameservers")
}); err != nil {
return nil, err
}
if r.Rcode != dns.RcodeSuccess {
return nil, errors.New("Query was not successful")
}
for _, a := range r.Ns {
if ns, ok := a.(*dns.NS); ok {
var addr net.IP
// We can't resolve the root nameserver names so store glue records
for _, e := range r.Extra {
if nsa, ok := e.(*dns.A); ok && nsa.Header().Name == ns.Ns {
addr = nsa.A
break
}
}
if addr != nil {
masters = append(masters, addr.String())
}
}
}
return masters, nil
}