chore: merge branch release/perth_narrows to devel

This commit is contained in:
Jakub
2023-02-15 13:13:08 +01:00
126 changed files with 4339 additions and 1484 deletions

File diff suppressed because it is too large Load Diff

View File

@ -448,6 +448,8 @@ message UserEvent {
UserDisconnectedEvent userDisconnected = 2;
UserChangedEvent userChanged = 3;
UserBadEvent userBadEvent = 4;
UsedBytesChangedEvent usedBytesChangedEvent = 5;
ImapLoginFailedEvent imapLoginFailedEvent = 6;
}
}
@ -468,6 +470,15 @@ message UserBadEvent {
string errorMessage = 2;
}
message UsedBytesChangedEvent {
string userID = 1;
int64 usedBytes = 2;
}
message ImapLoginFailedEvent {
string username = 1;
}
//**********************************************************
// Generic errors
//**********************************************************

View File

@ -1,66 +0,0 @@
// Copyright (c) 2023 Proton AG
//
// This file is part of Proton Mail Bridge.Bridge.
//
// Proton Mail Bridge is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// Proton Mail Bridge is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with Proton Mail Bridge. If not, see <https://www.gnu.org/licenses/>.
package grpc
import (
"encoding/json"
"os"
)
// Config is a structure containing the service configuration data that are exchanged by the gRPC server and client.
type Config struct {
Port int `json:"port"`
Cert string `json:"cert"`
Token string `json:"token"`
FileSocketPath string `json:"fileSocketPath"`
}
// save saves a gRPC service configuration to file.
func (s *Config) save(path string) error {
// Another process may be waiting for this file to be available. In order to prevent this process to open
// the file while we are writing in it, we write it with a temp file name, then rename it.
tempPath := path + "_"
if err := s._save(tempPath); err != nil {
return err
}
return os.Rename(tempPath, path)
}
func (s *Config) _save(path string) error {
f, err := os.Create(path) //nolint:errcheck,gosec
if err != nil {
return err
}
defer func() { _ = f.Close() }()
return json.NewEncoder(f).Encode(s)
}
// load loads a gRPC service configuration from file.
func (s *Config) load(path string) error {
f, err := os.Open(path) //nolint:errcheck,gosec
if err != nil {
return err
}
defer func() { _ = f.Close() }()
return json.NewDecoder(f).Decode(s)
}

View File

@ -1,57 +0,0 @@
// Copyright (c) 2023 Proton AG
//
// This file is part of Proton Mail Bridge.
//
// Proton Mail Bridge is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// Proton Mail Bridge is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with Proton Mail Bridge. If not, see <https://www.gnu.org/licenses/>.
package grpc
import (
"path/filepath"
"testing"
"github.com/stretchr/testify/require"
)
const (
dummyPort = 12
dummyCert = "A dummy cert"
dummyToken = "A dummy token"
tempFileName = "test.json"
socketPath = "/a/socket/file/path"
)
func TestConfig(t *testing.T) {
conf1 := Config{
Port: dummyPort,
Cert: dummyCert,
Token: dummyToken,
FileSocketPath: socketPath,
}
// Read-back test
tempDir := t.TempDir()
tempFilePath := filepath.Join(tempDir, tempFileName)
require.NoError(t, conf1.save(tempFilePath))
conf2 := Config{}
require.NoError(t, conf2.load(tempFilePath))
require.Equal(t, conf1, conf2)
// failure to load
require.Error(t, conf2.load(tempFilePath+"_"))
// failure to save
require.Error(t, conf2.save(filepath.Join(tempDir, "non/existing/folder", tempFileName)))
}

View File

