-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.js
282 lines (243 loc) · 9.37 KB
/
app.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
const electron = require("electron");
const path = require("path");
const fs = require("fs-extra");
const childProcess = require("child_process");
const app = electron.app;
const BrowserWindow = electron.BrowserWindow;
const ipc = electron.ipcMain;
const dialog = electron.dialog;
const shell = electron.shell;
const package = require(path.join(__dirname, "/package.json"));
const PwnCheck = require(path.join(__dirname, "/classes/PwnCheck.js"));
const updateCheck = require(path.join(__dirname, "classes/updateCheck.js"));
const language = {
"en": require(path.join(__dirname, "/language/en.json")),
"de-formal": require(path.join(__dirname, "/language/de_formal.json")),
"de-informal": require(path.join(__dirname, "/language/de_informal.json"))
}
var win = {
main: undefined,
startup: undefined
};
var ready, userdata, config, langOptions;
var count = 0;
function checkVersionGreater(base, check, allowEqual = false) {
let baseArr = base.toString().split(".");
let checkArr = check.toString().split(".");
if (baseArr.length !== 3 || checkArr.length !== 3) throw new TypeError("Invalid version style");
if (baseArr[0] < checkArr[0]) return true;
if (baseArr[0] > checkArr[0]) return false;
if (baseArr[1] < checkArr[1]) return true;
if (baseArr[1] > checkArr[1]) return false;
if (baseArr[2] < checkArr[2]) return true;
if (baseArr[2] > checkArr[2]) return false;
if (allowEqual) return true;
else return false;
}
function init() {
win.startup = new BrowserWindow({
show: false,
backgroundColor: "#333333",
width: 500,
height: 300,
center: true,
resizable: false,
movable: false,
closable: false,
fullscreenable: false,
title: "Loading...",
frame: false,
darkTheme: true,
webPreferences: {
nodeIntegration: true,
devTools: false,
contextIsolation: false
},
icon: path.join(__dirname, "/icon.png")
});
win.startup.once("ready-to-show", () => {
win.startup.show();
if (ready) load();
ready = true;
});
ipc.once("ready", (event, _args) => {
win.startup.sender = event.sender;
if (ready) load();
ready = true;
});
win.startup.setStatus = (title, barReset) => {
if (!win.startup.sender) throw new Error("Window is not ready yet");
win.startup.focus();
win.startup.sender.send('setStatus', title);
if (barReset) win.startup.sender.send('setBar', 0);
};
win.startup.setBar = (percent) => {
if (!win.startup.sender) throw new Error("Window is not ready yet");
win.startup.sender.send("setBar", percent);
}
win.startup.setVersion = (version) => {
if (!win.startup.sender) throw new Error("Window is not ready yet");
win.startup.sender.send("setVersion", version);
}
//win.startup.setIgnoreMouseEvents(true);
win.startup.loadURL(`file://${path.join(__dirname, "/views/startup/index.html")}`);
}
async function load() {
win.startup.setVersion(package.version);
win.startup.setStatus(`[00${++count}] Loading config`, true);
userdata = app.getPath('userData');
fs.ensureDirSync(userdata);
const defaultConfig = {
lang: "en",
version: package.version,
changelogShown: true
}
if (!fs.existsSync(path.join(userdata, "/config.json"))) {
fs.writeFileSync(path.join(userdata, "/config.json"), JSON.stringify(defaultConfig));
config = defaultConfig;
} else {
config = require(path.join(userdata, "/config.json"));
}
win.startup.setStatus(`[00${++count}] Checking version`, true);
if (checkVersionGreater(config.version, package.version)) {
win.startup.setStatus(`[00${++count}] Finishing Update to newer version`, true);
if (fs.existsSync(path.join(__dirname, "/update.js"))) {
const Updater = require(path.join(__dirname, "/update.js"));
try {
await Updater(path.join(userdata, "/config.json"), config);
} catch (error) {
fs.writeFileSync(path.join(userdata, "/update-error.log"), error.stack);
dialog.showMessageBoxSync(win.startup, {
type: "error",
buttons: ["Ok"],
title: "Error",
message: "We were unable to update your config to the newest version",
detail: `You can try to delete ${path.join(userdata, "/config.json")}, but this will delete your preferences. Otherwise delete the file, uninstall and reinstall the program and try again. If this isn't helping either, please contact support@astrogd.eu.\n\nAn error log has been created in ${path.join(userdata, "/update-error.log")}`
});
app.exit();
return;
}
app.relaunch();
app.exit();
return;
} else {
config.version = package.version;
fs.writeFileSync(path.join(userdata, "/config.json"), JSON.stringify(config));
}
}
//This means an older version of HIBP Checker is running than the config was created for
if (checkVersionGreater(package.version, config.version)) {
dialog.showMessageBoxSync(win.startup, {
type: "error",
buttons: ["Ok"],
title: "Error",
message: "Your running an older version of HaveIBeenPwned Checker",
detail: "The config shows you have been running a newer version of HaveIBeenPwned Checker. Opening the config with an older version can corrupt the data or crash the program in unexpected ways. The programm will not start. Please download the newest version of HaveIBeenPwned Checker."
});
shell.openExternal("https://www.astrogd.eu/software/haveibeenpwned-checker");
win.startup.close();
app.exit();
return;
}
win.startup.setStatus(`[00${++count}] Checking language`, true);
if (!language[config.lang]) {
config.lang = "en";
fs.writeFileSync(path.join(userdata, "/config.json"), JSON.stringify(config));
}
win.startup.setStatus(`[00${++count}] Preparing content`, true);
langOptions = "";
let availableLanguages = Object.keys(language);
for (let i = 0; i < availableLanguages.length; i++) {
const langID = availableLanguages[i];
langOptions = langOptions + `<option value="${langID}">${language[langID].lang}</option>`;
}
win.startup.setStatus(`[00${++count}] Preparing view`, true);
win.main = new BrowserWindow({
show: false,
backgroundColor: "#fff",
width: 600,
height: 750,
minWidth: 600,
minHeight: 750,
center: true,
resizable: true,
movable: true,
closable: true,
fullscreenable: true,
title: "AstroGD - HaveIBeenPwned Checker",
frame: false,
darkTheme: true,
webPreferences: {
nodeIntegration: true,
devTools: false,
contextIsolation: false
},
icon: path.join(__dirname, "/icon.png")
});
ipc.once("ready", () => {
win.main.show();
win.startup.destroy();
win.startup = undefined;
const update = new updateCheck();
update.on("update-needed", (response) => {
win.main.sender.send("update-available", response);
});
//Retry search for updates after 30 seconds if it has failed
update.on("failed", () => {
setTimeout(() => {
update.check();
}, 30000);
});
});
ipc.on("loaded", (event) => {
if (!win.main.sender) win.main.sender = event.sender;
win.main.sender.send("content", {
version: package.version,
lang: language[config.lang],
langCode: config.lang,
langOptions: langOptions,
showChangelog: config.changelogShown ? false : true
});
if (!config.changelogShown) {
config.changelogShown = true;
fs.writeFileSync(path.join(userdata, "/config.json"), JSON.stringify(config));
}
});
ipc.on("minimize", () => {
win.main.minimize();
});
ipc.on("maximize", () => {
if (win.main.isMaximized()) win.main.unmaximize();
else win.main.maximize();
});
ipc.on("close", () => {
win.main.close();
app.quit();
});
ipc.on("switchLang", (_event, lang) => {
if (language[lang]) {
config.lang = lang;
fs.writeFileSync(path.join(userdata, "/config.json"), JSON.stringify(config));
win.main.sender.send("languageFile", language[lang]);
} else {
win.main.sender.send("languageFile", false);
}
});
ipc.on("check", async (_event, password) => {
const timeout = setTimeout(() => {
win.main.sender.send("timeout");
}, 10000);
const check = new PwnCheck.Password(password);
check.once("ready", () => {
check.check().catch(console.error);
check.once("checked", (breached) => {
clearTimeout(timeout);
win.main.sender.send("result", breached);
});
});
});
win.main.loadURL(`file://${path.join(__dirname, "/views/main/index.html")}`);
}
app.on("ready", () => {
init();
});