This commit is contained in:
andrei-mihnea-cerbu
2024-04-22 16:30:40 +03:00
parent 7cf1729d08
commit 9ac718bea3
24 changed files with 275 additions and 163 deletions
+27 -13
View File
@@ -6,7 +6,6 @@ const {fork} = require('child_process');
const {encryptFileInPlace, decryptFileInPlace} = require('./aes_encrypt');
const {lockFile, unlockFile} = require("../../helpers/lock_mechanism");
//const {decryptUserFilesToDirectory} = require("../../jobs/backup");
const isMac = process.platform === 'darwin';
let html_page = undefined;
@@ -18,7 +17,7 @@ let backupProcess = null;
let externalEndpointsProcess = null;
let sendFileProcess = null;
const create_initial_keys = () => {
const createInitialKeys = () => {
const SECRET_KEY = crypto.randomBytes(32);
const IV = crypto.randomBytes(16);
@@ -32,7 +31,7 @@ const create_initial_keys = () => {
console.log(`IV saved to ${ivPath}`);
}
const delete_external_files = () => {
const deleteMainComponentsAtErrorStart = () => {
const ipConfigPath = path.join(__dirname, '..', '..', 'ipConfig.json');
const loginDataPath = path.join(__dirname, '..', '..', 'loginData.json');
@@ -54,6 +53,28 @@ const delete_external_files = () => {
}
const checkForServerConnection = async () => {
const pathToIpConfig = path.join(__dirname, '..', '..', 'ipConfig.json');
try {
await fs.promises.access(pathToIpConfig);
await lockFile(pathToIpConfig);
await decryptFileInPlace(pathToIpConfig);
const ipConfig = await fs.promises.readFile(pathToIpConfig, 'utf-8');
const { ip } = JSON.parse(ipConfig);
const response = await fetch(`http://${ip}/heartbeat`);
await encryptFileInPlace(pathToIpConfig)
await unlockFile(pathToIpConfig);
return response.ok;
} catch (error) {
console.error("Error:", error);
return false;
}
};
const createMainWindow = (async (title, width, height) => {
mainWindow = new BrowserWindow({
title: title,
@@ -70,18 +91,11 @@ const createMainWindow = (async (title, width, height) => {
await fs.promises.access(path.join(__dirname, '..', '..', 'secret.key'), fs.constants.F_OK);
await fs.promises.access(path.join(__dirname, '..', '..', 'iv.key'), fs.constants.F_OK);
} catch (err) {
create_initial_keys();
delete_external_files();
createInitialKeys();
deleteMainComponentsAtErrorStart();
}
html_page = 'ip_config.html';
try {
await fs.promises.access(path.join(__dirname, '..', '..', 'ipConfig.json'));
html_page = 'login.html';
} catch (err) {
html_page = 'ip_submit.html';
}
html_page = await checkForServerConnection() ? 'login.html' : 'ip_submit.html';
//mainWindow.setMenu(null);
mainWindow.loadFile(path.join(__dirname, '..', 'renderer', 'html', html_page))