refactor: no more pmapifactory

This commit is contained in:
James Houlahan
2020-04-08 13:07:13 +02:00
parent 5bf4d9c6f5
commit db02eb694d
22 changed files with 70 additions and 76 deletions

View File

@ -51,12 +51,12 @@ import (
"github.com/ProtonMail/proton-bridge/internal/events" "github.com/ProtonMail/proton-bridge/internal/events"
"github.com/ProtonMail/proton-bridge/internal/frontend" "github.com/ProtonMail/proton-bridge/internal/frontend"
"github.com/ProtonMail/proton-bridge/internal/imap" "github.com/ProtonMail/proton-bridge/internal/imap"
"github.com/ProtonMail/proton-bridge/internal/pmapifactory"
"github.com/ProtonMail/proton-bridge/internal/preferences" "github.com/ProtonMail/proton-bridge/internal/preferences"
"github.com/ProtonMail/proton-bridge/internal/smtp" "github.com/ProtonMail/proton-bridge/internal/smtp"
"github.com/ProtonMail/proton-bridge/pkg/args" "github.com/ProtonMail/proton-bridge/pkg/args"
"github.com/ProtonMail/proton-bridge/pkg/config" "github.com/ProtonMail/proton-bridge/pkg/config"
"github.com/ProtonMail/proton-bridge/pkg/listener" "github.com/ProtonMail/proton-bridge/pkg/listener"
"github.com/ProtonMail/proton-bridge/pkg/logs"
"github.com/ProtonMail/proton-bridge/pkg/pmapi" "github.com/ProtonMail/proton-bridge/pkg/pmapi"
"github.com/ProtonMail/proton-bridge/pkg/updates" "github.com/ProtonMail/proton-bridge/pkg/updates"
"github.com/allan-simon/go-singleinstance" "github.com/allan-simon/go-singleinstance"
@ -87,7 +87,7 @@ var (
longVersion = Version + " (" + Revision + ")" //nolint[gochecknoglobals] longVersion = Version + " (" + Revision + ")" //nolint[gochecknoglobals]
buildVersion = longVersion + " " + BuildTime //nolint[gochecknoglobals] buildVersion = longVersion + " " + BuildTime //nolint[gochecknoglobals]
log = config.GetLogEntry("main") //nolint[gochecknoglobals] log = logs.GetLogEntry("main") //nolint[gochecknoglobals]
// How many crashes in a row. // How many crashes in a row.
numberOfCrashes = 0 //nolint[gochecknoglobals] numberOfCrashes = 0 //nolint[gochecknoglobals]
@ -274,9 +274,8 @@ func run(context *cli.Context) (contextError error) { // nolint[funlen]
log.Error("Could not get credentials store: ", credentialsError) log.Error("Could not get credentials store: ", credentialsError)
} }
clientConfig := pmapifactory.GetClientConfig(cfg.GetAPIConfig()) cm := pmapi.NewClientManager(cfg.GetAPIConfig())
cm := pmapi.NewClientManager(clientConfig) cm.SetRoundTripper(cfg.GetRoundTripper(cm, eventListener))
pmapifactory.SetClientRoundTripper(cm, clientConfig, eventListener)
bridgeInstance := bridge.New(cfg, pref, panicHandler, eventListener, Version, cm, credentialsStore) bridgeInstance := bridge.New(cfg, pref, panicHandler, eventListener, Version, cm, credentialsStore)
imapBackend := imap.NewIMAPBackend(panicHandler, eventListener, cfg, bridgeInstance) imapBackend := imap.NewIMAPBackend(panicHandler, eventListener, cfg, bridgeInstance)

View File

@ -31,11 +31,12 @@ import (
"github.com/ProtonMail/proton-bridge/internal/preferences" "github.com/ProtonMail/proton-bridge/internal/preferences"
"github.com/ProtonMail/proton-bridge/pkg/config" "github.com/ProtonMail/proton-bridge/pkg/config"
"github.com/ProtonMail/proton-bridge/pkg/listener" "github.com/ProtonMail/proton-bridge/pkg/listener"
"github.com/ProtonMail/proton-bridge/pkg/logs"
"github.com/ProtonMail/proton-bridge/pkg/ports" "github.com/ProtonMail/proton-bridge/pkg/ports"
) )
var ( var (
log = config.GetLogEntry("api") //nolint[gochecknoglobals] log = logs.GetLogEntry("api") //nolint[gochecknoglobals]
) )
type apiServer struct { type apiServer struct {

View File

@ -28,8 +28,8 @@ import (
"github.com/ProtonMail/proton-bridge/internal/metrics" "github.com/ProtonMail/proton-bridge/internal/metrics"
"github.com/ProtonMail/proton-bridge/internal/preferences" "github.com/ProtonMail/proton-bridge/internal/preferences"
"github.com/ProtonMail/proton-bridge/internal/store" "github.com/ProtonMail/proton-bridge/internal/store"
"github.com/ProtonMail/proton-bridge/pkg/config"
"github.com/ProtonMail/proton-bridge/pkg/listener" "github.com/ProtonMail/proton-bridge/pkg/listener"
"github.com/ProtonMail/proton-bridge/pkg/logs"
"github.com/ProtonMail/proton-bridge/pkg/pmapi" "github.com/ProtonMail/proton-bridge/pkg/pmapi"
"github.com/hashicorp/go-multierror" "github.com/hashicorp/go-multierror"
"github.com/pkg/errors" "github.com/pkg/errors"
@ -37,8 +37,8 @@ import (
) )
var ( var (
log = config.GetLogEntry("bridge") //nolint[gochecknoglobals] log = logs.GetLogEntry("bridge") //nolint[gochecknoglobals]
isApplicationOutdated = false //nolint[gochecknoglobals] isApplicationOutdated = false //nolint[gochecknoglobals]
) )
// Bridge is a struct handling users. // Bridge is a struct handling users.

View File

@ -27,14 +27,14 @@ import (
"fmt" "fmt"
"strings" "strings"
"github.com/ProtonMail/proton-bridge/pkg/config" "github.com/ProtonMail/proton-bridge/pkg/logs"
"github.com/sirupsen/logrus" "github.com/sirupsen/logrus"
) )
const sep = "\x00" const sep = "\x00"
var ( var (
log = config.GetLogEntry("bridge") //nolint[gochecknoglobals] log = logs.GetLogEntry("bridge") //nolint[gochecknoglobals]
ErrWrongFormat = errors.New("backend/creds: malformed password") ErrWrongFormat = errors.New("backend/creds: malformed password")
) )

View File

@ -24,12 +24,13 @@ import (
"github.com/ProtonMail/proton-bridge/internal/preferences" "github.com/ProtonMail/proton-bridge/internal/preferences"
"github.com/ProtonMail/proton-bridge/pkg/config" "github.com/ProtonMail/proton-bridge/pkg/config"
"github.com/ProtonMail/proton-bridge/pkg/listener" "github.com/ProtonMail/proton-bridge/pkg/listener"
"github.com/ProtonMail/proton-bridge/pkg/logs"
"github.com/abiosoft/ishell" "github.com/abiosoft/ishell"
) )
var ( var (
log = config.GetLogEntry("frontend/cli") //nolint[gochecknoglobals] log = logs.GetLogEntry("frontend/cli") //nolint[gochecknoglobals]
) )
type frontendCLI struct { type frontendCLI struct {

View File

@ -26,10 +26,11 @@ import (
"github.com/ProtonMail/proton-bridge/internal/frontend/types" "github.com/ProtonMail/proton-bridge/internal/frontend/types"
"github.com/ProtonMail/proton-bridge/pkg/config" "github.com/ProtonMail/proton-bridge/pkg/config"
"github.com/ProtonMail/proton-bridge/pkg/listener" "github.com/ProtonMail/proton-bridge/pkg/listener"
"github.com/ProtonMail/proton-bridge/pkg/logs"
) )
var ( var (
log = config.GetLogEntry("frontend") // nolint[unused] log = logs.GetLogEntry("frontend") // nolint[unused]
) )
// Frontend is an interface to be implemented by each frontend type (cli, gui, html). // Frontend is an interface to be implemented by each frontend type (cli, gui, html).

View File

@ -43,6 +43,7 @@ import (
"github.com/ProtonMail/proton-bridge/internal/frontend/types" "github.com/ProtonMail/proton-bridge/internal/frontend/types"
"github.com/ProtonMail/proton-bridge/internal/preferences" "github.com/ProtonMail/proton-bridge/internal/preferences"
"github.com/ProtonMail/proton-bridge/pkg/config" "github.com/ProtonMail/proton-bridge/pkg/config"
"github.com/ProtonMail/proton-bridge/pkg/logs"
"github.com/ProtonMail/proton-bridge/pkg/ports" "github.com/ProtonMail/proton-bridge/pkg/ports"
"github.com/ProtonMail/proton-bridge/pkg/useragent" "github.com/ProtonMail/proton-bridge/pkg/useragent"
@ -58,7 +59,7 @@ import (
"github.com/therecipe/qt/widgets" "github.com/therecipe/qt/widgets"
) )
var log = config.GetLogEntry("frontend-qt") var log = logs.GetLogEntry("frontend-qt")
var accountMutex = &sync.Mutex{} var accountMutex = &sync.Mutex{}
// API between Bridge and Qt. // API between Bridge and Qt.

View File

@ -26,9 +26,10 @@ import (
"github.com/ProtonMail/proton-bridge/internal/frontend/types" "github.com/ProtonMail/proton-bridge/internal/frontend/types"
"github.com/ProtonMail/proton-bridge/pkg/config" "github.com/ProtonMail/proton-bridge/pkg/config"
"github.com/ProtonMail/proton-bridge/pkg/listener" "github.com/ProtonMail/proton-bridge/pkg/listener"
"github.com/ProtonMail/proton-bridge/pkg/logs"
) )
var log = config.GetLogEntry("frontend-nogui") //nolint[gochecknoglobals] var log = logs.GetLogEntry("frontend-nogui") //nolint[gochecknoglobals]
type FrontendHeadless struct{} type FrontendHeadless struct{}

