Incepere creare procese separate
This commit is contained in:
@@ -0,0 +1,76 @@
|
||||
const crypto = require('crypto');
|
||||
const fs = require('fs');
|
||||
const path = require('path');
|
||||
|
||||
async function readKeyFromFile(filePath) {
|
||||
try {
|
||||
await fs.promises.access(filePath, fs.constants.F_OK);
|
||||
return fs.promises.readFile(filePath); // Use asynchronous readFile
|
||||
} catch(error) {
|
||||
console.error('Error reading key file:', error);
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
// Paths to the key files remain the same
|
||||
const IV_FILE_PATH = path.join(__dirname, '..', '..', 'iv.key');
|
||||
const SECRET_KEY_FILE_PATH = path.join(__dirname, '..', '..', 'secret.key');
|
||||
|
||||
async function encryptFileInPlace(filePath) {
|
||||
// Await the resolution of these promises
|
||||
const IV = await readKeyFromFile(IV_FILE_PATH);
|
||||
const SECRET_KEY = await readKeyFromFile(SECRET_KEY_FILE_PATH);
|
||||
|
||||
if (!IV || !SECRET_KEY) {
|
||||
throw new Error('Failed to load IV or Secret Key');
|
||||
}
|
||||
|
||||
const tempEncryptedFilePath = filePath + '.enc'; // Temporary encrypted file
|
||||
return new Promise((resolve, reject) => {
|
||||
const cipher = crypto.createCipheriv('aes-256-cbc', Buffer.from(SECRET_KEY), IV);
|
||||
const input = fs.createReadStream(filePath);
|
||||
const output = fs.createWriteStream(tempEncryptedFilePath);
|
||||
|
||||
input.pipe(cipher).pipe(output);
|
||||
|
||||
output.on('finish', () => {
|
||||
fs.rename(tempEncryptedFilePath, filePath, (err) => {
|
||||
if (err) reject(err);
|
||||
else resolve('File encrypted successfully and replaced original.');
|
||||
});
|
||||
});
|
||||
output.on('error', reject);
|
||||
});
|
||||
}
|
||||
|
||||
async function decryptFileInPlace(filePath) {
|
||||
// Await the resolution of these promises
|
||||
const IV = await readKeyFromFile(IV_FILE_PATH);
|
||||
const SECRET_KEY = await readKeyFromFile(SECRET_KEY_FILE_PATH);
|
||||
|
||||
if (!IV || !SECRET_KEY) {
|
||||
throw new Error('Failed to load IV or Secret Key');
|
||||
}
|
||||
|
||||
const tempDecryptedFilePath = filePath + '.dec'; // Temporary decrypted file
|
||||
return new Promise((resolve, reject) => {
|
||||
const decipher = crypto.createDecipheriv('aes-256-cbc', Buffer.from(SECRET_KEY), IV);
|
||||
const input = fs.createReadStream(filePath);
|
||||
const output = fs.createWriteStream(tempDecryptedFilePath);
|
||||
|
||||
input.pipe(decipher).pipe(output);
|
||||
|
||||
output.on('finish', () => {
|
||||
fs.rename(tempDecryptedFilePath, filePath, (err) => {
|
||||
if (err) reject(err);
|
||||
else resolve('File decrypted successfully and replaced original.');
|
||||
});
|
||||
});
|
||||
output.on('error', reject);
|
||||
});
|
||||
}
|
||||
|
||||
module.exports = {
|
||||
encryptFileInPlace,
|
||||
decryptFileInPlace
|
||||
}
|
||||
+135
-13
@@ -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
|
||||
});
|
||||
|
||||
@@ -9,5 +9,11 @@ contextBridge.exposeInMainWorld('electronAPI', {
|
||||
closeAlertWindow: () => ipcRenderer.send('close-alert-window'),
|
||||
openBackupDirDialog: () => ipcRenderer.invoke('open-backup-dir-dialog'),
|
||||
openFileDialog: () => ipcRenderer.invoke('open-file-dialog'),
|
||||
checkFileExists: (fileName) => ipcRenderer.invoke('check-file-exists', fileName)
|
||||
checkFileExists: (fileName) => ipcRenderer.invoke('check-file-exists', fileName),
|
||||
|
||||
startFetcher: async (args) => ipcRenderer.invoke('start-fetcher', args),
|
||||
startBackup: async (args) => ipcRenderer.invoke('start-backup', args),
|
||||
startDecryptFiles: async (args) => ipcRenderer.invoke('start-decrypt-files', args),
|
||||
startSendFiles: async (args) => ipcRenderer.invoke('start-send_files', args),
|
||||
startReceiver: async (args) => ipcRenderer.invoke('start-receiver', args)
|
||||
});
|
||||
Reference in New Issue
Block a user