This repository has been archived by the owner on Sep 5, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2.1k
/
Copy pathclientBinaryManager.js
325 lines (258 loc) · 10.8 KB
/
clientBinaryManager.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
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
const _ = global._;
const Q = require('bluebird');
const fs = require('fs');
const { app, dialog } = require('electron');
const got = require('got');
const path = require('path');
const Settings = require('./settings');
const Windows = require('./windows');
const ClientBinaryManager = require('ethereum-client-binaries').Manager;
const EventEmitter = require('events').EventEmitter;
const log = require('./utils/logger').create('ClientBinaryManager');
// should be 'https://raw.githubusercontent.com/ethereum/mist/master/clientBinaries.json'
const BINARY_URL = 'https://raw.githubusercontent.com/ethereum/mist/master/clientBinaries.json';
const ALLOWED_DOWNLOAD_URLS_REGEX =
/^https:\/\/(?:(?:[A-Za-z0-9](?:[A-Za-z0-9-]{0,61}[A-Za-z0-9])?\.)?ethereum\.org\/|gethstore\.blob\.core\.windows\.net\/|bintray\.com\/artifact\/download\/karalabe\/ethereum\/)(?:.+)/; // eslint-disable-line max-len
class Manager extends EventEmitter {
constructor() {
super();
this._availableClients = {};
}
init(restart) {
log.info('Initializing...');
// check every hour
setInterval(() => this._checkForNewConfig(true), 1000 * 60 * 60);
this._resolveEthBinPath();
return this._checkForNewConfig(restart);
}
getClient(clientId) {
return this._availableClients[clientId.toLowerCase()];
}
_writeLocalConfig(json) {
log.info('Write new client binaries local config to disk ...');
fs.writeFileSync(
path.join(Settings.userDataPath, 'clientBinaries.json'),
JSON.stringify(json, null, 2)
);
}
_checkForNewConfig(restart) {
const nodeType = 'Geth';
let binariesDownloaded = false;
let nodeInfo;
log.info(`Checking for new client binaries config from: ${BINARY_URL}`);
this._emit('loadConfig', 'Fetching remote client config');
// fetch config
return got(BINARY_URL, {
timeout: 3000,
json: true,
})
.then((res) => {
if (!res || _.isEmpty(res.body)) {
throw new Error('Invalid fetch result');
} else {
return res.body;
}
})
.catch((err) => {
log.warn('Error fetching client binaries config from repo', err);
})
.then((latestConfig) => {
if(!latestConfig) return;
let localConfig;
let skipedVersion;
const nodeVersion = latestConfig.clients[nodeType].version;
this._emit('loadConfig', 'Fetching local config');
try {
// now load the local json
localConfig = JSON.parse(
fs.readFileSync(path.join(Settings.userDataPath, 'clientBinaries.json')).toString()
);
} catch (err) {
log.warn(`Error loading local config - assuming this is a first run: ${err}`);
if (latestConfig) {
localConfig = latestConfig;
this._writeLocalConfig(localConfig);
} else {
throw new Error('Unable to load local or remote config, cannot proceed!');
}
}
try {
skipedVersion = fs.readFileSync(path.join(Settings.userDataPath, 'skippedNodeVersion.json')).toString();
} catch (err) {
log.info('No "skippedNodeVersion.json" found.');
}
// prepare node info
const platform = process.platform.replace('darwin', 'mac').replace('win32', 'win').replace('freebsd', 'linux').replace('sunos', 'linux');
const binaryVersion = latestConfig.clients[nodeType].platforms[platform][process.arch];
const checksums = _.pick(binaryVersion.download, 'sha256', 'md5');
const algorithm = _.keys(checksums)[0].toUpperCase();
const hash = _.values(checksums)[0];
// get the node data, to be able to pass it to a possible error
nodeInfo = {
type: nodeType,
version: nodeVersion,
checksum: hash,
algorithm,
};
// if new config version available then ask user if they wish to update
if (latestConfig
&& JSON.stringify(localConfig) !== JSON.stringify(latestConfig)
&& nodeVersion !== skipedVersion) {
return new Q((resolve) => {
log.debug('New client binaries config found, asking user if they wish to update...');
const wnd = Windows.createPopup('clientUpdateAvailable', _.extend({
useWeb3: false,
electronOptions: {
width: 600,
height: 340,
alwaysOnTop: false,
resizable: false,
maximizable: false,
},
}, {
sendData: {
uiAction_sendData: {
name: nodeType,
version: nodeVersion,
checksum: `${algorithm}: ${hash}`,
downloadUrl: binaryVersion.download.url,
restart,
},
},
}), (update) => {
// update
if (update === 'update') {
this._writeLocalConfig(latestConfig);
resolve(latestConfig);
// skip
} else if (update === 'skip') {
fs.writeFileSync(
path.join(Settings.userDataPath, 'skippedNodeVersion.json'),
nodeVersion
);
resolve(localConfig);
}
wnd.close();
});
// if the window is closed, simply continue and as again next time
wnd.on('close', () => {
resolve(localConfig);
});
});
}
return localConfig;
})
.then((localConfig) => {
if (!localConfig) {
log.info('No config for the ClientBinaryManager could be loaded, using local clientBinaries.json.');
localConfig = require('../clientBinaries.json');
}
// scan for node
const mgr = new ClientBinaryManager(localConfig);
mgr.logger = log;
this._emit('scanning', 'Scanning for binaries');
return mgr.init({
folders: [
path.join(Settings.userDataPath, 'binaries', 'Geth', 'unpacked'),
path.join(Settings.userDataPath, 'binaries', 'Eth', 'unpacked'),
],
})
.then(() => {
const clients = mgr.clients;
this._availableClients = {};
const available = _.filter(clients, c => !!c.state.available);
if (!available.length) {
if (_.isEmpty(clients)) {
throw new Error('No client binaries available for this system!');
}
this._emit('downloading', 'Downloading binaries');
return Q.map(_.values(clients), (c) => {
binariesDownloaded = true;
return mgr.download(c.id, {
downloadFolder: path.join(Settings.userDataPath, 'binaries'),
urlRegex: ALLOWED_DOWNLOAD_URLS_REGEX,
});
});
}
})
.then(() => {
this._emit('filtering', 'Filtering available clients');
_.each(mgr.clients, (client) => {
if (client.state.available) {
const idlcase = client.id.toLowerCase();
this._availableClients[idlcase] = {
binPath: Settings[`${idlcase}Path`] || client.activeCli.fullPath,
version: client.version,
};
}
});
// restart if it downloaded while running
if (restart && binariesDownloaded) {
log.info('Restarting app ...');
app.relaunch();
app.quit();
}
this._emit('done');
});
})
.catch((err) => {
log.error(err);
this._emit('error', err.message);
// show error
if (err.message.indexOf('Hash mismatch') !== -1) {
// show hash mismatch error
dialog.showMessageBox({
type: 'warning',
buttons: ['OK'],
message: global.i18n.t('mist.errors.nodeChecksumMismatch.title'),
detail: global.i18n.t('mist.errors.nodeChecksumMismatch.description', {
type: nodeInfo.type,
version: nodeInfo.version,
algorithm: nodeInfo.algorithm,
hash: nodeInfo.checksum,
}),
}, () => {
app.quit();
});
// throw so the main.js can catch it
throw err;
}
});
}
_emit(status, msg) {
log.debug(`Status: ${status} - ${msg}`);
this.emit('status', status, msg);
}
_resolveEthBinPath() {
log.info('Resolving path to Eth client binary ...');
let platform = process.platform;
// "win32" -> "win" (because nodes are bundled by electron-builder)
if (platform.indexOf('win') === 0) {
platform = 'win';
} else if (platform.indexOf('darwin') === 0) {
platform = 'mac';
}
log.debug(`Platform: ${platform}`);
let binPath = path.join(
__dirname,
'..',
'nodes',
'eth',
`${platform}-${process.arch}`
);
if (Settings.inProductionMode) {
// get out of the ASAR
binPath = binPath.replace('nodes', path.join('..', '..', 'nodes'));
}
binPath = path.join(path.resolve(binPath), 'eth');
if (platform === 'win') {
binPath += '.exe';
}
log.info(`Eth client binary path: ${binPath}`);
this._availableClients.eth = {
binPath,
version: '1.3.0',
};
}
}
module.exports = new Manager();