2020-01-20 12:08:05 +00:00
|
|
|
package koushin
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"net"
|
|
|
|
"net/url"
|
|
|
|
"strings"
|
|
|
|
)
|
|
|
|
|
|
|
|
func discoverTCP(service, name string) (string, error) {
|
|
|
|
_, addrs, err := net.LookupSRV(service, "tcp", name)
|
|
|
|
if dnsErr, ok := err.(*net.DNSError); ok {
|
|
|
|
if dnsErr.IsTemporary {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
} else if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(addrs) == 0 {
|
|
|
|
return "", nil
|
|
|
|
}
|
|
|
|
addr := addrs[0]
|
|
|
|
|
|
|
|
target := strings.TrimSuffix(addr.Target, ".")
|
|
|
|
if target == "" {
|
|
|
|
return "", nil
|
|
|
|
}
|
|
|
|
|
|
|
|
return fmt.Sprintf("%v:%v", target, addr.Port), nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// discoverIMAP performs a DNS-based IMAP service discovery, as defined in
|
|
|
|
// RFC 6186 section 3.2.
|
|
|
|
func discoverIMAP(domain string) (*url.URL, error) {
|
|
|
|
imapsHost, err := discoverTCP("imaps", domain)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
if imapsHost != "" {
|
|
|
|
return &url.URL{Scheme: "imaps", Host: imapsHost}, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
imapHost, err := discoverTCP("imap", domain)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
if imapHost != "" {
|
|
|
|
return &url.URL{Scheme: "imap", Host: imapHost}, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil, fmt.Errorf("IMAP service discovery not configured for domain %q", domain)
|
|
|
|
}
|
|
|
|
|
|
|
|
// discoverSMTP performs a DNS-based SMTP submission service discovery, as
|
2020-02-19 08:43:01 +00:00
|
|
|
// defined in RFC 6186 section 3.1. RFC 8314 section 5.1 adds a new service for
|
|
|
|
// SMTP submission with implicit TLS.
|
2020-01-20 12:08:05 +00:00
|
|
|
func discoverSMTP(domain string) (*url.URL, error) {
|
2020-02-19 08:43:01 +00:00
|
|
|
smtpsHost, err := discoverTCP("submissions", domain)
|
2020-01-20 12:08:05 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2020-02-19 08:43:01 +00:00
|
|
|
if smtpsHost != "" {
|
|
|
|
return &url.URL{Scheme: "smtps", Host: smtpsHost}, nil
|
2020-01-20 12:08:05 +00:00
|
|
|
}
|
2020-02-19 08:43:01 +00:00
|
|
|
|
|
|
|
smtpHost, err := discoverTCP("submission", domain)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
if smtpHost != "" {
|
|
|
|
return &url.URL{Scheme: "smtp", Host: smtpHost}, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil, fmt.Errorf("SMTP service discovery not configured for domain %q", domain)
|
2020-01-20 12:08:05 +00:00
|
|
|
}
|