-
-
Notifications
You must be signed in to change notification settings - Fork 24
/
main.js
executable file
·355 lines (291 loc) · 7.96 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
// Inspired by https://github.com/pbarbiero/basic-electron-react-boilerplate
// TODO: Look into https://github.com/electron-userland/electron-builder
'use strict';
// Import parts of electron to use
const { app, BrowserWindow, Menu, Tray, clipboard } = require('electron');
var shouldQuit = app.makeSingleInstance(function(commandLine, workingDirectory) {
server.passArgs(commandLine)
})
if (shouldQuit) {
app.quit()
return
}
const path = require('path');
const url = require('url');
const process = require('process');
const MenuBuilder = require('./menu');
const getPort = require('get-port')
const AutoLaunch = require('auto-launch');
const autoLauncher = new AutoLaunch({
name: 'Powder Web'
})
const server = require('./server')
const streams = require('./server/streams')
const acestream = require('./server/acestream')
const sop = require('./server/sopcast')
const btoa = require('./server/utils/btoa')
const events = require('./server/utils/events')
const qrCode = require('./server/utils/qrcode')
const opn = require('opn')
if (app && app.commandLine && app.commandLine.appendSwitch)
app.commandLine.appendSwitch("ignore-certificate-errors")
// 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;
if (app.dock)
app.dock.hide()
// Keep a reference for dev mode
let dev = false;
if (process.env.NODE_ENV === 'development') {
dev = true;
require('dotenv').config({ silent: true });
}
function createServer() {
let frontPort
let backPort
let maybePort = 11485
var getPorts = function(cb) {
var fail = function() {
maybePort++
getPorts(cb)
}
getPort({ port: maybePort }).then(function(newPort) {
if (newPort == maybePort) {
cb(newPort)
} else
fail()
}).catch(function(e) {
fail()
})
}
getPorts(function(newPort) {
frontPort = newPort
maybePort = newPort -1
getPorts(function(newPort) {
backPort = newPort
server.init(frontPort, backPort)
setTimeout(createWindow)
})
})
}
const quit = () => {
// if (mainWindow)
// mainWindow.destroy()
if (mainWindow.isVisible())
mainWindow.hide()
tray.destroy()
var ticks = 3
const tick = () => {
ticks--
if (!ticks) {
app.quit()
}
}
sop.unhackSopPlayer(() => {
sop.kill(tick, tick)
})
streams.closeAll(tick)
acestream.binary.kill(tick, tick)
}
function createWindow() {
// Create the browser window.
mainWindow = new BrowserWindow({
width: 1268,
height: 768,
show: false,
webPreferences: {
nodeIntegration: false,
allowpopups: true,
webSecurity: false,
nativeWindowOpen: true,
},
resizable: true,
title: 'Powder Web',
center: true,
frame: false
});
// Disable top menu bar on Windows/Linux
mainWindow.setMenu(null);
// and load the index.html of the app.
let indexPath;
mainWindow.on('close', (e) => {
console.log('window-close')
// if (process.platform == 'linux') {
// quit()
// return
// }
e.preventDefault()
mainWindow.hide()
if (app.dock)
app.dock.hide()
});
server.setMainWindow(mainWindow)
// Build app menu
const menuBuilder = new MenuBuilder(mainWindow);
menuBuilder.buildMenu();
}
// 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.
let tray = false
app.on('ready', () => {
// Allow electron to serve static files
if (!dev) {
require('electron').protocol.interceptFileProtocol('file', (request, callback) => {
const url = request.url.substr(7) /* all urls start with 'file://' */
callback({ path: path.normalize(`${__dirname}/dist/${url}`)})
}, (err) => {
if (err) console.error('Failed to register protocol')
})
}
// Create the Browser window
if (dev) {
const {
default: installExtension,
REACT_DEVELOPER_TOOLS,
REDUX_DEVTOOLS
} = require('electron-devtools-installer');
installExtension(REACT_DEVELOPER_TOOLS)
.then(() => installExtension(REDUX_DEVTOOLS))
.then(() => createWindow());
} else {
createServer();
}
if (process.platform == 'darwin' || process.platform == 'win32') {
tray = new Tray(path.join(__dirname, 'packaging', 'osx_tray.png'))
} else {
tray = new Tray(path.join(__dirname, 'packaging', 'icons', 'powder-square-border.png'))
}
const showApp = () => {
if (!mainWindow.isVisible()) {
mainWindow.loadURL( 'http' + (server.isSSL ? 's': '') + '://127.0.0.1:' + server.port() + '/auth?token=' + server.masterKey );
mainWindow.show()
mainWindow.focus()
if (app.dock && !app.dock.isVisible())
app.dock.show()
if (dev) {
mainWindow.webContents.openDevTools()
}
} else {
mainWindow.focus()
}
}
const copyEmbedKey = () => {
const servUrl = 'http' + (server.isSSL ? 's': '') + '://127.0.0.1:' + server.port() + '/'
const embedKey = server.embedKey
clipboard.writeText(embedKey + '-' + btoa(servUrl));
}
const copyLink = (qrType) => {
qrCode(qrType, server.argsKey, server.port(), (resp) => {
if (resp && resp.url)
clipboard.writeText(resp.url);
}, (err) => {
})
}
const copyLanLink = () => { copyLink('local') }
const copyInternetLink = () => { copyLink('internet') }
const showBrowser = () => {
opn('http' + (server.isSSL ? 's': '') + '://127.0.0.1:' + server.port() + '/auth?token=' + server.masterKey)
}
const toggleStartUp = () => {
autoLauncher.isEnabled()
.then(function(isEnabled){
if(isEnabled){
autoLauncher.disable()
} else {
autoLauncher.enable()
}
})
.catch(function(err){ })
}
const relaunch = () => {
// mainWindow.destroy()
// streams.closeAll(() => {
app.relaunch()
quit()
// })
}
events.on('appQuit', quit)
events.on('appRelaunch', relaunch)
events.on('appShow', showApp)
events.on('appShowBrowser', showBrowser)
const buildContextMenu = (startUp) => {
const contextMenu = Menu.buildFromTemplate([
{
label: 'Show App',
type: 'normal',
click: showApp
},
{
label: 'Show in Browser',
type: 'normal',
click: showBrowser
},
{ type: 'separator' },
{
label: 'Copy LAN Link',
type: 'normal',
click: copyLanLink
},
{
label: 'Copy Internet Link',
type: 'normal',
click: copyInternetLink
},
{ type: 'separator' },
{
label: 'Copy Embed Key',
type: 'normal',
click: copyEmbedKey
},
{ type: 'separator' },
{
label: 'Run on Start-Up',
type: 'checkbox',
checked: startUp,
click: toggleStartUp
},
{ type: 'separator' },
{
label: 'Restart',
type: 'normal',
click: relaunch
},
{
label: 'Quit',
type: 'normal',
click: quit
}
])
tray.setContextMenu(contextMenu)
}
tray.on('click', showApp)
tray.setToolTip('Powder Web')
autoLauncher.isEnabled()
.then(function(isEnabled){
buildContextMenu(isEnabled)
}).catch(function(err){
buildContextMenu(false)
})
});
//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('window-all-closed', app.quit);
app.on('activate', () => {
// 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 (mainWindow === null) {
createWindow();
}
});
app.on('before-quit', () => {
if (!mainWindow) return
mainWindow.removeAllListeners('close');
mainWindow.destroy();
});