2013-07-23 11:10:38 +00:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
2013-07-23 16:37:38 +00:00
|
|
|
"strings"
|
2015-02-12 09:19:46 +00:00
|
|
|
"sync"
|
2013-07-23 11:10:38 +00:00
|
|
|
"time"
|
2015-02-10 09:00:59 +00:00
|
|
|
|
|
|
|
"github.com/miekg/dns"
|
2013-07-23 11:10:38 +00:00
|
|
|
)
|
|
|
|
|
2013-07-23 16:37:38 +00:00
|
|
|
type ResolvError struct {
|
2015-02-12 09:19:46 +00:00
|
|
|
qname, net string
|
2013-07-23 16:37:38 +00:00
|
|
|
nameservers []string
|
|
|
|
}
|
|
|
|
|
|
|
|
func (e ResolvError) Error() string {
|
2015-02-12 09:19:46 +00:00
|
|
|
errmsg := fmt.Sprintf("%s resolv failed on %s (%s)", e.qname, strings.Join(e.nameservers, "; "), e.net)
|
2013-07-23 16:37:38 +00:00
|
|
|
return errmsg
|
|
|
|
}
|
|
|
|
|
2013-07-23 11:10:38 +00:00
|
|
|
type Resolver struct {
|
|
|
|
config *dns.ClientConfig
|
|
|
|
}
|
|
|
|
|
2015-02-12 09:19:46 +00:00
|
|
|
// Lookup will ask each nameserver in top-to-bottom fashion, starting a new request
|
|
|
|
// in every second, and return as early as possbile (have an answer).
|
|
|
|
// It returns an error if no request has succeeded.
|
2013-07-23 16:37:38 +00:00
|
|
|
func (r *Resolver) Lookup(net string, req *dns.Msg) (message *dns.Msg, err error) {
|
2013-07-23 11:10:38 +00:00
|
|
|
c := &dns.Client{
|
|
|
|
Net: net,
|
|
|
|
ReadTimeout: r.Timeout(),
|
|
|
|
WriteTimeout: r.Timeout(),
|
|
|
|
}
|
|
|
|
|
2013-07-23 16:37:38 +00:00
|
|
|
qname := req.Question[0].Name
|
2013-07-23 11:10:38 +00:00
|
|
|
|
2015-02-12 09:19:46 +00:00
|
|
|
res := make(chan *dns.Msg, 1)
|
|
|
|
var wg sync.WaitGroup
|
|
|
|
L := func(nameserver string) {
|
|
|
|
defer wg.Done()
|
2013-07-23 16:37:38 +00:00
|
|
|
r, rtt, err := c.Exchange(req, nameserver)
|
|
|
|
if err != nil {
|
|
|
|
Debug("%s socket error on %s", qname, nameserver)
|
|
|
|
Debug("error:%s", err.Error())
|
2015-02-12 09:19:46 +00:00
|
|
|
return
|
2013-07-23 16:37:38 +00:00
|
|
|
}
|
|
|
|
if r != nil && r.Rcode != dns.RcodeSuccess {
|
|
|
|
Debug("%s failed to get an valid answer on %s", qname, nameserver)
|
2015-02-12 09:19:46 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
Debug("%s resolv on %s (%s) ttl: %d", UnFqdn(qname), nameserver, net, rtt)
|
|
|
|
select {
|
|
|
|
case res <- r:
|
|
|
|
default:
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
ticker := time.NewTicker(time.Second)
|
|
|
|
defer ticker.Stop()
|
|
|
|
// Start lookup on each nameserver top-down, in every second
|
|
|
|
for _, nameserver := range r.Nameservers() {
|
|
|
|
wg.Add(1)
|
|
|
|
go L(nameserver)
|
|
|
|
// but exit early, if we have an answer
|
|
|
|
select {
|
|
|
|
case r := <-res:
|
|
|
|
return r, nil
|
|
|
|
case <-ticker.C:
|
2013-07-23 16:37:38 +00:00
|
|
|
continue
|
|
|
|
}
|
2015-02-12 09:19:46 +00:00
|
|
|
}
|
|
|
|
// wait for all the namservers to finish
|
|
|
|
wg.Wait()
|
|
|
|
select {
|
|
|
|
case r := <-res:
|
2013-07-23 16:37:38 +00:00
|
|
|
return r, nil
|
2015-02-12 09:19:46 +00:00
|
|
|
default:
|
|
|
|
return nil, ResolvError{qname, net, r.Nameservers()}
|
2013-07-23 11:10:38 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2015-02-11 15:57:25 +00:00
|
|
|
// Namservers return the array of nameservers, with port number appended.
|
|
|
|
// '#' in the name is treated as port separator, as with dnsmasq.
|
2013-07-23 11:10:38 +00:00
|
|
|
func (r *Resolver) Nameservers() (ns []string) {
|
|
|
|
for _, server := range r.config.Servers {
|
2015-02-11 15:57:25 +00:00
|
|
|
if i := strings.IndexByte(server, '#'); i > 0 {
|
|
|
|
server = server[:i] + ":" + server[i+1:]
|
|
|
|
} else {
|
|
|
|
server = server + ":" + r.config.Port
|
|
|
|
}
|
|
|
|
ns = append(ns, server)
|
2013-07-23 11:10:38 +00:00
|
|
|
}
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
func (r *Resolver) Timeout() time.Duration {
|
|
|
|
return time.Duration(r.config.Timeout) * time.Second
|
|
|
|
}
|