about summary refs log tree commit diff stats
diff options
context:
space:
mode:
authorRobin Jarry <robin@jarry.cc>2021-12-07 00:00:01 +0100
committerRobin Jarry <robin@jarry.cc>2021-12-07 21:27:41 +0100
commit33aaf946633791f8954784a693de27990c43e0f5 (patch)
tree5718514146015774d71f1588baaa8800598ab1f3
parentcc432eefd32dc8652651ca0966ba733c7c47e088 (diff)
downloadaerc-33aaf946633791f8954784a693de27990c43e0f5.tar.gz
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>
-rw-r--r--worker/imap/worker.go28
1 files changed, 22 insertions, 6 deletions
diff --git a/worker/imap/worker.go b/worker/imap/worker.go
index 55469cf..80d861d 100644
--- a/worker/imap/worker.go
+++ b/worker/imap/worker.go
@@ -3,6 +3,7 @@ package imap
 import (
 	"crypto/tls"
 	"fmt"
+	"net"
 	"net/url"
 	"strings"
 
@@ -214,29 +215,44 @@ func (w *IMAPWorker) handleImapUpdate(update client.Update) {
 
 func (w *IMAPWorker) connect() (*client.Client, error) {
 	var (
-		c   *client.Client
-		err error
+		conn *net.TCPConn
+		c    *client.Client
 	)
+
+	addr, err := net.ResolveTCPAddr("tcp", w.config.addr)
+	if err != nil {
+		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.Dial(w.config.addr)
+		c, err = client.New(conn)
 		if err != nil {
 			return nil, err
 		}
-
 		if !w.config.insecure {
-			if err := c.StartTLS(&tls.Config{}); err != nil {
+			if err = c.StartTLS(tlsConfig); err != nil {
 				return nil, err
 			}
 		}
 	case "imaps":
-		c, err = client.DialTLS(w.config.addr, &tls.Config{})
+		tlsConn := tls.Client(conn, tlsConfig)
+		c, err = client.New(tlsConn)
 		if err != nil {
 			return nil, err
 		}
 	default:
 		return nil, fmt.Errorf("Unknown IMAP scheme %s", w.config.scheme)
 	}
+
 	c.ErrorLog = w.worker.Logger
 
 	if w.config.user != nil {