/
main.js
496 lines (441 loc) · 12.5 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
// Handle any Squirrel events first
if(require('electron-squirrel-startup')) return;
createMenubar()
const electron = require('electron')
// Module to control application life.
const app = electron.app
// Module to create native browser window.
const BrowserWindow = electron.BrowserWindow
// 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
// Preferences
const Config = require('electron-config')
const conf = new Config()
// Analytics
var mixpanel = require('mixpanel').init('6cae86bf1da092b800b30b27689bd665')
if (!conf.has('uuid')) {
conf.set('uuid', require('node-uuid').v1())
}
const uuid = conf.get('uuid')
// Networking
var bodyParser = require('body-parser')
var web = require('express')()
web.use(function(req, res, next) {
res.header("Access-Control-Allow-Origin", "*")
res.header("Access-Control-Allow-Headers", "X-Requested-With")
res.header("Access-Control-Allow-Headers", "Content-Type")
res.header("Access-Control-Allow-Methods", "PUT, GET, POST, DELETE, OPTIONS")
next()
})
web.use(bodyParser.urlencoded({ extended: false }))
web.use(bodyParser.json())
var http = require('http').Server(web)
var io = require('socket.io')(http)
var serverId = 'nodejs-server'
// Logging
var log = require('./res/js/console').setup()
var quitting
log.info('Zephyr v' + app.getVersion())
// Setup auto-updater
const autoUpdater = electron.autoUpdater
setupAutoUpdater()
// Limit Zephyr to one instance
var shouldQuit = app.makeSingleInstance(function(commandLine, workingDirectory) {
if (mainWindow) {
if (mainWindow.isMinimized()) {
mainWindow.restore()
}
mainWindow.focus()
} else {
createWindow()
}
return true
})
if (shouldQuit) {
mixpanel.track('event-second-instance')
quitting = true
app.quit()
return
}
function createMenubar() {
var menubar = require('menubar')
var mb = menubar({
icon: __dirname + '/res/img/icon-tray.png',
index: 'http://localhost:3753/login',
'show-dock-icon': true,
'preload-window': true
})
mb.on('ready', function ready() {
log.info('Menubar app running')
const contextMenu = electron.Menu.buildFromTemplate([
{
label: 'Show Window',
click() {
if(mainWindow != null) {
mainWindow.show()
} else {
createWindow()
}
}
},
{type: 'separator'},
{label: 'Quit', role: 'quit'}
])
mb.tray.setToolTip('Zephyr')
mb.tray.on('right-click', function() {
mb.tray.popUpContextMenu(contextMenu)
})
})
mb.on('hide', function hide() {
mb.window.loadURL('http://localhost:3753/login')
})
}
function createWindow () {
// Create the browser window.
mainWindow = new BrowserWindow({
width: 800,
height: 600,
minWidth: 400,
minHeight: 600,
frame: false,
icon: __dirname + '/res/img/icon.ico'
})
// Start onboarding if needed, otherwise load app
if (!conf.has('firstRun') || conf.get('firstRun')) {
log.info('Opening onboarding window...')
mainWindow.loadURL('file://' + __dirname + '/welcome.html')
mixpanel.track('first-run', {uuid: uuid})
} else {
log.info('Opening main window...')
mainWindow.loadURL('file://' + __dirname + '/index.html')
mixpanel.track('repeat-run', {uuid: uuid})
}
// Emitted when the window is closed.
mainWindow.on('closed', function () {
mainWindow = null
})
}
// 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', function() {
createWindow()
startServer()
startOverlay()
})
app.on('activate', function () {
// On OS X 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 (mainWindow === null) {
createWindow()
}
})
app.on('window-all-closed', function () {
// On OS X 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('before-quit', function (event) {
if (quitting) {
io.emit('broadcast', JSON.stringify({
metadata: {
version: 1,
type: 'broadcast-shutdown',
from: serverId,
to: ''
},
payload: {
message: 'Server is shutting down...'
}
}))
mixpanel.track('quitting', {uuid: uuid})
log.info('Quitting Zephyr v' + app.getVersion())
log.info('------------------------------------')
return
}
event.preventDefault()
const options = {
type: 'question',
title: 'Confirm Quit',
message: 'Are you sure you want to quit?',
detail: 'You won\'t receive notifications in VR until you restart Zephyr.',
buttons: ['Yes', 'No']
}
electron.dialog.showMessageBox(options, function (index) {
if (index === 0) {
mixpanel.track('event-quit-confirm-yes')
quitting = true
app.quit()
} else {
mixpanel.track('event-quit-confirm-no')
}
})
})
// In this file you can include the rest of your app's specific main process
// code. You can also put them in separate files and require them here.
function startServer() {
web.use('/res', require('express').static(__dirname + '/res'))
web.get('/login', function(req, res) {
require('dns').resolve('www.google.com', function(err) {
if (err) {
res.sendFile(__dirname + '/offline.html')
} else {
res.sendFile(__dirname + '/login.html')
}
})
})
web.post('/api/notification', function(req, res) {
res.setHeader('Content-Type', 'application/json')
res.send(JSON.stringify(handleNotification(JSON.stringify(req.body))))
mixpanel.track('api-http-notification', req.body.payload)
})
web.get('/api/version', function(req, res) {
res.setHeader('Content-Type', 'application/json')
res.send(JSON.stringify(handleVersionRequest(JSON.stringify(req.body))))
req.body.payload.uuid = uuid
mixpanel.track('api-http-version', req.body.payload)
})
web.get('*', function(req, res) {
res.status(404).sendFile(__dirname + '/404.html')
mixpanel.track('api-http-404', {uuid: uuid})
})
io.on('connection', function(socket) {
socket.on('notification', function(msg) {
var n = JSON.parse(msg)
io.emit(n.metadata.from, JSON.stringify(handleNotification(msg)))
n.payload.uuid = uuid
mixpanel.track('api-ws-notification', n.payload)
})
socket.on('version', function(msg) {
var v = JSON.parse(msg)
io.emit(v.metadata.from, JSON.stringify(handleVersionRequest(msg)))
broadcastPing()
v.payload.uuid = uuid
mixpanel.track('api-ws-version', v.payload)
})
socket.on('broadcast', function(msg) {
var b = JSON.parse(msg)
if (b.metadata.type == 'broadcast-pong') {
log.info('Received response from ' + b.metadata.from)
io.emit('devices', JSON.stringify({
metadata: {
version: 1,
type: 'devices-add',
from: serverId,
to: ''
},
payload: {
name: b.metadata.from
}
}))
mixpanel.track('api-ws-pong', {
uuid: uuid,
from: b.metadata.from
})
}
})
socket.on('disconnect', function() {
if (!quitting) {
log.info('Client disconnected, checking on other clients...')
mixpanel.track('api-ws-disconnect', {uuid: uuid})
broadcastPing()
}
})
})
http.listen(3753, function() {
log.info('Listening on *:3753')
})
}
function startOverlay() {
if(process.platform == 'win32') {
const execFile = require('child_process').execFile
const child = execFile('./build/overlay/zephyr.exe', (error, stdout, stderr) => {
log.info('Overlay no longer running!')
log.info('Overlay error: ' + error)
log.info('Overlay stdout: ' + stdout)
log.info('Overlay stderr: ' + stderr)
broadcastOverlayNotRunning(error)
mixpanel.track('overlay-error', {
uuid: uuid,
error: error
})
})
} else {
log.info('Overlay not running! (requires win32)')
broadcastOverlayNotRunning('Windows is required.')
mixpanel.track('overlay-error-win32', {
uuid: uuid,
platform: process.platform
})
}
}
function broadcastOverlayNotRunning(error) {
io.emit('broadcast', JSON.stringify({
metadata: {
version: 1,
type: 'broadcast-overlay-not-running',
from: serverId,
to: ''
},
payload: {
message: 'Overlay is no longer running.',
error: error
}
}))
}
function broadcastPing() {
io.emit('broadcast', JSON.stringify({
metadata: {
version: 1,
type: 'broadcast-ping',
from: serverId,
to: ''
},
payload: {}
}))
io.emit('devices', JSON.stringify({
metadata: {
version: 1,
type: 'devices-clear',
from: serverId,
to: ''
},
payload: {}
}))
}
function handleNotification(msg) {
log.info('Notification: ' + msg)
io.emit('notification', msg)
var n = JSON.parse(msg)
return {
metadata: {
version: 1,
type: 'notification-response',
from: serverId,
to: n.metadata.from
},
payload: {
result: true,
resultCode: 0,
resultMessage: 'Success',
id: n.payload.id
}
}
}
function handleVersionRequest(msg) {
log.info('Client requested version info, sending...')
log.info('Client info: ' + msg)
var v = JSON.parse(msg)
var t = v.metadata && v.metadata.from ? v.metadata.from : ''
return {
metadata: {
version: 1,
type: 'version',
from: serverId,
to: t
},
payload: {
name: 'Zephyr Server',
version: app.getVersion(),
versionCode: 1,
versions: [
{
name: 'platform',
value: process.platform
},
{
name: 'node',
value: process.version
}
]
}
}
}
function setupAutoUpdater() {
if(process.platform == 'win32') {
autoUpdater.addListener('update-available', function(event) {
log.info('Update available!')
io.emit('updates', JSON.stringify({
metadata: {
version: 1,
type: 'update-available',
from: serverId,
to: ''
},
payload: {}
}))
mixpanel.track('update-available', {uuid: uuid})
})
autoUpdater.addListener('update-downloaded', function(event, releaseNotes, releaseName, releaseDate, updateURL) {
log.info('Update downloaded!')
io.emit('updates', JSON.stringify({
metadata: {
version: 1,
type: 'update-downloaded',
from: serverId,
to: ''
},
payload: {
name: releaseName,
date: releaseDate,
changelog: releaseNotes
}
}))
mixpanel.track('update-downloaded', {
uuid: uuid,
releaseName: releaseName
})
})
autoUpdater.addListener('error', function(err) {
log.info('Error while checking for updates: ' + err)
io.emit('updates', JSON.stringify({
metadata: {
version: 1,
type: 'update-error',
from: serverId,
to: ''
},
payload: {
error: err
}
}))
mixpanel.track('update-error', {
uuid: uuid,
error: err
})
})
autoUpdater.addListener('checking-for-update', function(event) {
log.info('Checking for update...')
io.emit('updates', JSON.stringify({
metadata: {
version: 1,
type: 'update-checking',
from: serverId,
to: ''
},
payload: {}
}))
mixpanel.track('update-checking', {uuid: uuid})
})
autoUpdater.addListener('update-not-available', function(event) {
log.info('Update not available!')
io.emit('updates', JSON.stringify({
metadata: {
version: 1,
type: 'update-not-available',
from: serverId,
to: ''
},
payload: {}
}))
mixpanel.track('update-not-available', {uuid, uuid})
})
const feedURL = 'https://zephyr-updates.herokuapp.com/update/' + process.platform + '/' + app.getVersion()
log.info('Checking for updates at ' + feedURL + '...')
autoUpdater.setFeedURL(feedURL)
autoUpdater.checkForUpdates()
}
}