From 89766dd39795813a56d8122135036f9a90f26404 Mon Sep 17 00:00:00 2001 From: Elvis de Freitas Date: Wed, 12 Jul 2017 23:38:11 -0300 Subject: [PATCH] MG-337 fixing test --- .../dns-proxy-server/test/dnsserver.go | 35 +++++++++++++++++-- 1 file changed, 33 insertions(+), 2 deletions(-) diff --git a/src/github.com/mageddo/dns-proxy-server/test/dnsserver.go b/src/github.com/mageddo/dns-proxy-server/test/dnsserver.go index 233a7bd4f..bb2e15fb6 100644 --- a/src/github.com/mageddo/dns-proxy-server/test/dnsserver.go +++ b/src/github.com/mageddo/dns-proxy-server/test/dnsserver.go @@ -41,7 +41,8 @@ import ( "syscall" "github.com/miekg/dns" "github.com/mageddo/log" - "github.com/mageddo/dns-proxy-server/utils" + "net" + "errors" ) var ( @@ -68,7 +69,7 @@ func handleReflect(respWriter dns.ResponseWriter, reqMsg *dns.Msg) { log.Logger.Infof("m=handleReflect, questions=%d, 1stQuestion=%s", len(reqMsg.Question), questionName) - resp := utils.SolveName(questionName) + resp := SolveName(questionName) resp.SetReply(reqMsg) resp.Compress = *compress @@ -125,3 +126,33 @@ func main2() { s := <-sig fmt.Printf("Signal (%s) received, stopping\n", s) } + + +// reference https://miek.nl/2014/August/16/go-dns-package/ +func SolveName(hostname string) *dns.Msg { + + + //config, _ := dns.ClientConfigFromFile("/etc/resolv.conf") + c := new(dns.Client) + + m := new(dns.Msg) + m.SetQuestion(dns.Fqdn(hostname), dns.TypeA) // CAN BE A, AAA, MX, etc. + m.RecursionDesired = true + + //r, _, err := c.Exchange(m, net.JoinHostPort(config.Servers[0], config.Port)) // server and port to ask + r, _, err := c.Exchange(m, net.JoinHostPort("8.8.8.8", "53")) // server and port to ask + + // if the answer not be returned + if r == nil { + panic(err) + } + + // what the code of the return message ? + if r.Rcode != dns.RcodeSuccess { + panic(errors.New(fmt.Sprintf(" *** invalid answer name %s after MX query for %s", hostname, hostname))) + } + + // looping through the anwsers + return r + +} \ No newline at end of file