main.js 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183
  1. const { app, BrowserWindow, Menu, session,ipcMain } = 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. const loginFilePath = path.join(app.getPath('userData'), 'loginSession.json');
  11. const symbolFilePath = path.join(app.getPath('userData'), 'symbolSession.json');
  12. function readData(filePath) {
  13. try {
  14. if (fs.existsSync(filePath)) {
  15. const rawData = fs.readFileSync(filePath);
  16. return JSON.parse(rawData);
  17. }
  18. } catch (error) {
  19. console.error('Error reading data:', error);
  20. }
  21. return {};
  22. }
  23. function writeData(filePath, data) {
  24. try {
  25. fs.writeFileSync(filePath, JSON.stringify(data, null, 2));
  26. } catch (error) {
  27. console.error(`Error writing data to ${filePath}:`, error);
  28. }
  29. }
  30. // Function to decrypt a file and store its content in memory
  31. function decryptFile(filePath) {
  32. const fileContent = fs.readFileSync(filePath);
  33. const iv = fileContent.slice(0, 16); // Extract IV
  34. const encrypted = fileContent.slice(16); // Extract encrypted data
  35. const decipher = crypto.createDecipheriv(algorithm, key, iv);
  36. const decrypted = Buffer.concat([decipher.update(encrypted), decipher.final()]);
  37. let relativePath = path.relative(process.resourcesPath, filePath).replace(/\\/g, '/');
  38. if (isDev) relativePath = relativePath.replace('../../../..', '')
  39. memoryCache[relativePath] = decrypted;
  40. }
  41. // Recursively decrypt files in a directory and store their content in memory
  42. function decryptDirectory(directoryPath) {
  43. const files = fs.readdirSync(directoryPath);
  44. files.forEach(file => {
  45. const fullPath = path.join(directoryPath, file);
  46. if (fs.lstatSync(fullPath).isDirectory()) {
  47. decryptDirectory(fullPath);
  48. } else {
  49. decryptFile(fullPath);
  50. }
  51. });
  52. }
  53. function createWindow() {
  54. const directoryToDecrypt = isDev ? path.join(__dirname, 'build', 'static') : path.join(process.resourcesPath, 'app.asar', 'static');
  55. decryptDirectory(directoryToDecrypt);
  56. const win = new BrowserWindow({
  57. width: 1600,
  58. height: 900,
  59. icon: path.join(__dirname, 'favicon.ico'), // 设置窗口图标
  60. webPreferences: {
  61. preload: path.join(__dirname, 'src', 'preload.js'), // 确认预加载脚本路径正确
  62. nodeIntegration: false,
  63. contextIsolation: true,
  64. },
  65. });
  66. // Load the index.html from disk
  67. const index = isDev ? path.join(__dirname, 'build', 'index.html') : 'index.html';
  68. win.loadFile(index); // 确保路径正确
  69. // 打开调试工具
  70. if (isDev) win.webContents.openDevTools();
  71. // 创建菜单模板,只包含一个刷新按钮
  72. const menuTemplate = [
  73. {
  74. label: 'View',
  75. submenu: [
  76. {
  77. label: 'Reload',
  78. accelerator: 'CmdOrCtrl+R',
  79. click: () => {
  80. win.reload();
  81. },
  82. },
  83. ],
  84. },
  85. ];
  86. // 创建菜单
  87. const menu = Menu.buildFromTemplate(menuTemplate);
  88. // 设置应用程序的菜单
  89. Menu.setApplicationMenu(menu);
  90. console.log(Object.keys(memoryCache))
  91. }
  92. app.whenReady().then(() => {
  93. ipcMain.handle('get-login-info-data', () => {
  94. return readData(loginFilePath);
  95. });
  96. ipcMain.handle('set-login-info-data', (event, newData) => {
  97. writeData(loginFilePath, newData);
  98. });
  99. ipcMain.handle('get-symbol-data', () => {
  100. return readData(symbolFilePath);
  101. });
  102. ipcMain.handle('set-symbol-data', (event, newData) => {
  103. writeData(symbolFilePath, newData);
  104. });
  105. });
  106. app.on('ready', () => {
  107. // Intercept file requests and serve from memory
  108. session.defaultSession.webRequest.onBeforeRequest((details, callback) => {
  109. const url = new URL(details.url);
  110. const filePath = path.normalize(decodeURIComponent(url.pathname));
  111. let relativePath = filePath.replace(path.normalize(process.resourcesPath), '').replace(/\\/g, '/').replace('//', '');
  112. if (isDev && relativePath.indexOf('react-stock-heatmap/example') !== -1) {
  113. relativePath = relativePath.split('react-stock-heatmap/example')[1]
  114. }
  115. console.log(relativePath)
  116. if (memoryCache[relativePath]) {
  117. let sanitizedRelativePath = relativePath.replace('app.asar', 'app_asar'); // 避免路径中包含 app.asar
  118. console.log(app.getPath('temp'), sanitizedRelativePath)
  119. const tempFilePath = path.join(app.getPath('temp'), sanitizedRelativePath);
  120. console.log(`Temp File Path: ${tempFilePath}`);
  121. const tempDir = path.dirname(tempFilePath);
  122. console.log(`Temp Directory Path: ${tempDir}`);
  123. // 确保临时目录路径存在
  124. if (!fs.existsSync(tempDir)) {
  125. console.log(`Creating directory: ${tempDir}`);
  126. fs.mkdirSync(tempDir, { recursive: true });
  127. console.log(`Directory created: ${tempDir}`);
  128. } else {
  129. console.log(`Directory already exists: ${tempDir}`);
  130. }
  131. // 确保目录存在后再写入文件
  132. if (fs.existsSync(tempDir)) {
  133. console.log(`Writing file: ${tempFilePath}`);
  134. fs.writeFileSync(tempFilePath, memoryCache[relativePath]);
  135. console.log(`File written: ${tempFilePath}`);
  136. console.log(`Intercepting request for: ${relativePath}, redirectURL: ${tempFilePath}`);
  137. callback({ cancel: false, redirectURL: tempFilePath });
  138. } else {
  139. console.error(`Directory does not exist after creation attempt: ${tempDir}`);
  140. callback({ cancel: false });
  141. }
  142. } else {
  143. callback({ cancel: false });
  144. }
  145. });
  146. createWindow();
  147. });
  148. app.on('window-all-closed', () => {
  149. if (process.platform !== 'darwin') {
  150. app.quit();
  151. }
  152. });
  153. app.on('activate', () => {
  154. if (BrowserWindow.getAllWindows().length === 0) {
  155. createWindow();
  156. }
  157. });