imap: use low level tcp connection
In preparation for tcp keepalive options, we need access to the net.TCPConn object associated with an IMAP connection. The only way to do this is to create the connection ourselves. No functional change. Signed-off-by: Robin Jarry <robin@jarry.cc>
This commit is contained in:
parent
cc432eefd3
commit
33aaf94663
1 changed files with 22 additions and 6 deletions
|
@ -3,6 +3,7 @@ package imap
|
||||||
import (
|
import (
|
||||||
"crypto/tls"
|
"crypto/tls"
|
||||||
"fmt"
|
"fmt"
|
||||||
|
"net"
|
||||||
"net/url"
|
"net/url"
|
||||||
"strings"
|
"strings"
|
||||||
|
|
||||||
|
@ -214,29 +215,44 @@ func (w *IMAPWorker) handleImapUpdate(update client.Update) {
|
||||||
|
|
||||||
func (w *IMAPWorker) connect() (*client.Client, error) {
|
func (w *IMAPWorker) connect() (*client.Client, error) {
|
||||||
var (
|
var (
|
||||||
|
conn *net.TCPConn
|
||||||
c *client.Client
|
c *client.Client
|
||||||
err error
|
|
||||||
)
|
)
|
||||||
switch w.config.scheme {
|
|
||||||
case "imap":
|
addr, err := net.ResolveTCPAddr("tcp", w.config.addr)
|
||||||
c, err = client.Dial(w.config.addr)
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
|
||||||
|
conn, err = net.DialTCP("tcp", nil, addr)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
|
||||||
|
serverName, _, _ := net.SplitHostPort(w.config.addr)
|
||||||
|
tlsConfig := &tls.Config{ServerName: serverName}
|
||||||
|
|
||||||
|
switch w.config.scheme {
|
||||||
|
case "imap":
|
||||||
|
c, err = client.New(conn)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
if !w.config.insecure {
|
if !w.config.insecure {
|
||||||
if err := c.StartTLS(&tls.Config{}); err != nil {
|
if err = c.StartTLS(tlsConfig); err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
case "imaps":
|
case "imaps":
|
||||||
c, err = client.DialTLS(w.config.addr, &tls.Config{})
|
tlsConn := tls.Client(conn, tlsConfig)
|
||||||
|
c, err = client.New(tlsConn)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
default:
|
default:
|
||||||
return nil, fmt.Errorf("Unknown IMAP scheme %s", w.config.scheme)
|
return nil, fmt.Errorf("Unknown IMAP scheme %s", w.config.scheme)
|
||||||
}
|
}
|
||||||
|
|
||||||
c.ErrorLog = w.worker.Logger
|
c.ErrorLog = w.worker.Logger
|
||||||
|
|
||||||
if w.config.user != nil {
|
if w.config.user != nil {
|
||||||
|
|
Loading…
Reference in a new issue