From 8d028966c72b36aba390354422e6118cd2bbe5b6 Mon Sep 17 00:00:00 2001 From: Leander Beernaert Date: Tue, 18 Jul 2023 15:14:30 +0200 Subject: [PATCH] chore(GODT-2799): Move Identifier interface to separate module Required so we can move the smtp backend into the smtp service module. --- internal/bridge/bridge.go | 7 ++++--- internal/bridge/identifier.go | 24 +++++++++++++++++++++++ internal/bridge/smtp.go | 2 +- internal/bridge/smtp_backend.go | 5 +++-- internal/bridge/types.go | 14 -------------- internal/identifier/identifier.go | 32 +++++++++++++++++++++++++++++++ 6 files changed, 64 insertions(+), 20 deletions(-) create mode 100644 internal/identifier/identifier.go diff --git a/internal/bridge/bridge.go b/internal/bridge/bridge.go index 79453989..e1f322bc 100644 --- a/internal/bridge/bridge.go +++ b/internal/bridge/bridge.go @@ -38,6 +38,7 @@ import ( "github.com/ProtonMail/proton-bridge/v3/internal/constants" "github.com/ProtonMail/proton-bridge/v3/internal/events" "github.com/ProtonMail/proton-bridge/v3/internal/focus" + "github.com/ProtonMail/proton-bridge/v3/internal/identifier" "github.com/ProtonMail/proton-bridge/v3/internal/safe" "github.com/ProtonMail/proton-bridge/v3/internal/sentry" "github.com/ProtonMail/proton-bridge/v3/internal/telemetry" @@ -59,7 +60,7 @@ type Bridge struct { // api manages user API clients. api *proton.Manager proxyCtl ProxyController - identifier Identifier + identifier identifier.Identifier // tlsConfig holds the bridge TLS config used by the IMAP and SMTP servers. tlsConfig *tls.Config @@ -140,7 +141,7 @@ func New( apiURL string, // the URL of the API to use cookieJar http.CookieJar, // the cookie jar to use - identifier Identifier, // the identifier to keep track of the user agent + identifier identifier.Identifier, // the identifier to keep track of the user agent tlsReporter TLSReporter, // the TLS reporter to report TLS errors roundTripper http.RoundTripper, // the round tripper to use for API requests proxyCtl ProxyController, // the DoH controller @@ -207,7 +208,7 @@ func newBridge( reporter reporter.Reporter, api *proton.Manager, - identifier Identifier, + identifier identifier.Identifier, proxyCtl ProxyController, uidValidityGenerator imap.UIDValidityGenerator, diff --git a/internal/bridge/identifier.go b/internal/bridge/identifier.go index 97278971..f325cd18 100644 --- a/internal/bridge/identifier.go +++ b/internal/bridge/identifier.go @@ -45,6 +45,30 @@ type bridgeUserAgentUpdater struct { *Bridge } +func (b *bridgeUserAgentUpdater) GetUserAgent() string { + return b.identifier.GetUserAgent() +} + +func (b *bridgeUserAgentUpdater) HasClient() bool { + return b.identifier.HasClient() +} + +func (b *bridgeUserAgentUpdater) SetClient(name, version string) { + b.identifier.SetClient(name, version) +} + +func (b *bridgeUserAgentUpdater) SetPlatform(platform string) { + b.identifier.SetPlatform(platform) +} + +func (b *bridgeUserAgentUpdater) SetClientString(client string) { + b.identifier.SetClientString(client) +} + +func (b *bridgeUserAgentUpdater) GetClientString() string { + return b.identifier.GetClientString() +} + func (b *bridgeUserAgentUpdater) SetUserAgent(name, version string) { b.setUserAgent(name, version) } diff --git a/internal/bridge/smtp.go b/internal/bridge/smtp.go index 648a2cf1..8645eb28 100644 --- a/internal/bridge/smtp.go +++ b/internal/bridge/smtp.go @@ -36,7 +36,7 @@ func (bridge *Bridge) restartSMTP(ctx context.Context) error { func newSMTPServer(bridge *Bridge, accounts *bridgesmtp.Accounts, tlsConfig *tls.Config, logSMTP bool) *smtp.Server { logrus.WithField("logSMTP", logSMTP).Info("Creating SMTP server") - smtpServer := smtp.NewServer(&smtpBackend{bridge: bridge, accounts: accounts}) + smtpServer := smtp.NewServer(&smtpBackend{userAgent: &bridgeUserAgentUpdater{Bridge: bridge}, accounts: accounts}) smtpServer.TLSConfig = tlsConfig smtpServer.Domain = constants.Host diff --git a/internal/bridge/smtp_backend.go b/internal/bridge/smtp_backend.go index e3b2f25f..222b381e 100644 --- a/internal/bridge/smtp_backend.go +++ b/internal/bridge/smtp_backend.go @@ -24,6 +24,7 @@ import ( "io" "strings" + "github.com/ProtonMail/proton-bridge/v3/internal/identifier" smtpservice "github.com/ProtonMail/proton-bridge/v3/internal/services/smtp" "github.com/ProtonMail/proton-bridge/v3/internal/useragent" "github.com/emersion/go-smtp" @@ -32,12 +33,12 @@ import ( type smtpBackend struct { accounts *smtpservice.Accounts - userAgent UserAgentUpdater + userAgent identifier.UserAgentUpdater } type smtpSession struct { accounts *smtpservice.Accounts - userAgent UserAgentUpdater + userAgent identifier.UserAgentUpdater userID string authID string diff --git a/internal/bridge/types.go b/internal/bridge/types.go index 0b3064e5..dab3c816 100644 --- a/internal/bridge/types.go +++ b/internal/bridge/types.go @@ -34,20 +34,6 @@ type Locator interface { Clear(...string) error } -type Identifier interface { - GetUserAgent() string - HasClient() bool - SetClient(name, version string) - SetPlatform(platform string) - SetClientString(client string) - GetClientString() string -} - -type UserAgentUpdater interface { - Identifier - SetUserAgent(name, version string) -} - type ProxyController interface { AllowProxy() DisallowProxy() diff --git a/internal/identifier/identifier.go b/internal/identifier/identifier.go new file mode 100644 index 00000000..837aed58 --- /dev/null +++ b/internal/identifier/identifier.go @@ -0,0 +1,32 @@ +// 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 . + +package identifier + +type Identifier interface { + GetUserAgent() string + HasClient() bool + SetClient(name, version string) + SetPlatform(platform string) + SetClientString(client string) + GetClientString() string +} + +type UserAgentUpdater interface { + Identifier + SetUserAgent(name, version string) +}