backup + decriptare toate fisiere done
This commit is contained in:
@@ -6,7 +6,7 @@ async function readKeyFromFile(filePath) {
|
||||
try {
|
||||
await fs.promises.access(filePath, fs.constants.F_OK);
|
||||
return fs.promises.readFile(filePath); // Use asynchronous readFile
|
||||
} catch(error) {
|
||||
} catch (error) {
|
||||
console.error('Error reading key file:', error);
|
||||
return null;
|
||||
}
|
||||
@@ -70,7 +70,46 @@ async function decryptFileInPlace(filePath) {
|
||||
});
|
||||
}
|
||||
|
||||
function cryptForKey(content, key, decrypt = false) {
|
||||
const algorithm = 'aes-256-ctr';
|
||||
const secretKey = crypto.createHash('sha256').update(String(key)).digest('base64').substr(0, 32);
|
||||
let cipher;
|
||||
|
||||
if (decrypt) {
|
||||
cipher = crypto.createDecipheriv(algorithm, secretKey, Buffer.alloc(16, 0)); // Using a zeroed IV for CTR
|
||||
} else {
|
||||
cipher = crypto.createCipheriv(algorithm, secretKey, Buffer.alloc(16, 0));
|
||||
}
|
||||
|
||||
return Buffer.concat([cipher.update(content), cipher.final()]);
|
||||
}
|
||||
|
||||
// Encrypts a file in place with a given key
|
||||
async function encryptFileWithKey(filePath, key) {
|
||||
try {
|
||||
const fileContent = await fs.promises.readFile(filePath);
|
||||
const encryptedContent = cryptForKey(fileContent, key, false);
|
||||
await fs.promises.writeFile(filePath, encryptedContent);
|
||||
console.log(`File encrypted successfully: ${filePath}`);
|
||||
} catch (error) {
|
||||
console.error(`Error encrypting file: ${error.message}`);
|
||||
}
|
||||
}
|
||||
|
||||
async function decryptFileWithKey(filePath, key) {
|
||||
try {
|
||||
const fileContent = await fs.promises.readFile(filePath);
|
||||
const decryptedContent = cryptForKey(fileContent, key, true);
|
||||
await fs.promises.writeFile(filePath, decryptedContent);
|
||||
console.log(`File decrypted successfully: ${filePath}`);
|
||||
} catch (error) {
|
||||
console.error(`Error decrypting file: ${error.message}`);
|
||||
}
|
||||
}
|
||||
|
||||
module.exports = {
|
||||
encryptFileInPlace,
|
||||
decryptFileInPlace
|
||||
decryptFileInPlace,
|
||||
encryptFileWithKey,
|
||||
decryptFileWithKey,
|
||||
}
|
||||
|
||||
+23
-61
@@ -1,10 +1,11 @@
|
||||
const { app, BrowserWindow, screen, ipcMain, dialog } = require('electron');
|
||||
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 {fork} = require('child_process');
|
||||
|
||||
const {encryptFileInPlace, decryptFileInPlace} = require('./aes_encrypt');
|
||||
const {lockFile, unlockFile} = require("../../helpers/lock_mechanism");
|
||||
|
||||
const isMac = process.platform === 'darwin';
|
||||
let html_page = undefined;
|
||||
@@ -117,8 +118,8 @@ function showAlert(message) {
|
||||
if (alertWindow === undefined) {
|
||||
const title = 'Alert';
|
||||
const mainScreen = screen.getPrimaryDisplay();
|
||||
const { width, height } = mainScreen.size;
|
||||
createAlertWindow(title, width/4, height/4);
|
||||
const {width, height} = mainScreen.size;
|
||||
createAlertWindow(title, width / 4, height / 4);
|
||||
}
|
||||
|
||||
alertWindow.webContents.once('dom-ready', () => {
|
||||
@@ -129,11 +130,11 @@ function showAlert(message) {
|
||||
app.whenReady().then(() => {
|
||||
const title = "Application";
|
||||
const mainScreen = screen.getPrimaryDisplay();
|
||||
const { width, height } = mainScreen.size;
|
||||
const {width, height} = mainScreen.size;
|
||||
createMainWindow(title, width / 1.5, height / 1.5);
|
||||
|
||||
app.on('activate', () => {
|
||||
if(BrowserWindow.getAllWindows().length === 0){
|
||||
if (BrowserWindow.getAllWindows().length === 0) {
|
||||
createMainWindow(title, width, height);
|
||||
}
|
||||
});
|
||||
@@ -152,7 +153,7 @@ app.on('before-quit', () => {
|
||||
});
|
||||
|
||||
app.on('window-all-closed', () => {
|
||||
if(!isMac){
|
||||
if (!isMac) {
|
||||
app.quit();
|
||||
}
|
||||
});
|
||||
@@ -160,40 +161,45 @@ app.on('window-all-closed', () => {
|
||||
ipcMain.handle('write-file', async (event, fileName, content) => {
|
||||
try {
|
||||
let filePath = path.join(__dirname, '..', '..', fileName);
|
||||
await lockFile(filePath);
|
||||
await fs.promises.writeFile(filePath, content);
|
||||
await encryptFileInPlace(filePath);
|
||||
await unlockFile(filePath)
|
||||
console.log(`File successfully written to ${filePath}`);
|
||||
return { success: true };
|
||||
return {success: true};
|
||||
} catch (error) {
|
||||
console.error('Failed to write file:', error);
|
||||
return { success: false, error: error.message };
|
||||
return {success: false, error: error.message};
|
||||
}
|
||||
});
|
||||
|
||||
ipcMain.handle('delete-file', async (event, fileName) => {
|
||||
try {
|
||||
let filePath = path.join(__dirname, '..', '..', fileName);
|
||||
console.log(filePath);
|
||||
|
||||
await fs.promises.unlink(filePath);
|
||||
|
||||
console.log(`File ${filePath} successfully deleted`);
|
||||
return { success: true };
|
||||
return {success: true};
|
||||
} catch (error) {
|
||||
console.error('Failed to delete file:', error);
|
||||
return { success: false, error: error.message };
|
||||
return {success: false, error: error.message};
|
||||
}
|
||||
});
|
||||
|
||||
ipcMain.handle('read-file', async (event, fileName) => {
|
||||
try {
|
||||
let filePath = path.join(__dirname, '..', '..', fileName);
|
||||
await lockFile(filePath);
|
||||
await decryptFileInPlace(filePath);
|
||||
const content = await fs.promises.readFile(filePath, 'utf-8');
|
||||
|
||||
await encryptFileInPlace(filePath);
|
||||
return { success: true, content };
|
||||
await unlockFile(filePath);
|
||||
return {success: true, content};
|
||||
} catch (error) {
|
||||
console.error('Error reading file:', error);
|
||||
return { success: false, error: error.message };
|
||||
return {success: false, error: error.message};
|
||||
}
|
||||
});
|
||||
|
||||
@@ -229,7 +235,7 @@ ipcMain.handle('open-backup-dir-dialog', async (event) => {
|
||||
return true;
|
||||
} catch (error) {
|
||||
console.error('Error opening file dialog:', error);
|
||||
return { error: error.message };
|
||||
return {error: error.message};
|
||||
}
|
||||
});
|
||||
|
||||
@@ -253,7 +259,7 @@ ipcMain.handle('check-file-exists', async (event, fileName) => {
|
||||
}
|
||||
});
|
||||
|
||||
ipcMain.handle('show-alert', async (event, message) =>{
|
||||
ipcMain.handle('show-alert', async (event, message) => {
|
||||
showAlert(message);
|
||||
});
|
||||
|
||||
@@ -265,10 +271,9 @@ ipcMain.on('close-alert-window', () => {
|
||||
});
|
||||
|
||||
//External processes
|
||||
|
||||
ipcMain.handle('start-fetcher', async (event, args) => {
|
||||
if (fetcherProcess === null) {
|
||||
fetcherProcess = fork(path.join(__dirname, '..', '..', 'jobs', 'fetcher.js'), args, { silent: false });
|
||||
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
|
||||
@@ -276,46 +281,3 @@ ipcMain.handle('start-fetcher', async (event, args) => {
|
||||
}
|
||||
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
|
||||
});
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
const { contextBridge, ipcRenderer } = require('electron');
|
||||
const {contextBridge, ipcRenderer} = require('electron');
|
||||
|
||||
contextBridge.exposeInMainWorld('electronAPI', {
|
||||
writeFile: (fileName, content) => ipcRenderer.invoke('write-file', fileName, content),
|
||||
|
||||
Reference in New Issue
Block a user