-
Notifications
You must be signed in to change notification settings - Fork 526
/
ElectronUpdater.js
232 lines (213 loc) · 7.76 KB
/
ElectronUpdater.js
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
// @flow
import {autoUpdater} from 'electron-updater'
import {net} from "electron"
import forge from 'node-forge'
import type {DesktopNotifier} from "./DesktopNotifier"
import {NotificationResult} from './DesktopNotifier'
import {lang} from './DesktopLocalizationProvider.js'
import type {DesktopConfigHandler} from './DesktopConfigHandler'
import type {DeferredObject} from "../api/common/utils/Utils"
import {defer, neverNull} from "../api/common/utils/Utils"
import {handleRestError} from "../api/common/error/RestError"
import {UpdateError} from "../api/common/error/UpdateError"
import {DesktopTray} from "./DesktopTray"
export class ElectronUpdater {
_conf: DesktopConfigHandler;
_notifier: DesktopNotifier;
_updatePollInterval: ?IntervalID;
_keyRetrievalTimeout: ?TimeoutID;
_foundKey: DeferredObject<void>;
_checkUpdateSignature: boolean;
_pubKey: string;
_errorCount: number;
_fallbackPollInterval: number = 15 * 60 * 1000;
_logger: {info(string, ...args: any): void, warn(string, ...args: any): void, error(string, ...args: any): void}
constructor(conf: DesktopConfigHandler, notifier: DesktopNotifier, fallbackPollInterval: ?number) {
this._conf = conf
this._notifier = notifier
this._errorCount = 0
if (fallbackPollInterval) {
this._fallbackPollInterval = fallbackPollInterval
}
this._logger = {
info: (m: string, ...args: any) => console.log.apply(console, ["autoUpdater info:\n", m].concat(args)),
warn: (m: string, ...args: any) => console.warn.apply(console, ["autoUpdater warn:\n", m].concat(args)),
error: (m: string, ...args: any) => console.error.apply(console, ["autoUpdater error:\n", m].concat(args)),
}
this._foundKey = defer()
autoUpdater.logger = null
autoUpdater.on('update-available', updateInfo => {
this._logger.info("update-available")
this._stopPolling()
this._foundKey.promise
.then(() => this._verifySignature(((updateInfo: any): UpdateInfo)))
.then(() => this._downloadUpdate())
}).on('update-downloaded', info => {
this._logger.info("update-downloaded")
this._stopPolling()
this._notifyAndInstall(((info: any): UpdateInfo))
}).on('checking-for-update', () => {
this._logger.info("checking-for-update")
}).on('error', e => {
const ee: any = e
this._stopPolling()
this._errorCount += 1
if (this._errorCount >= 5) {
this._logger.error(`Auto Update Error ${this._errorCount}, shutting down updater:\n${ee.message}`)
autoUpdater.removeAllListeners('update-available')
autoUpdater.removeAllListeners('update-downloaded')
autoUpdater.removeAllListeners('checking-for-update')
autoUpdater.removeAllListeners('error')
throw new UpdateError(`Update failed multiple times. Last error:\n${ee.message}`)
} else {
this._logger.error(`Auto Update Error ${this._errorCount}, continuing polling:\n${ee.message}`)
this._notifyUpdateError()
setTimeout(() => this._startPolling(), this._fallbackPollInterval)
}
})
}
+_enableAutoUpdateListener = () => this.start()
start() {
// if user changes auto update setting, we want to know
this._conf.removeListener('enableAutoUpdate', this._enableAutoUpdateListener)
.on('enableAutoUpdate', this._enableAutoUpdateListener)
if (!this._conf.getDesktopConfig("enableAutoUpdate")) {
this._stopPolling()
return
}
if (this._updatePollInterval) { //already running
// TODO: reset any other fields?
return
}
this._checkUpdateSignature = this._conf.get('checkUpdateSignature')
autoUpdater.autoDownload = !this._checkUpdateSignature
if (this._checkUpdateSignature) {
this._trackPublicKey(this._conf.get("pubKeyUrl"))
} else {
this._foundKey.resolve()
}
this._foundKey.promise
.then(() => this._startPolling())
.catch((e: Error) => {
this._logger.error("ElectronUpdater.start() failed,", e.message)
})
}
_verifySignature(updateInfo: UpdateInfo): Promise<void> {
return !this._checkUpdateSignature
? Promise.resolve()
: new Promise((resolve, reject) => {
try {
let hash = Buffer.from(updateInfo.sha512, 'base64').toString('binary')
let signature = Buffer.from(updateInfo.signature, 'base64').toString('binary')
let publicKey = forge.pki.publicKeyFromPem(this._pubKey)
if (publicKey.verify(hash, signature)) {
this._logger.info('Signature verification successful, downloading update')
resolve()
} else {
reject(new UpdateError('Signature verification failed, skipping update'))
}
} catch (e) {
reject(new UpdateError(e.message))
}
})
}
// tries to find the public key. will retry on failure.
_trackPublicKey(url: string): void {
this._logger.info("trying to retrieve public key from", url)
if (!url.startsWith('https://')) {
this._logger.error('invalid public key URL')
this._retryKeyRetrieval(this._conf.get("pollingInterval"))
return
}
this._requestFile(url).then((result) => {
if (!result.startsWith('-----BEGIN PUBLIC KEY-----')) {
const newUrl = result
.split(':NEWURL:')
.find(part => part.startsWith(' https://'))
if (newUrl === undefined) { // nonsense, try again in a few hours
this._retryKeyRetrieval(this._conf.get("pollingInterval"))
} else { // key moved to a new location
this._trackPublicKey(newUrl.trim())
}
} else {
this._logger.info("found public key")
this._pubKey = result
this._foundKey.resolve()
}
}).catch(e => {
this._logger.error("public key retrieval failed:", e)
this._retryKeyRetrieval(this._conf.get("pollingInterval"))
})
}
_requestFile(url: string): Promise<string> {
let buf: Buffer = Buffer.alloc(0)
return new Promise((resolve, reject) => {
net.request(url)
.on('error', err => reject(err))
.on('response', response => {
if (response.statusCode > 399) {
reject(handleRestError(response.statusCode, `The request to ${url} failed: ${response.statusCode}`))
return
}
response
.on('error', err => reject(err))
.on('data', chunk => buf = Buffer.concat([buf, chunk]))
.on('end', () => resolve(buf.toString('utf-8')))
})
.end()
})
}
_retryKeyRetrieval(when: ?number) {
this._logger.info("retrying key retrieval in", when)
clearTimeout(neverNull(this._keyRetrievalTimeout))
this._keyRetrievalTimeout = setTimeout(() => this._trackPublicKey(this._conf.get("pubKeyUrl")), when || this._fallbackPollInterval)
}
_startPolling() {
if (!this._updatePollInterval) {
this._updatePollInterval = setInterval(() => this._checkUpdate(), this._conf.get("pollingInterval")
|| this._fallbackPollInterval)
this._checkUpdate()
}
}
_stopPolling() {
clearInterval(neverNull(this._updatePollInterval))
this._updatePollInterval = null
}
_checkUpdate(): void {
autoUpdater
.checkForUpdates()
.catch((e: Error) => {
this._logger.error("Update check failed,", e.message)
this._notifyUpdateError()
})
}
_downloadUpdate(): void {
autoUpdater
.downloadUpdate()
.catch(e => {
this._logger.error("Update Download failed,", e.message)
this._notifyUpdateError()
})
}
_notifyAndInstall(info: UpdateInfo): void {
this._notifier
.showOneShot({
title: lang.get('updateAvailable_label', {"{version}": info.version}),
body: lang.get('clickToUpdate_msg'),
icon: DesktopTray.getIcon(this._conf.get('iconName'))
})
.then((res) => {
if (res === NotificationResult.Click) {
autoUpdater.quitAndInstall(false, true)
}
})
.catch((e: Error) => this._logger.error("Notification failed,", e.message))
}
_notifyUpdateError() {
this._notifier.showOneShot({
title: lang.get("errorReport_label"),
body: lang.get("errorDuringUpdate_msg"),
icon: DesktopTray.getIcon(this._conf.get('iconName'))
}).catch(e => this._logger.error("Notification failed,", e.message))
}
}