2020-04-08 10:59:16 +00:00
|
|
|
package bridge
|
|
|
|
|
|
|
|
import (
|
2022-08-26 15:00:21 +00:00
|
|
|
"context"
|
|
|
|
"crypto/tls"
|
2021-01-07 13:53:50 +00:00
|
|
|
"fmt"
|
2022-08-26 15:00:21 +00:00
|
|
|
"net"
|
|
|
|
"net/http"
|
|
|
|
"sync"
|
2020-05-25 13:34:18 +00:00
|
|
|
|
2021-11-03 16:55:25 +00:00
|
|
|
"github.com/Masterminds/semver/v3"
|
2022-08-26 15:00:21 +00:00
|
|
|
"github.com/ProtonMail/gluon"
|
|
|
|
"github.com/ProtonMail/gluon/watcher"
|
2022-05-31 13:54:04 +00:00
|
|
|
"github.com/ProtonMail/proton-bridge/v2/internal/constants"
|
2022-08-26 15:00:21 +00:00
|
|
|
"github.com/ProtonMail/proton-bridge/v2/internal/cookies"
|
|
|
|
"github.com/ProtonMail/proton-bridge/v2/internal/events"
|
|
|
|
"github.com/ProtonMail/proton-bridge/v2/internal/focus"
|
|
|
|
"github.com/ProtonMail/proton-bridge/v2/internal/user"
|
|
|
|
"github.com/ProtonMail/proton-bridge/v2/internal/vault"
|
|
|
|
"github.com/bradenaw/juniper/xslices"
|
|
|
|
"github.com/emersion/go-smtp"
|
|
|
|
"github.com/go-resty/resty/v2"
|
|
|
|
"github.com/sirupsen/logrus"
|
|
|
|
"gitlab.protontech.ch/go/liteapi"
|
2020-04-08 10:59:16 +00:00
|
|
|
)
|
|
|
|
|
2022-08-26 15:00:21 +00:00
|
|
|
type Bridge struct {
|
|
|
|
// vault holds bridge-specific data, such as preferences and known users (authorized or not).
|
|
|
|
vault *vault.Vault
|
2020-04-08 10:59:16 +00:00
|
|
|
|
2022-08-26 15:00:21 +00:00
|
|
|
// users holds authorized users.
|
|
|
|
users map[string]*user.User
|
2021-11-17 12:19:37 +00:00
|
|
|
|
2022-08-26 15:00:21 +00:00
|
|
|
// api manages user API clients.
|
|
|
|
api *liteapi.Manager
|
|
|
|
cookieJar *cookies.Jar
|
|
|
|
proxyDialer ProxyDialer
|
|
|
|
identifier Identifier
|
2020-04-08 10:59:16 +00:00
|
|
|
|
2022-08-26 15:00:21 +00:00
|
|
|
// watchers holds all registered event watchers.
|
|
|
|
watchers []*watcher.Watcher[events.Event]
|
|
|
|
watchersLock sync.RWMutex
|
2021-12-01 12:23:01 +00:00
|
|
|
|
2022-08-26 15:00:21 +00:00
|
|
|
// tlsConfig holds the bridge TLS config used by the IMAP and SMTP servers.
|
|
|
|
tlsConfig *tls.Config
|
2020-04-08 10:59:16 +00:00
|
|
|
|
2022-08-26 15:00:21 +00:00
|
|
|
// imapServer is the bridge's IMAP server.
|
|
|
|
imapServer *gluon.Server
|
|
|
|
imapListener net.Listener
|
2020-06-17 08:52:53 +00:00
|
|
|
|
2022-08-26 15:00:21 +00:00
|
|
|
// smtpServer is the bridge's SMTP server.
|
|
|
|
smtpServer *smtp.Server
|
|
|
|
smtpBackend *smtpBackend
|
|
|
|
smtpListener net.Listener
|
2021-05-28 07:56:52 +00:00
|
|
|
|
2022-08-26 15:00:21 +00:00
|
|
|
// updater is the bridge's updater.
|
|
|
|
updater Updater
|
|
|
|
curVersion *semver.Version
|
|
|
|
updateCheckCh chan struct{}
|
2020-05-25 13:34:18 +00:00
|
|
|
|
2022-08-26 15:00:21 +00:00
|
|
|
// focusService is used to raise the bridge window when needed.
|
|
|
|
focusService *focus.FocusService
|
2020-05-25 13:34:18 +00:00
|
|
|
|
2022-08-26 15:00:21 +00:00
|
|
|
// autostarter is the bridge's autostarter.
|
|
|
|
autostarter Autostarter
|
2021-12-01 12:23:01 +00:00
|
|
|
|
2022-08-26 15:00:21 +00:00
|
|
|
// locator is the bridge's locator.
|
|
|
|
locator Locator
|
2020-05-25 13:34:18 +00:00
|
|
|
|
2022-08-26 15:00:21 +00:00
|
|
|
// errors contains errors encountered during startup.
|
|
|
|
errors []error
|
2020-05-25 13:34:18 +00:00
|
|
|
}
|
|
|
|
|
2022-08-26 15:00:21 +00:00
|
|
|
// New creates a new bridge.
|
|
|
|
func New(
|
|
|
|
apiURL string, // the URL of the API to use
|
|
|
|
locator Locator, // the locator to provide paths to store data
|
|
|
|
vault *vault.Vault, // the bridge's encrypted data store
|
|
|
|
identifier Identifier, // the identifier to keep track of the user agent
|
|
|
|
tlsReporter TLSReporter, // the TLS reporter to report TLS errors
|
|
|
|
proxyDialer ProxyDialer, // the DoH dialer
|
|
|
|
autostarter Autostarter, // the autostarter to manage autostart settings
|
|
|
|
updater Updater, // the updater to fetch and install updates
|
|
|
|
curVersion *semver.Version, // the current version of the bridge
|
|
|
|
) (*Bridge, error) {
|
|
|
|
if vault.GetProxyAllowed() {
|
|
|
|
proxyDialer.AllowProxy()
|
|
|
|
} else {
|
|
|
|
proxyDialer.DisallowProxy()
|
|
|
|
}
|
2021-01-07 13:53:50 +00:00
|
|
|
|
2022-08-26 15:00:21 +00:00
|
|
|
cookieJar, err := cookies.NewCookieJar(vault)
|
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("failed to create cookie jar: %w", err)
|
|
|
|
}
|
2021-01-07 13:53:50 +00:00
|
|
|
|
2022-08-26 15:00:21 +00:00
|
|
|
api := liteapi.New(
|
|
|
|
liteapi.WithHostURL(apiURL),
|
|
|
|
liteapi.WithAppVersion(constants.AppVersion),
|
|
|
|
liteapi.WithCookieJar(cookieJar),
|
|
|
|
liteapi.WithTransport(&http.Transport{DialTLSContext: proxyDialer.DialTLSContext}),
|
|
|
|
)
|
2021-01-07 13:53:50 +00:00
|
|
|
|
2022-08-26 15:00:21 +00:00
|
|
|
tlsConfig, err := loadTLSConfig(vault)
|
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("failed to load TLS config: %w", err)
|
2020-05-25 13:34:18 +00:00
|
|
|
}
|
2020-04-08 10:59:16 +00:00
|
|
|
|
2022-08-26 15:00:21 +00:00
|
|
|
imapServer, err := newIMAPServer(vault.GetGluonDir(), curVersion, tlsConfig)
|
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("failed to create IMAP server: %w", err)
|
|
|
|
}
|
2021-01-27 08:16:04 +00:00
|
|
|
|
2022-08-26 15:00:21 +00:00
|
|
|
smtpBackend, err := newSMTPBackend()
|
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("failed to create SMTP backend: %w", err)
|
|
|
|
}
|
2021-01-27 08:16:04 +00:00
|
|
|
|
2022-08-26 15:00:21 +00:00
|
|
|
smtpServer, err := newSMTPServer(smtpBackend, tlsConfig)
|
2021-01-27 08:16:04 +00:00
|
|
|
if err != nil {
|
2022-08-26 15:00:21 +00:00
|
|
|
return nil, fmt.Errorf("failed to create SMTP server: %w", err)
|
2021-02-23 07:50:18 +00:00
|
|
|
}
|
2021-06-21 08:48:09 +00:00
|
|
|
|
2022-08-26 15:00:21 +00:00
|
|
|
focusService, err := focus.NewService()
|
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("failed to create focus service: %w", err)
|
2021-01-27 08:16:04 +00:00
|
|
|
}
|
|
|
|
|
2022-08-26 15:00:21 +00:00
|
|
|
bridge := &Bridge{
|
|
|
|
vault: vault,
|
|
|
|
users: make(map[string]*user.User),
|
|
|
|
|
|
|
|
api: api,
|
|
|
|
cookieJar: cookieJar,
|
|
|
|
proxyDialer: proxyDialer,
|
|
|
|
identifier: identifier,
|
|
|
|
|
|
|
|
tlsConfig: tlsConfig,
|
|
|
|
imapServer: imapServer,
|
|
|
|
smtpServer: smtpServer,
|
|
|
|
smtpBackend: smtpBackend,
|
|
|
|
|
|
|
|
updater: updater,
|
|
|
|
curVersion: curVersion,
|
|
|
|
updateCheckCh: make(chan struct{}, 1),
|
|
|
|
|
|
|
|
focusService: focusService,
|
|
|
|
autostarter: autostarter,
|
|
|
|
locator: locator,
|
2021-01-27 08:16:04 +00:00
|
|
|
}
|
|
|
|
|
2022-08-26 15:00:21 +00:00
|
|
|
api.AddStatusObserver(func(status liteapi.Status) {
|
|
|
|
bridge.publish(events.ConnStatus{
|
|
|
|
Status: status,
|
|
|
|
})
|
|
|
|
})
|
2021-02-09 23:49:39 +00:00
|
|
|
|
2022-08-26 15:00:21 +00:00
|
|
|
api.AddErrorHandler(liteapi.AppVersionBadCode, func() {
|
|
|
|
bridge.publish(events.UpdateForced{})
|
|
|
|
})
|
2021-06-21 08:48:09 +00:00
|
|
|
|
2022-08-26 15:00:21 +00:00
|
|
|
api.AddPreRequestHook(func(_ *resty.Client, req *resty.Request) error {
|
|
|
|
req.SetHeader("User-Agent", bridge.identifier.GetUserAgent())
|
|
|
|
return nil
|
|
|
|
})
|
2021-11-03 16:55:25 +00:00
|
|
|
|
2022-08-26 15:00:21 +00:00
|
|
|
go func() {
|
|
|
|
for range tlsReporter.GetTLSIssueCh() {
|
|
|
|
bridge.publish(events.TLSIssue{})
|
2021-11-03 16:55:25 +00:00
|
|
|
}
|
2022-08-26 15:00:21 +00:00
|
|
|
}()
|
|
|
|
|
|
|
|
go func() {
|
|
|
|
for range focusService.GetRaiseCh() {
|
|
|
|
bridge.publish(events.Raise{})
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
|
|
|
|
go func() {
|
|
|
|
for event := range imapServer.AddWatcher() {
|
|
|
|
bridge.handleIMAPEvent(event)
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
|
|
|
|
if err := bridge.loadUsers(context.Background()); err != nil {
|
|
|
|
return nil, fmt.Errorf("failed to load connected users: %w", err)
|
2021-06-21 08:48:09 +00:00
|
|
|
}
|
|
|
|
|
2022-08-26 15:00:21 +00:00
|
|
|
if err := bridge.serveIMAP(); err != nil {
|
|
|
|
bridge.PushError(ErrServeIMAP)
|
2021-06-21 08:48:09 +00:00
|
|
|
}
|
2021-11-03 16:55:25 +00:00
|
|
|
|
2022-08-26 15:00:21 +00:00
|
|
|
if err := bridge.serveSMTP(); err != nil {
|
|
|
|
bridge.PushError(ErrServeSMTP)
|
2021-11-03 16:55:25 +00:00
|
|
|
}
|
2021-06-21 08:48:09 +00:00
|
|
|
|
2022-08-26 15:00:21 +00:00
|
|
|
if err := bridge.watchForUpdates(); err != nil {
|
|
|
|
bridge.PushError(ErrWatchUpdates)
|
|
|
|
}
|
|
|
|
|
|
|
|
return bridge, nil
|
2021-02-09 23:49:39 +00:00
|
|
|
}
|
|
|
|
|
2022-08-26 15:00:21 +00:00
|
|
|
// GetEvents returns a channel of events of the given type.
|
|
|
|
// If no types are supplied, all events are returned.
|
|
|
|
func (bridge *Bridge) GetEvents(ofType ...events.Event) (<-chan events.Event, func()) {
|
|
|
|
newWatcher := bridge.addWatcher(ofType...)
|
|
|
|
|
|
|
|
return newWatcher.GetChannel(), func() { bridge.remWatcher(newWatcher) }
|
2021-02-09 23:49:39 +00:00
|
|
|
}
|
2021-09-16 18:22:16 +00:00
|
|
|
|
2022-08-26 15:00:21 +00:00
|
|
|
func (bridge *Bridge) FactoryReset(ctx context.Context) error {
|
|
|
|
panic("TODO")
|
|
|
|
}
|
2021-09-16 18:22:16 +00:00
|
|
|
|
2022-08-26 15:00:21 +00:00
|
|
|
func (bridge *Bridge) PushError(err error) {
|
|
|
|
bridge.errors = append(bridge.errors, err)
|
|
|
|
}
|
2021-09-16 18:22:16 +00:00
|
|
|
|
2022-08-26 15:00:21 +00:00
|
|
|
func (bridge *Bridge) GetErrors() []error {
|
|
|
|
return bridge.errors
|
2021-09-16 18:22:16 +00:00
|
|
|
}
|
|
|
|
|
2022-08-26 15:00:21 +00:00
|
|
|
func (bridge *Bridge) Close(ctx context.Context) error {
|
|
|
|
// Close the IMAP server.
|
|
|
|
if err := bridge.closeIMAP(ctx); err != nil {
|
|
|
|
logrus.WithError(err).Error("Failed to close IMAP server")
|
2021-09-16 18:22:16 +00:00
|
|
|
}
|
|
|
|
|
2022-08-26 15:00:21 +00:00
|
|
|
// Close the SMTP server.
|
|
|
|
if err := bridge.closeSMTP(); err != nil {
|
|
|
|
logrus.WithError(err).Error("Failed to close SMTP server")
|
|
|
|
}
|
2021-09-16 18:22:16 +00:00
|
|
|
|
2022-08-26 15:00:21 +00:00
|
|
|
// Close all users.
|
|
|
|
for _, user := range bridge.users {
|
|
|
|
if err := user.Close(ctx); err != nil {
|
|
|
|
logrus.WithError(err).Error("Failed to close user")
|
|
|
|
}
|
|
|
|
}
|
2021-09-16 18:22:16 +00:00
|
|
|
|
2022-08-26 15:00:21 +00:00
|
|
|
// Persist the cookies.
|
|
|
|
if err := bridge.cookieJar.PersistCookies(); err != nil {
|
|
|
|
logrus.WithError(err).Error("Failed to persist cookies")
|
2021-09-16 18:22:16 +00:00
|
|
|
}
|
|
|
|
|
2022-08-26 15:00:21 +00:00
|
|
|
// Close the focus service.
|
|
|
|
bridge.focusService.Close()
|
|
|
|
|
|
|
|
// Save the last version of bridge that was run.
|
|
|
|
if err := bridge.vault.SetLastVersion(bridge.curVersion); err != nil {
|
|
|
|
logrus.WithError(err).Error("Failed to save last version")
|
|
|
|
}
|
2021-09-16 18:22:16 +00:00
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
2021-10-27 14:14:18 +00:00
|
|
|
|
2022-08-26 15:00:21 +00:00
|
|
|
func (bridge *Bridge) publish(event events.Event) {
|
|
|
|
bridge.watchersLock.RLock()
|
|
|
|
defer bridge.watchersLock.RUnlock()
|
|
|
|
|
|
|
|
for _, watcher := range bridge.watchers {
|
|
|
|
if watcher.IsWatching(event) {
|
|
|
|
if ok := watcher.Send(event); !ok {
|
|
|
|
logrus.WithField("event", event).Warn("Failed to send event to watcher")
|
|
|
|
}
|
|
|
|
}
|
2021-10-27 14:14:18 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-08-26 15:00:21 +00:00
|
|
|
func (bridge *Bridge) addWatcher(ofType ...events.Event) *watcher.Watcher[events.Event] {
|
|
|
|
bridge.watchersLock.Lock()
|
|
|
|
defer bridge.watchersLock.Unlock()
|
2021-11-17 12:19:37 +00:00
|
|
|
|
2022-08-26 15:00:21 +00:00
|
|
|
newWatcher := watcher.New(ofType...)
|
2021-11-17 12:19:37 +00:00
|
|
|
|
2022-08-26 15:00:21 +00:00
|
|
|
bridge.watchers = append(bridge.watchers, newWatcher)
|
|
|
|
|
|
|
|
return newWatcher
|
2021-11-17 12:19:37 +00:00
|
|
|
}
|
|
|
|
|
2022-08-26 15:00:21 +00:00
|
|
|
func (bridge *Bridge) remWatcher(oldWatcher *watcher.Watcher[events.Event]) {
|
|
|
|
bridge.watchersLock.Lock()
|
|
|
|
defer bridge.watchersLock.Unlock()
|
|
|
|
|
|
|
|
bridge.watchers = xslices.Filter(bridge.watchers, func(other *watcher.Watcher[events.Event]) bool {
|
|
|
|
return other != oldWatcher
|
|
|
|
})
|
2021-11-17 12:19:37 +00:00
|
|
|
}
|
|
|
|
|
2022-08-26 15:00:21 +00:00
|
|
|
func loadTLSConfig(vault *vault.Vault) (*tls.Config, error) {
|
|
|
|
cert, err := tls.X509KeyPair(vault.GetBridgeTLSCert(), vault.GetBridgeTLSKey())
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
2021-11-17 12:19:37 +00:00
|
|
|
}
|
|
|
|
|
2022-08-26 15:00:21 +00:00
|
|
|
return &tls.Config{
|
|
|
|
Certificates: []tls.Certificate{cert},
|
|
|
|
}, nil
|
2021-11-17 12:19:37 +00:00
|
|
|
}
|
2021-12-01 12:23:01 +00:00
|
|
|
|
2022-08-26 15:00:21 +00:00
|
|
|
func newListener(port int, useTLS bool, tlsConfig *tls.Config) (net.Listener, error) {
|
|
|
|
if useTLS {
|
|
|
|
tlsListener, err := tls.Listen("tcp", fmt.Sprintf(":%v", port), tlsConfig)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2021-12-09 10:50:42 +00:00
|
|
|
|
2022-08-26 15:00:21 +00:00
|
|
|
return tlsListener, nil
|
|
|
|
}
|
2022-07-28 08:52:40 +00:00
|
|
|
|
2022-08-26 15:00:21 +00:00
|
|
|
netListener, err := net.Listen("tcp", fmt.Sprintf(":%v", port))
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2022-07-28 08:52:40 +00:00
|
|
|
|
2022-08-26 15:00:21 +00:00
|
|
|
return netListener, nil
|
2022-07-28 08:52:40 +00:00
|
|
|
}
|