From 42ced6694e469138ea1aa841ce86f3a88f7efb24 Mon Sep 17 00:00:00 2001 From: Alexander Bilyak Date: Wed, 27 Oct 2021 16:14:18 +0200 Subject: [PATCH] GODT-1388: Refactor Alternative routing --- internal/bridge/bridge.go | 16 ++++++++++++++++ internal/frontend/cli/system.go | 10 ++++------ internal/frontend/qt/frontend_settings.go | 8 ++------ internal/frontend/qt/qml_backend.go | 3 ++- internal/frontend/types/types.go | 4 ++-- internal/users/users.go | 12 ------------ 6 files changed, 26 insertions(+), 27 deletions(-) diff --git a/internal/bridge/bridge.go b/internal/bridge/bridge.go index 94abb4d6..6143d59d 100644 --- a/internal/bridge/bridge.go +++ b/internal/bridge/bridge.go @@ -223,3 +223,19 @@ func (b *Bridge) MigrateCache(from, to string) error { return nil } + +// SetProxyAllowed instructs the app whether 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 (b *Bridge) SetProxyAllowed(proxyAllowed bool) { + b.settings.SetBool(settings.AllowProxyKey, proxyAllowed) + if proxyAllowed { + b.clientManager.AllowProxy() + } else { + b.clientManager.DisallowProxy() + } +} + +// GetProxyAllowed returns whether use of DoH is enabled to access an API proxy if necessary. +func (b *Bridge) GetProxyAllowed() bool { + return b.settings.GetBool(settings.AllowProxyKey) +} diff --git a/internal/frontend/cli/system.go b/internal/frontend/cli/system.go index 092ccfea..43b8143d 100644 --- a/internal/frontend/cli/system.go +++ b/internal/frontend/cli/system.go @@ -129,7 +129,7 @@ func (f *frontendCLI) changePort(c *ishell.Context) { } func (f *frontendCLI) allowProxy(c *ishell.Context) { - if f.settings.GetBool(settings.AllowProxyKey) { + if f.bridge.GetProxyAllowed() { f.Println("Bridge is already set to use alternative routing to connect to Proton if it is being blocked.") return } @@ -137,13 +137,12 @@ func (f *frontendCLI) allowProxy(c *ishell.Context) { f.Println("Bridge is currently set to NOT use alternative routing to connect to Proton if it is being blocked.") if f.yesNoQuestion("Are you sure you want to allow bridge to do this") { - f.settings.SetBool(settings.AllowProxyKey, true) - f.bridge.AllowProxy() + f.bridge.SetProxyAllowed(true) } } func (f *frontendCLI) disallowProxy(c *ishell.Context) { - if !f.settings.GetBool(settings.AllowProxyKey) { + if !f.bridge.GetProxyAllowed() { f.Println("Bridge is already set to NOT use alternative routing to connect to Proton if it is being blocked.") return } @@ -151,8 +150,7 @@ func (f *frontendCLI) disallowProxy(c *ishell.Context) { f.Println("Bridge is currently set to use alternative routing to connect to Proton if it is being blocked.") if f.yesNoQuestion("Are you sure you want to stop bridge from doing this") { - f.settings.SetBool(settings.AllowProxyKey, false) - f.bridge.DisallowProxy() + f.bridge.SetProxyAllowed(false) } } diff --git a/internal/frontend/qt/frontend_settings.go b/internal/frontend/qt/frontend_settings.go index 5c810962..3a2467a9 100644 --- a/internal/frontend/qt/frontend_settings.go +++ b/internal/frontend/qt/frontend_settings.go @@ -99,12 +99,8 @@ func (f *FrontendQt) toggleAutostart(makeItEnabled bool) { } func (f *FrontendQt) toggleDoH(makeItEnabled bool) { - if f.settings.GetBool(settings.AllowProxyKey) == makeItEnabled { - f.qml.SetIsDoHEnabled(makeItEnabled) - return - } - f.settings.SetBool(settings.AllowProxyKey, makeItEnabled) - f.restart() + f.bridge.SetProxyAllowed(makeItEnabled) + f.qml.SetIsDoHEnabled(f.bridge.GetProxyAllowed()) } func (f *FrontendQt) toggleUseSSLforSMTP(makeItEnabled bool) { diff --git a/internal/frontend/qt/qml_backend.go b/internal/frontend/qt/qml_backend.go index e1f9c5a5..589aa03a 100644 --- a/internal/frontend/qt/qml_backend.go +++ b/internal/frontend/qt/qml_backend.go @@ -15,6 +15,7 @@ // You should have received a copy of the GNU General Public License // along with ProtonMail Bridge. If not, see . +//go:build build_qt // +build build_qt package qt @@ -232,7 +233,7 @@ func (q *QMLBackend) setup(f *FrontendQt) { }() }) - q.SetIsDoHEnabled(f.settings.GetBool(settings.AllowProxyKey)) + q.SetIsDoHEnabled(f.bridge.GetProxyAllowed()) q.ConnectToggleDoH(f.toggleDoH) q.SetUseSSLforSMTP(f.settings.GetBool(settings.SMTPSSLKey)) diff --git a/internal/frontend/types/types.go b/internal/frontend/types/types.go index 31a7fe75..926f12fa 100644 --- a/internal/frontend/types/types.go +++ b/internal/frontend/types/types.go @@ -75,8 +75,8 @@ type Bridger interface { UserManager ReportBug(osType, osVersion, description, accountName, address, emailClient string, attachLogs bool) error - AllowProxy() - DisallowProxy() + SetProxyAllowed(bool) + GetProxyAllowed() bool EnableCache() error DisableCache() error MigrateCache(from, to string) error diff --git a/internal/users/users.go b/internal/users/users.go index 9b1005a4..b02f3122 100644 --- a/internal/users/users.go +++ b/internal/users/users.go @@ -419,18 +419,6 @@ func (u *Users) SendMetric(m metrics.Metric) error { return nil } -// 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() { - u.clientManager.AllowProxy() -} - -// DisallowProxy instructs the app to not 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) DisallowProxy() { - u.clientManager.DisallowProxy() -} - // hasUser returns whether the struct currently has a user with ID `id`. func (u *Users) hasUser(id string) (user *User, ok bool) { for _, u := range u.users {