-
Notifications
You must be signed in to change notification settings - Fork 2
/
main.js
384 lines (323 loc) · 9.92 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
const remoteMain = require('@electron/remote/main');
// remoteMain.enable(window.webContents)
remoteMain.initialize();
// Modules to control application life and create native browser window
const path = require('path')
var fp = require("find-free-port")
const {app, BrowserWindow,Tray,Menu,session, protocol,ipcMain, clipboard, shell, Notification, dialog} = require('electron')
const fs = require('fs');
const cp = require('child_process');
const origFs = require('original-fs');
const fixPath = require('fix-path');
const log = require('electron-log');
const { autoUpdater } = require("electron-updater");
fixPath();
const {fstat} = require('./lib/fs');
const menuTemplate = require('./menu')
const HOST = process.env.GSHOST || 'gitspeak.com';
const DEBUG = process.env.DEBUG;
// const AFFINITY = DEBUG ? 'debug' : 'myAffinity';
// const PARTITION = DEBUG ? 'persist:debug' : 'persist:main';
const PARTITION = 'persist:' + HOST;
const AFFINITY = 'affinity:' + HOST;
// process.noAsar = true;
const gotTheLock = app.requestSingleInstanceLock();
// 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 main
let splash
let tunnel
let initialUrl = process.env.INITIAL_URL || '/';
let state = {
tunnelPort: null
};
var logQueue = [];
// Start tunnel in separate process to avoid blocking main thread.
// Pages will communicate with this via local websocket server
// Auto updating stuff
autoUpdater.logger = log;
autoUpdater.logger.transports.file.level = 'info';
log.info('App starting...');
autoUpdater.on('checking-for-update', () => {
devToolsLog('Checking for update...');
})
autoUpdater.on('update-available', (info) => {
devToolsLog('Update available.');
})
autoUpdater.on('update-not-available', (info) => {
devToolsLog('Update not available.');
})
autoUpdater.on('error', (err) => {
devToolsLog('Error in auto-updater. ' + err);
})
autoUpdater.on('download-progress', (progressObj) => {
let log_message = "Download speed: " + progressObj.bytesPerSecond;
log_message = log_message + ' - Downloaded ' + progressObj.percent + '%';
log_message = log_message + ' (' + progressObj.transferred + "/" + progressObj.total + ')';
devToolsLog(log_message);
})
autoUpdater.on('update-downloaded', (info) => {
devToolsLog('Update downloaded');
dialog.showMessageBox({
type: 'info',
title: 'Update downloaded',
message: "We've downloaded a new version of GitSpeak for you, do you want to relaunch the app?",
buttons: ['Sure', 'No']
}, (buttonIndex) => {
if (buttonIndex === 0) {
if(main) main.forceClose = true;
autoUpdater.quitAndInstall();
}
else {
devToolsLog('Do not quit and install');
}
})
});
function rpc(name,...args){
var doc = main.webContents;
var res = doc.send('message',{type: 'rpc', data: [name,args]});
}
function send(name,args){
if(main && main.webContents) main.webContents.send('message',{type: name, data: args});
}
async function setupTunnel(){
state.ports = await fp(48000, 49000, '127.0.0.1', 1);
state.tunnelPort = state.ports[0];
process.env.TUNNEL_PORT = state.tunnelPort;
let env = {
PATH: process.env.PATH,
TUNNEL_PORT: state.tunnelPort
};
['HOME','TMPDIR','USER'].forEach(function(k){
if(process.env[k]) env[k] = process.env[k];
})
tunnel = cp.fork('./lib/wss', [], {
env: env,
cwd: __dirname,
silent: true
})
tunnel.stdout.on('data', (data) => {
devToolsLog(String(data));
});
tunnel.stderr.on('data', (data) => {
devToolsLog(String("error from tunnel: " + String(data)));
})
}
function openIDE(params){
console.log('open ide',params);
params.port = state.tunnelPort;
main.webContents.send('message',{type: 'openSession', data: params});
}
function devToolsLog(s) {
log.info(s);
if (main && main.webContents && DEBUG) {
main.webContents.send('message',{type: 'log', data: s});
} else {
logQueue.push(s);
}
}
var url_scheme = "gitspeak";
protocol.registerSchemesAsPrivileged([
{ scheme: url_scheme }
])
app.setAsDefaultProtocolClient(url_scheme);
async function setupApplication () {
let opts = {
width: 420,
height: 280,
title: "GitSpeak",
titleBarStyle: 'hidden',
hasShadow: true,
vibrancy: null,
center: true,
movable: false,
resizable: false,
minimizable: false,
maximizable: false,
closable: false,
alwaysOnTop: true,
show: false
}
// Create loading-screen that will show until we have loaded GitSpeak
splash = new BrowserWindow(Object.assign({
autoHideMenuBar: true
},opts));
splash.once('ready-to-show', function(event, url) {
splash.show();
return this;
});
splash.loadURL(`file://${__dirname}/splash.html`);
await setupTunnel();
// Create the browser window.
main = new BrowserWindow({
width: 1280,
height: 900,
title: "GitSpeak",
titleBarStyle: 'hidden',
vibrancy: null,
show: false,
webPreferences: {
partition: PARTITION,
preload: path.join(__dirname, 'preload.js'),
nodeIntegration: false,
contextIsolation: false,
nativeWindowOpen: true,
allowRunningInsecureContent: false,
backgroundThrottling: false
}
})
var menu = Menu.buildFromTemplate(menuTemplate.template);
Menu.setApplicationMenu(menu);
main.loadURL("https://" + HOST + initialUrl);
devToolsLog(logQueue);
main.setAutoHideMenuBar(true);
main.on('show',function(event){
if(splash){
splash.hide();
splash.destroy();
splash = null;
}
});
var doc = main.webContents;
remoteMain.enable(doc);
doc.on('will-navigate', function(event, url) {
console.log("will-navigate somewhere?!?",url);
event.preventDefault();
shell.openExternal(url);
return this;
});
doc.on('new-window', (event, url, frameName, disposition, options, additionalFeatures) => {
console.log('new-window',frameName);
shell.openExternal(url);
return;
var outerPos = main.getPosition();
var outerSize = main.getSize();
var defaults = {}
if(frameName == 'ghlogin'){
shell.openExternal(url);
// Always open externally now
return;
}
if (true) {
event.preventDefault()
var frameDefaults = defaults[frameName] || {};
Object.assign(options, {
titleBarStyle: 'default',
modal: false,
parent: main,
width: 1020,
height: 790,
resizable: false
},frameDefaults);
// center over parent window
options.x = outerPos[0] + Math.round((outerSize[0] - options.width) * 0.5);
options.y = outerPos[1] + Math.round((outerSize[1] - options.height) * 0.5);
options.webPreferences = Object.assign({
preload: path.join(__dirname, 'extwindow.js'),
partition: PARTITION,
affinity: AFFINITY
},
options.webPreferences,
frameDefaults.webPreferences || {}
);
event.newGuest = new BrowserWindow(options)
}
})
if (process.platform === 'darwin') {
// when user tries to close main window on mac
// we simply hide it instead, to be able to quicky
// show it again when app is reactivated.
main.on('close', (e) => {
// if main window is closing because the user
// is quitting the app, we don't want to preventDefault
if(main.forceClose) return;
e.preventDefault();
main.hide();
})
}
// Emitted when the window is closed.
main.on('closed', function () {
main = null;
})
}
ipcMain.on("client", function(event, arg) {
console.log("ipcmain app",arg);
if(arg == 'focus'){
app.focus();
}
});
ipcMain.on("app_release", function(event, arg) {
autoUpdater.checkForUpdatesAndNotify();
})
ipcMain.on("state.get", function(event, arg) {
console.log('state.get',arg);
event.returnValue = state[arg];
})
ipcMain.on("fstat", function(event, dir) {
console.log('fstat',dir);
event.returnValue = fstat(dir);
})
function setupRequestInterceptor(){
// try to intercept http requests
const filter = {
urls: ['https://sindre.gitspeak.com:8443/*', '*://electron.github.io']
}
main.webContents.session.webRequest.onBeforeSendHeaders(filter, (details, callback) => {
console.log('intercept webRequest!!');
// details.requestHeaders['User-Agent'] = 'MyAgent'
callback({cancel: false, requestHeaders: details.requestHeaders})
})
}
// 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', setupApplication)
// app.on('ready', setupRequestInterceptor)
app.on('ready', () => {
autoUpdater.checkForUpdatesAndNotify();
})
app.on('open-url', (event,url) => {
if(main && main.webContents){
console.log("trying to open url through application! " + url);
send('openUrl',url);
} else {
initialUrl = url.slice(10);
}
})
app.on('before-quit-for-update', () => {
if(main) main.forceClose = true;
});
app.on('before-quit', () => {
if(main) main.forceClose = true;
tunnel.send({type: 'kill'});
tunnel.kill('SIGINT')
});
app.on('will-quit', () => {
console.log('will-quit')
});
app.on('quit', () => {
console.log('quit')
});
// Quit when all windows are closed.
app.on('window-all-closed', function () {
console.log('window-all-closed triggered')
// 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('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 (main == null) {
// should not be possible(!)
// createWindow()
} else {
console.log("showing main window");
main.show();
app.focus();
}
})
// 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.