2021-01-04 10:55:15 +00:00
|
|
|
// Copyright (c) 2021 Proton Technologies AG
|
2020-04-08 10:59:16 +00:00
|
|
|
//
|
|
|
|
// This file is part of ProtonMail Bridge.
|
|
|
|
//
|
|
|
|
// ProtonMail 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.
|
|
|
|
//
|
|
|
|
// ProtonMail 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 ProtonMail Bridge. If not, see <https://www.gnu.org/licenses/>.
|
|
|
|
|
2020-05-21 12:37:15 +00:00
|
|
|
// Package bridge provides core functionality of Bridge app.
|
2020-04-08 10:59:16 +00:00
|
|
|
package bridge
|
|
|
|
|
|
|
|
import (
|
2021-01-07 13:53:50 +00:00
|
|
|
"fmt"
|
2020-05-25 13:34:18 +00:00
|
|
|
"strconv"
|
|
|
|
"time"
|
|
|
|
|
2020-11-23 10:56:57 +00:00
|
|
|
"github.com/ProtonMail/proton-bridge/internal/config/settings"
|
|
|
|
"github.com/ProtonMail/proton-bridge/internal/constants"
|
2020-05-25 13:34:18 +00:00
|
|
|
"github.com/ProtonMail/proton-bridge/internal/metrics"
|
2020-05-21 12:37:15 +00:00
|
|
|
"github.com/ProtonMail/proton-bridge/internal/users"
|
2020-06-23 13:35:54 +00:00
|
|
|
"github.com/ProtonMail/proton-bridge/pkg/pmapi"
|
2020-04-08 10:59:16 +00:00
|
|
|
|
|
|
|
"github.com/ProtonMail/proton-bridge/pkg/listener"
|
|
|
|
logrus "github.com/sirupsen/logrus"
|
|
|
|
)
|
|
|
|
|
|
|
|
var (
|
2020-05-21 12:37:15 +00:00
|
|
|
log = logrus.WithField("pkg", "bridge") //nolint[gochecknoglobals]
|
2020-04-08 10:59:16 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
type Bridge struct {
|
2020-05-21 12:37:15 +00:00
|
|
|
*users.Users
|
2020-04-08 10:59:16 +00:00
|
|
|
|
2020-11-23 10:56:57 +00:00
|
|
|
settings SettingsProvider
|
2020-05-21 12:37:15 +00:00
|
|
|
clientManager users.ClientManager
|
2020-04-20 10:26:11 +00:00
|
|
|
|
2020-04-08 10:59:16 +00:00
|
|
|
userAgentClientName string
|
|
|
|
userAgentClientVersion string
|
|
|
|
userAgentOS string
|
|
|
|
}
|
|
|
|
|
|
|
|
func New(
|
2020-11-23 10:56:57 +00:00
|
|
|
locations Locator,
|
|
|
|
cache Cacher,
|
|
|
|
s SettingsProvider,
|
2020-05-21 12:37:15 +00:00
|
|
|
panicHandler users.PanicHandler,
|
2020-04-08 10:59:16 +00:00
|
|
|
eventListener listener.Listener,
|
2020-05-21 12:37:15 +00:00
|
|
|
clientManager users.ClientManager,
|
|
|
|
credStorer users.CredentialsStorer,
|
2020-04-08 10:59:16 +00:00
|
|
|
) *Bridge {
|
2020-06-17 08:52:53 +00:00
|
|
|
// Allow DoH before starting the app if the user has previously set this setting.
|
|
|
|
// This allows us to start even if protonmail is blocked.
|
2020-11-23 10:56:57 +00:00
|
|
|
if s.GetBool(settings.AllowProxyKey) {
|
2020-06-17 08:52:53 +00:00
|
|
|
clientManager.AllowProxy()
|
|
|
|
}
|
|
|
|
|
2020-11-23 10:56:57 +00:00
|
|
|
storeFactory := newStoreFactory(cache, panicHandler, clientManager, eventListener)
|
|
|
|
u := users.New(locations, panicHandler, eventListener, clientManager, credStorer, storeFactory, true)
|
2020-05-25 13:34:18 +00:00
|
|
|
b := &Bridge{
|
2020-05-21 12:37:15 +00:00
|
|
|
Users: u,
|
2020-04-08 10:59:16 +00:00
|
|
|
|
2020-11-23 10:56:57 +00:00
|
|
|
settings: s,
|
2020-04-01 13:16:36 +00:00
|
|
|
clientManager: clientManager,
|
2020-04-08 10:59:16 +00:00
|
|
|
}
|
2020-05-25 13:34:18 +00:00
|
|
|
|
2020-11-23 10:56:57 +00:00
|
|
|
if s.GetBool(settings.FirstStartKey) {
|
2021-01-07 13:53:50 +00:00
|
|
|
if err := b.SendMetric(metrics.New(metrics.Setup, metrics.FirstStart, metrics.Label(constants.Version))); err != nil {
|
|
|
|
logrus.WithError(err).Error("Failed to send metric")
|
|
|
|
}
|
|
|
|
|
2020-11-23 10:56:57 +00:00
|
|
|
s.SetBool(settings.FirstStartKey, false)
|
2020-05-25 13:34:18 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
go b.heartbeat()
|
|
|
|
|
|
|
|
return b
|
|
|
|
}
|
|
|
|
|
|
|
|
// heartbeat sends a heartbeat signal once a day.
|
|
|
|
func (b *Bridge) heartbeat() {
|
2021-01-07 13:53:50 +00:00
|
|
|
for range time.Tick(time.Minute) {
|
|
|
|
lastHeartbeatDay, err := strconv.ParseInt(b.settings.Get(settings.LastHeartbeatKey), 10, 64)
|
2020-05-25 13:34:18 +00:00
|
|
|
if err != nil {
|
|
|
|
continue
|
|
|
|
}
|
2021-01-07 13:53:50 +00:00
|
|
|
|
|
|
|
// If we're still on the same day, don't send a heartbeat.
|
|
|
|
if time.Now().YearDay() == int(lastHeartbeatDay) {
|
|
|
|
continue
|
2020-05-25 13:34:18 +00:00
|
|
|
}
|
2021-01-07 13:53:50 +00:00
|
|
|
|
|
|
|
// We're on the next (or a different) day, so send a heartbeat.
|
|
|
|
if err := b.SendMetric(metrics.New(metrics.Heartbeat, metrics.Daily, metrics.NoLabel)); err != nil {
|
|
|
|
logrus.WithError(err).Error("Failed to send heartbeat")
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
// Heartbeat was sent successfully so update the last heartbeat day.
|
|
|
|
b.settings.Set(settings.LastHeartbeatKey, fmt.Sprintf("%v", time.Now().YearDay()))
|
2020-05-25 13:34:18 +00:00
|
|
|
}
|
2020-04-08 10:59:16 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// GetCurrentClient returns currently connected client (e.g. Thunderbird).
|
|
|
|
func (b *Bridge) GetCurrentClient() string {
|
|
|
|
res := b.userAgentClientName
|
|
|
|
if b.userAgentClientVersion != "" {
|
|
|
|
res = res + " " + b.userAgentClientVersion
|
|
|
|
}
|
|
|
|
return res
|
|
|
|
}
|
|
|
|
|
|
|
|
// SetCurrentClient updates client info (e.g. Thunderbird) and sets the user agent
|
|
|
|
// on pmapi. By default no client is used, IMAP has to detect it on first login.
|
|
|
|
func (b *Bridge) SetCurrentClient(clientName, clientVersion string) {
|
|
|
|
b.userAgentClientName = clientName
|
|
|
|
b.userAgentClientVersion = clientVersion
|
2020-04-27 12:28:40 +00:00
|
|
|
b.updateUserAgent()
|
2020-04-08 10:59:16 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// SetCurrentOS updates OS and sets the user agent on pmapi. By default we use
|
|
|
|
// `runtime.GOOS`, but this can be overridden in case of better detection.
|
|
|
|
func (b *Bridge) SetCurrentOS(os string) {
|
|
|
|
b.userAgentOS = os
|
2020-04-27 12:28:40 +00:00
|
|
|
b.updateUserAgent()
|
|
|
|
}
|
|
|
|
|
|
|
|
func (b *Bridge) updateUserAgent() {
|
2021-01-08 11:13:29 +00:00
|
|
|
logrus.
|
|
|
|
WithField("clientName", b.userAgentClientName).
|
|
|
|
WithField("clientVersion", b.userAgentClientVersion).
|
|
|
|
WithField("OS", b.userAgentOS).
|
|
|
|
Info("Updating user agent")
|
|
|
|
|
2020-04-27 12:28:40 +00:00
|
|
|
b.clientManager.SetUserAgent(b.userAgentClientName, b.userAgentClientVersion, b.userAgentOS)
|
2020-04-08 10:59:16 +00:00
|
|
|
}
|
|
|
|
|
2020-05-21 12:37:15 +00:00
|
|
|
// ReportBug reports a new bug from the user.
|
|
|
|
func (b *Bridge) ReportBug(osType, osVersion, description, accountName, address, emailClient string) error {
|
|
|
|
c := b.clientManager.GetAnonymousClient()
|
|
|
|
defer c.Logout()
|
2020-04-20 10:26:11 +00:00
|
|
|
|
2020-05-21 12:37:15 +00:00
|
|
|
title := "[Bridge] Bug"
|
2020-06-23 13:35:54 +00:00
|
|
|
report := pmapi.ReportReq{
|
|
|
|
OS: osType,
|
|
|
|
OSVersion: osVersion,
|
|
|
|
Browser: emailClient,
|
|
|
|
Title: title,
|
|
|
|
Description: description,
|
|
|
|
Username: accountName,
|
|
|
|
Email: address,
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := c.Report(report); err != nil {
|
2020-05-21 12:37:15 +00:00
|
|
|
log.Error("Reporting bug failed: ", err)
|
|
|
|
return err
|
2020-04-08 10:59:16 +00:00
|
|
|
}
|
|
|
|
|
2020-05-21 12:37:15 +00:00
|
|
|
log.Info("Bug successfully reported")
|
2020-04-08 10:59:16 +00:00
|
|
|
|
2020-05-21 12:37:15 +00:00
|
|
|
return nil
|
2020-04-08 10:59:16 +00:00
|
|
|
}
|