| 1 |
package autoconfig |
| 2 |
|
| 3 |
import ( |
| 4 |
"context" |
| 5 |
"fmt" |
| 6 |
"net" |
| 7 |
"sync" |
| 8 |
|
| 9 |
"git.sr.ht/~rjarry/aerc/lib/log" |
| 10 |
) |
| 11 |
|
| 12 |
type portEncryption struct { |
| 13 |
port int |
| 14 |
enc encryption |
| 15 |
} |
| 16 |
|
| 17 |
// guessMailserver tries to guess mailserver configuration based on commonly |
| 18 |
// used settings |
| 19 |
// |
| 20 |
// It tries to find imap, smtp, and mail.domain.com and attempts to guess the |
| 21 |
// encryption based on a TCP ping to the relevant port. |
| 22 |
func guessMailserver(ctx context.Context, localpart, domain string, result chan *Config) { |
| 23 |
defer log.PanicHandler() |
| 24 |
defer close(result) |
| 25 |
|
| 26 |
res := make(chan *Config) |
| 27 |
go func(res chan *Config) { |
| 28 |
defer log.PanicHandler() |
| 29 |
defer close(res) |
| 30 |
|
| 31 |
var imapConfig, smtpConfig *Credentials |
| 32 |
var wg sync.WaitGroup |
| 33 |
wg.Add(2) |
| 34 |
|
| 35 |
go func() { |
| 36 |
defer log.PanicHandler() |
| 37 |
defer wg.Done() |
| 38 |
imapConfig = guessServername( |
| 39 |
domain, |
| 40 |
[]string{"imap", "mail"}, |
| 41 |
[]portEncryption{ |
| 42 |
{143, EncryptionSTARTTLS}, |
| 43 |
{993, EncryptionTLS}, |
| 44 |
}, |
| 45 |
) |
| 46 |
}() |
| 47 |
|
| 48 |
go func() { |
| 49 |
defer log.PanicHandler() |
| 50 |
defer wg.Done() |
| 51 |
smtpConfig = guessServername( |
| 52 |
domain, |
| 53 |
[]string{"smtp", "mail"}, |
| 54 |
[]portEncryption{ |
| 55 |
{587, EncryptionSTARTTLS}, |
| 56 |
{465, EncryptionTLS}, |
| 57 |
}, |
| 58 |
) |
| 59 |
}() |
| 60 |
wg.Wait() |
| 61 |
|
| 62 |
if imapConfig == nil || smtpConfig == nil { |
| 63 |
return |
| 64 |
} |
| 65 |
log.Debugf("successfully guessed server: %#v %#v", imapConfig, smtpConfig) |
| 66 |
|
| 67 |
imapConfig.Username = localpart + "@" + domain |
| 68 |
smtpConfig.Username = localpart + "@" + domain |
| 69 |
|
| 70 |
res <- &Config{ |
| 71 |
Found: ProtocolIMAP, |
| 72 |
IMAP: *imapConfig, |
| 73 |
SMTP: *smtpConfig, |
| 74 |
} |
| 75 |
}(res) |
| 76 |
|
| 77 |
select { |
| 78 |
case r, next := <-res: |
| 79 |
if next { |
| 80 |
result <- r |
| 81 |
} |
| 82 |
case <-ctx.Done(): |
| 83 |
} |
| 84 |
} |
| 85 |
|
| 86 |
func guessServername( |
| 87 |
domain string, |
| 88 |
subdomains []string, |
| 89 |
portenc []portEncryption, |
| 90 |
) *Credentials { |
| 91 |
for _, subdomain := range subdomains { |
| 92 |
cred := tryPort(subdomain+"."+domain, portenc) |
| 93 |
if cred != nil { |
| 94 |
return cred |
| 95 |
} |
| 96 |
} |
| 97 |
|
| 98 |
return nil |
| 99 |
} |
| 100 |
|
| 101 |
func tryPort(host string, ports []portEncryption) *Credentials { |
| 102 |
var res Credentials |
| 103 |
|
| 104 |
for _, pe := range ports { |
| 105 |
res.Address = host |
| 106 |
res.Port = pe.port |
| 107 |
res.Encryption = pe.enc |
| 108 |
c, err := netDial("tcp", fmt.Sprintf("%s:%d", host, pe.port)) |
| 109 |
if err != nil { |
| 110 |
continue |
| 111 |
} |
| 112 |
c.Close() |
| 113 |
return &res |
| 114 |
} |
| 115 |
|
| 116 |
return nil |
| 117 |
} |
| 118 |
|
| 119 |
var netDial = net.Dial |