/
dns_types.go
145 lines (126 loc) · 2.93 KB
/
dns_types.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
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
package dns
import (
"encoding/json"
"errors"
"strings"
"github.com/invopop/jsonschema"
)
// DNSRequestType is the type of the method specified
type DNSRequestType int
// name:DNSRequestType
const (
// name:A
A DNSRequestType = iota + 1
// name:NS
NS
// name:DS
DS
// name:CNAME
CNAME
// name:SOA
SOA
// name:PTR
PTR
// name:MX
MX
// name:TXT
TXT
// name:AAAA
AAAA
// name:CAA
CAA
// name:TLSA
TLSA
// name:ANY
ANY
// name:SRV
SRV
limit
)
// DNSRequestTypeMapping is a table for conversion of method from string.
var DNSRequestTypeMapping = map[DNSRequestType]string{
A: "A",
NS: "NS",
DS: "DS",
CNAME: "CNAME",
SOA: "SOA",
PTR: "PTR",
MX: "MX",
TXT: "TXT",
AAAA: "AAAA",
CAA: "CAA",
TLSA: "TLSA",
ANY: "ANY",
SRV: "SRV",
}
// GetSupportedDNSRequestTypes returns list of supported types
func GetSupportedDNSRequestTypes() []DNSRequestType {
var result []DNSRequestType
for index := DNSRequestType(1); index < limit; index++ {
result = append(result, index)
}
return result
}
func toDNSRequestTypes(valueToMap string) (DNSRequestType, error) {
normalizedValue := normalizeValue(valueToMap)
for key, currentValue := range DNSRequestTypeMapping {
if normalizedValue == currentValue {
return key, nil
}
}
return -1, errors.New("Invalid DNS request type: " + valueToMap)
}
func normalizeValue(value string) string {
return strings.TrimSpace(strings.ToUpper(value))
}
func (t DNSRequestType) String() string {
return DNSRequestTypeMapping[t]
}
// DNSRequestTypeHolder is used to hold internal type of the DNS type
type DNSRequestTypeHolder struct {
DNSRequestType DNSRequestType `mapping:"true"`
}
func (holder DNSRequestTypeHolder) String() string {
return holder.DNSRequestType.String()
}
func (holder DNSRequestTypeHolder) JSONSchema() *jsonschema.Schema {
gotType := &jsonschema.Schema{
Type: "string",
Title: "type of DNS request to make",
Description: "Type is the type of DNS request to make",
}
for _, types := range GetSupportedDNSRequestTypes() {
gotType.Enum = append(gotType.Enum, types.String())
}
return gotType
}
func (holder *DNSRequestTypeHolder) UnmarshalYAML(unmarshal func(interface{}) error) error {
var marshalledTypes string
if err := unmarshal(&marshalledTypes); err != nil {
return err
}
computedType, err := toDNSRequestTypes(marshalledTypes)
if err != nil {
return err
}
holder.DNSRequestType = computedType
return nil
}
func (holder *DNSRequestTypeHolder) UnmarshalJSON(data []byte) error {
s := strings.Trim(string(data), `"`)
if s == "" {
return nil
}
computedType, err := toDNSRequestTypes(s)
if err != nil {
return err
}
holder.DNSRequestType = computedType
return nil
}
func (holder *DNSRequestTypeHolder) MarshalJSON() ([]byte, error) {
return json.Marshal(holder.DNSRequestType.String())
}
func (holder DNSRequestTypeHolder) MarshalYAML() (interface{}, error) {
return holder.DNSRequestType.String(), nil
}