main.js 5.6 KB

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