forked from Silverfish/proton-bridge
fix(GODT-2693): Duplicate messages in sent folder
* Only call `RemoveOnFail` on error. It was also called on success.
* Add some logging to track send times.
* Fix non-deterministic hash from `rfc8222.GetMessageHash`.
https://github.com/ProtonMail/gluon/pull/395
This commit is contained in:
2
go.mod
2
go.mod
@ -5,7 +5,7 @@ go 1.20
|
||||
require (
|
||||
github.com/0xAX/notificator v0.0.0-20220220101646-ee9b8921e557
|
||||
github.com/Masterminds/semver/v3 v3.2.0
|
||||
github.com/ProtonMail/gluon v0.17.1-0.20230817061728-c2e6c5429251
|
||||
github.com/ProtonMail/gluon v0.17.1-0.20230829112217-5d5c25c504b5
|
||||
github.com/ProtonMail/go-autostart v0.0.0-20210130080809-00ed301c8e9a
|
||||
github.com/ProtonMail/go-proton-api v0.4.1-0.20230829100923-d12b1baf9bf3
|
||||
github.com/ProtonMail/gopenpgp/v2 v2.7.1-proton
|
||||
|
||||
2
go.sum
2
go.sum
@ -25,6 +25,8 @@ github.com/ProtonMail/docker-credential-helpers v1.1.0 h1:+kvUIpwWcbtP3WFv5sSvkF
|
||||
github.com/ProtonMail/docker-credential-helpers v1.1.0/go.mod h1:mK0aBveCxhnQ756AmaTfXMZDeULvheYVhF/MWMErN5g=
|
||||
github.com/ProtonMail/gluon v0.17.1-0.20230817061728-c2e6c5429251 h1:WjVsvfisn0B0LdDCIncggk3KMAz0uaEyj7nI+f0dU/0=
|
||||
github.com/ProtonMail/gluon v0.17.1-0.20230817061728-c2e6c5429251/go.mod h1:Og5/Dz1MiGpCJn51XujZwxiLG7WzvvjE5PRpZBQmAHo=
|
||||
github.com/ProtonMail/gluon v0.17.1-0.20230829112217-5d5c25c504b5 h1:C/8P5NHAKi2yCKez+OZ5rSR8SsL7k8si4pK4SE2QtV8=
|
||||
github.com/ProtonMail/gluon v0.17.1-0.20230829112217-5d5c25c504b5/go.mod h1:Og5/Dz1MiGpCJn51XujZwxiLG7WzvvjE5PRpZBQmAHo=
|
||||
github.com/ProtonMail/go-autostart v0.0.0-20210130080809-00ed301c8e9a h1:D+aZah+k14Gn6kmL7eKxoo/4Dr/lK3ChBcwce2+SQP4=
|
||||
github.com/ProtonMail/go-autostart v0.0.0-20210130080809-00ed301c8e9a/go.mod h1:oTGdE7/DlWIr23G0IKW3OXK9wZ5Hw1GGiaJFccTvZi4=
|
||||
github.com/ProtonMail/go-crypto v0.0.0-20230321155629-9a39f2531310/go.mod h1:8TI4H3IbrackdNgv+92dI+rhpCaLqM0IfpgCgenFvRE=
|
||||
|
||||
@ -22,6 +22,7 @@ import (
|
||||
"errors"
|
||||
"fmt"
|
||||
"io"
|
||||
"time"
|
||||
|
||||
"github.com/ProtonMail/gluon/async"
|
||||
"github.com/ProtonMail/gluon/logging"
|
||||
@ -254,6 +255,13 @@ type sendMailReq struct {
|
||||
|
||||
func (s *Service) sendMail(ctx context.Context, req *sendMailReq) error {
|
||||
defer async.HandlePanic(s.panicHandler)
|
||||
start := time.Now()
|
||||
s.log.Debug("Received send mail request")
|
||||
defer func() {
|
||||
end := time.Now()
|
||||
s.log.Debugf("Send mail request finished in %v", end.Sub(start))
|
||||
}()
|
||||
|
||||
if err := s.smtpSendMail(ctx, req.authID, req.from, req.to, req.r); err != nil {
|
||||
if apiErr := new(proton.APIError); errors.As(err, &apiErr) {
|
||||
s.log.WithError(apiErr).WithField("Details", apiErr.DetailsToString()).Error("failed to send message")
|
||||
|
||||
@ -75,6 +75,7 @@ func (s *Service) smtpSendMail(ctx context.Context, authID string, from string,
|
||||
}
|
||||
|
||||
// Check if we already tried to send this message recently.
|
||||
s.log.Debug("Checking for duplicate message")
|
||||
srID, ok, err := s.recorder.TryInsertWait(ctx, hash, to, time.Now().Add(90*time.Second))
|
||||
if err != nil {
|
||||
return fmt.Errorf("failed to check send hash: %w", err)
|
||||
@ -83,12 +84,11 @@ func (s *Service) smtpSendMail(ctx context.Context, authID string, from string,
|
||||
return nil
|
||||
}
|
||||
|
||||
// If we fail to send this message, we should remove the hash from the send recorder.
|
||||
defer s.recorder.RemoveOnFail(hash, srID)
|
||||
|
||||
// Create a new message parser from the reader.
|
||||
parser, err := parser.New(bytes.NewReader(b))
|
||||
if err != nil {
|
||||
s.log.Debug("Message failed to send, removing from send recorder")
|
||||
s.recorder.RemoveOnFail(hash, srID)
|
||||
return fmt.Errorf("failed to create parser: %w", err)
|
||||
}
|
||||
|
||||
@ -100,10 +100,12 @@ func (s *Service) smtpSendMail(ctx context.Context, authID string, from string,
|
||||
// Load the user's mail settings.
|
||||
settings, err := s.client.GetMailSettings(ctx)
|
||||
if err != nil {
|
||||
s.log.Debug("Message failed to send, removing from send recorder")
|
||||
s.recorder.RemoveOnFail(hash, srID)
|
||||
return fmt.Errorf("failed to get mail settings: %w", err)
|
||||
}
|
||||
|
||||
return usertypes.WithAddrKR(s.identityState.User, fromAddr, s.keyPassProvider.KeyPass(), func(userKR, addrKR *crypto.KeyRing) error {
|
||||
if err := usertypes.WithAddrKR(s.identityState.User, fromAddr, s.keyPassProvider.KeyPass(), func(userKR, addrKR *crypto.KeyRing) error {
|
||||
// Use the first key for encrypting the message.
|
||||
addrKR, err := addrKR.FirstKey()
|
||||
if err != nil {
|
||||
@ -150,10 +152,17 @@ func (s *Service) smtpSendMail(ctx context.Context, authID string, from string,
|
||||
}
|
||||
|
||||
// If the message was successfully sent, we can update the message ID in the record.
|
||||
s.log.Debug("Message sent successfully, signaling recorder")
|
||||
s.recorder.SignalMessageSent(hash, srID, sent.ID)
|
||||
|
||||
return nil
|
||||
})
|
||||
}); err != nil {
|
||||
s.log.Debug("Message failed to send, removing from send recorder")
|
||||
s.recorder.RemoveOnFail(hash, srID)
|
||||
return err
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
// sendWithKey sends the message with the given address key.
|
||||
|
||||
Reference in New Issue
Block a user