-
Notifications
You must be signed in to change notification settings - Fork 17
/
Copy pathmain.js
494 lines (427 loc) · 13.2 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
const { app, BrowserWindow, ipcMain } = require('electron')
const {clipboard} = require('electron')
const path = require('path')
const URL = require('url').URL
const { initConfig, saveConfig } = require('./src/config')
let ioHook = null
try {
ioHook = require('iohook')
} catch(e) {
console.log(e)
ioHook = false
}
'use strict';
let mainWindow
let logWindow
let settingsWindow
let devMode = false
let selfMute = false
let webViewSession = null
//let isTalking = false
let muteTimeout = null
let configObj
let micPermissionGranted = false
let isChangingPTTKey = false
let pttEnable = 'mousedown' // init to mousedown/up
let pttDisable = 'mouseup'
let pttWatch = 'button'
// Set Dev mode
if (process.argv.length === 3) {
if (process.argv[2] === 'dev'){
devMode = true
}
}
function unmuteMic() {
//if ( selfMute === false){
//isTalking = true
console.log("Talking")
mainWindow.webContents.send('micOpen', 'mic-open')
mainWindow.setTitle("MIC OPEN")
//}
}
function muteMic() {
console.log("Not Talking")
mainWindow.webContents.send('micClose', 'mic-closed')
mainWindow.setTitle("MIC CLOSED")
}
function createMainWindow () {
// Create the browser window.
mainWindow = new BrowserWindow({
width: 1230,
height: 800,
webPreferences: {
preload: path.join(__dirname, 'src/mainLoad.js'),
partition: 'persist:discord',
nodeIntegration: false, // https://electronjs.org/docs/tutorial/security#2-do-not-enable-nodejs-integration-for-remote-content
enableRemoteModule: false, // https://electronjs.org/docs/tutorial/security#15-disable-the-remote-module
webviewTag: true,
sandbox: true,
nodeIntegrationInSubFrames: false,
webSecurity: true,
allowRunningInsecureContent: false,
plugins: false,
experimentalFeatures: false
},
frame: false
})
mainWindow.loadFile('./views/index.html')
mainWindow.setTitle("Discord Sandboxed")
mainWindow.on('closed', function () {
mainWindow = null
})
}
function createLogWindow() {
logWindow = new BrowserWindow({
width: 700,
height: 400,
resizable: false,
webPreferences: {
preload: path.join(__dirname, 'src/logLoad.js'),
nodeIntegration: false, // https://electronjs.org/docs/tutorial/security#2-do-not-enable-nodejs-integration-for-remote-content
enableRemoteModule: false, // https://electronjs.org/docs/tutorial/security#15-disable-the-remote-module
webviewTag: true,
sandbox: true,
nodeIntegrationInSubFrames: false,
webSecurity: true,
allowRunningInsecureContent: false,
plugins: false,
experimentalFeatures: false
},
frame: false
})
logWindow.loadFile('./views/log.html')
logWindow.setTitle("Logs")
logWindow.on('closed', function () {
logWindow = null
})
}
function createSettingsWindow() {
settingsWindow = new BrowserWindow({
width: 700,
height: 400,
show: true,
resizable: false,
alwaysOnTop:true,
webPreferences: {
preload: path.join(__dirname, 'src/settingsLoad.js'),
nodeIntegration: false,
enableRemoteModule: false,
},
frame: false
})
settingsWindow.loadFile('./views/settings.html')
settingsWindow.setTitle("Settings")
settingsWindow.on('closed', function () {
isChangingPTTKey = false
settingsWindow = null
})
}
function maximizeMinimizeState(windowName){
if (windowName.isMaximized()) {
windowName.unmaximize()
} else {
windowName.maximize()
}
}
function restartioHook() {
if (ioHook) {
console.log("restarting io Hook")
return new Promise((resolve, reject) => {
return new Promise((resolve, reject) => {
ioHook.removeAllListeners('mousedown', () => {})
ioHook.removeAllListeners('mouseup', () => {})
ioHook.removeAllListeners('keydown', () => {})
ioHook.removeAllListeners('keyup', () => {})
ioHook.unload()
console.log("ioHook stopped")
return resolve(true)
}).then (v => {
return new Promise((resolve, reject) => {
ioHook.load()
console.log("ioHook started")
return resolve(true)
}).then(v => {
ioHook.start()
return resolve(true)
})
})
})
}
}
function setPTTKey() {
if (ioHook && configObj.pttDevice && configObj.pttDevice) {
console.log("Set PTT Key")
if (configObj.pttDevice === 'mouse'){
pttEnable = 'mousedown'
pttDisable = 'mouseup'
pttWatch = 'button'
}else if (configObj.pttDevice === 'keyboard'){
pttEnable = 'keydown'
pttDisable = 'keyup'
pttWatch = 'keycode'
}else {
console.log("ERROR: configObj did not set PTT device to mouse or keyboard.")
}
ioHook.on(pttEnable, event => {
if (event[pttWatch] == configObj.key && (micPermissionGranted === true) && (isChangingPTTKey === false)) {
clearTimeout(muteTimeout)
unmuteMic()
}
})
ioHook.on(pttDisable, event => {
if (event[pttWatch] == configObj.key) {
console.log("PTT pushed down")
//if (isTalking === true) {
//isTalking = false
muteTimeout = setTimeout(() => muteMic(), configObj.delay)
//}
}
})
}else {
console.log("Not listening for keypresses. ioHook library error or PTT keys not set.")
}
}
app.on('ready', createMainWindow)
// Quit when all windows are closed.
app.on('window-all-closed', function () {
if (process.platform !== 'darwin') app.quit()
})
app.on('activate', function () {
if (mainWindow === null) createMainWindow()
})
// Force single instance
let isSingleInstance = app.requestSingleInstanceLock()
if (!isSingleInstance) {
app.quit()
}
// Force focus on single instance
app.on('second-instance', (event, argv, cwd) => {
if (mainWindow) {
if (mainWindow.isMinimized()) mainWindow.restore()
mainWindow.focus()
}
if (logWindow) {
if (logWindow.isMinimized()) mainWindow.restore()
logWindow.focus()
}
})
/* Security Stuff */
app.on('web-contents-created', (event, contents) => { // https://electronjs.org/docs/tutorial/security#11-verify-webview-options-before-creation
contents.on('will-attach-webview', (event, webPreferences, params) => {
// Strip away preload scripts if unused or verify their location is legitimate
delete webPreferences.preload
delete webPreferences.preloadURL
// Disable Node.js integration
webPreferences.nodeIntegration = false
console.log(`web-contents-created: ${params.src}`)
// Verify discord.com is being loaded
if (!params.src.startsWith('https://discord.com/')) {
event.preventDefault()
}
})
})
app.on('web-contents-created', (event, contents) => { // https://electronjs.org/docs/tutorial/security#12-disable-or-limit-navigation
contents.on('will-navigate', (event, navigationUrl) => { // https://electronjs.org/docs/tutorial/security#13-disable-or-limit-creation-of-new-windows
const parsedUrl = new URL(navigationUrl)
console.log(`will-navigate ${navigationUrl}`)
if (parsedUrl.origin !== 'https://discord.com/') { // Limit navigation to discordapp.com; not really relevant
event.preventDefault()
}
})
contents.on('new-window', async (event, navigationUrl) => {
clipboard.writeText(navigationUrl, 'selection') // I really hope this is safe to do. Could also do a little URL cleaning here to remove trackers
console.log(`URL ${navigationUrl.toString().slice(0, 20)} Copied to Clipboard`)
mainWindow.webContents.send('URLCopied', null)
//event.preventDefault() // Prevents external links from opening
})
})
/* ---- */
app.on ('browser-window-blur', function (event, browserWindow) {
browserWindow.webContents.send('unfocused', null)
})
app.on ('browser-window-focus', function (event, browserWindow) {
browserWindow.webContents.send('focused', null)
})
app.on('ready', () => {
// Handle permission requests
webViewSession = mainWindow.webContents.session
webViewSession.setPermissionRequestHandler((webContents, permission, callback) => { // deny all permissions
const url = webContents.getURL()
if (url.startsWith('https://discord.com/')) {
if (permission === 'media') { // if user is connected to Discord voice then enable microphone
console.log("User connected to Discord VOIP server. Granted permission for microphone")
micPermissionGranted = true
return callback(true)
}
}
console.log("Denied permission: ", permission)
return callback(false)
})
})
ipcMain.on('asynchronous-message', (event, _data) => {
let msg = _data.msg
if (msg === 'connected') {
console.log("User connected to Discord VOIP server")
if (micPermissionGranted === false && selfMute === false){
micPermissionGranted = true
}
}
if (msg === 'self-muted') {
console.log("User self-muted")
webViewSession.setPermissionRequestHandler(null)
selfMute = true
}
if (msg === 'self-unmuted') {
console.log("User self-unmuted")
selfMute = false
}
if (msg === 'DOMready') {
console.log("Discord webview loaded")
mainWindow.webContents.send('devMode', devMode)
}
if (msg === 'confirmMicClose') {
unmuteMic()
//}
}
if (msg === 'blockUpdate') {
if (logWindow){
logWindow.webContents.send('blockUpdate', _data.data)
}
}
if (msg === 'minimizeApplication') {
if (_data.data.wName === 0) {
mainWindow.minimize()
}
if (_data.data.wName === 1) {
logWindow.minimize()
}
if (_data.data.wName === 2) {
settingsWindow.minimize()
}
}
if (msg === 'maximizeApplication') {
if (_data.data.wName === 0) {
maximizeMinimizeState(mainWindow)
}
if (_data.data.wName === 1) {
maximizeMinimizeState(logWindow)
}
if (_data.data.wName === 2) {
maximizeMinimizeState(settingsWindow)
}
}
if (msg === 'closeApplication') {
if (_data.data.wName === 0) {
app.quit()
}
if (_data.data.wName === 1) {
logWindow.close()
}
if (_data.data.wName === 2) {
settingsWindow.close()
}
}
if (msg === 'openLog') {
if (logWindow) {
if (logWindow.isMinimized()) logWindow.restore()
logWindow.focus()
}else {
createLogWindow()
logWindow.center()
}
}
if (msg === 'openSettings') {
if (settingsWindow) {
if (settingsWindow.isMinimized()) settingsWindow.restore()
settingsWindow.focus()
}else {
createSettingsWindow()
settingsWindow.center()
}
}
if (msg === 'SettingsDOMReady') {
if (settingsWindow) {
console.log("SettingsDOMReady. Sending Settings DOM obj")
settingsWindow.webContents.send('settingsObj', configObj)
}
}
if (msg === 'setPTTKey') {
if (settingsWindow) {
if (ioHook) {
isChangingPTTKey = true
console.log("waiting for user to rebind")
if (settingsWindow && isChangingPTTKey) {
restartioHook().then(v => {
mainWindow.blur()
ioHook.once('keydown', event => {
if (settingsWindow && isChangingPTTKey) {
console.log("rebind success")
configObj.pttDevice = 'keyboard'
configObj.key = event.keycode
isChangingPTTKey = false
saveConfig(configObj)
settingsWindow.webContents.send('settingsObj', configObj)
setPTTKey()
}
})
// Ignore using left click (mouse1)
ioHook.once('mousedown', event => {
if (settingsWindow && isChangingPTTKey && event.button !== 1) {
console.log("rebind success")
configObj.pttDevice = 'mouse'
configObj.key = event.button
isChangingPTTKey = false
saveConfig(configObj)
settingsWindow.webContents.send('settingsObj', configObj)
setPTTKey()
}
})
})
}
}
}
}
if (msg === 'cancelSetPTTKey') {
console.log("cancel set new PTT")
isChangingPTTKey = false
saveConfig(configObj)
settingsWindow.webContents.send('settingsObj', configObj)
}
if (msg === 'setPTTDelay') {
console.log(`New PTT Delay: ${_data.data} ms`)
configObj.delay = _data.data
saveConfig(configObj)
settingsWindow.webContents.send('settingsObj', configObj)
}
if (msg === 'disablePTT') {
if (_data.data === false) {
console.log(`PTT Disabled`)
configObj.delay = null
configObj.key = null
configObj.pttDevice = null
saveConfig(configObj)
settingsWindow.webContents.send('settingsObj', configObj)
}
if (_data.data === true) {
console.log(`PTT Enabled`)
configObj.delay = 1000
configObj.key = "none"
configObj.pttDevice = "none"
saveConfig(configObj)
settingsWindow.webContents.send('settingsObj', configObj)
}
}
})
app.on('ready', event => {
console.log(`Dev Mode: ${devMode}`)
initConfig()
.then(value => {
configObj = value
return configObj
})
.then(configObj => {
console.log(configObj)
ioHook.start()
setPTTKey()
})
})