2013-07-23 11:10:38 +00:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
2015-02-03 14:53:57 +00:00
|
|
|
"bufio"
|
2013-07-23 11:10:38 +00:00
|
|
|
"fmt"
|
2018-02-01 09:43:28 +00:00
|
|
|
"net"
|
2015-02-03 14:53:57 +00:00
|
|
|
"os"
|
2015-02-04 08:21:08 +00:00
|
|
|
"strconv"
|
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
|
|
|
|
}
|
|
|
|
|
2018-02-01 10:30:41 +00:00
|
|
|
type RResp struct {
|
|
|
|
msg *dns.Msg
|
|
|
|
nameserver string
|
|
|
|
rtt time.Duration
|
|
|
|
}
|
|
|
|
|
2013-07-23 11:10:38 +00:00
|
|
|
type Resolver struct {
|
2015-02-04 08:21:08 +00:00
|
|
|
servers []string
|
2015-02-03 14:53:57 +00:00
|
|
|
domain_server *suffixTreeNode
|
2015-02-04 08:21:08 +00:00
|
|
|
config *ResolvSettings
|
2015-02-03 14:53:57 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func NewResolver(c ResolvSettings) *Resolver {
|
2015-02-04 08:21:08 +00:00
|
|
|
r := &Resolver{
|
|
|
|
servers: []string{},
|
|
|
|
domain_server: newSuffixTreeRoot(),
|
|
|
|
config: &c,
|
2015-02-03 14:53:57 +00:00
|
|
|
}
|
|
|
|
|
2015-02-04 08:21:08 +00:00
|
|
|
if len(c.ServerListFile) > 0 {
|
|
|
|
r.ReadServerListFile(c.ServerListFile)
|
|
|
|
}
|
2015-02-03 14:53:57 +00:00
|
|
|
|
2015-02-04 08:21:08 +00:00
|
|
|
if len(c.ResolvFile) > 0 {
|
|
|
|
clientConfig, err := dns.ClientConfigFromFile(c.ResolvFile)
|
|
|
|
if err != nil {
|
2018-02-01 07:39:47 +00:00
|
|
|
logger.Error(":%s is not a valid resolv.conf file\n", c.ResolvFile)
|
|
|
|
logger.Error("%s", err)
|
2015-02-04 08:21:08 +00:00
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
for _, server := range clientConfig.Servers {
|
2018-02-01 09:43:28 +00:00
|
|
|
nameserver := net.JoinHostPort(server, clientConfig.Port)
|
2015-02-04 08:21:08 +00:00
|
|
|
r.servers = append(r.servers, nameserver)
|
|
|
|
}
|
2015-02-03 14:53:57 +00:00
|
|
|
}
|
2015-02-04 08:21:08 +00:00
|
|
|
|
2015-02-03 14:53:57 +00:00
|
|
|
return r
|
|
|
|
}
|
|
|
|
|
2015-02-04 08:21:08 +00:00
|
|
|
func (r *Resolver) ReadServerListFile(file string) {
|
2015-02-03 14:53:57 +00:00
|
|
|
buf, err := os.Open(file)
|
|
|
|
if err != nil {
|
|
|
|
panic("Can't open " + file)
|
|
|
|
}
|
|
|
|
scanner := bufio.NewScanner(buf)
|
|
|
|
for scanner.Scan() {
|
|
|
|
line := scanner.Text()
|
|
|
|
line = strings.TrimSpace(line)
|
|
|
|
|
|
|
|
if !strings.HasPrefix(line, "server") {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
sli := strings.Split(line, "=")
|
|
|
|
if len(sli) != 2 {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
line = strings.TrimSpace(sli[1])
|
|
|
|
|
|
|
|
tokens := strings.Split(line, "/")
|
2015-02-04 08:21:08 +00:00
|
|
|
switch len(tokens) {
|
|
|
|
case 3:
|
|
|
|
domain := tokens[1]
|
|
|
|
ip := tokens[2]
|
|
|
|
if !isDomain(domain) || !isIP(ip) {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
r.domain_server.sinsert(strings.Split(domain, "."), ip)
|
|
|
|
case 1:
|
|
|
|
srv_port := strings.Split(line, "#")
|
|
|
|
if len(srv_port) > 2 {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
ip := ""
|
|
|
|
if ip = srv_port[0]; !isIP(ip) {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
port := "53"
|
|
|
|
if len(srv_port) == 2 {
|
|
|
|
if _, err := strconv.Atoi(srv_port[1]); err != nil {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
port = srv_port[1]
|
|
|
|
}
|
2018-02-01 09:43:28 +00:00
|
|
|
r.servers = append(r.servers, net.JoinHostPort(ip, port))
|
2015-02-03 14:53:57 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-07-23 11:10:38 +00:00
|
|
|
}
|
|
|
|
|
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(),
|
|
|
|
}
|
|
|
|
|
2017-02-14 17:08:08 +00:00
|
|
|
if net == "udp" && settings.ResolvConfig.SetEDNS0 {
|
|
|
|
req = req.SetEdns0(65535, true)
|
|
|
|
}
|
|
|
|
|
2013-07-23 16:37:38 +00:00
|
|
|
qname := req.Question[0].Name
|
2013-07-23 11:10:38 +00:00
|
|
|
|
2018-02-01 10:30:41 +00:00
|
|
|
res := make(chan *RResp, 1)
|
2015-02-12 09:19:46 +00:00
|
|
|
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 {
|
2015-10-13 17:00:28 +00:00
|
|
|
logger.Warn("%s socket error on %s", qname, nameserver)
|
|
|
|
logger.Warn("error:%s", err.Error())
|
2015-02-12 09:19:46 +00:00
|
|
|
return
|
2013-07-23 16:37:38 +00:00
|
|
|
}
|
2015-10-30 04:24:22 +00:00
|
|
|
// If SERVFAIL happen, should return immediately and try another upstream resolver.
|
|
|
|
// However, other Error code like NXDOMAIN is an clear response stating
|
|
|
|
// that it has been verified no such domain existas and ask other resolvers
|
|
|
|
// would make no sense. See more about #20
|
2013-07-23 16:37:38 +00:00
|
|
|
if r != nil && r.Rcode != dns.RcodeSuccess {
|
2015-10-13 17:00:28 +00:00
|
|
|
logger.Warn("%s failed to get an valid answer on %s", qname, nameserver)
|
2015-10-30 04:24:22 +00:00
|
|
|
if r.Rcode == dns.RcodeServerFailure {
|
|
|
|
return
|
|
|
|
}
|
2015-02-12 09:19:46 +00:00
|
|
|
}
|
2018-02-01 10:30:41 +00:00
|
|
|
re := &RResp{r, nameserver, rtt}
|
2015-02-12 09:19:46 +00:00
|
|
|
select {
|
2018-02-01 10:30:41 +00:00
|
|
|
case res <- re:
|
2015-02-12 09:19:46 +00:00
|
|
|
default:
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-05-07 09:58:31 +00:00
|
|
|
ticker := time.NewTicker(time.Duration(settings.ResolvConfig.Interval) * time.Millisecond)
|
2015-02-12 09:19:46 +00:00
|
|
|
defer ticker.Stop()
|
|
|
|
// Start lookup on each nameserver top-down, in every second
|
2018-02-01 07:39:47 +00:00
|
|
|
nameservers := r.Nameservers(qname)
|
|
|
|
for _, nameserver := range nameservers {
|
2015-02-12 09:19:46 +00:00
|
|
|
wg.Add(1)
|
|
|
|
go L(nameserver)
|
|
|
|
// but exit early, if we have an answer
|
|
|
|
select {
|
2018-02-01 10:30:41 +00:00
|
|
|
case re := <-res:
|
|
|
|
logger.Debug("%s resolv on %s rtt: %v", UnFqdn(qname), re.nameserver, re.rtt)
|
|
|
|
return re.msg, nil
|
2015-02-12 09:19:46 +00:00
|
|
|
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 {
|
2018-02-01 10:30:41 +00:00
|
|
|
case re := <-res:
|
|
|
|
logger.Debug("%s resolv on %s rtt: %v", UnFqdn(qname), re.nameserver, re.rtt)
|
|
|
|
return re.msg, nil
|
2015-02-12 09:19:46 +00:00
|
|
|
default:
|
2018-02-01 07:39:47 +00:00
|
|
|
return nil, ResolvError{qname, net, 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.
|
2015-02-03 14:53:57 +00:00
|
|
|
|
2018-02-01 07:39:47 +00:00
|
|
|
func (r *Resolver) Nameservers(qname string) []string {
|
2015-02-03 14:53:57 +00:00
|
|
|
queryKeys := strings.Split(qname, ".")
|
|
|
|
queryKeys = queryKeys[:len(queryKeys)-1] // ignore last '.'
|
|
|
|
|
|
|
|
ns := []string{}
|
|
|
|
if v, found := r.domain_server.search(queryKeys); found {
|
2018-02-01 10:30:41 +00:00
|
|
|
logger.Debug("%s be found in domain server list, upstream: %v", qname, v)
|
2015-02-03 14:53:57 +00:00
|
|
|
server := v
|
2018-02-01 10:30:41 +00:00
|
|
|
nameserver := net.JoinHostPort(server, "53")
|
2015-02-03 14:53:57 +00:00
|
|
|
ns = append(ns, nameserver)
|
2018-02-01 10:30:41 +00:00
|
|
|
//Ensure query the specific upstream nameserver in async Lookup() function.
|
|
|
|
return ns
|
2015-02-03 14:53:57 +00:00
|
|
|
}
|
|
|
|
|
2015-02-04 08:21:08 +00:00
|
|
|
for _, nameserver := range r.servers {
|
2013-07-23 11:10:38 +00:00
|
|
|
ns = append(ns, nameserver)
|
|
|
|
}
|
2015-02-03 14:53:57 +00:00
|
|
|
return ns
|
2013-07-23 11:10:38 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (r *Resolver) Timeout() time.Duration {
|
|
|
|
return time.Duration(r.config.Timeout) * time.Second
|
|
|
|
}
|