generated from compscore/check-template
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
109 lines (98 loc) · 2.4 KB
/
main.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
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
package dns
import (
"context"
"fmt"
"net"
"strings"
)
func Run(ctx context.Context, target string, command string, expectedOutput string, username string, password string, options map[string]interface{}) (bool, string) {
deadline, ok := ctx.Deadline()
if !ok {
return false, "failed to get deadline from context"
}
if !strings.Contains(target, ":") {
target = target + ":53"
}
r := &net.Resolver{
PreferGo: true,
Dial: func(ctx context.Context, network, address string) (net.Conn, error) {
d := net.Dialer{
Deadline: deadline,
}
return d.DialContext(ctx, "udp", target)
},
}
// Resolve based on the record type
var addresses []string
commandSplit := strings.Split(command, " ")
if len(commandSplit) != 2 {
return false, fmt.Sprintf("invalid command; format should be \"[record type] [domain]\"; provided: \"%s\"", command)
}
command = commandSplit[0]
domain := commandSplit[1]
switch command {
case "A":
ips, err := r.LookupIP(ctx, "ip4", domain)
if err != nil {
return false, err.Error()
}
for _, ip := range ips {
addresses = append(addresses, ip.String())
}
case "AAAA":
ips, err := r.LookupIP(ctx, "ip6", domain)
if err != nil {
return false, err.Error()
}
for _, ip := range ips {
addresses = append(addresses, ip.String())
}
case "MX":
mxs, err := r.LookupMX(ctx, domain)
if err != nil {
return false, err.Error()
}
for _, mx := range mxs {
addresses = append(addresses, mx.Host)
}
case "TXT":
txts, err := r.LookupTXT(ctx, domain)
if err != nil {
return false, err.Error()
}
addresses = append(addresses, txts...)
case "CNAME":
cname, err := r.LookupCNAME(ctx, domain)
if err != nil {
return false, err.Error()
}
addresses = append(addresses, cname)
case "NS":
nss, err := r.LookupNS(ctx, domain)
if err != nil {
return false, err.Error()
}
for _, ns := range nss {
addresses = append(addresses, ns.Host)
}
default:
return false, "unsupported record type: " + command
}
// Check if the expected result is in the list of resolved addresses
for _, addr := range addresses {
if addr == expectedOutput {
return true, ""
}
}
return false, fmt.Sprintf(
"expected output \"%s\" not found in resolved addresses: [%s]",
expectedOutput,
func() string {
var s []string
for _, addr := range addresses {
s = append(s, "\""+addr+"\"")
}
return strings.Join(s, ", ")
}(),
)
}