| 1 |
package autoconfig |
| 2 |
|
| 3 |
import ( |
| 4 |
"cmp" |
| 5 |
"context" |
| 6 |
"fmt" |
| 7 |
"net" |
| 8 |
"slices" |
| 9 |
"strings" |
| 10 |
"sync" |
| 11 |
|
| 12 |
"git.sr.ht/~rjarry/aerc/lib/log" |
| 13 |
) |
| 14 |
|
| 15 |
// getFromProviderDNS retrieves the config from the provider using SRV DNS records (RFC2782) |
| 16 |
func getFromProviderDNS(ctx context.Context, localpart, domain string, result chan *Config) { |
| 17 |
defer log.PanicHandler() |
| 18 |
defer close(result) |
| 19 |
|
| 20 |
res := make(chan *Config) |
| 21 |
go func(res chan *Config) { |
| 22 |
defer log.PanicHandler() |
| 23 |
defer close(res) |
| 24 |
|
| 25 |
type Serverconf struct { |
| 26 |
Hostname string |
| 27 |
Port int |
| 28 |
} |
| 29 |
|
| 30 |
configs := map[string]*Serverconf{ |
| 31 |
"jmap": {}, |
| 32 |
"imap": {}, |
| 33 |
"imaps": {}, |
| 34 |
"submission": {}, |
| 35 |
"submissions": {}, |
| 36 |
"smtps": {}, |
| 37 |
} |
| 38 |
|
| 39 |
var wg sync.WaitGroup |
| 40 |
for key, conf := range configs { |
| 41 |
wg.Add(1) |
| 42 |
go func(service string, conf *Serverconf) { |
| 43 |
defer log.PanicHandler() |
| 44 |
defer wg.Done() |
| 45 |
|
| 46 |
_, srvList, err := lookupSRV(service, "tcp", domain) |
| 47 |
if err != nil { |
| 48 |
return |
| 49 |
} |
| 50 |
srv := getHighestSRV(srvList) |
| 51 |
srv.Target = strings.TrimRight(srv.Target, ".") |
| 52 |
if srv.Target == "" { |
| 53 |
return |
| 54 |
} |
| 55 |
if srv.Port == 0 { |
| 56 |
return |
| 57 |
} |
| 58 |
*conf = Serverconf{ |
| 59 |
Hostname: srv.Target, |
| 60 |
Port: int(srv.Port), |
| 61 |
} |
| 62 |
}(key, conf) |
| 63 |
} |
| 64 |
wg.Wait() |
| 65 |
|
| 66 |
cfg := &Config{} |
| 67 |
cfg.Found = ProtocolIMAP |
| 68 |
|
| 69 |
var increds Credentials |
| 70 |
var outcreds Credentials |
| 71 |
|
| 72 |
increds.Username = fmt.Sprintf("%s@%s", localpart, domain) |
| 73 |
outcreds.Username = fmt.Sprintf("%s@%s", localpart, domain) |
| 74 |
|
| 75 |
if configs["imap"].Hostname == "" && |
| 76 |
configs["imaps"].Hostname == "" && |
| 77 |
configs["jmap"].Hostname == "" { |
| 78 |
return |
| 79 |
} |
| 80 |
|
| 81 |
if configs["jmap"].Hostname != "" { |
| 82 |
cfg.Found = ProtocolJMAP |
| 83 |
increds.Address = configs["jmap"].Hostname |
| 84 |
increds.Port = configs["jmap"].Port |
| 85 |
increds.Encryption = EncryptionTLS |
| 86 |
cfg.JMAP = increds |
| 87 |
} |
| 88 |
|
| 89 |
increds.Encryption = EncryptionSTARTTLS |
| 90 |
if configs["imap"].Hostname == "" { |
| 91 |
configs["imap"] = configs["imaps"] |
| 92 |
increds.Encryption = EncryptionTLS |
| 93 |
} |
| 94 |
increds.Address = configs["imap"].Hostname |
| 95 |
increds.Port = configs["imap"].Port |
| 96 |
|
| 97 |
outcreds.Encryption = EncryptionSTARTTLS |
| 98 |
if configs["submission"].Hostname == "" { |
| 99 |
configs["submission"] = configs["smtps"] |
| 100 |
if configs["submission"].Hostname == "" { |
| 101 |
configs["submission"] = configs["submissions"] |
| 102 |
} |
| 103 |
outcreds.Encryption = EncryptionTLS |
| 104 |
} |
| 105 |
outcreds.Address = configs["submission"].Hostname |
| 106 |
outcreds.Port = configs["submission"].Port |
| 107 |
|
| 108 |
cfg.IMAP = increds |
| 109 |
cfg.SMTP = outcreds |
| 110 |
|
| 111 |
log.Debugf("found SRV config: %#v", cfg) |
| 112 |
res <- cfg |
| 113 |
}(res) |
| 114 |
|
| 115 |
select { |
| 116 |
case r := <-res: |
| 117 |
result <- r |
| 118 |
case <-ctx.Done(): |
| 119 |
} |
| 120 |
} |
| 121 |
|
| 122 |
func getHighestSRV(list []*net.SRV) *net.SRV { |
| 123 |
slices.SortStableFunc(list, func(a, b *net.SRV) int { |
| 124 |
return cmp.Compare(a.Priority, b.Priority) |
| 125 |
}) |
| 126 |
|
| 127 |
var max int |
| 128 |
for i := range list { |
| 129 |
if list[i].Weight > list[max].Weight { |
| 130 |
max = i |
| 131 |
} |
| 132 |
} |
| 133 |
return list[max] |
| 134 |
} |
| 135 |
|
| 136 |
var lookupSRV = net.LookupSRV |