| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133 |
- const { app, BrowserWindow, Menu, session } = require('electron');
- const path = require('path');
- const fs = require('fs');
- const crypto = require('crypto');
- 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) {
- 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).replace(/\\/g, '/');
- memoryCache[relativePath] = decrypted;
- }
- // 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);
- if (fs.lstatSync(fullPath).isDirectory()) {
- decryptDirectory(fullPath);
- } else {
- decryptFile(fullPath);
- }
- });
- }
- function createWindow() {
- const directoryToDecrypt = path.join(process.resourcesPath, 'app.asar', 'static');
- decryptDirectory(directoryToDecrypt);
- const win = new BrowserWindow({
- width: 1600,
- height: 900,
- icon: path.join(__dirname, 'favicon.ico'), // 设置窗口图标
- webPreferences: {
- nodeIntegration: false,
- contextIsolation: true,
- },
- });
- // Load the index.html from disk
- win.loadFile('index.html');
- // 打开调试工具
- // win.webContents.openDevTools();
- // 创建菜单模板,只包含一个刷新按钮
- const menuTemplate = [
- {
- label: 'View',
- submenu: [
- {
- label: 'Reload',
- accelerator: 'CmdOrCtrl+R',
- click: () => {
- win.reload();
- },
- },
- ],
- },
- ];
- // 创建菜单
- const menu = Menu.buildFromTemplate(menuTemplate);
- // 设置应用程序的菜单
- Menu.setApplicationMenu(menu);
- console.log(Object.keys(memoryCache))
- }
- app.on('ready', () => {
- // Intercept file requests and serve from memory
- session.defaultSession.webRequest.onBeforeRequest((details, callback) => {
- const url = new URL(details.url);
- const filePath = path.normalize(decodeURIComponent(url.pathname));
- const relativePath = filePath.replace(path.normalize(process.resourcesPath), '').replace(/\\/g, '/').replace('//', '');
- if (memoryCache[relativePath]) {
- let sanitizedRelativePath = relativePath.replace('app.asar', 'app_asar'); // 避免路径中包含 app.asar
- const tempFilePath = path.join(app.getPath('temp'), sanitizedRelativePath);
- console.log(`Temp File Path: ${tempFilePath}`);
- const tempDir = path.dirname(tempFilePath);
- console.log(`Temp Directory Path: ${tempDir}`);
- // 确保临时目录路径存在
- if (!fs.existsSync(tempDir)) {
- console.log(`Creating directory: ${tempDir}`);
- fs.mkdirSync(tempDir, { recursive: true });
- console.log(`Directory created: ${tempDir}`);
- } else {
- console.log(`Directory already exists: ${tempDir}`);
- }
- // 确保目录存在后再写入文件
- if (fs.existsSync(tempDir)) {
- console.log(`Writing file: ${tempFilePath}`);
- fs.writeFileSync(tempFilePath, memoryCache[relativePath]);
- console.log(`File written: ${tempFilePath}`);
- console.log(`Intercepting request for: ${relativePath}, redirectURL: ${tempFilePath}`);
- callback({ cancel: false, redirectURL: tempFilePath });
- } else {
- console.error(`Directory does not exist after creation attempt: ${tempDir}`);
- callback({ cancel: false });
- }
- } else {
- callback({ cancel: false });
- }
- });
- createWindow();
- });
- app.on('window-all-closed', () => {
- if (process.platform !== 'darwin') {
- app.quit();
- }
- });
- app.on('activate', () => {
- if (BrowserWindow.getAllWindows().length === 0) {
- createWindow();
- }
- });
|