View File

@ -17,7 +17,7 @@
package imap package imap
import "github.com/ProtonMail/proton-bridge/pkg/config" import "github.com/ProtonMail/proton-bridge/pkg/logs"
const ( const (
fetchMessagesWorkers = 5 // In how many workers to fetch message (group list on IMAP). fetchMessagesWorkers = 5 // In how many workers to fetch message (group list on IMAP).
@ -31,5 +31,5 @@ const (
) )
var ( var (
log = config.GetLogEntry("imap") //nolint[gochecknoglobals] log = logs.GetLogEntry("imap") //nolint[gochecknoglobals]
) )

View File

@ -18,8 +18,8 @@
// Package smtp provides SMTP server of the Bridge. // Package smtp provides SMTP server of the Bridge.
package smtp package smtp
import "github.com/ProtonMail/proton-bridge/pkg/config" import "github.com/ProtonMail/proton-bridge/pkg/logs"
var ( var (
log = config.GetLogEntry("smtp") //nolint[gochecknoglobals] log = logs.GetLogEntry("smtp") //nolint[gochecknoglobals]
) )

View File

@ -21,15 +21,14 @@ import (
"io/ioutil" "io/ioutil"
"os" "os"
"path/filepath" "path/filepath"
"strings"
"github.com/ProtonMail/go-appdir" "github.com/ProtonMail/go-appdir"
"github.com/ProtonMail/proton-bridge/pkg/pmapi" "github.com/ProtonMail/proton-bridge/pkg/logs"
"github.com/hashicorp/go-multierror" "github.com/hashicorp/go-multierror"
) )
var ( var (
log = GetLogEntry("config") //nolint[gochecknoglobals] log = logs.GetLogEntry("config") //nolint[gochecknoglobals]
) )
type appDirProvider interface { type appDirProvider interface {
@ -45,7 +44,6 @@ type Config struct {
cacheVersion string cacheVersion string
appDirs appDirProvider appDirs appDirProvider
appDirsVersion appDirProvider appDirsVersion appDirProvider
apiConfig *pmapi.ClientConfig
} }
// New returns fully initialized config struct. // New returns fully initialized config struct.
@ -67,11 +65,6 @@ func newConfig(appName, version, revision, cacheVersion string, appDirs, appDirs
cacheVersion: cacheVersion, cacheVersion: cacheVersion,
appDirs: appDirs, appDirs: appDirs,
appDirsVersion: appDirsVersion, appDirsVersion: appDirsVersion,
apiConfig: &pmapi.ClientConfig{
AppVersion: strings.Title(appName) + "_" + version,
ClientID: appName,
SentryDSN: "https://bacfb56338a7471a9fede610046afdda:ab437b0d13f54602a0f5feb684e6d319@api.protonmail.ch/reports/sentry/8",
},
} }
} }
@ -229,11 +222,6 @@ func (c *Config) GetPreferencesPath() string {
return filepath.Join(c.appDirsVersion.UserCache(), "prefs.json") return filepath.Join(c.appDirsVersion.UserCache(), "prefs.json")
} }
// GetAPIConfig returns config for ProtonMail API.
func (c *Config) GetAPIConfig() *pmapi.ClientConfig {
return c.apiConfig
}
// GetDefaultAPIPort returns default Bridge local API port. // GetDefaultAPIPort returns default Bridge local API port.
func (c *Config) GetDefaultAPIPort() int { func (c *Config) GetDefaultAPIPort() int {
return 1042 return 1042

View File

@ -54,13 +54,6 @@ var logFile *os.File //nolint[gochecknoglobals]
var logFileRgx = regexp.MustCompile("^v.*\\.log$") //nolint[gochecknoglobals] var logFileRgx = regexp.MustCompile("^v.*\\.log$") //nolint[gochecknoglobals]
var logCrashRgx = regexp.MustCompile("^v.*_crash_.*\\.log$") //nolint[gochecknoglobals] var logCrashRgx = regexp.MustCompile("^v.*_crash_.*\\.log$") //nolint[gochecknoglobals]
// GetLogEntry returns logrus.Entry with PID and `packageName`.
func GetLogEntry(packageName string) *logrus.Entry {
return logrus.WithFields(logrus.Fields{
"pkg": packageName,
})
}
// HandlePanic reports the crash to sentry or local file when sentry fails. // HandlePanic reports the crash to sentry or local file when sentry fails.
func HandlePanic(cfg *Config, output string) { func HandlePanic(cfg *Config, output string) {
if !cfg.IsDevMode() { if !cfg.IsDevMode() {

View File

@ -17,18 +17,27 @@
// +build !pmapi_prod // +build !pmapi_prod
// Package pmapifactory creates pmapi client instances. package config
package pmapifactory
import ( import (
"net/http"
"strings"
"github.com/ProtonMail/proton-bridge/pkg/listener" "github.com/ProtonMail/proton-bridge/pkg/listener"
"github.com/ProtonMail/proton-bridge/pkg/pmapi" "github.com/ProtonMail/proton-bridge/pkg/pmapi"
) )
func GetClientConfig(clientConfig *pmapi.ClientConfig) *pmapi.ClientConfig { func (c *Config) GetAPIConfig() *pmapi.ClientConfig {
return clientConfig return &pmapi.ClientConfig{
AppVersion: strings.Title(c.appName) + "_" + c.version,
ClientID: c.appName,
}
} }
func SetClientRoundTripper(_ *pmapi.ClientManager, _ *pmapi.ClientConfig, _ listener.Listener) { func SetClientRoundTripper(_ *pmapi.ClientManager, _ *pmapi.ClientConfig, _ listener.Listener) {
// Use the default roundtripper; do nothing. // Use the default roundtripper; do nothing.
} }
func (c *Config) GetRoundTripper(_ *pmapi.ClientManager, _ listener.Listener) http.RoundTripper {
return http.DefaultTransport
}

View File

@ -17,35 +17,34 @@
// +build pmapi_prod // +build pmapi_prod
// Package pmapifactory creates pmapi client instances. package config
package pmapifactory
import ( import (
"net/http"
"strings"
"time" "time"
"github.com/ProtonMail/proton-bridge/internal/events" "github.com/ProtonMail/proton-bridge/internal/events"
"github.com/ProtonMail/proton-bridge/pkg/listener" "github.com/ProtonMail/proton-bridge/pkg/listener"
"github.com/ProtonMail/proton-bridge/pkg/pmapi" "github.com/ProtonMail/proton-bridge/pkg/pmapi"
"github.com/sirupsen/logrus"
) )
func GetClientConfig(clientConfig *pmapi.ClientConfig) *pmapi.ClientConfig { func (c *Config) GetAPIConfig() *pmapi.ClientConfig {
// We set additional timeouts/thresholds for the request as a whole: return &pmapi.ClientConfig{
clientConfig.Timeout = 10 * time.Minute // Overall request timeout (~25MB / 10 mins => ~40kB/s, should be reasonable). AppVersion: strings.Title(c.appName) + "_" + c.version,
clientConfig.FirstReadTimeout = 30 * time.Second // 30s to match 30s response header timeout. ClientID: c.appName,
clientConfig.MinSpeed = 1 << 13 // Enforce minimum download speed of 8kB/s. Timeout: 10 * time.Minute, // Overall request timeout (~25MB / 10 mins => ~40kB/s, should be reasonable).
FirstReadTimeout: 30 * time.Second, // 30s to match 30s response header timeout.
return clientConfig MinSpeed: 1 << 13, // Enforce minimum download speed of 8kB/s.
}
} }
func SetClientRoundTripper(cm *pmapi.ClientManager, cfg *pmapi.ClientConfig, listener listener.Listener) { func (c *Config) GetRoundTripper(cm *pmapi.ClientManager, listener listener.Listener) http.RoundTripper {
logrus.Info("Setting ClientManager to create clients with key pinning") pin := pmapi.NewDialerWithPinning(cm, c.GetAPIConfig().AppVersion)
pin := pmapi.NewDialerWithPinning(cm, cfg.AppVersion)
pin.ReportCertIssueLocal = func() { pin.ReportCertIssueLocal = func() {
listener.Emit(events.TLSCertIssue, "") listener.Emit(events.TLSCertIssue, "")
} }
cm.SetRoundTripper(pin.TransportWithPinning()) return pin.TransportWithPinning()
} }

View File

@ -22,7 +22,7 @@ import (
"fmt" "fmt"
"net/http" "net/http"
"github.com/ProtonMail/proton-bridge/pkg/config" "github.com/ProtonMail/proton-bridge/pkg/logs"
"github.com/ProtonMail/proton-bridge/pkg/pmapi" "github.com/ProtonMail/proton-bridge/pkg/pmapi"
) )
@ -30,7 +30,7 @@ import (
var ( var (
ErrNoInternetConnection = errors.New("no internet connection") ErrNoInternetConnection = errors.New("no internet connection")
ErrCanNotReachAPI = errors.New("can not reach PM API") ErrCanNotReachAPI = errors.New("can not reach PM API")
log = config.GetLogEntry("connection") //nolint[gochecknoglobals] log = logs.GetLogEntry("connection") //nolint[gochecknoglobals]
) )
// CheckInternetConnection does a check of API connection. It checks two of our endpoints in parallel. // CheckInternetConnection does a check of API connection. It checks two of our endpoints in parallel.

View File

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

View File

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

10
pkg/logs/logs.go Normal file
View File

@ -0,0 +1,10 @@
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

@ -76,9 +76,6 @@ type ClientConfig struct {
// The client ID. // The client ID.
ClientID string ClientID string
// The sentry DSN.
SentryDSN string
// Timeout specifies the timeout from request to getting response headers to our API. // Timeout specifies the timeout from request to getting response headers to our API.
// Passed to http.Client, empty means no timeout. // Passed to http.Client, empty means no timeout.
Timeout time.Duration Timeout time.Duration

View File

@ -6,7 +6,6 @@ import (
"sync" "sync"
"time" "time"
"github.com/getsentry/raven-go"
"github.com/pkg/errors" "github.com/pkg/errors"
"github.com/sirupsen/logrus" "github.com/sirupsen/logrus"
) )
@ -59,10 +58,6 @@ type tokenExpiration struct {
// NewClientManager creates a new ClientMan which manages clients configured with the given client config. // NewClientManager creates a new ClientMan which manages clients configured with the given client config.
func NewClientManager(config *ClientConfig) (cm *ClientManager) { func NewClientManager(config *ClientConfig) (cm *ClientManager) {
if err := raven.SetDSN(config.SentryDSN); err != nil {
logrus.WithError(err).Error("Could not set up sentry DSN")
}
cm = &ClientManager{ cm = &ClientManager{
config: config, config: config,
roundTripper: http.DefaultTransport, roundTripper: http.DefaultTransport,

View File

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

View File

@ -50,7 +50,6 @@ func (c *fakeConfig) GetAPIConfig() *pmapi.ClientConfig {
return &pmapi.ClientConfig{ return &pmapi.ClientConfig{
AppVersion: "Bridge_" + os.Getenv("VERSION"), AppVersion: "Bridge_" + os.Getenv("VERSION"),
ClientID: "bridge", ClientID: "bridge",
SentryDSN: "",
} }
} }
func (c *fakeConfig) GetDBDir() string { func (c *fakeConfig) GetDBDir() string {