aboutsummaryrefslogtreecommitdiff
path: root/discover.go
blob: 08d4aa1efd30d9eaf0100dd44478307a084a92eb (plain) (blame)
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
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
// defined in RFC 6186 section 3.1.
func discoverSMTP(domain string) (*url.URL, error) {
	host, err := discoverTCP("submission", domain)
	if err != nil {
		return nil, err
	}
	if host == "" {
		return nil, fmt.Errorf("SMTP service discovery not configured for domain %q", domain)
	}
	return &url.URL{Scheme: "smtp", Host: host}, nil
}