Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 19 additions & 0 deletions config/config.go
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
package config

import (
"crypto/tls"
"encoding/json"
"fmt"
"log"
"os"
"path/filepath"
"strings"
"sync"

"github.com/google/uuid"
"github.com/zalando/go-keyring"
Expand All @@ -29,6 +31,11 @@ const (
DateFormatEU = "DD/MM/YYYY HH:MM"
)

type SessionCache struct {
once sync.Once
cache tls.ClientSessionCache
}

// Account stores the configuration for a single email account.
type Account struct {
ID string `json:"id"`
Expand All @@ -46,6 +53,8 @@ type Account struct {
// regardless of which address they were delivered to.
CatchAll bool `json:"catch_all,omitempty"`

SC *SessionCache `json:"-"` // "-" prevents the SessionCache from being saved to config.json

// Custom server settings (used when ServiceProvider is "custom")
IMAPServer string `json:"imap_server,omitempty"`
IMAPPort int `json:"imap_port,omitempty"`
Expand Down Expand Up @@ -229,6 +238,14 @@ func (a *Account) GetSMTPServer() string {
}
}

func (a *Account) GetClientSessionCache() tls.ClientSessionCache {
a.SC.once.Do(func() {
a.SC.cache = tls.NewLRUClientSessionCache(64)
})

return a.SC.cache
}

Comment thread
andrinoff marked this conversation as resolved.
// GetSMTPPort returns the SMTP port for the account.
func (a *Account) GetSMTPPort() int {
switch a.ServiceProvider {
Expand Down Expand Up @@ -580,6 +597,7 @@ func LoadConfig() (*Config, error) {
Password: legacyConfig.Password,
ServiceProvider: legacyConfig.ServiceProvider,
FetchEmail: legacyConfig.Email,
SC: &SessionCache{},
},
},
}
Expand Down Expand Up @@ -631,6 +649,7 @@ func LoadConfig() (*Config, error) {
POP3Server: rawAcc.POP3Server,
POP3Port: rawAcc.POP3Port,
CatchAll: rawAcc.CatchAll,
SC: &SessionCache{},
}

// Validate PGPKeySource
Expand Down
2 changes: 2 additions & 0 deletions config/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ func TestSaveAndLoadConfig(t *testing.T) {
Password: "supersecret",
ServiceProvider: "gmail",
SendAsEmail: "alias@example.com",
SC: &SessionCache{},
},
{
ID: "test-id-2",
Expand All @@ -44,6 +45,7 @@ func TestSaveAndLoadConfig(t *testing.T) {
SMTPServer: "smtp.custom.com",
SMTPPort: 587,
CatchAll: true,
SC: &SessionCache{},
},
},
}
Expand Down
5 changes: 5 additions & 0 deletions fetcher/fetcher.go
Original file line number Diff line number Diff line change
Expand Up @@ -376,6 +376,11 @@ func connectWithOptions(account *config.Account, extraOpts *imapclient.Options)
ServerName: imapServer,
InsecureSkipVerify: account.Insecure,
MinVersion: tls.VersionTLS12,
ClientSessionCache: account.GetClientSessionCache(),
VerifyConnection: func(cs tls.ConnectionState) error {
log.Printf("IMAP TLS connection resumed: %t", cs.DidResume)
return nil
},
Comment on lines +379 to +383
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this will fire on every connection

},
}
if extraOpts != nil {
Expand Down
2 changes: 2 additions & 0 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -383,6 +383,7 @@ func (m *mainModel) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
POP3Server: msg.POP3Server,
POP3Port: msg.POP3Port,
MaildirPath: msg.MaildirPath,
SC: &config.SessionCache{},
}

if msg.Provider == "custom" || msg.Protocol == "pop3" {
Expand Down Expand Up @@ -428,6 +429,7 @@ func (m *mainModel) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
POP3Server: msg.POP3Server,
POP3Port: msg.POP3Port,
MaildirPath: msg.MaildirPath,
SC: &config.SessionCache{},
}

if msg.Provider == "custom" || msg.Protocol == "pop3" {
Expand Down
12 changes: 12 additions & 0 deletions sender/sender.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import (
"errors"
"fmt"
"io"
"log"
"mime"
"mime/multipart"
"mime/quotedprintable"
Expand Down Expand Up @@ -660,6 +661,11 @@ func SendEmail(account *config.Account, to, cc, bcc []string, subject, plainBody
ServerName: smtpServer,
InsecureSkipVerify: account.Insecure,
MinVersion: tls.VersionTLS12,
ClientSessionCache: account.GetClientSessionCache(),
VerifyConnection: func(cs tls.ConnectionState) error {
log.Printf("SMTP TLS connection resumed: %t", cs.DidResume)
return nil
},
Comment on lines +664 to +668
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this will too

}

var c *smtp.Client
Expand Down Expand Up @@ -870,10 +876,16 @@ func SendCalendarReply(account *config.Account, to []string, subject, plainBody

// Send via SMTP
addr := fmt.Sprintf("%s:%d", smtpServer, smtpPort)

tlsConfig := &tls.Config{
ServerName: smtpServer,
InsecureSkipVerify: account.Insecure,
MinVersion: tls.VersionTLS12,
ClientSessionCache: account.GetClientSessionCache(),
VerifyConnection: func(cs tls.ConnectionState) error {
log.Printf("SMTP TLS connection resumed: %t", cs.DidResume)
return nil
},
Comment on lines +885 to +888
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

and here

}

var c *smtp.Client
Expand Down
Loading