-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.go
More file actions
161 lines (131 loc) · 3.31 KB
/
main.go
File metadata and controls
161 lines (131 loc) · 3.31 KB
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
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
package dns
import (
"context"
"fmt"
"net"
"slices"
"strings"
"github.com/scorify/schema"
)
type Schema struct {
Server string `key:"dns_server"`
Port int `key:"port" default:"53"`
Record string `key:"record" default:"A" enum:"A,AAAA,CNAME,MX,NS,PTR,TXT"`
Domain string `key:"domain"`
ExpectedOutput string `key:"expected_output"`
}
func Validate(config string) error {
conf := Schema{}
err := schema.Unmarshal([]byte(config), &conf)
if err != nil {
return err
}
if conf.Server == "" {
return fmt.Errorf("server is required; got %q", conf.Server)
}
if conf.Port == 0 {
return fmt.Errorf("port is required; got %d", conf.Port)
}
if conf.Port < 1 || conf.Port > 65535 {
return fmt.Errorf("port must be between 1 and 65535; got %d", conf.Port)
}
if conf.Record == "" {
return fmt.Errorf("record is required; got %q", conf.Record)
}
if !slices.Contains([]string{"A", "AAAA", "CNAME", "MX", "NS", "PTR", "TXT"}, conf.Record) {
return fmt.Errorf("record must be one of A, AAAA, CNAME, MX, NS, PTR, TXT; got %q", conf.Record)
}
if conf.Domain == "" {
return fmt.Errorf("domain is required; got %q", conf.Domain)
}
if conf.ExpectedOutput == "" {
return fmt.Errorf("expected_output is required; got %q", conf.ExpectedOutput)
}
return nil
}
func Run(ctx context.Context, config string) error {
conf := Schema{}
err := schema.Unmarshal([]byte(config), &conf)
if err != nil {
return err
}
connStr := fmt.Sprintf("%s:%d", conf.Server, conf.Port)
r := new(net.Resolver)
r.Dial = func(ctx context.Context, network, address string) (net.Conn, error) {
deadline, ok := ctx.Deadline()
if !ok {
return nil, fmt.Errorf("deadline not set")
}
d := net.Dialer{
Deadline: deadline,
}
return d.DialContext(ctx, network, connStr)
}
var addresses []string
switch conf.Record {
case "A":
ips, err := r.LookupIP(ctx, "ip4", conf.Domain)
if err != nil {
return err
}
addresses = make([]string, len(ips))
for i, ip := range ips {
addresses[i] = ip.String()
}
case "AAAA":
ips, err := r.LookupIP(ctx, "ip6", conf.Domain)
if err != nil {
return err
}
addresses = make([]string, len(ips))
for i, ip := range ips {
addresses[i] = ip.String()
}
case "CNAME":
cname, err := r.LookupCNAME(ctx, conf.Domain)
if err != nil {
return err
}
addresses = []string{cname}
case "MX":
mxs, err := r.LookupMX(ctx, conf.Domain)
if err != nil {
return err
}
addresses = make([]string, len(mxs))
for i, mx := range mxs {
addresses[i] = mx.Host
}
case "NS":
nss, err := r.LookupNS(ctx, conf.Domain)
if err != nil {
return err
}
addresses = make([]string, len(nss))
for i, ns := range nss {
addresses[i] = ns.Host
}
case "PTR":
ptrs, err := r.LookupAddr(ctx, conf.Domain)
if err != nil {
return err
}
addresses = make([]string, len(ptrs))
copy(addresses, ptrs)
case "TXT":
txts, err := r.LookupTXT(ctx, conf.Domain)
if err != nil {
return err
}
addresses = make([]string, len(txts))
copy(addresses, txts)
default:
return fmt.Errorf("unsupported record type: %q", conf.Record)
}
for _, address := range addresses {
if address == conf.ExpectedOutput {
return nil
}
}
return fmt.Errorf("expected out %q not found in [%s]", conf.ExpectedOutput, strings.Join(addresses, ", "))
}