| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112 |
- const { app, BrowserWindow, Menu, protocol } = require('electron');
- const path = require('path');
- const fs = require('fs');
- const crypto = require('crypto');
- const mime = require('mime');
- const algorithm = 'aes-256-ctr';
- const password = 'skyfffire-password';
- const key = crypto.createHash('sha256').update(password).digest();
- let memoryCache = {};
- // Function to decrypt a file and store its content in memory
- function decryptFile(filePath) {
- console.log(`Decrypting: ${filePath}`);
- const fileContent = fs.readFileSync(filePath);
- const iv = fileContent.slice(0, 16); // Extract IV
- const encrypted = fileContent.slice(16); // Extract encrypted data
- const decipher = crypto.createDecipheriv(algorithm, key, iv);
- const decrypted = Buffer.concat([decipher.update(encrypted), decipher.final()]);
- const relativePath = path.relative(process.resourcesPath, filePath);
- memoryCache[relativePath] = decrypted;
- console.log(`Decrypted: ${filePath}, content: ${decrypted.slice(0, 100)}`);
- }
- // Recursively decrypt files in a directory and store their content in memory
- function decryptDirectory(directoryPath) {
- const files = fs.readdirSync(directoryPath);
- files.forEach(file => {
- const fullPath = path.join(directoryPath, file);
- console.log(`Processing: ${fullPath}`);
- if (fs.lstatSync(fullPath).isDirectory()) {
- decryptDirectory(fullPath);
- } else {
- decryptFile(fullPath);
- }
- });
- }
- function createWindow() {
- const directoryToDecrypt = path.join(process.resourcesPath, 'app.asar', 'static');
- console.log(`Decrypting directory: ${directoryToDecrypt}`);
- decryptDirectory(directoryToDecrypt);
- // Register a custom protocol to serve content from memory
- protocol.interceptBufferProtocol('file', (request, callback) => {
- const url = request.url.substr(7); // Remove 'file://' prefix
- const relativePath = path.relative(process.resourcesPath, url);
- if (memoryCache[relativePath]) {
- callback({ mimeType: 'text/javascript', data: memoryCache[relativePath] });
- } else {
- callback({ error: -6 }); // FILE_NOT_FOUND
- }
- }, (error) => {
- if (error) {
- console.error('Failed to register protocol');
- }
- });
- const win = new BrowserWindow({
- width: 1600,
- height: 900,
- icon: path.join(__dirname, 'favicon.ico'), // 设置窗口图标
- webPreferences: {
- nodeIntegration: false,
- contextIsolation: true
- },
- });
- win.loadFile('index.html');
- // 打开调试工具
- win.webContents.openDevTools();
- // // 创建菜单模板,只包含一个刷新按钮
- // const menuTemplate = [
- // {
- // label: 'View',
- // submenu: [
- // {
- // label: 'Reload',
- // accelerator: 'CmdOrCtrl+R',
- // click: () => {
- // mainWindow.reload();
- // },
- // },
- // ],
- // },
- // ];
- //
- // // 创建菜单
- // const menu = Menu.buildFromTemplate(menuTemplate);
- //
- // // 设置应用程序的菜单
- // Menu.setApplicationMenu(menu);
- }
- app.on('ready', createWindow);
- app.on('window-all-closed', () => {
- if (process.platform !== 'darwin') {
- app.quit();
- }
- });
- app.on('activate', () => {
- if (BrowserWindow.getAllWindows().length === 0) {
- createWindow();
- }
- });
|