Other: Factory reset

This commit is contained in:
James Houlahan
2022-10-13 03:26:31 +02:00
parent cec44be7c3
commit a74b025de3
8 changed files with 62 additions and 12 deletions

View File

@ -23,7 +23,7 @@ func runFrontend(
) error {
switch {
case c.Bool(flagCLI):
return bridgeCLI.New(bridge, eventCh).Loop()
return bridgeCLI.New(bridge, restarter, eventCh).Loop()
case c.Bool(flagNonInteractive):
select {}

View File

@ -283,10 +283,6 @@ func (bridge *Bridge) GetEvents(ofType ...events.Event) (<-chan events.Event, fu
return newWatcher.GetChannel(), func() { bridge.remWatcher(newWatcher) }
}
func (bridge *Bridge) FactoryReset(ctx context.Context) error {
panic("TODO")
}
func (bridge *Bridge) PushError(err error) {
bridge.errors = append(bridge.errors, err)
}

View File

@ -8,6 +8,7 @@ import (
"github.com/ProtonMail/proton-bridge/v2/internal/updater"
"github.com/ProtonMail/proton-bridge/v2/internal/user"
"github.com/ProtonMail/proton-bridge/v2/internal/vault"
"github.com/sirupsen/logrus"
)
func (bridge *Bridge) GetKeychainApp() (string, error) {
@ -238,3 +239,19 @@ func (bridge *Bridge) GetColorScheme() string {
func (bridge *Bridge) SetColorScheme(colorScheme string) error {
return bridge.vault.SetColorScheme(colorScheme)
}
func (bridge *Bridge) FactoryReset(ctx context.Context) {
// First delete all users.
for _, userID := range bridge.GetUserIDs() {
if bridge.users.Has(userID) {
if err := bridge.DeleteUser(ctx, userID); err != nil {
logrus.WithError(err).Errorf("Failed to delete user %s", userID)
}
}
}
// Then delete all files.
if err := bridge.locator.Clear(); err != nil {
logrus.WithError(err).Error("Failed to clear data paths")
}
}

View File

@ -9,6 +9,7 @@ type Locator interface {
ProvideLogsPath() (string, error)
GetLicenseFilePath() string
GetDependencyLicensesLink() string
Clear() error
}
type Identifier interface {

View File

@ -240,6 +240,10 @@ func (f *frontendCLI) deleteEverything(c *ishell.Context) {
f.bridge.FactoryReset(context.Background())
c.Println("Everything cleared")
f.restarter.Set(true, false)
f.Stop()
}
func (f *frontendCLI) changeMode(c *ishell.Context) {

View File

@ -24,6 +24,7 @@ import (
"github.com/ProtonMail/proton-bridge/v2/internal/bridge"
"github.com/ProtonMail/proton-bridge/v2/internal/constants"
"github.com/ProtonMail/proton-bridge/v2/internal/events"
"github.com/ProtonMail/proton-bridge/v2/pkg/restarter"
"github.com/abiosoft/ishell"
"github.com/sirupsen/logrus"
@ -34,14 +35,16 @@ var log = logrus.WithField("pkg", "frontend/cli") //nolint:gochecknoglobals
type frontendCLI struct {
*ishell.Shell
bridge *bridge.Bridge
bridge *bridge.Bridge
restarter *restarter.Restarter
}
// New returns a new CLI frontend configured with the given options.
func New(bridge *bridge.Bridge, eventCh <-chan events.Event) *frontendCLI {
func New(bridge *bridge.Bridge, restarter *restarter.Restarter, eventCh <-chan events.Event) *frontendCLI {
fe := &frontendCLI{
Shell: ishell.New(),
bridge: bridge,
Shell: ishell.New(),
bridge: bridge,
restarter: restarter,
}
// Clear commands.

View File

@ -27,6 +27,7 @@ import (
"net"
"path/filepath"
"sync"
"time"
"github.com/ProtonMail/proton-bridge/v2/internal/bridge"
"github.com/ProtonMail/proton-bridge/v2/internal/certs"
@ -298,6 +299,9 @@ func (s *Service) finishLogin() {
return
}
eventCh, done := s.bridge.GetEvents(events.UserLoggedIn{})
defer done()
userID, err := s.bridge.LoginUser(context.Background(), s.authClient, s.auth, s.password)
if err != nil {
s.log.WithError(err).Errorf("Finish login failed")
@ -305,18 +309,34 @@ func (s *Service) finishLogin() {
return
}
s.waitForUserChangeDone(eventCh, userID)
s.log.WithField("userID", userID).Debug("Login finished")
_ = s.SendEvent(NewLoginFinishedEvent(userID))
}
func (s *Service) waitForUserChangeDone(eventCh <-chan events.Event, userID string) {
for {
select {
case event := <-eventCh:
if login, ok := event.(events.UserLoggedIn); ok && login.UserID == userID {
return
}
case <-time.After(2 * time.Second):
s.log.WithField("ID", userID).Warning("Login finished but user not added within 2 seconds")
return
}
}
}
func (s *Service) triggerReset() {
defer func() {
_ = s.SendEvent(NewResetFinishedEvent())
}()
if err := s.bridge.FactoryReset(context.Background()); err != nil {
s.log.WithError(err).Error("Failed to reset")
}
s.bridge.FactoryReset(context.Background())
}
func newTLSConfig() (*tls.Config, []byte, error) {

View File

@ -151,6 +151,15 @@ func (vault *Vault) DeleteUser(userID string) error {
}
func (vault *Vault) Close() error {
vault.refLock.Lock()
defer vault.refLock.Unlock()
if len(vault.ref) > 0 {
return errors.New("vault is still in use")
}
vault.gcm = nil
return nil
}