main.js 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  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 isDev = process.env.NODE_ENV === 'development';
  6. const algorithm = 'aes-256-ctr';
  7. const password = 'skyfffire-password';
  8. const key = crypto.createHash('sha256').update(password).digest();
  9. let memoryCache = {};
  10. // Function to decrypt a file and store its content in memory
  11. function decryptFile(filePath) {
  12. const fileContent = fs.readFileSync(filePath);
  13. const iv = fileContent.slice(0, 16); // Extract IV
  14. const encrypted = fileContent.slice(16); // Extract encrypted data
  15. const decipher = crypto.createDecipheriv(algorithm, key, iv);
  16. const decrypted = Buffer.concat([decipher.update(encrypted), decipher.final()]);
  17. let relativePath = path.relative(process.resourcesPath, filePath).replace(/\\/g, '/');
  18. if (isDev) relativePath = relativePath.replace('../../../..', '')
  19. memoryCache[relativePath] = decrypted;
  20. }
  21. // Recursively decrypt files in a directory and store their content in memory
  22. function decryptDirectory(directoryPath) {
  23. const files = fs.readdirSync(directoryPath);
  24. files.forEach(file => {
  25. const fullPath = path.join(directoryPath, file);
  26. if (fs.lstatSync(fullPath).isDirectory()) {
  27. decryptDirectory(fullPath);
  28. } else {
  29. decryptFile(fullPath);
  30. }
  31. });
  32. }
  33. function createWindow() {
  34. const directoryToDecrypt = isDev ? path.join(__dirname, 'build', 'static') : path.join(process.resourcesPath, 'app.asar', 'static');
  35. decryptDirectory(directoryToDecrypt);
  36. const win = new BrowserWindow({
  37. width: 1600,
  38. height: 900,
  39. icon: path.join(__dirname, 'favicon.ico'), // 设置窗口图标
  40. webPreferences: {
  41. nodeIntegration: false,
  42. contextIsolation: true,
  43. },
  44. });
  45. // Load the index.html from disk
  46. const index = isDev ? path.join(__dirname, 'build', 'index.html') : 'index.html';
  47. win.loadFile(index); // 确保路径正确
  48. // 打开调试工具
  49. if (isDev) win.webContents.openDevTools();
  50. // 创建菜单模板,只包含一个刷新按钮
  51. const menuTemplate = [
  52. {
  53. label: 'View',
  54. submenu: [
  55. {
  56. label: 'Reload',
  57. accelerator: 'CmdOrCtrl+R',
  58. click: () => {
  59. win.reload();
  60. },
  61. },
  62. ],
  63. },
  64. ];
  65. // 创建菜单
  66. const menu = Menu.buildFromTemplate(menuTemplate);
  67. // 设置应用程序的菜单
  68. Menu.setApplicationMenu(menu);
  69. console.log(Object.keys(memoryCache))
  70. }
  71. app.on('ready', () => {
  72. // Intercept file requests and serve from memory
  73. session.defaultSession.webRequest.onBeforeRequest((details, callback) => {
  74. const url = new URL(details.url);
  75. const filePath = path.normalize(decodeURIComponent(url.pathname));
  76. let relativePath = filePath.replace(path.normalize(process.resourcesPath), '').replace(/\\/g, '/').replace('//', '');
  77. if (isDev && relativePath.indexOf('react-stock-heatmap/example') !== -1) {
  78. relativePath = relativePath.split('react-stock-heatmap/example')[1]
  79. }
  80. console.log(relativePath)
  81. if (memoryCache[relativePath]) {
  82. let sanitizedRelativePath = relativePath.replace('app.asar', 'app_asar'); // 避免路径中包含 app.asar
  83. console.log(app.getPath('temp'), sanitizedRelativePath)
  84. const tempFilePath = path.join(app.getPath('temp'), sanitizedRelativePath);
  85. console.log(`Temp File Path: ${tempFilePath}`);
  86. const tempDir = path.dirname(tempFilePath);
  87. console.log(`Temp Directory Path: ${tempDir}`);
  88. // 确保临时目录路径存在
  89. if (!fs.existsSync(tempDir)) {
  90. console.log(`Creating directory: ${tempDir}`);
  91. fs.mkdirSync(tempDir, { recursive: true });
  92. console.log(`Directory created: ${tempDir}`);
  93. } else {
  94. console.log(`Directory already exists: ${tempDir}`);
  95. }
  96. // 确保目录存在后再写入文件
  97. if (fs.existsSync(tempDir)) {
  98. console.log(`Writing file: ${tempFilePath}`);
  99. fs.writeFileSync(tempFilePath, memoryCache[relativePath]);
  100. console.log(`File written: ${tempFilePath}`);
  101. console.log(`Intercepting request for: ${relativePath}, redirectURL: ${tempFilePath}`);
  102. callback({ cancel: false, redirectURL: tempFilePath });
  103. } else {
  104. console.error(`Directory does not exist after creation attempt: ${tempDir}`);
  105. callback({ cancel: false });
  106. }
  107. } else {
  108. callback({ cancel: false });
  109. }
  110. });
  111. createWindow();
  112. });
  113. app.on('window-all-closed', () => {
  114. if (process.platform !== 'darwin') {
  115. app.quit();
  116. }
  117. });
  118. app.on('activate', () => {
  119. if (BrowserWindow.getAllWindows().length === 0) {
  120. createWindow();
  121. }
  122. });