diff --git a/Changelog.md b/Changelog.md index 838935bc..d53f1e48 100644 --- a/Changelog.md +++ b/Changelog.md @@ -3,7 +3,13 @@ Changelog [format](http://keepachangelog.com/en/1.0.0/) ## Unreleased + +### Added +* GODT-682 Persistent anonymous API cookies for Import-Export. + +### Changed * GODT-511 User agent format changed. + ### Removed * GODT-519 Unused AUTH scope parsing methods diff --git a/cmd/Import-Export/main.go b/cmd/Import-Export/main.go index cc0ae6ff..26af6fc5 100644 --- a/cmd/Import-Export/main.go +++ b/cmd/Import-Export/main.go @@ -21,9 +21,11 @@ import ( "runtime/pprof" "github.com/ProtonMail/proton-bridge/internal/cmd" + "github.com/ProtonMail/proton-bridge/internal/cookies" "github.com/ProtonMail/proton-bridge/internal/events" "github.com/ProtonMail/proton-bridge/internal/frontend" "github.com/ProtonMail/proton-bridge/internal/importexport" + "github.com/ProtonMail/proton-bridge/internal/preferences" "github.com/ProtonMail/proton-bridge/internal/updates" "github.com/ProtonMail/proton-bridge/internal/users/credentials" "github.com/ProtonMail/proton-bridge/pkg/config" @@ -36,6 +38,10 @@ import ( ) const ( + // cacheVersion is used for cache files such as lock, or preferences. + // Different number will drop old files and create new ones. + cacheVersion = "c11" + appName = "importExport" appNameDash = "import-export-app" ) @@ -58,7 +64,7 @@ func main() { // IMPORTANT: ***Read the comments before CHANGING the order *** func run(context *cli.Context) (contextError error) { // nolint[funlen] // We need to have config instance to setup a logs, panic handler, etc ... - cfg := config.New(appName, constants.Version, constants.Revision, "") + cfg := config.New(appName, constants.Version, constants.Revision, cacheVersion) // We want to know about any problem. Our PanicHandler calls sentry which is // not dependent on anything else. If that fails, it tries to create crash @@ -132,6 +138,16 @@ func run(context *cli.Context) (contextError error) { // nolint[funlen] // implementation depending on whether build flag pmapi_prod is used or not. cm.SetRoundTripper(cfg.GetRoundTripper(cm, eventListener)) + pref := preferences.New(cfg) + + // Cookies must be persisted across restarts. + jar, err := cookies.NewCookieJar(pref) + if err != nil { + logrus.WithError(err).Warn("Could not create cookie jar") + } else { + cm.SetCookieJar(jar) + } + importexportInstance := importexport.New(cfg, panicHandler, eventListener, cm, credentialsStore) // Decide about frontend mode before initializing rest of import-export. diff --git a/pkg/pmapi/client.go b/pkg/pmapi/client.go index 8fbd6f6f..3e1c9cea 100644 --- a/pkg/pmapi/client.go +++ b/pkg/pmapi/client.go @@ -266,6 +266,9 @@ func (c *client) doBuffered(req *http.Request, bodyBuffer []byte, retryUnauthori return } + // Cookies are returned only after request was sent. + c.log.Tracef("REQCOOKIES '%v'", req.Cookies()) + resDate := res.Header.Get("Date") if resDate != "" { if serverTime, err := http.ParseTime(resDate); err == nil {