Send unilateral responses before sending OK

This commit is contained in:
Michal Horejsek
2020-11-24 14:53:38 +01:00
parent 33dfc5ce09
commit f469d34781
22 changed files with 370 additions and 268 deletions

View File

@ -27,7 +27,6 @@ import (
"github.com/ProtonMail/proton-bridge/internal/users/credentials"
"github.com/ProtonMail/proton-bridge/pkg/listener"
"github.com/ProtonMail/proton-bridge/pkg/pmapi"
imapBackend "github.com/emersion/go-imap/backend"
"github.com/pkg/errors"
"github.com/sirupsen/logrus"
)
@ -43,8 +42,6 @@ type User struct {
clientManager ClientManager
credStorer CredentialsStorer
imapUpdatesChannel chan imapBackend.Update
storeFactory StoreMaker
store *store.Store
@ -95,7 +92,7 @@ func (u *User) client() pmapi.Client {
// have the apitoken and password), authorising the user against the api, loading the user store (creating a new one
// if necessary), and setting the imap idle updates channel (used to send imap idle updates to the imap backend if
// something in the store changed).
func (u *User) init(idleUpdates chan imapBackend.Update) (err error) {
func (u *User) init() (err error) {
u.log.Info("Initialising user")
// Reload the user's credentials (if they log out and back in we need the new
@ -134,20 +131,9 @@ func (u *User) init(idleUpdates chan imapBackend.Update) (err error) {
}
u.store = store
// Save the imap updates channel here so it can be set later when imap connects.
u.imapUpdatesChannel = idleUpdates
return err
}
func (u *User) SetIMAPIdleUpdateChannel() {
if u.store == nil {
return
}
u.store.SetIMAPUpdateChannel(u.imapUpdatesChannel)
}
// authorizeIfNecessary checks whether user is logged in and is connected to api auth channel.
// If user is not already connected to the api auth channel (for example there was no internet during start),
// it tries to connect it.
@ -539,7 +525,7 @@ func (u *User) CloseAllConnections() {
}
if u.store != nil {
u.store.SetIMAPUpdateChannel(nil)
u.store.SetChangeNotifier(nil)
}
}

View File

@ -221,7 +221,7 @@ func TestCheckBridgeLoginLoggedOut(t *testing.T) {
m.pmapiClient.EXPECT().Addresses().Return(nil),
)
err = user.init(nil)
err = user.init()
assert.Error(t, err)
defer cleanUpUserData(user)

View File

@ -139,7 +139,7 @@ func checkNewUserHasCredentials(creds *credentials.Credentials, m mocks) {
user, _ := newUser(m.PanicHandler, "user", m.eventListener, m.credentialsStore, m.clientManager, m.storeMaker)
defer cleanUpUserData(user)
_ = user.init(nil)
_ = user.init()
waitForEvents()

View File

@ -34,7 +34,7 @@ func testNewUser(m mocks) *User {
user, err := newUser(m.PanicHandler, "user", m.eventListener, m.credentialsStore, m.clientManager, m.storeMaker)
assert.NoError(m.t, err)
err = user.init(nil)
err = user.init()
assert.NoError(m.t, err)
mockAuthUpdate(user, "reftok", m)
@ -51,7 +51,7 @@ func testNewUserForLogout(m mocks) *User {
user, err := newUser(m.PanicHandler, "user", m.eventListener, m.credentialsStore, m.clientManager, m.storeMaker)
assert.NoError(m.t, err)
err = user.init(nil)
err = user.init()
assert.NoError(m.t, err)
return user

View File

@ -26,7 +26,6 @@ import (
"github.com/ProtonMail/proton-bridge/internal/metrics"
"github.com/ProtonMail/proton-bridge/pkg/listener"
"github.com/ProtonMail/proton-bridge/pkg/pmapi"
imapBackend "github.com/emersion/go-imap/backend"
"github.com/hashicorp/go-multierror"
"github.com/pkg/errors"
logrus "github.com/sirupsen/logrus"
@ -58,11 +57,6 @@ type Users struct {
// as is, without requesting server again.
useOnlyActiveAddresses bool
// idleUpdates is a channel which the imap backend listens to and which it
// uses to send idle updates to the mail client (eg thunderbird).
// The user stores should send idle updates on this channel.
idleUpdates chan imapBackend.Update
lock sync.RWMutex
// stopAll can be closed to stop all goroutines from looping (watchAppOutdated, watchAPIAuths, heartbeat etc).
@ -88,7 +82,6 @@ func New(
credStorer: credStorer,
storeFactory: storeFactory,
useOnlyActiveAddresses: useOnlyActiveAddresses,
idleUpdates: make(chan imapBackend.Update),
lock: sync.RWMutex{},
stopAll: make(chan struct{}),
}
@ -132,7 +125,7 @@ func (u *Users) loadUsersFromCredentialsStore() (err error) {
u.users = append(u.users, user)
if initUserErr := user.init(u.idleUpdates); initUserErr != nil {
if initUserErr := user.init(); initUserErr != nil {
l.WithField("user", userID).WithError(initUserErr).Warn("Could not initialise user")
}
}
@ -285,7 +278,7 @@ func (u *Users) connectExistingUser(user *User, auth *pmapi.Auth, hashedPassphra
return errors.Wrap(err, "failed to update token of user in credentials store")
}
if err = user.init(u.idleUpdates); err != nil {
if err = user.init(); err != nil {
return errors.Wrap(err, "failed to initialise user")
}
@ -326,7 +319,7 @@ func (u *Users) addNewUser(apiUser *pmapi.User, auth *pmapi.Auth, hashedPassphra
// The user needs to be part of the users list in order for it to receive an auth during initialisation.
u.users = append(u.users, user)
if err = user.init(u.idleUpdates); err != nil {
if err = user.init(); err != nil {
u.users = u.users[:len(u.users)-1]
return errors.Wrap(err, "failed to initialise user")
}
@ -464,15 +457,6 @@ func (u *Users) SendMetric(m metrics.Metric) {
}).Debug("Metric successfully sent")
}
// GetIMAPUpdatesChannel sets the channel on which idle events should be sent.
func (u *Users) GetIMAPUpdatesChannel() chan imapBackend.Update {
if u.idleUpdates == nil {
log.Warn("IMAP updates channel is nil")
}
return u.idleUpdates
}
// AllowProxy instructs the app to use DoH to access an API proxy if necessary.
// It also needs to work before the app is initialised (because we may need to use the proxy at startup).
func (u *Users) AllowProxy() {