forked from brierjon/starttls-check
-
Notifications
You must be signed in to change notification settings - Fork 0
/
domain.go
121 lines (111 loc) · 3.92 KB
/
domain.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
package checker
import (
"context"
"fmt"
"net"
"strings"
"time"
"golang.org/x/net/idna"
)
// Reports an error during the domain checks.
func (d DomainResult) reportError(err error) DomainResult {
d.Status = DomainError
d.Message = err.Error()
return d
}
type DomainStatus int32
// In order of precedence.
const (
DomainSuccess DomainStatus = 0
DomainWarning DomainStatus = 1
DomainFailure DomainStatus = 2
DomainError DomainStatus = 3
DomainNoSTARTTLSFailure DomainStatus = 4
DomainCouldNotConnect DomainStatus = 5
)
// DomainResult wraps all the results for a particular mail domain.
type DomainResult struct {
// Domain being checked against.
Domain string `json:"domain"`
// Message if a failure or error occurs on the domain lookup level.
Message string `json:"message,omitempty"`
// Status of this check, inherited from the results of preferred hostnames.
Status DomainStatus `json:"status"`
// Results of this check, on each hostname.
HostnameResults map[string]HostnameResult `json:"results"`
// The list of hostnames which impact the Status of this result.
// Determined by the hostnames with the lowest MX priority.
PreferredHostnames []string `json:"preferred_hostnames"`
// Expected MX hostnames supplied by the caller of CheckDomain.
MxHostnames []string `json:"mx_hostnames,omitempty"`
// Extra global results
ExtraResults map[string]CheckResult `json:"extra_results,omitempty"`
}
func lookupMXWithTimeout(domain string) ([]*net.MX, error) {
const timeout = 2 * time.Second
ctx, cancel := context.WithTimeout(context.TODO(), timeout)
defer cancel()
var r net.Resolver
return r.LookupMX(ctx, domain)
}
func lookupHostnames(domain string) ([]string, error) {
domainASCII, err := idna.ToASCII(domain)
if err != nil {
return nil, fmt.Errorf("domain name %s couldn't be converted to ASCII", domain)
}
mxs, err := lookupMXWithTimeout(domainASCII)
if err != nil || len(mxs) == 0 {
return nil, fmt.Errorf("No MX records found")
}
hostnames := make([]string, 0)
for _, mx := range mxs {
hostnames = append(hostnames, strings.ToLower(mx.Host))
}
return hostnames, nil
}
// CheckDomain performs all associated checks for a particular domain.
// First performs an MX lookup, then performs subchecks on each of the
// resulting hostnames.
//
// The status of DomainResult is inherited from the check status of the MX
// records with highest priority. This check succeeds only if the hostname
// checks on the highest priority mailservers succeed.
//
// `domain` is the mail domain to perform the lookup on.
// `mxHostnames` is a list of expected hostnames for certificate validation.
func CheckDomain(domain string, mxHostnames []string) DomainResult {
// 1. Look up hostnames
// 2. Perform and aggregate checks from those hostnames.
// 3. Set a summary message.
result := DomainResult{Domain: domain, MxHostnames: mxHostnames}
hostnames, err := lookupHostnames(domain)
if err != nil {
return result.reportError(err)
}
checkedHostnames := make([]string, 0)
result.HostnameResults = make(map[string]HostnameResult)
for _, hostname := range hostnames {
result.HostnameResults[hostname] = CheckHostname(domain, hostname, mxHostnames)
if result.HostnameResults[hostname].couldConnect() {
checkedHostnames = append(checkedHostnames, hostname)
}
}
result.PreferredHostnames = checkedHostnames
// Derive Domain code from Hostname results.
// We couldn't connect to any of those hostnames.
if len(checkedHostnames) == 0 {
result.Status = DomainCouldNotConnect
return result
}
for _, hostname := range checkedHostnames {
hostnameResult := result.HostnameResults[hostname]
// Any of the connected hostnames don't support STARTTLS.
if !hostnameResult.couldSTARTTLS() {
result.Status = DomainNoSTARTTLSFailure
return result
}
result.Status = DomainStatus(
SetStatus(CheckStatus(result.Status), CheckStatus(result.HostnameResults[hostname].Status)))
}
return result
}