-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathmain.js
More file actions
327 lines (286 loc) · 10.9 KB
/
Copy pathmain.js
File metadata and controls
327 lines (286 loc) · 10.9 KB
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
const {
app,
BrowserWindow,
Tray,
Menu,
nativeImage,
ipcMain,
} = require('electron');
const https = require('https');
const path = require('path');
const cert = `
LS0tLS1CRUdJTiBDRVJUSUZJQ0FURS0tLS0tCk1JSURVakNDQWpvQ0NRQ21EMm1GdzVPc2pEQU5C
Z2txaGtpRzl3MEJBUXNGQURCck1Rc3dDUVlEVlFRR0V3SlYKVXpFTE1Ba0dBMVVFQ0F3Q1RrZ3hF
REFPQmdOVkJBY01CMGhoYm05MlpYSXhHVEFYQmdO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jdWZpc09qL1BjbjZVdjRaZlNQbzRhMDFQNU8w
Y0V2ekF0OWNBUUtCZ0FYUwo4WW9UZkdRNVJCMlhiZ3JmK3hiSW5aUHUvaWR3UExPUlRDUXpZUW1Z
MkZEWHN6bUJqcU1STyswVWZoL1BLa0ZjCnZKZW5rMnhLZkYrZVdwMzhkdEtFcnM3SGg5NGY1YVp4
MkhsMUQ4UEtvdjkwbk9FRmY5WU1sRXhONUZ6V3JjQXIKcVIvQzcxR0RYWU1YcmdSNzRiZnZleUYr
eFhDMHRyTFVNb3BER3dEdEFvR0FUeUdXVk9weTRXR1JrWmIzL0FjLwpIUjVaUmJWbE02QVVxSElD
VFVzSlFiTHZhMFladUMwOFVaOVA2L1cxNTMwZC9Xd2w1S1RuYlc1cEcvaDN3djJWClZkNEIzWG5i
SmlXUHorUnhmRHNuNEluaDJnTHNxZ0VUbFFWQk1PVTQvOC9ZOWZJNW1oVVJoUDFaRFcxMzhFbjUK
T1p3eTdEK0owbndrcE5hMW5pTkN2MjA9Ci0tLS0tRU5EIFBSSVZBVEUgS0VZLS0tLS0`;
// Only auto-update on Windows, Mac requires signed app
if (process.platform === 'win32') {
require('update-electron-app')({
updateInterval: '1 hour',
});
}
// https://github.com/electron/windows-installer/blob/0336cc646af849f125979ba623efdf5440852c4e/README.md#handling-squirrel-events
// this should be placed at top of main.js to handle setup events quickly
if (handleSquirrelEvent()) {
// squirrel event handled and app will exit in 1000ms, so don't do anything else
return;
}
function handleSquirrelEvent() {
if (process.argv.length === 1) {
return false;
}
const ChildProcess = require('child_process');
const path = require('path');
const appFolder = path.resolve(process.execPath, '..');
const rootAtomFolder = path.resolve(appFolder, '..');
const updateDotExe = path.resolve(path.join(rootAtomFolder, 'Update.exe'));
const exeName = path.basename(process.execPath);
const spawn = function (command, args) {
let spawnedProcess, error;
try {
spawnedProcess = ChildProcess.spawn(command, args, { detached: true });
} catch (error) {}
return spawnedProcess;
};
const spawnUpdate = function (args) {
return spawn(updateDotExe, args);
};
const squirrelEvent = process.argv[1];
switch (squirrelEvent) {
case '--squirrel-install':
case '--squirrel-updated':
// Optionally do things such as:
// - Add your .exe to the PATH
// - Write to the registry for things like file associations and
// explorer context menus
// Install desktop and start menu shortcuts
spawnUpdate(['--createShortcut', exeName]);
setTimeout(app.quit, 1000);
return true;
case '--squirrel-uninstall':
// Undo anything you did in the --squirrel-install and
// --squirrel-updated handlers
// Remove desktop and start menu shortcuts
spawnUpdate(['--removeShortcut', exeName]);
setTimeout(app.quit, 1000);
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;
}
}
class AuthManager {
constructor(config) {
this.tray = null;
this.server = null;
this.win = null;
this.token = null;
this.username = null;
this.loggedOutIcon = config.loggedOutIcon;
this.loggedInIcon = nativeImage.createFromPath(
path.join(__dirname, 'img', 'coin-16x16.png')
);
this.ipc = ipcMain;
// Electron runs an old node version, no auto-binding in this env.
this.createContextMenu = this.createContextMenu.bind(this);
this.createWindow = this.createWindow.bind(this);
this.handleIpcMessage = this.handleIpcMessage.bind(this);
this.requestListener = this.requestListener.bind(this);
this.startHttpsServer = this.startHttpsServer.bind(this);
this.startup = this.startup.bind(this);
this.quit = this.quit.bind(this);
this.quitHttpsServer = this.quitHttpsServer.bind(this);
this.updateContextMenu = this.updateContextMenu.bind(this);
this.updateTrayIcon = this.updateTrayIcon.bind(this);
}
createContextMenu() {
if (this.token) {
return Menu.buildFromTemplate([
{ label: `Logged in as: ${this.username}`, click: this.createWindow },
{ label: 'Quit', click: this.quit },
]);
} else {
return Menu.buildFromTemplate([
{ label: 'Login', click: this.createWindow },
{ label: 'Quit', click: this.quit },
]);
}
}
createWindow() {
if (this.win) {
//don't create another window (process) if existing window is hidden
this.win.show();
return false;
}
this.win = new BrowserWindow({
width: 600,
height: 400,
autoHideMenuBar: true,
webPreferences: {
nodeIntegration: true,
contextIsolation: false,
devTools: false,
},
});
this.win.loadFile('index.html');
//this.win.webContents.openDevTools();
this.win.on('minimize', (event) => {
event.preventDefault();
this.win.hide();
});
this.win.on('activate', (event) => {
event.preventDefault();
// On macOS it's common to re-create a window in the app when the
// dock icon is clicked and there are no other windows open.
if (BrowserWindow.getAllWindows().length === 0) {
this.createWindow();
} else {
this.win.show();
}
});
this.win.on('close', (event) => {
if (!app.isQuiting) {
event.preventDefault();
this.win.hide();
} else {
console.log('quitting');
}
return false;
});
// Quit when all windows are closed, except on macOS. There, it's common
// for applications and their menu bar to stay active until the user quits
// explicitly with Cmd + Q.
this.win.on('window-all-closed', (event) => {
if (process.platform !== 'darwin') {
app.quit();
}
return false;
});
}
handleIpcMessage(event, msg) {
this.token = msg.token;
this.username = msg.username;
this.updateContextMenu();
this.updateTrayIcon();
// reply to the ui that we're good
event.sender.send('asynchronous-reply', {
status: 200,
username: this.username,
});
}
requestListener(req, res) {
if (this.token) {
res.writeHead(200);
res.end(this.token);
} else {
res.writeHead(404);
res.end('');
}
}
startHttpsServer() {
// Creating object of key and certificate for SSL
this.options = {
key: Buffer.from(pk, 'base64').toString('utf8'),
cert: Buffer.from(cert, 'base64').toString('utf8'),
};
this.server = https
.createServer(this.options, this.requestListener)
.listen(50123, 'localhost');
}
startup() {
if (!app.requestSingleInstanceLock()) {
this.quit();
}
this.tray = new Tray(this.loggedOutIcon);
//this.updateContextMenu();
this.updateTrayIcon();
this.startHttpsServer();
this.ipc.on('asynchronous-message', this.handleIpcMessage);
this.createWindow();
this.ipc.on('error', () => {
// this will bring the app window to the foreground
// and login will be preseted to user
app.relaunch();
app.exit();
});
}
quit() {
app.exit(0);
}
quitHttpsServer() {
this.server.close();
}
updateContextMenu() {
this.tray.setContextMenu(this.createContextMenu());
}
updateTrayIcon() {
if (this.token) {
this.tray.setImage(this.loggedInIcon);
this.tray.setToolTip('tokn - CWBI Auth (Logged In)');
} else {
this.tray.setImage(this.loggedOutIcon);
this.tray.setToolTip('tokn - CWBI Auth (Logged Out)');
}
this.updateContextMenu();
}
}
app.whenReady().then(() => {
const auth = new AuthManager({
loggedOutIcon: nativeImage.createFromPath(
path.join(__dirname, 'img', 'coin-16x16-inactive.png')
),
});
auth.startup();
});