/
updates.go
342 lines (295 loc) · 10.6 KB
/
updates.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
// Copyright (c) 2020 Proton Technologies AG
//
// 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/>.
package updates
import (
"bytes"
"encoding/json"
"errors"
"io/ioutil"
"os/exec"
"path/filepath"
"runtime"
"strings"
"github.com/ProtonMail/proton-bridge/internal/bridge"
"github.com/ProtonMail/proton-bridge/internal/importexport"
"github.com/ProtonMail/proton-bridge/pkg/constants"
"github.com/kardianos/osext"
"github.com/sirupsen/logrus"
)
const (
sigExtension = ".sig"
)
var (
Host = "https://protonmail.com" //nolint[gochecknoglobals]
DownloadPath = "download" //nolint[gochecknoglobals]
// BuildType specifies type of build (e.g. QA or beta).
BuildType = "" //nolint[gochecknoglobals]
)
var (
log = logrus.WithField("pkg", "bridgeUtils/updates") //nolint[gochecknoglobals]
ErrDownloadFailed = errors.New("error happened during download") //nolint[gochecknoglobals]
ErrUpdateVerifyFailed = errors.New("cannot verify signature") //nolint[gochecknoglobals]
)
type Updates struct {
version string
revision string
buildTime string
releaseNotes string
releaseFixedBugs string
updateTempDir string
landingPagePath string // Based on Host/; default landing page for download.
winInstallerFile string // File for initial install or manual reinstall for windows
macInstallerFile string // File for initial install or manual reinstall for mac
linInstallerFile string // File for initial install or manual reinstall for linux
versionFileBaseName string // Text file containing information about current file. per goos [_linux,_darwin,_windows].json (have .sig file).
updateFileBaseName string // File for automatic update. per goos [_linux,_darwin,_windows].tgz (have .sig file).
linuxFileBaseName string // Prefix of linux package names.
macAppBundleName string // Name of Mac app file in the bundle for update procedure.
cachedNewerVersion *VersionInfo // To have info about latest version even when the internet connection drops.
}
// NewBridge inits Updates struct for bridge.
func NewBridge(updateTempDir string) *Updates {
return &Updates{
version: constants.Version,
revision: constants.Revision,
buildTime: constants.BuildTime,
releaseNotes: bridge.ReleaseNotes,
releaseFixedBugs: bridge.ReleaseFixedBugs,
updateTempDir: updateTempDir,
landingPagePath: "bridge/download",
winInstallerFile: "Bridge-Installer.exe",
macInstallerFile: "Bridge-Installer.dmg",
linInstallerFile: "Bridge-Installer.sh",
versionFileBaseName: "current_version",
updateFileBaseName: "bridge_upgrade",
linuxFileBaseName: "protonmail-bridge",
macAppBundleName: "ProtonMail Bridge.app",
}
}
// NewImportExport inits Updates struct for import-export.
func NewImportExport(updateTempDir string) *Updates {
return &Updates{
version: constants.Version,
revision: constants.Revision,
buildTime: constants.BuildTime,
releaseNotes: importexport.ReleaseNotes,
releaseFixedBugs: importexport.ReleaseFixedBugs,
updateTempDir: updateTempDir,
landingPagePath: "import-export",
winInstallerFile: "ie/Import-Export-app-installer.exe",
macInstallerFile: "ie/Import-Export-app.dmg",
linInstallerFile: "ie/Import-Export-app-installer.sh",
versionFileBaseName: "current_version_ie",
updateFileBaseName: "ie/ie_upgrade",
linuxFileBaseName: "ie/protonmail-import-export-app",
macAppBundleName: "Import-Export app.app",
}
}
func (u *Updates) CreateJSONAndSign(deployDir, goos string) error {
versionInfo := u.getLocalVersion(goos)
versionInfo.Version = sanitizeVersion(versionInfo.Version)
versionFileName := filepath.Base(u.versionFileURL(goos))
versionFilePath := filepath.Join(deployDir, versionFileName)
txt, err := json.Marshal(versionInfo)
if err != nil {
return err
}
if err = ioutil.WriteFile(versionFilePath, txt, 0600); err != nil {
return err
}
if err := singAndVerify(versionFilePath); err != nil {
return err
}
updateFileName := filepath.Base(versionInfo.UpdateFile)
updateFilePath := filepath.Join(deployDir, updateFileName)
if err := singAndVerify(updateFilePath); err != nil {
return err
}
return nil
}
func (u *Updates) CheckIsUpToDate() (isUpToDate bool, latestVersion VersionInfo, err error) {
localVersion := u.GetLocalVersion()
latestVersion, err = u.getLatestVersion()
if err != nil {
return
}
localIsOld, err := isFirstVersionNewer(latestVersion.Version, localVersion.Version)
return !localIsOld, latestVersion, err
}
func (u *Updates) GetDownloadLink() string {
latestVersion, err := u.getLatestVersion()
if err != nil || latestVersion.InstallerFile == "" {
localVersion := u.GetLocalVersion()
return localVersion.GetDownloadLink()
}
return latestVersion.GetDownloadLink()
}
func (u *Updates) GetLocalVersion() VersionInfo {
return u.getLocalVersion(runtime.GOOS)
}
func (u *Updates) getLocalVersion(goos string) VersionInfo {
version := u.version
if BuildType != "" {
version += " " + BuildType
}
versionInfo := VersionInfo{
Version: version,
Revision: u.revision,
ReleaseDate: u.buildTime,
ReleaseNotes: u.releaseNotes,
ReleaseFixedBugs: u.releaseFixedBugs,
FixedBugs: strings.Split(u.releaseFixedBugs, "\n"),
URL: u.installerFileURL(goos),
LandingPage: u.landingPageURL(),
UpdateFile: u.updateFileURL(goos),
InstallerFile: u.installerFileURL(goos),
}
if goos == "linux" {
pkgName := u.linuxFileBaseName
pkgRel := "1"
pkgBaseFile := strings.Join([]string{Host, DownloadPath, pkgName}, "/")
pkgBasePath := DownloadPath + "/" + pkgName // add at least one dir
pkgBasePath = filepath.Dir(pkgBasePath) // keep only last dir
pkgBasePath = Host + "/" + pkgBasePath // add host in the end to not strip off double slash in URL
versionInfo.DebFile = pkgBaseFile + "_" + u.version + "-" + pkgRel + "_amd64.deb"
versionInfo.RpmFile = pkgBaseFile + "-" + u.version + "-" + pkgRel + ".x86_64.rpm"
versionInfo.PkgFile = strings.Join([]string{pkgBasePath, "PKGBUILD"}, "/")
}
return versionInfo
}
func (u *Updates) getLatestVersion() (latestVersion VersionInfo, err error) {
version, err := downloadToBytes(u.versionFileURL(runtime.GOOS))
if err != nil {
if u.cachedNewerVersion != nil {
return *u.cachedNewerVersion, nil
}
return
}
signature, err := downloadToBytes(u.signatureFileURL(runtime.GOOS))
if err != nil {
if u.cachedNewerVersion != nil {
return *u.cachedNewerVersion, nil
}
return
}
if err = verifyBytes(bytes.NewReader(version), bytes.NewReader(signature)); err != nil {
return
}
if err = json.NewDecoder(bytes.NewReader(version)).Decode(&latestVersion); err != nil {
return
}
if localIsOld, _ := isFirstVersionNewer(latestVersion.Version, u.version); localIsOld {
u.cachedNewerVersion = &latestVersion
}
return
}
func (u *Updates) landingPageURL() string {
return strings.Join([]string{Host, u.landingPagePath}, "/")
}
func (u *Updates) signatureFileURL(goos string) string {
return u.versionFileURL(goos) + sigExtension
}
func (u *Updates) versionFileURL(goos string) string {
return strings.Join([]string{Host, DownloadPath, u.versionFileBaseName + "_" + goos + ".json"}, "/")
}
func (u *Updates) installerFileURL(goos string) string {
installerFile := u.linInstallerFile
switch goos {
case "darwin": //nolint[goconst]
installerFile = u.macInstallerFile
case "windows": //nolint[goconst]
installerFile = u.winInstallerFile
}
return strings.Join([]string{Host, DownloadPath, installerFile}, "/")
}
func (u *Updates) updateFileURL(goos string) string {
return strings.Join([]string{Host, DownloadPath, u.updateFileBaseName + "_" + goos + ".tgz"}, "/")
}
func (u *Updates) StartUpgrade(currentStatus chan<- Progress) { // nolint[funlen]
status := &Progress{channel: currentStatus}
defer status.Update()
// Get latest version.
var verInfo VersionInfo
status.UpdateDescription(InfoCurrentVersion)
if verInfo, status.Err = u.getLatestVersion(); status.Err != nil {
return
}
if verInfo.UpdateFile == "" {
log.Warn("Empty update URL. Update manually.")
status.Err = ErrDownloadFailed
return
}
// Download.
status.UpdateDescription(InfoDownloading)
if status.Err = mkdirAllClear(u.updateTempDir); status.Err != nil {
return
}
var updateTar string
updateTar, status.Err = downloadWithSignature(
status,
verInfo.UpdateFile,
u.updateTempDir,
)
if status.Err != nil {
return
}
// Check signature.
status.UpdateDescription(InfoVerifying)
status.Err = verifyFile(updateTar)
if status.Err != nil {
log.Warnf("Cannot verify update file %s: %v", updateTar, status.Err)
status.Err = ErrUpdateVerifyFailed
return
}
// Untar.
status.UpdateDescription(InfoUnpacking)
status.Err = untarToDir(updateTar, u.updateTempDir, status)
if status.Err != nil {
return
}
// Run upgrade (OS specific).
status.UpdateDescription(InfoUpgrading)
switch runtime.GOOS {
case "windows": //nolint[goconst]
installerFile := strings.Split(u.winInstallerFile, "/")[1]
cmd := exec.Command("./" + installerFile) // nolint[gosec]
cmd.Dir = u.updateTempDir
status.Err = cmd.Start()
case "darwin": //nolint[goconst]
// current path is better then appDir = filepath.Join("/Applications")
var exePath string
exePath, status.Err = osext.Executable()
if status.Err != nil {
return
}
localPath := filepath.Dir(exePath) // Macos
localPath = filepath.Dir(localPath) // Contents
localPath = filepath.Dir(localPath) // .app
updatePath := filepath.Join(u.updateTempDir, u.macAppBundleName)
log.Warn("localPath ", localPath)
log.Warn("updatePath ", updatePath)
status.Err = syncFolders(localPath, updatePath)
if status.Err != nil {
return
}
status.UpdateDescription(InfoRestartApp)
return
default:
status.Err = errors.New("upgrade for " + runtime.GOOS + " not implemented")
}
status.UpdateDescription(InfoQuitApp)
}