feat: Extract the resource record value without switch cases

pull/14/head
Karan Sharma 2021-02-24 23:19:30 +05:30
parent 3be58442a4
commit 9f19ef372c
1 changed files with 16 additions and 43 deletions

View File

@ -3,6 +3,7 @@ package resolvers
import ( import (
"fmt" "fmt"
"strconv" "strconv"
"strings"
"time" "time"
"github.com/miekg/dns" "github.com/miekg/dns"
@ -107,49 +108,21 @@ func parseMessage(msg *dns.Msg, rtt time.Duration, server string) Response {
} }
// Parse Answers section. // Parse Answers section.
for _, a := range msg.Answer { for _, a := range msg.Answer {
addr := "" var (
switch t := a.(type) { h = a.Header()
case *dns.A: // Source https://github.com/jvns/dns-lookup/blob/main/dns.go#L121.
addr = t.A.String() parts = strings.Split(a.String(), "\t")
case *dns.AAAA: ans = Answer{
addr = t.AAAA.String() Name: h.Name,
case *dns.CNAME: Type: dns.Type(h.Rrtype).String(),
addr = t.Target TTL: strconv.FormatInt(int64(h.Ttl), 10) + "s",
case *dns.CAA: Class: dns.Class(h.Class).String(),
addr = t.Tag + " " + t.Value Address: parts[len(parts)-1],
case *dns.HINFO:
addr = t.Cpu + " " + t.Os
case *dns.PTR:
addr = t.Ptr
case *dns.SRV:
addr = strconv.Itoa(int(t.Priority)) + " " +
strconv.Itoa(int(t.Weight)) + " " +
t.Target + ":" + strconv.Itoa(int(t.Port))
case *dns.TXT:
addr = t.String()
case *dns.NS:
addr = t.Ns
case *dns.MX:
addr = strconv.Itoa(int(t.Preference)) + " " + t.Mx
case *dns.SOA:
addr = t.String()
case *dns.NAPTR:
addr = t.String()
}
h := a.Header()
name := h.Name
qclass := dns.Class(h.Class).String()
ttl := strconv.FormatInt(int64(h.Ttl), 10) + "s"
qtype := dns.Type(h.Rrtype).String()
ans := Answer{
Name: name,
Type: qtype,
TTL: ttl,
Class: qclass,
Address: addr,
RTT: timeTaken, RTT: timeTaken,
Nameserver: server, Nameserver: server,
} }
)
resp.Answers = append(resp.Answers, ans) resp.Answers = append(resp.Answers, ans)
} }
return resp return resp