electron.js 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  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. console.log('2')
  33. const directoryToDecrypt = path.join(process.resourcesPath, 'app.asar', 'static');
  34. decryptDirectory(directoryToDecrypt);
  35. const win = new BrowserWindow({
  36. width: 1600,
  37. height: 900,
  38. icon: path.join(__dirname, 'favicon.ico'), // 设置窗口图标
  39. webPreferences: {
  40. nodeIntegration: false,
  41. contextIsolation: true,
  42. },
  43. });
  44. // Load the index.html from disk
  45. win.loadFile('index.html');
  46. // 打开调试工具
  47. win.webContents.openDevTools();
  48. // 创建菜单模板,只包含一个刷新按钮
  49. const menuTemplate = [
  50. {
  51. label: 'View',
  52. submenu: [
  53. {
  54. label: 'Reload',
  55. accelerator: 'CmdOrCtrl+R',
  56. click: () => {
  57. win.reload();
  58. },
  59. },
  60. ],
  61. },
  62. ];
  63. // 创建菜单
  64. const menu = Menu.buildFromTemplate(menuTemplate);
  65. // 设置应用程序的菜单
  66. Menu.setApplicationMenu(menu);
  67. console.log(Object.keys(memoryCache))
  68. }
  69. app.on('ready', () => {
  70. console.log('1')
  71. // Intercept file requests and serve from memory
  72. session.defaultSession.webRequest.onBeforeRequest((details, callback) => {
  73. const url = new URL(details.url);
  74. const filePath = path.normalize(decodeURIComponent(url.pathname));
  75. const relativePath = filePath.replace(path.normalize(process.resourcesPath), '').replace(/\\/g, '/').replace('//', '');
  76. if (memoryCache[relativePath]) {
  77. const tempFilePath = path.join(app.getPath('temp'), relativePath);
  78. fs.mkdirSync(path.dirname(tempFilePath), { recursive: true });
  79. fs.writeFileSync(tempFilePath, memoryCache[relativePath]);
  80. console.log(`Intercepting request for: ${relativePath}, redirectURL: ${tempFilePath}`);
  81. let mimeType = 'text/plain';
  82. if (relativePath.endsWith('.js')) {
  83. mimeType = 'application/javascript';
  84. } else if (relativePath.endsWith('.css')) {
  85. mimeType = 'text/css';
  86. } else if (relativePath.endsWith('.html')) {
  87. mimeType = 'text/html';
  88. }
  89. console.log(`Serving from memory: ${relativePath} as ${mimeType}`);
  90. callback({ cancel: false, redirectURL: tempFilePath });
  91. } else {
  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. });