-
Notifications
You must be signed in to change notification settings - Fork 0
/
txt.go
62 lines (50 loc) · 961 Bytes
/
txt.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
package dns
import (
"context"
"net"
)
func LookupTXT(host, nameserver string) *string {
resolve := &net.Resolver{
PreferGo: true,
Dial: func(context context.Context, _, address string) (net.Conn, error) {
var dialer net.Dialer
conn, err := dialer.DialContext(context, "udp", nameserver)
if err != nil {
return nil, err
}
return conn, nil
},
}
li, err := resolve.LookupTXT(context.Background(), host)
if err == nil {
for i := range li {
return &li[i]
}
}
return nil
}
func ResolveTxt(nameserver []string, host string, verify func(string) bool) *string {
if len(nameserver) == 0 {
return nil
}
ch := make(chan *string)
for i := range nameserver {
go func(server string) {
txt := LookupTXT(host, server)
ch <- txt
}(nameserver[i])
}
total := 1
for {
txt := <-ch
if txt != nil {
if verify(*txt) {
return txt
}
}
if total >= len(nameserver) {
return nil
}
total++
}
}