This repository has been archived by the owner on Mar 28, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 186
/
main.js
686 lines (599 loc) · 16.9 KB
/
main.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
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
import {
app, BrowserWindow, ipcMain,
Menu, Tray, session, crashReporter,
autoUpdater, shell, dialog,
} from 'electron';
import homedir from 'homedir';
import { argv } from 'yargs';
import path from 'path';
import fs from 'fs';
import childProcess from 'child_process';
import { guid } from './js/utils';
import LocalServer from './js/utils/localServer';
import { bindLocalServerEvent } from './js/utils/mainProcLocalServerEvents';
// Keep a global reference of the window object, if you don't, the window will
// be closed automatically when the JavaScript object is garbage collected.
let mainWindow;
let trayMenu;
let closeConfirmed = false;
const version = app.getVersion();
// We no longer support win32, but process.platform returns Windows 64 bit as win32.
const plat = process.platform === 'win32' ? 'win64' : process.platform;
const feedURL = `https://updates2.openbazaar.org:5001/update/${plat}/${version}`;
global.serverLog = '';
const handleStartupEvent = function () {
if (process.platform !== 'win32') {
return false;
}
const squirrelCommand = process.argv[1];
function exeSquirrelCommand(args, cb) {
const updateDotExe = path.resolve(path.dirname(process.execPath), '..', 'update.exe');
const child = childProcess.spawn(updateDotExe, args, { detached: true });
child.on('close', cb());
}
function install(cb) {
const target = path.basename(process.execPath);
exeSquirrelCommand(['--createShortcut', target, ' --shortcut-locations=Desktop,StartMenu'], cb);
}
function uninstall(cb) {
const target = path.basename(process.execPath);
exeSquirrelCommand(['--removeShortcut', target], cb);
}
switch (squirrelCommand) {
case '--squirrel-install':
install(app.quit);
break;
case '--squirrel-updated':
// Always quit when done
app.quit();
return true;
case '--squirrel-uninstall':
// Always quit when done
uninstall(app.quit);
return true;
case '--squirrel-obsolete':
// This is called on the outgoing version of your app before
// we update to the new version - it's the opposite of
// --squirrel-updated
app.quit();
return true;
default:
break;
}
return true;
};
if (handleStartupEvent()) {
console.log('OpenBazaar started on Windows...');
}
const serverPath = `${__dirname}${path.sep}..${path.sep}openbazaar-go${path.sep}`;
const serverFilename = process.platform === 'darwin' || process.platform === 'linux' ?
'openbazaard' : 'openbazaard.exe';
const isBundledApp = fs.existsSync(serverPath + serverFilename);
global.isBundledApp = isBundledApp;
let localServer;
if (isBundledApp) {
global.localServer = localServer = new LocalServer({
getMainWindow: () => mainWindow,
serverPath,
serverFilename,
errorLogPath: `${__dirname}${path.sep}..${path.sep}..${path.sep}error.log`,
// IMPORTANT: From the main process, only bind events to the localServer instance
// using the functions in the mainProcLocalServerEvents module. The reasons for that
// will be explained in the module.
});
global.authCookie = guid();
}
const updatesSupported = process.platform === 'win32' || process.platform === 'darwin';
global.updatesSupported = updatesSupported;
// set the client data path
let defaultUserDataPath;
switch (process.platform) {
case 'win32':
defaultUserDataPath = `${homedir()}\\OpenBazaar2.0-ClientData`;
break;
case 'darwin':
defaultUserDataPath = `${homedir()}/Library/Application Support/OpenBazaar2.0-ClientData`;
break;
default:
defaultUserDataPath = `${homedir()}/.openbazaar2.0-clientData`;
}
const userDataPath = argv.userData || defaultUserDataPath;
// If you pass in a specific userData value as a command line option, we'll use that.
// Otherwise, if you're using the bundled app, we'll use a custom data dir (as defined
// in the switch above). Otherwise (running client from source), we won't update the data
// dir and the electron default will be used.
if (isBundledApp || argv.userData) {
try {
app.setPath('userData', userDataPath);
} catch (e) {
dialog.showErrorBox('Error setting the user data path',
`There was an error setting the user data path to ${userDataPath}\n\n${e}`);
app.exit();
}
}
crashReporter.start({
productName: 'OpenBazaar 2',
companyName: 'OpenBazaar',
submitURL: 'http://104.131.17.128:1127/post',
uploadToServer: true,
extra: {
bundled: isBundledApp,
},
});
/**
* Prevent window navigation
*
* @param {Object} win Contains a browserwindow object
*/
function preventWindowNavigation(win) {
win.webContents.on('will-navigate', (e, url) => {
// NB: Let page reloads through.
if (url === win.webContents.getURL()) return;
e.preventDefault();
});
}
function createWindow() {
// Check for updates an hour after the last check
let checkForUpdatesInterval;
const checkForUpdates = () => {
clearInterval(checkForUpdatesInterval);
autoUpdater.checkForUpdates();
checkForUpdatesInterval = setInterval(() => {
autoUpdater.checkForUpdates();
}, 60 * 60 * 1000);
};
let helpSubmenu = [
{
label: 'Website',
click() {
shell.openExternal('https://openbazaar.org');
},
},
// until the documentation page is updated, don't show it
/*
{
label: 'Documentation',
click() {
shell.openExternal('https://docs.openbazaar.org');
},
},
*/
{
label: 'Support',
click() {
shell.openExternal('https://openbazaar.org/support');
},
},
];
if (isBundledApp) {
helpSubmenu = [
{
label: updatesSupported ? 'Check for Updates...' : 'Download Latest',
click() {
if (updatesSupported) {
checkForUpdates();
} else {
shell.openExternal('https://www.openbazaar.org/download/');
}
},
},
{
type: 'separator',
},
...helpSubmenu,
];
}
const viewSubmenu = [
{
label: 'Reload',
accelerator: 'CmdOrCtrl+R',
click(item, focusedWindow) {
if (focusedWindow) focusedWindow.reload();
},
},
{
label: 'Toggle Developer Tools',
accelerator: process.platform === 'darwin' ? 'Alt+Command+I' : 'Ctrl+Shift+I',
click(item, focusedWindow) {
if (focusedWindow) focusedWindow.webContents.toggleDevTools();
},
},
{
role: 'togglefullscreen',
},
{
role: 'zoomin',
accelerator: 'CommandOrControl+=',
},
{
role: 'zoomout',
accelerator: 'CommandOrControl+-',
},
{
role: 'resetzoom',
},
];
const template = [
{
label: 'Edit',
submenu: [
{
role: 'undo',
},
{
role: 'redo',
},
{
type: 'separator',
},
{
role: 'cut',
},
{
role: 'copy',
},
{
role: 'paste',
},
{
role: 'pasteandmatchstyle',
},
{
role: 'delete',
},
{
role: 'selectall',
},
],
},
{
label: 'View',
submenu: viewSubmenu,
},
{
role: 'window',
submenu: [
{
role: 'minimize',
},
{
role: 'close',
},
],
},
{
role: 'help',
submenu: helpSubmenu,
},
];
if (process.platform === 'darwin') {
const name = app.getName();
template.unshift({
label: name,
submenu: [
{
role: 'about',
},
{
type: 'separator',
},
{
role: 'services',
submenu: [],
},
{
type: 'separator',
},
{
role: 'hide',
},
{
role: 'hideothers',
},
{
role: 'unhide',
},
{
type: 'separator',
},
{
role: 'quit',
accelerator: 'CmdOrCtrl+Q',
click() {
closeConfirmed = true;
app.quit();
},
},
],
});
// Edit menu.
template[1].submenu.push(
{
type: 'separator',
},
{
label: 'Speech',
submenu: [
{
role: 'startspeaking',
},
{
role: 'stopspeaking',
},
],
}
);
// Window menu.
template[3].submenu = [
{
label: 'Close',
accelerator: 'CmdOrCtrl+W',
role: 'close',
},
{
label: 'Minimize',
accelerator: 'CmdOrCtrl+M',
role: 'minimize',
},
{
label: 'Zoom',
role: 'zoom',
},
{
type: 'separator',
},
{
label: 'Bring All to Front',
role: 'front',
},
];
}
const menu = Menu.buildFromTemplate(template);
Menu.setApplicationMenu(menu);
ipcMain.on('contextmenu-click', () => {
menu.popup({});
});
// put logic here to set tray icon based on OS
let osTrayIcon = 'openbazaar-system-tray.png';
if (process.platform === 'darwin') osTrayIcon = 'openbazaar-mac-system-tray.png';
trayMenu = new Tray(`${__dirname}/imgs/${osTrayIcon}`);
let trayTemplate = [];
if (localServer) {
trayTemplate = [
{
label: 'Start Local Server',
type: 'normal',
click() { localServer.start(); },
},
{
label: 'Shutdown Local Server',
type: 'normal',
click() { localServer.stop(); },
},
{
label: 'View Server Debug Log',
type: 'normal',
click() {
mainWindow.focus();
mainWindow.restore();
mainWindow.webContents.send('show-server-log');
},
},
{
type: 'separator',
},
];
}
trayTemplate.push({
label: 'Quit',
type: 'normal',
accelerator: 'Command+Q',
click() {
app.quit();
},
});
const contextMenu = Menu.buildFromTemplate(trayTemplate);
trayMenu.setContextMenu(contextMenu);
if (localServer) {
if (localServer.isRunning) {
contextMenu.items[0].enabled = false;
} else {
contextMenu.items[1].enabled = false;
}
bindLocalServerEvent('start', () => {
contextMenu.items[0].enabled = false;
contextMenu.items[1].enabled = true;
});
bindLocalServerEvent('exit', () => {
contextMenu.items[0].enabled = true;
contextMenu.items[1].enabled = false;
});
// we'll enable the debug log when our serverConnect module is ready
contextMenu.items[2].enabled = false;
ipcMain.on('server-connect-ready', () => (contextMenu.items[2].enabled = true));
}
// Create the browser window.
mainWindow = new BrowserWindow({
width: 1200,
height: 760,
minWidth: 1170,
minHeight: 700,
center: true,
title: 'OpenBazaar',
frame: false,
icon: `${__dirname}/imgs/icon.png`,
webPreferences: {
nodeIntegration: true,
},
});
// and load the index.html of the app.
mainWindow.loadURL(`file://${__dirname}/.tmp/index.html`);
ipcMain.on('set-proxy', (e, id, socks5Setting = '') => {
if (!id) {
throw new Error('Please provide an id that will be passed back with the "proxy-set" ' +
'event.');
}
mainWindow.webContents.session.setProxy({
proxyRules: socks5Setting,
proxyBypassRules: '<local>',
}, () => mainWindow.webContents.send('proxy-set', id));
});
// Open the DevTools.
if (process.env.NODE_ENV === 'development') {
mainWindow.webContents.openDevTools({
detach: true,
});
}
// Emitted when the window is closed.
mainWindow.on('closed', () => {
// Dereference the window object, usually you would store windows
// in an array if your app supports multi windows, this is the time
// when you should delete the corresponding element.
mainWindow = null;
app.quit();
});
mainWindow.on('close', (e) => {
mainWindow.send('close-attempt');
if (mainWindow && !closeConfirmed) {
e.preventDefault();
}
});
/**
* If there is an update available then we will send an IPC message to the
* render process to notify the user. If the user wants to update
* the software then they will send an IPC message back to the main process and we will
* begin to download the file and update the software.
*/
if (isBundledApp) {
autoUpdater.on('checking-for-update', () => {
mainWindow.send('updateChecking');
mainWindow.send('consoleMsg', `Checking for update at ${autoUpdater.getFeedURL()}`);
});
autoUpdater.on('error', (err, msg) => {
mainWindow.send('consoleMsg', msg);
mainWindow.send('updateError', msg);
});
autoUpdater.on('update-not-available', () => {
mainWindow.send('updateNotAvailable');
});
autoUpdater.on('update-available', () => {
mainWindow.send('updateAvailable');
});
autoUpdater.on('update-downloaded', (e, releaseNotes, releaseName,
releaseDate, updateUrl) => {
const opts = {};
opts.Name = releaseName;
opts.URL = updateUrl;
opts.Date = releaseDate;
opts.Notes = releaseNotes;
mainWindow.send('updateReadyForInstall', opts);
});
// Listen for installUpdate command to install the update
ipcMain.on('installUpdate', () => {
autoUpdater.quitAndInstall();
});
// Listen for checkForUpdate command to manually check for new versions
ipcMain.on('checkForUpdate', () => {
checkForUpdates();
});
autoUpdater.setFeedURL(feedURL);
}
mainWindow.webContents.on('dom-ready', () => {
// Check for an update once the DOM is ready so the update dialog box can be shown
if (isBundledApp) checkForUpdates();
});
// Set up protocol
app.setAsDefaultProtocolClient('ob');
// Check for URL hijacking in the browser
preventWindowNavigation(mainWindow);
}
// This method will be called when Electron has finished
// initialization and is ready to create browser windows.
// Some APIs can only be used after this event occurs.
app.on('ready', createWindow);
// Quit when all windows are closed.
app.on('window-all-closed', () => {
// On macOS it is common for applications and their Menu bar
// to stay active until the user quits explicitly with Cmd + Q
if (process.platform !== 'darwin') {
app.quit();
}
});
app.on('activate', () => {
if (mainWindow) mainWindow.show();
});
const handleObLink = (route) => {
if (!route || typeof route !== 'string') {
throw new Error('Please provide a route as a string.');
}
global.externalRoute = route;
if (mainWindow) {
// if our app router is fully loaded it will process the event sent below, otherwise
// the global.externalRoute will be used
mainWindow.webContents.send('external-route', route);
}
};
app.on('open-url', (e, uri) => {
e.preventDefault();
const url = uri.split('://')[1];
if (url) {
handleObLink(url);
}
});
ipcMain.on('close-confirmed', () => {
closeConfirmed = true;
if (mainWindow) mainWindow.close();
});
// If appropriate, add in Basic Auth headers to each request. If connecting to
// the built-in server, we'll add in the auth token.
ipcMain.on('active-server-set', (e, server) => {
const filter = {
urls: [`${server.httpUrl}*`, `${server.socketUrl}*`],
};
session.defaultSession.webRequest.onBeforeSendHeaders(filter, (details, callback) => {
if (server.authenticate) {
const un = server.username;
const pw = server.password;
details.requestHeaders.Authorization =
`Basic ${new Buffer(`${un}:${pw}`).toString('base64')}`;
}
if (global.authCookie && server.builtIn) {
details.requestHeaders.Cookie = `OpenBazaar_Auth_Cookie=${global.authCookie}`;
}
callback({ cancel: false, requestHeaders: details.requestHeaders });
});
});
// some cleanup when our app is exiting
process.on('exit', () => {
closeConfirmed = true;
app.quit();
if (localServer) localServer.stop();
});
// Aggreate and make available the localServer and serverConnect
// module logs into one cohesive server log.
const log = msg => {
if (typeof msg !== 'string') {
throw new Error('Please provide a message as a string.');
}
if (!msg) return;
// Prevent the logs / msg from getting so large it eats up all the ram
// and crashes the client.
const message = msg.slice(msg.length - 500000);
global.serverLog += message;
global.serverLog = global.serverLog.slice(global.serverLog.length - 2000000);
if (mainWindow) {
mainWindow.webContents.send('server-log', message);
}
};
if (localServer) bindLocalServerEvent('log', (localServ, msg) => log(msg));
ipcMain.on('server-connect-log', (e, msg) => log(msg));
ipcMain.on('set-badge-count', (event, count) => {
// setBadgeCount is only available on certain environements:
// https://github.com/electron/electron/blob/master/docs/api/app.md#appsetbadgecountcount-linux-macos
try {
app.setBadgeCount(count);
} catch (err) {
// pass
console.log(err);
}
});