-
Notifications
You must be signed in to change notification settings - Fork 0
/
init.js
161 lines (125 loc) · 3.75 KB
/
init.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
const { app, BrowserWindow } = require('electron');
const { NotifyManager } = require('notify-manager-electron');
const { Client } = require('qurre-socket');
const Setuper = require('./modules/Setuper');
const ProxyManager = require('./modules/ProxyManager');
const Extensions = require('./modules/Extensions');
const tpu = require('./modules/TCPPortUsing');
let win;
const dev = false;
const AppPort = dev ? 3535 : 45828;
app.on('window-all-closed', () => {
if (process.platform !== 'darwin') {
app.quit();
}
});
app.on('web-contents-created', (ev, contents) => {
try { console.log('window created: ' + contents.getType()); } catch { }
setTimeout(() => {
const interval = setInterval(() => {
const value = enableIdle();
if (value === 0) {
clearInterval(interval);
}
}, 10000);
enableIdle();
}, 1000);
Setuper.hookNewWindow(contents);
Setuper.cors(contents.session);
if (dev) {
contents.openDevTools({ mode: 'detach' });
}
function enableIdle() {
if (contents.isDestroyed()) {
return 0;
}
const pid = contents.getOSProcessId();
if (pid === 0) {
return 1;
}
if (contents.getType() === 'webview' && Setuper.getIsPlaying()) {
Extensions.setEfficiency(pid, false);
return 1;
}
if (contents.getType() === 'window' && Setuper.getisActive()) {
Extensions.setEfficiency(pid, false);
return 1;
}
Extensions.setEfficiency(pid);
return 1;
}
});
app.whenReady().then(() => {
startup();
app.on('activate', () => {
if (BrowserWindow.getAllWindows().length === 0) {
startup();
}
});
});
async function startup() {
let _portUse = await PortUsing();
if (_portUse) {
setTimeout(() => app.quit(), 1000);
return;
}
if (Setuper.getCloseAll()) {
setTimeout(() => app.quit(), 1000);
return;
}
app.configureHostResolver({
secureDnsMode: 'secure',
secureDnsServers: [
'https://dns.quad9.net/dns-query',
'https://dns9.quad9.net/dns-query',
'https://cloudflare-dns.com/dns-query'
]
});
const loaderWin = await Setuper.loaderWin();
const nmanager = new NotifyManager();
await Setuper.autoUpdate();
await ProxyManager.Init(nmanager);
setTimeout(() => { try { nmanager.getWindow().destroy() } catch { } }, 15000);
Setuper.setupTasks();
Extensions.protocolInject();
win = Setuper.create();
win.once('ready-to-show', () => {
setTimeout(() => {
win.show();
try { loaderWin.close(); } catch { }
}, 1000); // safe eyes from blink by chromium
});
win.on('close', (e) => {
e.preventDefault();
win?.hide();
});
require('./modules/startupMenu')(win);
Setuper.cors(win.webContents.session);
Setuper.binds(win);
_portUse = await PortUsing();
if (_portUse) {
setTimeout(() => app.quit(), 1000);
return;
}
require('./modules/Server')(AppPort, win);
await win.loadFile(__dirname + '/frontend/main.html');
win.send('load-url', await Setuper.getStartUrl());
Extensions.sleeper.enable();
}
async function PortUsing() {
const _portUse = await tpu(AppPort, '127.0.0.1');
if (!_portUse) {
return false;
}
setTimeout(() => app.quit(), 1000);
const _client = new Client(AppPort);
_client.emit('OpenApp');
const url = Setuper.getStartArgsUrl();
if (url.length > 1) {
_client.emit('SetUrl', url);
}
if (Setuper.getCloseAll()) {
_client.emit('CloseAll');
}
return true;
}