@ -177,6 +177,14 @@ func NewUserBadEvent(userID string, errorMessage string) *StreamEvent {
return userEvent(&UserEvent{Event: &UserEvent_UserBadEvent{UserBadEvent: &UserBadEvent{UserID: userID, ErrorMessage: errorMessage}}})
}
func NewUsedBytesChangedEvent(userID string, usedBytes int) *StreamEvent {
return userEvent(&UserEvent{Event: &UserEvent_UsedBytesChangedEvent{UsedBytesChangedEvent: &UsedBytesChangedEvent{UserID: userID, UsedBytes: int64(usedBytes)}}})
}
func newIMAPLoginFailedEvent(username string) *StreamEvent {
return userEvent(&UserEvent{Event: &UserEvent_ImapLoginFailedEvent{ImapLoginFailedEvent: &ImapLoginFailedEvent{Username: username}}})
}
func NewGenericErrorEvent(errorCode ErrorCode) *StreamEvent {
return genericErrorEvent(&GenericErrorEvent{Code: errorCode})
}

View File

@ -25,6 +25,7 @@ import (
"errors"
"fmt"
"io/fs"
"math/rand"
"net"
"os"
"path/filepath"
@ -37,6 +38,7 @@ import (
"github.com/ProtonMail/proton-bridge/v3/internal/certs"
"github.com/ProtonMail/proton-bridge/v3/internal/events"
"github.com/ProtonMail/proton-bridge/v3/internal/safe"
"github.com/ProtonMail/proton-bridge/v3/internal/service"
"github.com/ProtonMail/proton-bridge/v3/internal/updater"
"github.com/bradenaw/juniper/xslices"
"github.com/elastic/go-sysinfo"
@ -93,12 +95,10 @@ type Service struct { // nolint:structcheck
}
// NewService returns a new instance of the service.
//
// nolint:funlen
func NewService(
panicHandler CrashHandler,
restarter Restarter,
locations Locator,
locations service.Locator,
bridge *bridge.Bridge,
eventCh <-chan events.Event,
quitCh <-chan struct{},
@ -110,7 +110,7 @@ func NewService(
logrus.WithError(err).Panic("Could not generate gRPC TLS config")
}
config := Config{
config := service.Config{
Cert: string(certPEM),
Token: uuid.NewString(),
}
@ -141,7 +141,7 @@ func NewService(
config.Port = address.Port
}
if path, err := saveGRPCServerConfigFile(locations, &config); err != nil {
if path, err := service.SaveGRPCServerConfigFile(locations, &config, serverConfigFileName); err != nil {
logrus.WithError(err).WithField("path", path).Panic("Could not write gRPC service config file")
} else {
logrus.WithField("path", path).Info("Successfully saved gRPC service config file")
@ -245,7 +245,7 @@ func (s *Service) WaitUntilFrontendIsReady() {
s.initializing.Wait()
}
// nolint:funlen,gocyclo
// nolint:gocyclo
func (s *Service) watchEvents() {
// GODT-1949 Better error events.
for _, err := range s.bridge.GetErrors() {
@ -255,12 +255,6 @@ func (s *Service) watchEvents() {
case errors.Is(err, bridge.ErrVaultInsecure):
_ = s.SendEvent(NewKeychainHasNoKeychainEvent())
case errors.Is(err, bridge.ErrServeIMAP):
_ = s.SendEvent(NewMailServerSettingsErrorEvent(MailServerSettingsErrorType_IMAP_PORT_STARTUP_ERROR))
case errors.Is(err, bridge.ErrServeSMTP):
_ = s.SendEvent(NewMailServerSettingsErrorEvent(MailServerSettingsErrorType_SMTP_PORT_STARTUP_ERROR))
}
}
@ -272,6 +266,12 @@ func (s *Service) watchEvents() {
case events.ConnStatusDown:
_ = s.SendEvent(NewInternetStatusEvent(false))
case events.IMAPServerError:
_ = s.SendEvent(NewMailServerSettingsErrorEvent(MailServerSettingsErrorType_IMAP_PORT_STARTUP_ERROR))
case events.SMTPServerError:
_ = s.SendEvent(NewMailServerSettingsErrorEvent(MailServerSettingsErrorType_SMTP_PORT_STARTUP_ERROR))
case events.Raise:
_ = s.SendEvent(NewShowMainWindowEvent())
@ -305,6 +305,12 @@ func (s *Service) watchEvents() {
case events.AddressModeChanged:
_ = s.SendEvent(NewUserChangedEvent(event.UserID))
case events.UsedSpaceChanged:
_ = s.SendEvent(NewUsedBytesChangedEvent(event.UserID, event.UsedSpace))
case events.IMAPLoginFailed:
_ = s.SendEvent(newIMAPLoginFailedEvent(event.Username))
case events.UserDeauth:
// This is the event the GUI cares about.
_ = s.SendEvent(NewUserChangedEvent(event.UserID))
@ -481,17 +487,6 @@ func newTLSConfig() (*tls.Config, []byte, error) {
}, certPEM, nil
}
func saveGRPCServerConfigFile(locations Locator, config *Config) (string, error) {
settingsPath, err := locations.ProvideSettingsPath()
if err != nil {
return "", err
}
configPath := filepath.Join(settingsPath, serverConfigFileName)
return configPath, config.save(configPath)
}
// validateServerToken verify that the server token provided by the client is valid.
func validateServerToken(ctx context.Context, wantToken string) error {
values, ok := metadata.FromIncomingContext(ctx)
@ -577,10 +572,17 @@ func (s *Service) monitorParentPID() {
func computeFileSocketPath() (string, error) {
tempPath := os.TempDir()
for i := 0; i < 1000; i++ {
path := filepath.Join(tempPath, fmt.Sprintf("bridge_%v.sock", uuid.NewString()))
path := filepath.Join(tempPath, fmt.Sprintf("bridge%04d", rand.Intn(10000))) // nolint:gosec
if _, err := os.Stat(path); errors.Is(err, fs.ErrNotExist) {
return path, nil
}
if err := os.Remove(path); err != nil {
logrus.WithField("path", path).WithError(err).Warning("Could not remove existing socket file")
continue
}
return path, nil
}
return "", errors.New("unable to find a suitable file socket in user config folder")

View File

@ -32,6 +32,7 @@ import (
"github.com/ProtonMail/proton-bridge/v3/internal/events"
"github.com/ProtonMail/proton-bridge/v3/internal/frontend/theme"
"github.com/ProtonMail/proton-bridge/v3/internal/safe"
"github.com/ProtonMail/proton-bridge/v3/internal/service"
"github.com/ProtonMail/proton-bridge/v3/internal/updater"
"github.com/ProtonMail/proton-bridge/v3/pkg/keychain"
"github.com/ProtonMail/proton-bridge/v3/pkg/ports"
@ -51,8 +52,8 @@ func (s *Service) CheckTokens(ctx context.Context, clientConfigPath *wrapperspb.
path := clientConfigPath.Value
logEntry := s.log.WithField("path", path)
var clientConfig Config
if err := clientConfig.load(path); err != nil {
var clientConfig service.Config
if err := clientConfig.Load(path); err != nil {
logEntry.WithError(err).Error("Could not read gRPC client config file")
return nil, err

View File

@ -110,7 +110,7 @@ func (s *Service) SendEvent(event *StreamEvent) error {
}
// StartEventTest sends all the known event via gRPC.
func (s *Service) StartEventTest() error { //nolint:funlen
func (s *Service) StartEventTest() error {
const dummyAddress = "dummy@proton.me"
events := []*StreamEvent{
// app
@ -174,6 +174,7 @@ func (s *Service) StartEventTest() error { //nolint:funlen
NewUserToggleSplitModeFinishedEvent("userID"),
NewUserDisconnectedEvent("username"),
NewUserChangedEvent("userID"),
NewUsedBytesChangedEvent("userID", 1000),
}
for _, event := range events {

View File

@ -26,7 +26,3 @@ type Restarter interface {
AddFlags(flags ...string)
Override(exe string)
}
type Locator interface {
ProvideSettingsPath() (string, error)
}