electron.js 3.7 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 disk
  48. win.loadFile('index.html');
  49. // 打开调试工具
  50. win.webContents.openDevTools();
  51. // 创建菜单模板,只包含一个刷新按钮
  52. const menuTemplate = [
  53. {
  54. label: 'View',
  55. submenu: [
  56. {
  57. label: 'Reload',
  58. accelerator: 'CmdOrCtrl+R',
  59. click: () => {
  60. win.reload();
  61. },
  62. },
  63. ],
  64. },
  65. ];
  66. // 创建菜单
  67. const menu = Menu.buildFromTemplate(menuTemplate);
  68. // 设置应用程序的菜单
  69. Menu.setApplicationMenu(menu);
  70. console.log(Object.keys(memoryCache))
  71. }
  72. app.on('ready', () => {
  73. // Intercept file requests and serve from memory
  74. session.defaultSession.webRequest.onBeforeRequest((details, callback) => {
  75. const url = new URL(details.url);
  76. const filePath = path.normalize(decodeURIComponent(url.pathname));
  77. const relativePath = filePath.replace(path.normalize(process.resourcesPath), '').replace(/\\/g, '/').replace('//', '');
  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. });