Use dnsforward for checking if upstream DNS server is working.

This commit is contained in:
Eugene Bujak 2018-12-05 19:17:17 +03:00
parent 478ce03386
commit f6942213c8

View File

@ -5,6 +5,7 @@ import (
"fmt" "fmt"
"io/ioutil" "io/ioutil"
"log" "log"
"net"
"net/http" "net/http"
"os" "os"
"strconv" "strconv"
@ -12,7 +13,7 @@ import (
"time" "time"
"github.com/AdguardTeam/AdGuardHome/dnsforward" "github.com/AdguardTeam/AdGuardHome/dnsforward"
"github.com/AdguardTeam/AdGuardHome/upstream" "github.com/miekg/dns"
"gopkg.in/asaskevich/govalidator.v4" "gopkg.in/asaskevich/govalidator.v4"
) )
@ -202,23 +203,32 @@ func handleTestUpstreamDNS(w http.ResponseWriter, r *http.Request) {
} }
func checkDNS(input string) error { func checkDNS(input string) error {
u, err := upstream.NewUpstream(input, config.CoreDNS.BootstrapDNS) log.Printf("Checking if DNS %s works...", input)
u, err := dnsforward.GetUpstream(input)
if err != nil { if err != nil {
return err return fmt.Errorf("Failed to choose upstream for %s: %s", input, err)
} }
defer u.Close()
alive, err := upstream.IsAlive(u)
req := dns.Msg{}
req.Id = dns.Id()
req.RecursionDesired = true
req.Question = []dns.Question{
{Name: "google-public-dns-a.google.com.", Qtype: dns.TypeA, Qclass: dns.ClassINET},
}
reply, err := u.Exchange(&req)
if err != nil { if err != nil {
return fmt.Errorf("couldn't communicate with DNS server %s: %s", input, err) return fmt.Errorf("couldn't communicate with DNS server %s: %s", input, err)
} }
if len(reply.Answer) != 1 {
if !alive { return fmt.Errorf("DNS server %s returned wrong answer", input)
return fmt.Errorf("DNS server has not passed the healthcheck: %s", input) }
if t, ok := reply.Answer[0].(*dns.A); ok {
if !net.IPv4(8, 8, 8, 8).Equal(t.A) {
return fmt.Errorf("DNS server %s returned wrong answer: %v", input, t.A)
}
} }
log.Printf("DNS %s works OK", input)
return nil return nil
} }