electron.js 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  1. const { app, BrowserWindow, Menu, session } = require('electron');
  2. const path = require('path');
  3. const fs = require('fs');
  4. const crypto = require('crypto');
  5. const algorithm = 'aes-256-ctr';
  6. const password = 'skyfffire-password';
  7. const key = crypto.createHash('sha256').update(password).digest();
  8. let memoryCache = {};
  9. // Function to decrypt a file and store its content in memory
  10. function decryptFile(filePath) {
  11. const fileContent = fs.readFileSync(filePath);
  12. const iv = fileContent.slice(0, 16); // Extract IV
  13. const encrypted = fileContent.slice(16); // Extract encrypted data
  14. const decipher = crypto.createDecipheriv(algorithm, key, iv);
  15. const decrypted = Buffer.concat([decipher.update(encrypted), decipher.final()]);
  16. const relativePath = path.relative(process.resourcesPath, filePath).replace(/\\/g, '/');
  17. memoryCache[relativePath] = decrypted;
  18. }
  19. // Recursively decrypt files in a directory and store their content in memory
  20. function decryptDirectory(directoryPath) {
  21. const files = fs.readdirSync(directoryPath);
  22. files.forEach(file => {
  23. const fullPath = path.join(directoryPath, file);
  24. if (fs.lstatSync(fullPath).isDirectory()) {
  25. decryptDirectory(fullPath);
  26. } else {
  27. decryptFile(fullPath);
  28. }
  29. });
  30. }
  31. function createWindow() {
  32. const directoryToDecrypt = path.join(process.resourcesPath, 'app.asar', 'static');
  33. decryptDirectory(directoryToDecrypt);
  34. // Load index.html into memory
  35. const indexPath = path.join(process.resourcesPath, 'app.asar', 'index.html');
  36. const indexRelativePath = path.relative(process.resourcesPath, indexPath).replace(/\\/g, '/');
  37. memoryCache[indexRelativePath] = fs.readFileSync(indexPath);
  38. const win = new BrowserWindow({
  39. width: 1600,
  40. height: 900,
  41. icon: path.join(__dirname, 'favicon.ico'), // 设置窗口图标
  42. webPreferences: {
  43. nodeIntegration: false,
  44. contextIsolation: true,
  45. },
  46. });
  47. // Load the index.html from memory using a data URL
  48. // const indexHtmlContent = memoryCache[indexRelativePath].toString();
  49. // win.loadURL('data:text/html;charset=utf-8,' + encodeURIComponent(indexHtmlContent));
  50. win.loadFile('index.html');
  51. // 打开调试工具
  52. win.webContents.openDevTools();
  53. // 创建菜单模板,只包含一个刷新按钮
  54. const menuTemplate = [
  55. {
  56. label: 'View',
  57. submenu: [
  58. {
  59. label: 'Reload',
  60. accelerator: 'CmdOrCtrl+R',
  61. click: () => {
  62. win.reload();
  63. },
  64. },
  65. ],
  66. },
  67. ];
  68. // 创建菜单
  69. const menu = Menu.buildFromTemplate(menuTemplate);
  70. // 设置应用程序的菜单
  71. Menu.setApplicationMenu(menu);
  72. }
  73. app.on('ready', () => {
  74. // Intercept file requests and serve from memory
  75. session.defaultSession.webRequest.onBeforeRequest((details, callback) => {
  76. const url = details.url.substr(7); // Remove 'file://' prefix
  77. const relativePath = path.relative(process.resourcesPath, url).replace(/\\/g, '/');
  78. console.log(`Intercepting request for: ${relativePath}`);
  79. if (memoryCache[relativePath]) {
  80. let mimeType = 'text/plain';
  81. if (relativePath.endsWith('.js')) {
  82. mimeType = 'application/javascript';
  83. } else if (relativePath.endsWith('.css')) {
  84. mimeType = 'text/css';
  85. } else if (relativePath.endsWith('.html')) {
  86. mimeType = 'text/html';
  87. }
  88. console.log(`Serving from memory: ${relativePath} as ${mimeType}`);
  89. callback({ mimeType: mimeType, data: memoryCache[relativePath] });
  90. } else {
  91. console.error(`File not found in memory: ${relativePath}`);
  92. callback({ cancel: false });
  93. }
  94. });
  95. createWindow();
  96. });
  97. app.on('window-all-closed', () => {
  98. if (process.platform !== 'darwin') {
  99. app.quit();
  100. }
  101. });
  102. app.on('activate', () => {
  103. if (BrowserWindow.getAllWindows().length === 0) {
  104. createWindow();
  105. }
  106. });