main.js 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  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. const win = new BrowserWindow({
  35. width: 1600,
  36. height: 900,
  37. icon: path.join(__dirname, 'favicon.ico'), // 设置窗口图标
  38. webPreferences: {
  39. nodeIntegration: false,
  40. contextIsolation: true,
  41. },
  42. });
  43. // Load the index.html from disk
  44. win.loadFile('index.html');
  45. // 打开调试工具
  46. // win.webContents.openDevTools();
  47. // 创建菜单模板,只包含一个刷新按钮
  48. const menuTemplate = [
  49. {
  50. label: 'View',
  51. submenu: [
  52. {
  53. label: 'Reload',
  54. accelerator: 'CmdOrCtrl+R',
  55. click: () => {
  56. win.reload();
  57. },
  58. },
  59. ],
  60. },
  61. ];
  62. // 创建菜单
  63. const menu = Menu.buildFromTemplate(menuTemplate);
  64. // 设置应用程序的菜单
  65. Menu.setApplicationMenu(menu);
  66. console.log(Object.keys(memoryCache))
  67. }
  68. app.on('ready', () => {
  69. // Intercept file requests and serve from memory
  70. session.defaultSession.webRequest.onBeforeRequest((details, callback) => {
  71. const url = new URL(details.url);
  72. const filePath = path.normalize(decodeURIComponent(url.pathname));
  73. const relativePath = filePath.replace(path.normalize(process.resourcesPath), '').replace(/\\/g, '/').replace('//', '');
  74. if (memoryCache[relativePath]) {
  75. let sanitizedRelativePath = relativePath.replace('app.asar', 'app_asar'); // 避免路径中包含 app.asar
  76. const tempFilePath = path.join(app.getPath('temp'), sanitizedRelativePath);
  77. console.log(`Temp File Path: ${tempFilePath}`);
  78. const tempDir = path.dirname(tempFilePath);
  79. console.log(`Temp Directory Path: ${tempDir}`);
  80. // 确保临时目录路径存在
  81. if (!fs.existsSync(tempDir)) {
  82. console.log(`Creating directory: ${tempDir}`);
  83. fs.mkdirSync(tempDir, { recursive: true });
  84. console.log(`Directory created: ${tempDir}`);
  85. } else {
  86. console.log(`Directory already exists: ${tempDir}`);
  87. }
  88. // 确保目录存在后再写入文件
  89. if (fs.existsSync(tempDir)) {
  90. console.log(`Writing file: ${tempFilePath}`);
  91. fs.writeFileSync(tempFilePath, memoryCache[relativePath]);
  92. console.log(`File written: ${tempFilePath}`);
  93. console.log(`Intercepting request for: ${relativePath}, redirectURL: ${tempFilePath}`);
  94. callback({ cancel: false, redirectURL: tempFilePath });
  95. } else {
  96. console.error(`Directory does not exist after creation attempt: ${tempDir}`);
  97. callback({ cancel: false });
  98. }
  99. } else {
  100. callback({ cancel: false });
  101. }
  102. });
  103. createWindow();
  104. });
  105. app.on('window-all-closed', () => {
  106. if (process.platform !== 'darwin') {
  107. app.quit();
  108. }
  109. });
  110. app.on('activate', () => {
  111. if (BrowserWindow.getAllWindows().length === 0) {
  112. createWindow();
  113. }
  114. });