Skip to content

Commit

Permalink
enhance primary domain checks
Browse files Browse the repository at this point in the history
  • Loading branch information
mattbr0wn committed Oct 21, 2024
1 parent 50807e9 commit fdad8e2
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 3 deletions.
43 changes: 40 additions & 3 deletions domaincheck/domain.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,13 @@ import (
"fmt"
"net"
"net/http"
"net/url"
"sort"
"strings"
"time"

"golang.org/x/net/publicsuffix"

"github.com/customeros/mailsherpa/internal/syntax"
)

Expand All @@ -20,12 +23,21 @@ type DNS struct {
}

func PrimaryDomainCheck(domain string) (bool, string) {
dns := CheckDNS(domain)
redirects, primaryDomain := CheckRedirects(domain)
root, subdomain, err := parseRootAndSubdomain(domain)
if err != nil {
root = domain
}
dns := CheckDNS(root)
redirects, primaryDomain := CheckRedirects(root)

if !redirects && dns.CNAME == "" && len(dns.MX) > 0 && dns.HasA {
return true, ""
if subdomain == "" {
return true, ""
} else {
return false, root
}
}

return false, primaryDomain
}

Expand Down Expand Up @@ -82,6 +94,31 @@ func CheckRedirects(domain string) (bool, string) {
return false, ""
}

func parseRootAndSubdomain(input string) (string, string, error) {
// Ensure the input has a scheme
if !strings.Contains(input, "://") {
input = "https://" + input
}

// Parse the URL
u, err := url.Parse(input)
if err != nil {
return "", "", err
}

// Get the domain and TLD using the public suffix list
domain, err := publicsuffix.EffectiveTLDPlusOne(u.Hostname())
if err != nil {
return "", "", err
}

// The subdomain is everything before the domain
subdomain := strings.TrimSuffix(u.Hostname(), domain)
subdomain = strings.TrimSuffix(subdomain, ".")

return domain, subdomain, nil
}

func getMXRecordsForDomain(domain string) ([]string, error) {
mxRecords, err := getRawMXRecords(domain)
if err != nil {
Expand Down
1 change: 1 addition & 0 deletions mailvalidate/role_emails.toml
Original file line number Diff line number Diff line change
Expand Up @@ -490,6 +490,7 @@ contains = [
'member',
'mentors',
'merchant',
'messenger',
'metrics',
'mgmt',
'microsoft',
Expand Down

0 comments on commit fdad8e2

Please sign in to comment.