electron.js 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. const { app, BrowserWindow, Menu, protocol } = require('electron');
  2. const path = require('path');
  3. const fs = require('fs');
  4. const crypto = require('crypto');
  5. const mime = require('mime');
  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. console.log(`Decrypting: ${filePath}`);
  13. const fileContent = fs.readFileSync(filePath);
  14. const iv = fileContent.slice(0, 16); // Extract IV
  15. const encrypted = fileContent.slice(16); // Extract encrypted data
  16. const decipher = crypto.createDecipheriv(algorithm, key, iv);
  17. const decrypted = Buffer.concat([decipher.update(encrypted), decipher.final()]);
  18. const relativePath = path.relative(process.resourcesPath, filePath);
  19. memoryCache[relativePath] = decrypted;
  20. console.log(`Decrypted: ${filePath}, content: ${decrypted.slice(0, 100)}`);
  21. }
  22. // Recursively decrypt files in a directory and store their content in memory
  23. function decryptDirectory(directoryPath) {
  24. const files = fs.readdirSync(directoryPath);
  25. files.forEach(file => {
  26. const fullPath = path.join(directoryPath, file);
  27. console.log(`Processing: ${fullPath}`);
  28. if (fs.lstatSync(fullPath).isDirectory()) {
  29. decryptDirectory(fullPath);
  30. } else {
  31. decryptFile(fullPath);
  32. }
  33. });
  34. }
  35. function createWindow() {
  36. const directoryToDecrypt = path.join(process.resourcesPath, 'app.asar', 'static');
  37. console.log(`Decrypting directory: ${directoryToDecrypt}`);
  38. decryptDirectory(directoryToDecrypt);
  39. // Register a custom protocol to serve content from memory
  40. protocol.interceptBufferProtocol('file', (request, callback) => {
  41. const url = request.url.substr(7); // Remove 'file://' prefix
  42. const relativePath = path.relative(process.resourcesPath, url);
  43. if (memoryCache[relativePath]) {
  44. callback({ mimeType: 'text/javascript', data: memoryCache[relativePath] });
  45. } else {
  46. callback({ error: -6 }); // FILE_NOT_FOUND
  47. }
  48. }, (error) => {
  49. if (error) {
  50. console.error('Failed to register protocol');
  51. }
  52. });
  53. const win = new BrowserWindow({
  54. width: 1600,
  55. height: 900,
  56. icon: path.join(__dirname, 'favicon.ico'), // 设置窗口图标
  57. webPreferences: {
  58. nodeIntegration: false,
  59. contextIsolation: true
  60. },
  61. });
  62. win.loadFile('index.html');
  63. // 打开调试工具
  64. win.webContents.openDevTools();
  65. // // 创建菜单模板,只包含一个刷新按钮
  66. // const menuTemplate = [
  67. // {
  68. // label: 'View',
  69. // submenu: [
  70. // {
  71. // label: 'Reload',
  72. // accelerator: 'CmdOrCtrl+R',
  73. // click: () => {
  74. // mainWindow.reload();
  75. // },
  76. // },
  77. // ],
  78. // },
  79. // ];
  80. //
  81. // // 创建菜单
  82. // const menu = Menu.buildFromTemplate(menuTemplate);
  83. //
  84. // // 设置应用程序的菜单
  85. // Menu.setApplicationMenu(menu);
  86. }
  87. app.on('ready', createWindow);
  88. app.on('window-all-closed', () => {
  89. if (process.platform !== 'darwin') {
  90. app.quit();
  91. }
  92. });
  93. app.on('activate', () => {
  94. if (BrowserWindow.getAllWindows().length === 0) {
  95. createWindow();
  96. }
  97. });