-
-
Notifications
You must be signed in to change notification settings - Fork 18
/
mx.go
269 lines (221 loc) · 7.64 KB
/
mx.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
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
package truemail
import (
"fmt"
"golang.org/x/net/idna"
)
// DNS (MX) validation resolver interface
type resolver interface {
aRecord(string) (string, error)
aRecords(string) ([]string, error)
cnameRecord(string) (string, error)
ptrRecords(string) ([]string, error)
mxRecords(string) ([]uint16, []string, error)
}
// DNS (MX) validation, second validation level
type validationMx struct {
result *ValidatorResult
resolver
}
// interface implementation
func (validation *validationMx) check(validatorResult *ValidatorResult) *ValidatorResult {
validation.result = validatorResult
validation.setValidatorResultPunycodeRepresentation()
validation.initDnsResolver()
validation.runMxLookup()
if validation.isMailServerNotFound() {
validatorResult.Success = false
validatorResult.addError(validationTypeMx, mxErrorContext)
}
return validatorResult
}
// validationMx methods
// Returns punycode domain representation
func (validation *validationMx) punycodeDomain(domain string) string {
punycodeDomain, _ := idna.New().ToASCII(domain)
return punycodeDomain
}
// Assigns punycodeEmail, punycodeDomain representations to validatorResult
func (validation *validationMx) setValidatorResultPunycodeRepresentation() {
email := validation.result.Email
user := regexCaptureGroup(email, regexEmailPattern, 2)
punycodeDomain := validation.punycodeDomain(regexCaptureGroup(email, regexEmailPattern, 3))
validation.result.punycodeEmail = user + "@" + punycodeDomain
validation.result.punycodeDomain = punycodeDomain
}
// Initializes MX validation DNS resolver
func (validation *validationMx) initDnsResolver() {
validation.resolver = newDnsResolver(validation.result.Configuration)
}
// Returns true if validatorResult contains no mail servers, otherwise returns false
func (validation *validationMx) isMailServerNotFound() bool {
return len(validation.result.MailServers) == 0
}
// Returns true if validatorResult contains mail servers, otherwise returns false
func (validation *validationMx) isMailServerFound() bool {
return len(validation.result.MailServers) > 0
}
// Addes just uniques hosts to validatorResult.MailServers
func (validation *validationMx) fetchTargetHosts(hosts ...string) {
mailServers := validation.result.MailServers
validation.result.MailServers = append(mailServers, sliceDiff(uniqStrings(hosts), mailServers)...)
}
// Returns true if connection attempts more than zero, otherwise returns false
func (validation *validationMx) isConnectionAttemptsAvailable(connectionAttempts int) bool {
return connectionAttempts > 0
}
// Casts is wrapped error is an DnsNotFound error
func (validation *validationMx) isDnsNotFoundError(err error) bool {
e, ok := err.(*validationError)
return ok && e.isDnsNotFound
}
// Casts is wrapped error is an NullMxError error
func (validation *validationMx) isNullMxError(err error) bool {
e, ok := err.(*validationError)
return ok && e.isNullMxFound
}
// A records resolver, the part of MX records resolver
func (validation *validationMx) aRecords(hostName string) (ipAddresses []string, err error) {
connectionAttempts := validation.result.Configuration.ConnectionAttempts
for validation.isConnectionAttemptsAvailable(connectionAttempts) {
ipAddresses, err = validation.resolver.aRecords(hostName)
connectionAttempts -= 1
if err == nil {
break
} else {
if validation.isDnsNotFoundError(err) {
break
}
}
}
return ipAddresses, err
}
// MX records resolver
func (validation *validationMx) hostsFromMxRecords(hostName string) (resolvedIpAddresses []string, err error) {
var priorities []uint16
var hostNames, ipAddresses []string
connectionAttempts := validation.result.Configuration.ConnectionAttempts
// Resolves MX hostnames by hostname
for validation.isConnectionAttemptsAvailable(connectionAttempts) {
priorities, hostNames, err = validation.resolver.mxRecords(hostName)
connectionAttempts -= 1
if err == nil {
break
} else {
if validation.isDnsNotFoundError(err) {
return resolvedIpAddresses, err
}
}
}
// Checkes null MX record
if len(hostNames) == 1 && priorities[0] == 0 && hostNames[0] == emptyString {
return resolvedIpAddresses, wrapNullMxError(fmt.Errorf("%s includes null MX record", hostName))
}
// Resolves host addresses by MX hostname
for _, hostName := range hostNames {
ipAddresses, err = validation.aRecords(hostName)
if err != nil {
continue
}
resolvedIpAddresses = append(resolvedIpAddresses, ipAddresses...)
}
return resolvedIpAddresses, err
}
// A record resolver
func (validation *validationMx) hostFromARecord(hostName string) (resolvedIpAddress string, err error) {
connectionAttempts := validation.result.Configuration.ConnectionAttempts
for validation.isConnectionAttemptsAvailable(connectionAttempts) {
resolvedIpAddress, err = validation.resolver.aRecord(hostName)
connectionAttempts -= 1
if err == nil {
break
} else {
if validation.isDnsNotFoundError(err) {
break
}
}
}
return resolvedIpAddress, err
}
func (validation *validationMx) ptrRecords(hostAddress string) (resolvedHostNames []string, err error) {
connectionAttempts := validation.result.Configuration.ConnectionAttempts
for validation.isConnectionAttemptsAvailable(connectionAttempts) {
resolvedHostNames, err = validation.resolver.ptrRecords(hostAddress)
connectionAttempts -= 1
if err == nil {
break
} else {
if validation.isDnsNotFoundError(err) {
break
}
}
}
return resolvedHostNames, err
}
// CNAME record resolver
func (validation *validationMx) hostsFromCnameRecord(hostName string) (resolvedIpAddresses []string, err error) {
var resolvedHostNameByCname, resolvedIpAddressByARecord string
var resolvedHostNamesByPtrRecords, resolvedIpAddressesByMxRecords []string
connectionAttempts := validation.result.Configuration.ConnectionAttempts
// Resolves hostname by CNAME record
for validation.isConnectionAttemptsAvailable(connectionAttempts) {
resolvedHostNameByCname, err = validation.resolver.cnameRecord(hostName)
connectionAttempts -= 1
if err == nil {
break
} else {
if validation.isDnsNotFoundError(err) {
break
}
}
}
if err != nil {
return resolvedIpAddresses, err
}
// Resolves host address by A record
resolvedIpAddressByARecord, err = validation.hostFromARecord(resolvedHostNameByCname)
if err != nil {
return resolvedIpAddresses, err
}
// Resolves hostnames by PTR records
resolvedHostNamesByPtrRecords, err = validation.ptrRecords(resolvedIpAddressByARecord)
if err != nil {
return resolvedIpAddresses, err
}
// Resolves host addresses by MX records
for _, resolvedHostName := range resolvedHostNamesByPtrRecords {
resolvedIpAddressesByMxRecords, err = validation.hostsFromMxRecords(resolvedHostName)
if err != nil {
continue
}
resolvedIpAddresses = append(resolvedIpAddresses, resolvedIpAddressesByMxRecords...)
}
return resolvedIpAddresses, err
}
// Complex MX lookup for target domain, uses step by step MX, CNAME and A resolvers
func (validation *validationMx) runMxLookup() {
var hostAddress string
var hostAddresses []string
var err error
targetHostname := validation.result.punycodeDomain
// MX records resolver
hostAddresses, err = validation.hostsFromMxRecords(targetHostname)
if err == nil {
validation.fetchTargetHosts(hostAddresses...)
return
}
if validation.isNullMxError(err) || validation.result.Configuration.NotRfcMxLookupFlow {
return
}
// CNAME record resolver
hostAddresses, err = validation.hostsFromCnameRecord(targetHostname)
if err == nil {
validation.fetchTargetHosts(hostAddresses...)
return
}
// A record resolver
hostAddress, err = validation.hostFromARecord(targetHostname)
if err == nil {
validation.fetchTargetHosts(hostAddress)
return
}
}