Incepere creare procese separate

This commit is contained in:
andrei-mihnea-cerbu
2024-04-03 23:22:38 +03:00
parent 038b809dd8
commit ffeebf1177
58 changed files with 913 additions and 479 deletions
+135 -13
View File
@@ -1,13 +1,57 @@
const { app, BrowserWindow, screen, ipcMain, dialog } = require('electron');
const fs = require('fs');
const path = require('path');
const crypto = require('crypto');
const { fork } = require('child_process');
const {encryptFileInPlace, decryptFileInPlace} = require('./aes_encrypt');
const isMac = process.platform === 'darwin';
let html_page = undefined;
let mainWindow = undefined;
let alertWindow = undefined;
const createMainWindow = ((title, width, height) => {
let backupProcess = null;
let receiverProcess = null;
let fetcherProcess = null;
const create_initial_keys = () => {
const SECRET_KEY = crypto.randomBytes(32);
const IV = crypto.randomBytes(16);
const secretKeyPath = path.join(__dirname, '..', '..', 'secret.key');
const ivPath = path.join(__dirname, '..', '..', 'iv.key');
fs.writeFileSync(secretKeyPath, SECRET_KEY);
console.log(`Secret Key saved to ${secretKeyPath}`);
fs.writeFileSync(ivPath, IV);
console.log(`IV saved to ${ivPath}`);
}
const delete_external_files = () => {
const ipConfigPath = path.join(__dirname, '..', '..', 'ipConfig.json');
const loginDataPath = path.join(__dirname, '..', '..', 'loginData.json');
fs.unlink(ipConfigPath, (err) => {
if (err) {
console.error('Error deleting file:', err);
return;
}
console.log('File deleted successfully: ' + 'ipConfig.json');
});
fs.unlink(loginDataPath, (err) => {
if (err) {
console.error('Error deleting file:', err);
return;
}
console.log('File deleted successfully: ' + 'loginData.json');
});
}
const createMainWindow = (async (title, width, height) => {
mainWindow = new BrowserWindow({
title: title,
width: width,
@@ -19,9 +63,25 @@ const createMainWindow = ((title, width, height) => {
}
});
html_page = 'login.html';
try {
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();
}
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';
}
//mainWindow.setMenu(null);
mainWindow.loadFile(path.join(__dirname, '..', 'renderer', 'html', 'login.html'))
mainWindow.loadFile(path.join(__dirname, '..', 'renderer', 'html', html_page))
.then(() => {
console.log('Main window loaded!')
})
@@ -79,6 +139,18 @@ app.whenReady().then(() => {
});
});
app.on('before-quit', () => {
if (backupProcess !== null) {
backupProcess.kill();
}
if (receiverProcess !== null) {
receiverProcess.kill();
}
if (fetcherProcess !== null) {
fetcherProcess.kill();
}
});
app.on('window-all-closed', () => {
if(!isMac){
app.quit();
@@ -89,6 +161,7 @@ ipcMain.handle('write-file', async (event, fileName, content) => {
try {
let filePath = path.join(__dirname, '..', '..', fileName);
await fs.promises.writeFile(filePath, content);
await encryptFileInPlace(filePath);
console.log(`File successfully written to ${filePath}`);
return { success: true };
} catch (error) {
@@ -113,8 +186,10 @@ ipcMain.handle('delete-file', async (event, fileName) => {
ipcMain.handle('read-file', async (event, fileName) => {
try {
let filePath = path.join(__dirname, '..', '..', fileName);
await decryptFileInPlace(filePath);
const content = await fs.promises.readFile(filePath, 'utf-8');
await encryptFileInPlace(filePath);
return { success: true, content };
} catch (error) {
console.error('Error reading file:', error);
@@ -125,16 +200,7 @@ ipcMain.handle('read-file', async (event, fileName) => {
ipcMain.handle('change-content', async (event, nextPage) => {
try {
html_page = nextPage;
mainWindow.webContents.executeJavaScript(`
document.body.classList.add('fade-out');
`);
await new Promise(resolve => setTimeout(resolve, 1000));
await mainWindow.loadFile(path.join(__dirname, '..', 'renderer', 'html', nextPage));
mainWindow.webContents.executeJavaScript(`
document.body.classList.add('fade-in');
`);
return true;
} catch (error) {
console.error('Error changing content:', error);
@@ -197,3 +263,59 @@ ipcMain.on('close-alert-window', () => {
alertWindow = undefined;
}
});
//External processes
ipcMain.handle('start-fetcher', async (event, args) => {
if (fetcherProcess === null) {
fetcherProcess = fork(path.join(__dirname, '..', '..', 'jobs', 'fetcher.js'), args, { silent: false });
fetcherProcess.on('exit', () => {
fetcherProcess = null;
// Optionally, notify the renderer process that the fetcher has finished
});
}
return true; // Indicate that the operation has started
});
// Handler to start the backup process
ipcMain.handle('start-backup', async (event, args) => {
if (backupProcess === null) { // Should this be a unique variable for backupProcess instead?
backupProcess = fork(path.join(__dirname, '..', '..', 'jobs', 'backup.js'), args, { silent: false });
backupProcess.on('exit', () => {
backupProcess = null;
// Optionally, notify the renderer process that the backup has finished
});
}
return true; // Indicate that the operation has started
});
// Handler to start the decrypt-files process
ipcMain.handle('start-decrypt-files', async (event, args) => {
const decryptFilesProcess = fork(path.join(__dirname, '..', '..', 'jobs', 'decrypt_files.js'), args, { silent: false });
decryptFilesProcess.on('exit', () => {
event.sender.send('decrypt-files-finished', true); // Notify renderer process
});
return true; // Indicate that the operation has started
});
// Handler to start the send_files process
ipcMain.handle('start-send_files', async (event, args) => {
// This seems to duplicate the 'start-decrypt-files' process; assuming a different script is intended
const sendFilesProcess = fork(path.join(__dirname, '..', '..', 'jobs', 'send_files.js'), args, { silent: false });
sendFilesProcess.on('exit', () => {
event.sender.send('send-files-finished', true); // Notify renderer process
});
return true; // Indicate that the operation has started
});
// Handler to start the receiver process
ipcMain.handle('start-receiver', async (event, args) => {
if (receiverProcess === null) {
receiverProcess = fork(path.join(__dirname, '..', '..', 'jobs', 'receiver.js'), args, { silent: false });
receiverProcess.on('exit', () => {
receiverProcess = null;
// Optionally, notify the renderer process that the receiver has finished
});
}
return true; // Indicate that the operation has started
});