forked from pereiro/smtprelay
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdns.go
45 lines (38 loc) · 1013 Bytes
/
dns.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
package main
import (
"errors"
"fmt"
"net"
)
func lookupMailServer(domain string, errorCount int) (string, error) {
if domain == "localhost" || domain == "127.0.0.1" {
return "",errors.New(fmt.Sprintf("WTF? %s is invalid domain",domain))
}
mxList, err := net.LookupMX(domain)
if err != nil {
return "", err
}
mx, err := getRoundElement(mxList, errorCount)
if err != nil {
return "", err
}
if len(mx.Host) == 0 {
return "", errors.New(fmt.Sprintf("incorrect MX record for domain %s - %v;", domain, mx))
}
host := mx.Host[:len(mx.Host)-1]
if host == "localhost" || host == "127.0.0.1" {
return "",errors.New(fmt.Sprintf("WTF? %s is invalid MX record for domain %s",host,domain))
}
return mx.Host[:len(mx.Host)-1] + ":25", nil
}
func getRoundElement(mxList []*net.MX, errorCount int) (mx net.MX, err error) {
l := len(mxList)
if l == 0 {
return mx, errors.New("MX record not found")
}
for errorCount >= l {
errorCount = errorCount - l
}
mx = *mxList[errorCount]
return
}