-
Notifications
You must be signed in to change notification settings - Fork 0
/
helpers.go
94 lines (82 loc) · 1.79 KB
/
helpers.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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
package main
import (
"context"
"errors"
"fmt"
"net"
"sync"
"time"
)
var errInconsistent = errors.New("[error] Inconsistent record count from CF_NS1 and CF_NS2")
func resolver(address string) net.Resolver {
return net.Resolver{
PreferGo: true,
Dial: func(ctx context.Context, network, _ string) (conn net.Conn, err error) {
d := net.Dialer{}
var addr net.Addr
if network == "tcp" {
addr, err = net.ResolveTCPAddr(network, address)
} else if network == "udp" {
addr, err = net.ResolveUDPAddr(network, address)
} else {
err = fmt.Errorf("unknown DNS resolver network type %q", network)
}
if err != nil {
return
}
conn, err = d.DialContext(ctx, network, addr.String())
return
},
}
}
func lookupCompareTXT(rs1, rs2 net.Resolver, name string) ([]string, error) {
wg := &sync.WaitGroup{}
var res1, res2 []string
var err1, err2 error
ctx, cancel := context.WithTimeout(context.TODO(), 10*time.Second)
wg.Add(2)
go func() {
res1, err1 = rs1.LookupTXT(ctx, name)
wg.Done()
}()
go func() {
res2, err2 = rs2.LookupTXT(ctx, name)
wg.Done()
}()
wg.Wait()
cancel()
if err1 != nil {
return nil, err1
} else if err2 != nil {
return nil, err2
}
if res1 == nil {
return nil, errors.New("[error] Nameserver from CF_NS1 did not respond")
} else if res2 == nil {
return nil, errors.New("[error] Nameserver from CF_NS2 did not respond")
}
if len(res1) != len(res2) {
return nil, errInconsistent
}
for i := range res1 {
found := false
for j := range res2 {
if res2[j] == res1[i] {
found = true
break
}
}
if !found {
return nil, errInconsistent
}
}
return res1, nil
}
func strSliceLookup(haystack []string, needle string) bool {
for i := range haystack {
if haystack[i] == needle {
return true
}
}
return false
}