-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
enhanced error handling, reduce number of dns calls
- Loading branch information
Showing
8 changed files
with
176 additions
and
155 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,98 @@ | ||
package dns | ||
|
||
import ( | ||
"fmt" | ||
"net" | ||
"sort" | ||
"strings" | ||
|
||
"github.com/customeros/mailsherpa/internal/syntax" | ||
) | ||
|
||
type DNS struct { | ||
MX []string | ||
SPF string | ||
Errors []string | ||
} | ||
|
||
func GetDNS(email string) DNS { | ||
var dns DNS | ||
var mxErr error | ||
var spfErr error | ||
|
||
dns.MX, mxErr = getMXRecordsForEmail(email) | ||
dns.SPF, spfErr = getSPFRecord(email) | ||
if mxErr != nil { | ||
dns.Errors = append(dns.Errors, mxErr.Error()) | ||
} | ||
if spfErr != nil { | ||
dns.Errors = append(dns.Errors, spfErr.Error()) | ||
} | ||
return dns | ||
} | ||
|
||
func getMXRecordsForEmail(email string) ([]string, error) { | ||
mxRecords, err := getRawMXRecords(email) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
// Sort MX records by priority (lower number = higher priority) | ||
sort.Slice(mxRecords, func(i, j int) bool { | ||
return mxRecords[i].Pref < mxRecords[j].Pref | ||
}) | ||
|
||
stripDot := func(s string) string { | ||
return strings.ToLower(strings.TrimSuffix(s, ".")) | ||
} | ||
|
||
// Extract hostnames into a string array | ||
result := make([]string, len(mxRecords)) | ||
for i, mx := range mxRecords { | ||
result[i] = stripDot(mx.Host) | ||
} | ||
|
||
return result, nil | ||
} | ||
|
||
func getRawMXRecords(email string) ([]*net.MX, error) { | ||
_, domain, ok := syntax.GetEmailUserAndDomain(email) | ||
if !ok { | ||
return nil, fmt.Errorf("Invalid domain") | ||
} | ||
|
||
mxRecords, err := net.LookupMX(domain) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
return mxRecords, nil | ||
} | ||
|
||
func getSPFRecord(email string) (string, error) { | ||
_, domain, ok := syntax.GetEmailUserAndDomain(email) | ||
if !ok { | ||
return "", fmt.Errorf("invalid email address") | ||
} | ||
records, err := net.LookupTXT(domain) | ||
if err != nil { | ||
return "", fmt.Errorf("error looking up TXT records: %w", err) | ||
} | ||
for _, record := range records { | ||
spfRecord := parseTXTRecord(record) | ||
if strings.HasPrefix(spfRecord, "v=spf1") { | ||
return spfRecord, nil | ||
} | ||
} | ||
return "", fmt.Errorf("no SPF record found for domain %s", domain) | ||
} | ||
|
||
func parseTXTRecord(record string) string { | ||
// Remove surrounding quotes if present | ||
record = strings.Trim(record, "\"") | ||
|
||
// Replace multiple spaces with a single space | ||
record = strings.Join(strings.Fields(record), " ") | ||
|
||
return record | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
package dns | ||
|
||
func GetEmailProviderFromMx(dns DNS, knownProviders KnownProviders) (emailProvider, firewall string) { | ||
if len(dns.MX) == 0 { | ||
return "", "" | ||
} | ||
for _, record := range dns.MX { | ||
domain := extractRootDomain(record) | ||
provider, category := knownProviders.GetProviderByDomain(domain) | ||
if provider == "" { | ||
return domain, "" | ||
} | ||
|
||
switch category { | ||
case "enterprise": | ||
return provider, "" | ||
case "webmail": | ||
return provider, "" | ||
case "hosting": | ||
return provider, "" | ||
case "security": | ||
return "", provider | ||
default: | ||
return "", "" | ||
} | ||
} | ||
|
||
return "", "" | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.