/
backend.go
122 lines (108 loc) · 3.89 KB
/
backend.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
// Copyright (c) 2021 Proton Technologies AG
//
// This file is part of ProtonMail Bridge.
//
// ProtonMail Bridge is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// ProtonMail Bridge is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with ProtonMail Bridge. If not, see <https://www.gnu.org/licenses/>.
package smtp
import (
"strings"
"time"
"github.com/ProtonMail/proton-bridge/internal/bridge"
"github.com/ProtonMail/proton-bridge/internal/config/settings"
"github.com/ProtonMail/proton-bridge/pkg/confirmer"
"github.com/ProtonMail/proton-bridge/pkg/listener"
goSMTPBackend "github.com/emersion/go-smtp"
"github.com/pkg/errors"
"github.com/sirupsen/logrus"
)
type panicHandler interface {
HandlePanic()
}
type settingsProvider interface {
GetBool(string) bool
}
type smtpBackend struct {
panicHandler panicHandler
eventListener listener.Listener
settings settingsProvider
bridge bridger
confirmer *confirmer.Confirmer
sendRecorder *sendRecorder
}
// NewSMTPBackend returns struct implementing go-smtp/backend interface.
func NewSMTPBackend(
panicHandler panicHandler,
eventListener listener.Listener,
settings settingsProvider,
bridge *bridge.Bridge,
) *smtpBackend { //nolint[golint]
return newSMTPBackend(panicHandler, eventListener, settings, newBridgeWrap(bridge))
}
func newSMTPBackend(
panicHandler panicHandler,
eventListener listener.Listener,
settings settingsProvider,
bridge bridger,
) *smtpBackend {
return &smtpBackend{
panicHandler: panicHandler,
eventListener: eventListener,
settings: settings,
bridge: bridge,
confirmer: confirmer.New(),
sendRecorder: newSendRecorder(),
}
}
// Login authenticates a user.
func (sb *smtpBackend) Login(_ *goSMTPBackend.ConnectionState, username, password string) (goSMTPBackend.Session, error) {
// Called from go-smtp in goroutines - we need to handle panics for each function.
defer sb.panicHandler.HandlePanic()
username = strings.ToLower(username)
user, err := sb.bridge.GetUser(username)
if err != nil {
log.Warn("Cannot get user: ", err)
return nil, err
}
if err := user.CheckBridgeLogin(password); err != nil {
log.WithError(err).Error("Could not check bridge password")
// Apple Mail sometimes generates a lot of requests very quickly. It's good practice
// to have a timeout after bad logins so that we can slow those requests down a little bit.
time.Sleep(10 * time.Second)
return nil, err
}
// Client can log in only using address so we can properly close all SMTP connections.
addressID, err := user.GetAddressID(username)
if err != nil {
log.Error("Cannot get addressID: ", err)
return nil, err
}
// AddressID is only for split mode--it has to be empty for combined mode.
if user.IsCombinedAddressMode() {
addressID = ""
}
return newSMTPUser(sb.panicHandler, sb.eventListener, sb, user, username, addressID)
}
func (sb *smtpBackend) AnonymousLogin(_ *goSMTPBackend.ConnectionState) (goSMTPBackend.Session, error) {
// Called from go-smtp in goroutines - we need to handle panics for each function.
defer sb.panicHandler.HandlePanic()
return nil, errors.New("anonymous login not supported")
}
func (sb *smtpBackend) shouldReportOutgoingNoEnc() bool {
return sb.settings.GetBool(settings.ReportOutgoingNoEncKey)
}
func (sb *smtpBackend) ConfirmNoEncryption(messageID string, shouldSend bool) {
if err := sb.confirmer.SetResult(messageID, shouldSend); err != nil {
logrus.WithError(err).Error("Failed to set confirmation value")
}
}