-
Notifications
You must be signed in to change notification settings - Fork 6
/
main.js
245 lines (200 loc) · 5.79 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
const electron = require('electron');
const { ipcMain, app, protocol, globalShortcut, BrowserWindow } = electron;
const path = require('path');
const tar = require('tar');
const fs = require('fs');
const os = require('os');
const rimraf = require('rimraf');
const server = require('./server/server');
const serverUtil = require('./server/util');
const util = require('./util');
const name = 'Console Lite';
app.setName(name);
console.log(`userData locates at ${app.getPath('userData')}`);
serverUtil.setDataDir(app.getPath('userData'));
// Windows, not the OS, but windows
let controller;
let projector;
let serverStarted = false;
const controllerOpt = {
width: 800,
height: 600,
frame: false,
background: '#FFF',
icon: path.join(__dirname, 'images', 'icon_256x256.png'),
};
const projectorOpt = {
width: 800,
height: 600,
frame: false,
autoHideMenuBar: true,
icon: path.join(__dirname, 'images', 'icon_256x256.png'),
};
if(util.supportsTitlebarStyle()) {
controllerOpt.frame = true;
projectorOpt.frame = true;
controllerOpt.titleBarStyle = 'hidden';
projectorOpt.titleBarStyle = 'hidden';
} else if(util.isWindows()) {
controllerOpt.frame = true;
projectorOpt.frame = true;
}
function initController() {
controller = new BrowserWindow(controllerOpt);
util.applyControllerMenu(controller);
controller.loadURL(`file://${__dirname}/controller/index.html`);
controller.on('closed', () => {
controller = null;
/* Close projector as well */
if(projector) projector.close();
});
}
function initProjector() {
// Ensures that previous windows are closed
if(projector) projector.close();
projector = new BrowserWindow(projectorOpt);
projector.loadURL(`file://${__dirname}/projector/index.html`);
util.applyProjectorMenu(projector);
projector.on('closed', () => {
projector = null;
if(controller) controller.webContents.send('projectorClosed');
});
}
function createExportStream() {
if(serverStarted) throw new Error('Server is running');
const dir = serverUtil.storagePath();
return tar.c({
C: dir,
prefix: 'storage',
}, fs.readdirSync(dir));
}
function setupExportHandler() {
protocol.registerBufferProtocol('clexport', (request, callback) => {
if(serverStarted) return void callback({ error: 'Server is running' });
const buffers = [];
createExportStream()
.on('data', data => {
buffers.push(data);
})
.on('end', () => {
callback(Buffer.concat(buffers));
})
.on('error', err => callback({ error: err }));
});
}
app.on('ready', () => {
setupExportHandler();
initController();
globalShortcut.register('CommandOrControl+\\', () => {
if(controller) controller.focus();
else initController();
});
globalShortcut.register('CommandOrControl+Shift+|', () => {
if(projector) projector.focus();
else initProjector();
});
});
app.on('window-all-closed', () => {
if(process.platform !== 'darwin')
app.quit();
});
app.on('activate', () => {
if(!controller)
initController();
});
let passkey;
let idkey;
let backendPort = 4928;
let shutdown;
/**
* Get a random external IPv4 address
*/
function getExtIPv4Addr() {
const ifaces = os.networkInterfaces();
// This is an plain object. Prototype properties shouldn't be a problem
// eslint-disable-next-line guard-for-in
for(const ifname in ifaces)
for(const alias of ifaces[ifname])
if(alias.family === 'IPv4' && !alias.internal)
return alias.address;
// No external address. So no host other than localhost can connect to this server.
// Hence loopback is safe to be returned here.
return '127.0.0.1';
}
ipcMain.on('startServer', (event, opts) => {
if(serverStarted) {
event.sender.send(
'serverCallback',
{ url: `http://${getExtIPv4Addr()}:${backendPort}`, passkey, idkey },
);
return;
}
server((err, pk, ik, port, sd) => {
if(err) {
console.error(err);
event.sender.send('serverCallback', { error: err });
return;
}
serverStarted = true;
passkey = pk;
idkey = ik;
shutdown = sd;
backendPort = port;
event.sender.send(
'serverCallback',
{ url: `http://${getExtIPv4Addr()}:${port}`, passkey, idkey },
);
}, opts);
});
ipcMain.on('isServerRunning', event => {
event.returnValue = serverStarted;
});
ipcMain.on('openProjector', () => {
initProjector();
});
ipcMain.on('closeProjector', () => {
if(projector) projector.close();
});
ipcMain.on('toProjector', (event, data) => {
if(projector) projector.webContents.send('fromController', data);
});
ipcMain.on('getProjector', event => {
if(!projector) event.returnValue = null;
else event.returnValue = projector.id;
});
ipcMain.on('projectorInitialized', () => {
if(controller) controller.webContents.send('projectorReady');
});
ipcMain.on('checkForUpdate', ev => {
util.checkForUpdate().then(([data, ver]) => {
if(!data) return;
ev.sender.send('updateAvailable', { detail: data, version: `v${ver[0]}.${ver[1]}.${ver[2]}` });
}).catch(e => console.error(e.stack));
});
ipcMain.on('doImport', (ev, data) => {
const targetDir = serverUtil.storagePath();
rimraf(path.join(targetDir, '*'), err => {
if(err) return void ev.sender.send('importCb', err);
fs.createReadStream(data)
.pipe(tar.x({
C: targetDir,
strip: 1,
}))
.on('end', () => ev.sender.send('importCb', null))
.on('error', err => ev.sender.send('importCb', err));
});
});
ipcMain.on('directExport', (ev, dir) => {
const f = fs.createWriteStream(dir);
createExportStream().pipe(f)
.on('finish', () => {
f.close(() => ev.sender.send('exportCb', null));
})
.on('error', e => {
fs.unlinkSync(dir);
ev.sender.send('exportCb', e);
});
});
app.on('quit', () => {
if(serverStarted) shutdown(e => console.error(e));
});