From abca7284dd35b19b3df380ce0b184549d27c34ec Mon Sep 17 00:00:00 2001 From: James Houlahan Date: Wed, 8 Apr 2020 13:08:35 +0200 Subject: [PATCH] refactor: make getHost and getScheme private --- pkg/pmapi/clientmanager.go | 34 +++++++++++++++++----------------- pkg/pmapi/dialer_with_proxy.go | 2 +- pkg/pmapi/proxy_test.go | 20 ++++++++++---------- 3 files changed, 28 insertions(+), 28 deletions(-) diff --git a/pkg/pmapi/clientmanager.go b/pkg/pmapi/clientmanager.go index a7d226a8..1943adbb 100644 --- a/pkg/pmapi/clientmanager.go +++ b/pkg/pmapi/clientmanager.go @@ -149,23 +149,6 @@ func (cm *ClientManager) LogoutClient(userID string) { return } -// GetHost returns the host to make requests to. -// It does not include the protocol i.e. no "https://" (use GetScheme for that). -func (cm *ClientManager) GetHost() string { - cm.hostLocker.Lock() - defer cm.hostLocker.Unlock() - - return cm.host -} - -// GetScheme returns the scheme with which to make requests to the host. -func (cm *ClientManager) GetScheme() string { - cm.hostLocker.Lock() - defer cm.hostLocker.Unlock() - - return cm.scheme -} - // GetRootURL returns the full root URL (scheme+host). func (cm *ClientManager) GetRootURL() string { cm.hostLocker.Lock() @@ -174,6 +157,23 @@ func (cm *ClientManager) GetRootURL() string { return fmt.Sprintf("%v://%v", cm.scheme, cm.host) } +// getHost returns the host to make requests to. +// It does not include the protocol i.e. no "https://" (use getScheme for that). +func (cm *ClientManager) getHost() string { + cm.hostLocker.Lock() + defer cm.hostLocker.Unlock() + + return cm.host +} + +// getScheme returns the scheme with which to make requests to the host. +func (cm *ClientManager) getScheme() string { + cm.hostLocker.Lock() + defer cm.hostLocker.Unlock() + + return cm.scheme +} + // IsProxyAllowed returns whether the user has allowed us to switch to a proxy if need be. func (cm *ClientManager) IsProxyAllowed() bool { cm.hostLocker.Lock() diff --git a/pkg/pmapi/dialer_with_proxy.go b/pkg/pmapi/dialer_with_proxy.go index 5adc34fa..e03e628b 100644 --- a/pkg/pmapi/dialer_with_proxy.go +++ b/pkg/pmapi/dialer_with_proxy.go @@ -297,7 +297,7 @@ func (p *DialerWithPinning) dialWithProxyFallback(network, address string) (conn // If DoH is not allowed, give up. Or, if we are dialing something other than the API // (e.g. we dial protonmail.com/... to check for updates), there's also no point in // continuing since a proxy won't help us reach that. - if !p.cm.IsProxyAllowed() || host != p.cm.GetHost() { + if !p.cm.IsProxyAllowed() || host != p.cm.getHost() { p.log.WithField("address", address).Debug("Aborting dial, cannot switch to a proxy") return } diff --git a/pkg/pmapi/proxy_test.go b/pkg/pmapi/proxy_test.go index aa678549..579e873e 100644 --- a/pkg/pmapi/proxy_test.go +++ b/pkg/pmapi/proxy_test.go @@ -134,7 +134,7 @@ func TestProxyProvider_UseProxy(t *testing.T) { url, err := cm.switchToReachableServer() require.NoError(t, err) require.Equal(t, proxy.URL, url) - require.Equal(t, proxy.URL, cm.GetHost()) + require.Equal(t, proxy.URL, cm.getHost()) } func TestProxyProvider_UseProxy_MultipleTimes(t *testing.T) { @@ -157,7 +157,7 @@ func TestProxyProvider_UseProxy_MultipleTimes(t *testing.T) { url, err := cm.switchToReachableServer() require.NoError(t, err) require.Equal(t, proxy1.URL, url) - require.Equal(t, proxy1.URL, cm.GetHost()) + require.Equal(t, proxy1.URL, cm.getHost()) // Have to wait so as to not get rejected. time.Sleep(proxyLookupWait) @@ -166,7 +166,7 @@ func TestProxyProvider_UseProxy_MultipleTimes(t *testing.T) { url, err = cm.switchToReachableServer() require.NoError(t, err) require.Equal(t, proxy2.URL, url) - require.Equal(t, proxy2.URL, cm.GetHost()) + require.Equal(t, proxy2.URL, cm.getHost()) // Have to wait so as to not get rejected. time.Sleep(proxyLookupWait) @@ -175,7 +175,7 @@ func TestProxyProvider_UseProxy_MultipleTimes(t *testing.T) { url, err = cm.switchToReachableServer() require.NoError(t, err) require.Equal(t, proxy3.URL, url) - require.Equal(t, proxy3.URL, cm.GetHost()) + require.Equal(t, proxy3.URL, cm.getHost()) } func TestProxyProvider_UseProxy_RevertAfterTime(t *testing.T) { @@ -195,10 +195,10 @@ func TestProxyProvider_UseProxy_RevertAfterTime(t *testing.T) { url, err := cm.switchToReachableServer() require.NoError(t, err) require.Equal(t, proxy.URL, url) - require.Equal(t, proxy.URL, cm.GetHost()) + require.Equal(t, proxy.URL, cm.getHost()) time.Sleep(2 * time.Second) - require.Equal(t, RootURL, cm.GetHost()) + require.Equal(t, RootURL, cm.getHost()) } func TestProxyProvider_UseProxy_RevertIfProxyStopsWorkingAndOriginalAPIIsReachable(t *testing.T) { @@ -217,7 +217,7 @@ func TestProxyProvider_UseProxy_RevertIfProxyStopsWorkingAndOriginalAPIIsReachab url, err := cm.switchToReachableServer() require.NoError(t, err) require.Equal(t, proxy.URL, url) - require.Equal(t, proxy.URL, cm.GetHost()) + require.Equal(t, proxy.URL, cm.getHost()) // Simulate that the proxy stops working and that the standard api is reachable again. proxy.Close() @@ -228,7 +228,7 @@ func TestProxyProvider_UseProxy_RevertIfProxyStopsWorkingAndOriginalAPIIsReachab url, err = cm.switchToReachableServer() require.NoError(t, err) require.Equal(t, RootURL, url) - require.Equal(t, RootURL, cm.GetHost()) + require.Equal(t, RootURL, cm.getHost()) } func TestProxyProvider_UseProxy_FindSecondAlternativeIfFirstFailsAndAPIIsStillBlocked(t *testing.T) { @@ -250,7 +250,7 @@ func TestProxyProvider_UseProxy_FindSecondAlternativeIfFirstFailsAndAPIIsStillBl url, err := cm.switchToReachableServer() require.NoError(t, err) require.Equal(t, proxy1.URL, url) - require.Equal(t, proxy1.URL, cm.GetHost()) + require.Equal(t, proxy1.URL, cm.getHost()) // Have to wait so as to not get rejected. time.Sleep(proxyLookupWait) @@ -262,7 +262,7 @@ func TestProxyProvider_UseProxy_FindSecondAlternativeIfFirstFailsAndAPIIsStillBl url, err = cm.switchToReachableServer() require.NoError(t, err) require.Equal(t, proxy2.URL, url) - require.Equal(t, proxy2.URL, cm.GetHost()) + require.Equal(t, proxy2.URL, cm.getHost()) } func TestProxyProvider_DoHLookup_Quad9(t *testing.T) {