-
-
Notifications
You must be signed in to change notification settings - Fork 3
/
dns.go
64 lines (55 loc) · 1.27 KB
/
dns.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
package main
import (
"context"
"fmt"
"log"
"net"
"github.com/miekg/dns"
)
const resolver = "8.8.8.8:53"
func lookupIP(ctx context.Context, host string) (addrs []net.IP, err error) {
return net.LookupIP(host)
}
func lookupTXT(ctx context.Context, host string) ([]string, error) {
msg := &dns.Msg{
MsgHdr: dns.MsgHdr{
RecursionDesired: true,
CheckingDisabled: false,
},
Question: []dns.Question{{
Name: dns.Fqdn(host),
Qtype: dns.TypeTXT,
Qclass: uint16(dns.ClassINET),
}},
}
msg.Id = dns.Id()
conn, err := net.Dial("udp", resolver)
if err != nil {
return nil, fmt.Errorf("socket.Dial(%v, %v): %v", "udp", resolver, err)
}
defer conn.Close()
dnsc := &dns.Conn{Conn: conn}
defer dnsc.Close()
log.Printf("Making DNS query: %v", msg)
if err := dnsc.WriteMsg(msg); err != nil {
return nil, fmt.Errorf("dnsc.WriteMsg(%v): %v", msg, err)
}
rmsg, err := dnsc.ReadMsg()
if err != nil {
return nil, fmt.Errorf("dnsc.ReadMsg(): %v", err)
}
log.Printf("Got DNS response: %v", rmsg)
var answers []string
for _, m := range rmsg.Answer {
m, ok := m.(*dns.TXT)
if !ok {
continue
}
if m.Hdr.Name != msg.Question[0].Name {
continue
}
answers = append(answers, m.Txt...)
}
log.Printf("Extracted TXTs: %v", answers)
return answers, nil
}