fix: address review comments

This commit is contained in:
James Houlahan
2020-04-21 07:17:50 +00:00
parent febdf98349
commit 1457005f86
31 changed files with 88 additions and 104 deletions

View File

@ -23,12 +23,12 @@ import (
"path/filepath"
"github.com/ProtonMail/go-appdir"
"github.com/ProtonMail/proton-bridge/pkg/logs"
"github.com/hashicorp/go-multierror"
"github.com/sirupsen/logrus"
)
var (
log = logs.GetLogEntry("config") //nolint[gochecknoglobals]
log = logrus.WithField("pkg", "config") //nolint[gochecknoglobals]
)
type appDirProvider interface {

View File

@ -23,8 +23,8 @@ import (
"strings"
"sync"
"github.com/ProtonMail/proton-bridge/pkg/logs"
"github.com/docker/docker-credential-helpers/credentials"
"github.com/sirupsen/logrus"
)
const (
@ -32,7 +32,7 @@ const (
)
var (
log = logs.GetLogEntry("bridgeUtils/keychain") //nolint[gochecknoglobals]
log = logrus.WithField("pkg", "bridgeUtils/keychain") //nolint[gochecknoglobals]
ErrWrongKeychainURL = errors.New("wrong keychain base URL")
ErrMacKeychainRebuild = errors.New("keychain error -25293")

View File

@ -21,10 +21,10 @@ import (
"sync"
"time"
"github.com/ProtonMail/proton-bridge/pkg/logs"
"github.com/sirupsen/logrus"
)
var log = logs.GetLogEntry("bridgeUtils/listener") //nolint[gochecknoglobals]
var log = logrus.WithField("pkg", "bridgeUtils/listener") //nolint[gochecknoglobals]
// Listener has a list of channels watching for updates.
type Listener interface {

View File

@ -1,10 +0,0 @@
package logs
import "github.com/sirupsen/logrus"
// GetLogEntry returns logrus.Entry with PID and `packageName`.
func GetLogEntry(packageName string) *logrus.Entry {
return logrus.WithFields(logrus.Fields{
"pkg": packageName,
})
}

View File

@ -218,7 +218,7 @@ func (c *client) sendAuth(auth *Auth) {
}
go func(auth ClientAuth) {
c.cm.GetClientAuthChannel() <- auth
c.cm.clientAuths <- auth
}(ClientAuth{
UserID: c.userID,
Auth: auth,
@ -425,7 +425,7 @@ func (c *client) Unlock(password string) (kr *pmcrypto.KeyRing, err error) {
func (c *client) AuthRefresh(uidAndRefreshToken string) (auth *Auth, err error) {
// If we don't yet have a saved access token, save this one in case the refresh fails!
// That way we can try again later (see handleUnauthorizedStatus).
c.cm.SetTokenIfUnset(c.userID, uidAndRefreshToken)
c.cm.setTokenIfUnset(c.userID, uidAndRefreshToken)
split := strings.Split(uidAndRefreshToken, ":")
if len(split) != 2 {

View File

@ -32,8 +32,8 @@ type ClientManager struct {
expiredTokens chan string
expirationsLocker sync.Locker
bridgeAuths chan ClientAuth
clientAuths chan ClientAuth
clientAuths chan ClientAuth // auths received by clients from the API are received here and handled.
forwardedAuths chan ClientAuth // once auths are handled, they are forwarded on this channel.
host, scheme string
hostLocker sync.RWMutex
@ -82,12 +82,12 @@ func NewClientManager(config *ClientConfig) (cm *ClientManager) {
expiredTokens: make(chan string),
expirationsLocker: &sync.Mutex{},
host: RootURL,
host: rootURL,
scheme: rootScheme,
hostLocker: sync.RWMutex{},
bridgeAuths: make(chan ClientAuth),
clientAuths: make(chan ClientAuth),
forwardedAuths: make(chan ClientAuth),
clientAuths: make(chan ClientAuth),
proxyProvider: newProxyProvider(dohProviders, proxyQuery),
proxyUseDuration: proxyUseDuration,
@ -211,7 +211,7 @@ func (cm *ClientManager) DisallowProxy() {
defer cm.hostLocker.Unlock()
cm.allowProxy = false
cm.host = RootURL
cm.host = rootURL
}
// IsProxyEnabled returns whether we are currently proxying requests.
@ -219,7 +219,7 @@ func (cm *ClientManager) IsProxyEnabled() bool {
cm.hostLocker.RLock()
defer cm.hostLocker.RUnlock()
return cm.host != RootURL
return cm.host != rootURL
}
// switchToReachableServer switches to using a reachable server (either proxy or standard API).
@ -236,12 +236,12 @@ func (cm *ClientManager) switchToReachableServer() (proxy string, err error) {
logrus.WithField("proxy", proxy).Info("Switching to a proxy")
// If the host is currently the RootURL, it's the first time we are enabling a proxy.
// If the host is currently the rootURL, it's the first time we are enabling a proxy.
// This means we want to disable it again in 24 hours.
if cm.host == RootURL {
if cm.host == rootURL {
go func() {
<-time.After(cm.proxyUseDuration)
cm.host = RootURL
cm.host = rootURL
}()
}
@ -260,12 +260,7 @@ func (cm *ClientManager) GetToken(userID string) string {
// GetAuthUpdateChannel returns a channel on which client auths can be received.
func (cm *ClientManager) GetAuthUpdateChannel() chan ClientAuth {
return cm.bridgeAuths
}
// GetClientAuthChannel returns a channel on which clients should send auths.
func (cm *ClientManager) GetClientAuthChannel() chan ClientAuth {
return cm.clientAuths
return cm.forwardedAuths
}
// Errors for possible connection issues
@ -330,19 +325,19 @@ func checkConnection(client *http.Client, url string, errorChannel chan error) {
errorChannel <- nil
}
// forwardClientAuths handles all incoming auths from clients before forwarding them on the bridge auth channel.
// forwardClientAuths handles all incoming auths from clients before forwarding them on the forwarded auths channel.
func (cm *ClientManager) forwardClientAuths() {
for auth := range cm.clientAuths {
logrus.Debug("ClientManager received auth from client")
cm.handleClientAuth(auth)
logrus.Debug("ClientManager is forwarding auth to bridge")
cm.bridgeAuths <- auth
logrus.Debug("ClientManager is forwarding auth")
cm.forwardedAuths <- auth
}
}
// SetTokenIfUnset sets the token for the given userID if it wasn't already set.
// setTokenIfUnset sets the token for the given userID if it wasn't already set.
// The set token does not expire.
func (cm *ClientManager) SetTokenIfUnset(userID, token string) {
func (cm *ClientManager) setTokenIfUnset(userID, token string) {
cm.tokensLocker.Lock()
defer cm.tokensLocker.Unlock()

View File

@ -22,13 +22,13 @@ import (
"runtime"
)
// RootURL is the API root URL.
// rootURL is the API root URL.
//
// This can be changed using build flags: pmapi_local for "localhost/api", pmapi_dev or pmapi_prod.
// Default is pmapi_prod.
//
// It must not contain the protocol! The protocol should be in rootScheme.
var RootURL = "api.protonmail.ch" //nolint[gochecknoglobals]
var rootURL = "api.protonmail.ch" //nolint[gochecknoglobals]
var rootScheme = "https" //nolint[gochecknoglobals]
// CurrentUserAgent is the default User-Agent for go-pmapi lib. This can be changed to program

View File

@ -20,6 +20,6 @@
package pmapi
func init() {
RootURL = "dev.protonmail.com/api"
rootURL = "dev.protonmail.com/api"
rootScheme = "https"
}

View File

@ -27,7 +27,7 @@ import (
func init() {
// Use port above 1000 which doesn't need root access to start anything on it.
// Now the port is rounded pi. :-)
RootURL = "127.0.0.1:3142/api"
rootURL = "127.0.0.1:3142/api"
rootScheme = "http"
// TLS certificate is self-signed

View File

@ -330,7 +330,7 @@ func (p *DialerWithPinning) dial(network, address string) (conn net.Conn, err er
// If we are not dialing the standard API then we should skip cert verification checks.
var tlsConfig *tls.Config = nil
if address != RootURL {
if address != rootURL {
tlsConfig = &tls.Config{InsecureSkipVerify: true} // nolint[gosec]
}

View File

@ -21,3 +21,8 @@ package pmapi
func (s *Auth) DANGEROUSLYSetUID(uid string) {
s.uid = uid
}
// GetClientAuthChannel returns a channel on which clients should send auths.
func (cm *ClientManager) GetClientAuthChannel() chan ClientAuth {
return cm.clientAuths
}

View File

@ -88,9 +88,9 @@ func (p *proxyProvider) findReachableServer() (proxy string, err error) {
logrus.WithError(err).Warn("Failed to refresh proxy cache, cache may be out of date")
}
// We want to switch back to the RootURL if possible.
if p.canReach(RootURL) {
proxyResult <- RootURL
// We want to switch back to the rootURL if possible.
if p.canReach(rootURL) {
proxyResult <- rootURL
return
}

View File

@ -198,7 +198,7 @@ func TestProxyProvider_UseProxy_RevertAfterTime(t *testing.T) {
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) {
@ -227,8 +227,8 @@ func TestProxyProvider_UseProxy_RevertIfProxyStopsWorkingAndOriginalAPIIsReachab
// We should now find the original API URL if it is working again.
url, err = cm.switchToReachableServer()
require.NoError(t, err)
require.Equal(t, RootURL, url)
require.Equal(t, RootURL, cm.getHost())
require.Equal(t, rootURL, url)
require.Equal(t, rootURL, cm.getHost())
}
func TestProxyProvider_UseProxy_FindSecondAlternativeIfFirstFailsAndAPIIsStillBlocked(t *testing.T) {
@ -298,14 +298,14 @@ func TestProxyProvider_DoHLookup_FindProxyFirstProviderUnreachable(t *testing.T)
}
// testAPIURLBackup is used to hold the globalOriginalURL because we clear it for test purposes and need to restore it.
var testAPIURLBackup = RootURL
var testAPIURLBackup = rootURL
// blockAPI prevents tests from reaching the standard API, forcing them to find a proxy.
func blockAPI() {
RootURL = ""
rootURL = ""
}
// unblockAPI allow tests to reach the standard API again.
func unblockAPI() {
RootURL = testAPIURLBackup
rootURL = testAPIURLBackup
}

View File

@ -27,8 +27,8 @@ import (
"runtime"
"strings"
"github.com/ProtonMail/proton-bridge/pkg/logs"
"github.com/kardianos/osext"
"github.com/sirupsen/logrus"
)
const (
@ -44,7 +44,7 @@ var (
)
var (
log = logs.GetLogEntry("bridgeUtils/updates") //nolint[gochecknoglobals]
log = logrus.WithField("pkg", "bridgeUtils/updates") //nolint[gochecknoglobals]
installFileSuffix = map[string]string{ //nolint[gochecknoglobals]
"darwin": ".dmg",