electron.js 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  1. const { app, BrowserWindow, Menu, protocol } = 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. // Register a custom protocol to serve content from memory
  39. protocol.registerBufferProtocol('file', (request, callback) => {
  40. const url = request.url.substr(7); // Remove 'file://' prefix
  41. const relativePath = path.relative(process.resourcesPath, url).replace(/\\/g, '/');
  42. console.log(`Intercepting request for: ${relativePath}`);
  43. if (memoryCache[relativePath]) {
  44. let mimeType = 'text/plain';
  45. if (relativePath.endsWith('.js')) {
  46. mimeType = 'application/javascript';
  47. } else if (relativePath.endsWith('.css')) {
  48. mimeType = 'text/css';
  49. } else if (relativePath.endsWith('.html')) {
  50. mimeType = 'text/html';
  51. }
  52. console.log(`Serving from memory: ${relativePath} as ${mimeType}`);
  53. callback({ mimeType: mimeType, data: memoryCache[relativePath] });
  54. } else {
  55. console.error(`File not found in memory: ${relativePath}`);
  56. callback({ error: -6 }); // FILE_NOT_FOUND
  57. }
  58. });
  59. const win = new BrowserWindow({
  60. width: 1600,
  61. height: 900,
  62. icon: path.join(__dirname, 'favicon.ico'), // 设置窗口图标
  63. webPreferences: {
  64. nodeIntegration: false,
  65. contextIsolation: true,
  66. },
  67. });
  68. // Load the index.html from memory using a data URL
  69. const indexHtmlContent = memoryCache[indexRelativePath].toString();
  70. win.loadURL('data:text/html;charset=utf-8,' + encodeURIComponent(indexHtmlContent));
  71. // 打开调试工具
  72. win.webContents.openDevTools();
  73. // 创建菜单模板,只包含一个刷新按钮
  74. const menuTemplate = [
  75. {
  76. label: 'View',
  77. submenu: [
  78. {
  79. label: 'Reload',
  80. accelerator: 'CmdOrCtrl+R',
  81. click: () => {
  82. win.reload();
  83. },
  84. },
  85. ],
  86. },
  87. ];
  88. // 创建菜单
  89. const menu = Menu.buildFromTemplate(menuTemplate);
  90. // 设置应用程序的菜单
  91. Menu.setApplicationMenu(menu);
  92. }
  93. app.on('ready', createWindow);
  94. app.on('window-all-closed', () => {
  95. if (process.platform !== 'darwin') {
  96. app.quit();
  97. }
  98. });
  99. app.on('activate', () => {
  100. if (BrowserWindow.getAllWindows().length === 0) {
  101. createWindow();
  102. }
  103. });