mirror of
https://github.com/ProtonMail/proton-bridge.git
synced 2025-12-11 05:06:51 +00:00
refactor: make getHost and getScheme private
This commit is contained in:
@ -149,23 +149,6 @@ func (cm *ClientManager) LogoutClient(userID string) {
|
|||||||
return
|
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).
|
// GetRootURL returns the full root URL (scheme+host).
|
||||||
func (cm *ClientManager) GetRootURL() string {
|
func (cm *ClientManager) GetRootURL() string {
|
||||||
cm.hostLocker.Lock()
|
cm.hostLocker.Lock()
|
||||||
@ -174,6 +157,23 @@ func (cm *ClientManager) GetRootURL() string {
|
|||||||
return fmt.Sprintf("%v://%v", cm.scheme, cm.host)
|
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.
|
// IsProxyAllowed returns whether the user has allowed us to switch to a proxy if need be.
|
||||||
func (cm *ClientManager) IsProxyAllowed() bool {
|
func (cm *ClientManager) IsProxyAllowed() bool {
|
||||||
cm.hostLocker.Lock()
|
cm.hostLocker.Lock()
|
||||||
|
|||||||
@ -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
|
// 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
|
// (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.
|
// 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")
|
p.log.WithField("address", address).Debug("Aborting dial, cannot switch to a proxy")
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|||||||
@ -134,7 +134,7 @@ func TestProxyProvider_UseProxy(t *testing.T) {
|
|||||||
url, err := cm.switchToReachableServer()
|
url, err := cm.switchToReachableServer()
|
||||||
require.NoError(t, err)
|
require.NoError(t, err)
|
||||||
require.Equal(t, proxy.URL, url)
|
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) {
|
func TestProxyProvider_UseProxy_MultipleTimes(t *testing.T) {
|
||||||
@ -157,7 +157,7 @@ func TestProxyProvider_UseProxy_MultipleTimes(t *testing.T) {
|
|||||||
url, err := cm.switchToReachableServer()
|
url, err := cm.switchToReachableServer()
|
||||||
require.NoError(t, err)
|
require.NoError(t, err)
|
||||||
require.Equal(t, proxy1.URL, url)
|
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.
|
// Have to wait so as to not get rejected.
|
||||||
time.Sleep(proxyLookupWait)
|
time.Sleep(proxyLookupWait)
|
||||||
@ -166,7 +166,7 @@ func TestProxyProvider_UseProxy_MultipleTimes(t *testing.T) {
|
|||||||
url, err = cm.switchToReachableServer()
|
url, err = cm.switchToReachableServer()
|
||||||
require.NoError(t, err)
|
require.NoError(t, err)
|
||||||
require.Equal(t, proxy2.URL, url)
|
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.
|
// Have to wait so as to not get rejected.
|
||||||
time.Sleep(proxyLookupWait)
|
time.Sleep(proxyLookupWait)
|
||||||
@ -175,7 +175,7 @@ func TestProxyProvider_UseProxy_MultipleTimes(t *testing.T) {
|
|||||||
url, err = cm.switchToReachableServer()
|
url, err = cm.switchToReachableServer()
|
||||||
require.NoError(t, err)
|
require.NoError(t, err)
|
||||||
require.Equal(t, proxy3.URL, url)
|
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) {
|
func TestProxyProvider_UseProxy_RevertAfterTime(t *testing.T) {
|
||||||
@ -195,10 +195,10 @@ func TestProxyProvider_UseProxy_RevertAfterTime(t *testing.T) {
|
|||||||
url, err := cm.switchToReachableServer()
|
url, err := cm.switchToReachableServer()
|
||||||
require.NoError(t, err)
|
require.NoError(t, err)
|
||||||
require.Equal(t, proxy.URL, url)
|
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)
|
time.Sleep(2 * time.Second)
|
||||||
require.Equal(t, RootURL, cm.GetHost())
|
require.Equal(t, RootURL, cm.getHost())
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestProxyProvider_UseProxy_RevertIfProxyStopsWorkingAndOriginalAPIIsReachable(t *testing.T) {
|
func TestProxyProvider_UseProxy_RevertIfProxyStopsWorkingAndOriginalAPIIsReachable(t *testing.T) {
|
||||||
@ -217,7 +217,7 @@ func TestProxyProvider_UseProxy_RevertIfProxyStopsWorkingAndOriginalAPIIsReachab
|
|||||||
url, err := cm.switchToReachableServer()
|
url, err := cm.switchToReachableServer()
|
||||||
require.NoError(t, err)
|
require.NoError(t, err)
|
||||||
require.Equal(t, proxy.URL, url)
|
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.
|
// Simulate that the proxy stops working and that the standard api is reachable again.
|
||||||
proxy.Close()
|
proxy.Close()
|
||||||
@ -228,7 +228,7 @@ func TestProxyProvider_UseProxy_RevertIfProxyStopsWorkingAndOriginalAPIIsReachab
|
|||||||
url, err = cm.switchToReachableServer()
|
url, err = cm.switchToReachableServer()
|
||||||
require.NoError(t, err)
|
require.NoError(t, err)
|
||||||
require.Equal(t, RootURL, url)
|
require.Equal(t, RootURL, url)
|
||||||
require.Equal(t, RootURL, cm.GetHost())
|
require.Equal(t, RootURL, cm.getHost())
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestProxyProvider_UseProxy_FindSecondAlternativeIfFirstFailsAndAPIIsStillBlocked(t *testing.T) {
|
func TestProxyProvider_UseProxy_FindSecondAlternativeIfFirstFailsAndAPIIsStillBlocked(t *testing.T) {
|
||||||
@ -250,7 +250,7 @@ func TestProxyProvider_UseProxy_FindSecondAlternativeIfFirstFailsAndAPIIsStillBl
|
|||||||
url, err := cm.switchToReachableServer()
|
url, err := cm.switchToReachableServer()
|
||||||
require.NoError(t, err)
|
require.NoError(t, err)
|
||||||
require.Equal(t, proxy1.URL, url)
|
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.
|
// Have to wait so as to not get rejected.
|
||||||
time.Sleep(proxyLookupWait)
|
time.Sleep(proxyLookupWait)
|
||||||
@ -262,7 +262,7 @@ func TestProxyProvider_UseProxy_FindSecondAlternativeIfFirstFailsAndAPIIsStillBl
|
|||||||
url, err = cm.switchToReachableServer()
|
url, err = cm.switchToReachableServer()
|
||||||
require.NoError(t, err)
|
require.NoError(t, err)
|
||||||
require.Equal(t, proxy2.URL, url)
|
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) {
|
func TestProxyProvider_DoHLookup_Quad9(t *testing.T) {
|
||||||
|
|||||||
Reference in New Issue
Block a user