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/frontend"
"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/smtp"
"github.com/ProtonMail/proton-bridge/pkg/args"
"github.com/ProtonMail/proton-bridge/pkg/config"
"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/updates"
"github.com/allan-simon/go-singleinstance"
@ -87,7 +87,7 @@ var (
longVersion = Version + " (" + Revision + ")" //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.
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)
}
clientConfig := pmapifactory.GetClientConfig(cfg.GetAPIConfig())
cm := pmapi.NewClientManager(clientConfig)
pmapifactory.SetClientRoundTripper(cm, clientConfig, eventListener)
cm := pmapi.NewClientManager(cfg.GetAPIConfig())
cm.SetRoundTripper(cfg.GetRoundTripper(cm, eventListener))
bridgeInstance := bridge.New(cfg, pref, panicHandler, eventListener, Version, cm, credentialsStore)
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/pkg/config"
"github.com/ProtonMail/proton-bridge/pkg/listener"
"github.com/ProtonMail/proton-bridge/pkg/logs"
"github.com/ProtonMail/proton-bridge/pkg/ports"
)
var (
log = config.GetLogEntry("api") //nolint[gochecknoglobals]
log = logs.GetLogEntry("api") //nolint[gochecknoglobals]
)
type apiServer struct {

View File

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

View File

@ -27,14 +27,14 @@ import (
"fmt"
"strings"
"github.com/ProtonMail/proton-bridge/pkg/config"
"github.com/ProtonMail/proton-bridge/pkg/logs"
"github.com/sirupsen/logrus"
)
const sep = "\x00"
var (
log = config.GetLogEntry("bridge") //nolint[gochecknoglobals]
log = logs.GetLogEntry("bridge") //nolint[gochecknoglobals]
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/pkg/config"
"github.com/ProtonMail/proton-bridge/pkg/listener"
"github.com/ProtonMail/proton-bridge/pkg/logs"
"github.com/abiosoft/ishell"
)
var (
log = config.GetLogEntry("frontend/cli") //nolint[gochecknoglobals]
log = logs.GetLogEntry("frontend/cli") //nolint[gochecknoglobals]
)
type frontendCLI struct {

View File

@ -26,10 +26,11 @@ import (
"github.com/ProtonMail/proton-bridge/internal/frontend/types"
"github.com/ProtonMail/proton-bridge/pkg/config"
"github.com/ProtonMail/proton-bridge/pkg/listener"
"github.com/ProtonMail/proton-bridge/pkg/logs"
)
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).

View File

@ -43,6 +43,7 @@ import (
"github.com/ProtonMail/proton-bridge/internal/frontend/types"
"github.com/ProtonMail/proton-bridge/internal/preferences"
"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/useragent"
@ -58,7 +59,7 @@ import (
"github.com/therecipe/qt/widgets"
)
var log = config.GetLogEntry("frontend-qt")
var log = logs.GetLogEntry("frontend-qt")
var accountMutex = &sync.Mutex{}
// 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/pkg/config"
"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{}

View File

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

View File

@ -21,15 +21,14 @@ import (
"io/ioutil"
"os"
"path/filepath"
"strings"
"github.com/ProtonMail/go-appdir"
"github.com/ProtonMail/proton-bridge/pkg/pmapi"
"github.com/ProtonMail/proton-bridge/pkg/logs"
"github.com/hashicorp/go-multierror"
)
var (
log = GetLogEntry("config") //nolint[gochecknoglobals]
log = logs.GetLogEntry("config") //nolint[gochecknoglobals]
)
type appDirProvider interface {
@ -45,7 +44,6 @@ type Config struct {
cacheVersion string
appDirs appDirProvider
appDirsVersion appDirProvider
apiConfig *pmapi.ClientConfig
}
// New returns fully initialized config struct.
@ -67,11 +65,6 @@ func newConfig(appName, version, revision, cacheVersion string, appDirs, appDirs
cacheVersion: cacheVersion,
appDirs: appDirs,
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")
}
// GetAPIConfig returns config for ProtonMail API.
func (c *Config) GetAPIConfig() *pmapi.ClientConfig {
return c.apiConfig
}
// GetDefaultAPIPort returns default Bridge local API port.
func (c *Config) GetDefaultAPIPort() int {
return 1042

View File

@ -54,13 +54,6 @@ var logFile *os.File //nolint[gochecknoglobals]
var logFileRgx = regexp.MustCompile("^v.*\\.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.
func HandlePanic(cfg *Config, output string) {
if !cfg.IsDevMode() {

View File

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

View File

@ -22,7 +22,7 @@ import (
"fmt"
"net/http"
"github.com/ProtonMail/proton-bridge/pkg/config"
"github.com/ProtonMail/proton-bridge/pkg/logs"
"github.com/ProtonMail/proton-bridge/pkg/pmapi"
)
@ -30,7 +30,7 @@ import (
var (
ErrNoInternetConnection = errors.New("no internet connection")
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.

View File

@ -23,7 +23,7 @@ import (
"strings"
"sync"
"github.com/ProtonMail/proton-bridge/pkg/config"
"github.com/ProtonMail/proton-bridge/pkg/logs"
"github.com/docker/docker-credential-helpers/credentials"
)
@ -32,7 +32,7 @@ const (
)
var (
log = config.GetLogEntry("bridgeUtils/keychain") //nolint[gochecknoglobals]
log = logs.GetLogEntry("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/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.
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.
ClientID string
// The sentry DSN.
SentryDSN string
// Timeout specifies the timeout from request to getting response headers to our API.
// Passed to http.Client, empty means no timeout.
Timeout time.Duration

View File

@ -6,7 +6,6 @@ import (
"sync"
"time"
"github.com/getsentry/raven-go"
"github.com/pkg/errors"
"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.
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{
config: config,
roundTripper: http.DefaultTransport,

View File

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

View File

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