Browse Source

混淆。

skyffire 1 year ago
parent
commit
581bc55cba
3 changed files with 748 additions and 15 deletions
  1. 38 0
      example/obfuscate-main.js
  2. 705 13
      example/package-lock.json
  3. 5 2
      example/package.json

+ 38 - 0
example/obfuscate-main.js

@@ -0,0 +1,38 @@
+const fs = require('fs');
+const path = require('path');
+const JavaScriptObfuscator = require('javascript-obfuscator');
+
+// 指定要混淆的文件
+const mainFilePath = path.join(__dirname, 'electron.js');
+const distFilePath = path.join(__dirname, 'build', 'electron.js');
+
+// 读取 `main.js` 文件内容
+const fileContent = fs.readFileSync(mainFilePath, 'utf8');
+
+// 配置混淆选项
+const obfuscationResult = JavaScriptObfuscator.obfuscate(fileContent, {
+  compact: true,
+  controlFlowFlattening: true,
+  controlFlowFlatteningThreshold: 0.75,
+  deadCodeInjection: true,
+  deadCodeInjectionThreshold: 0.4,
+  debugProtection: true,
+  debugProtectionInterval: 3000,
+  disableConsoleOutput: true,
+  identifierNamesGenerator: 'hexadecimal',
+  renameGlobals: false,
+  selfDefending: true,
+  stringArray: true,
+  stringArrayEncoding: ['base64'],
+  stringArrayThreshold: 0.75,
+  transformObjectKeys: true,
+  unicodeEscapeSequence: false,
+});
+
+if (!fs.existsSync(path.dirname(distFilePath))) {
+  fs.mkdirSync(path.dirname(distFilePath), { recursive: true });
+}
+
+// 将混淆后的代码写入 `dist/main.js`
+fs.writeFileSync(distFilePath, obfuscationResult.getObfuscatedCode());
+console.log(`Successfully obfuscated ${mainFilePath} to ${distFilePath}`);

File diff suppressed because it is too large
+ 705 - 13
example/package-lock.json


+ 5 - 2
example/package.json

@@ -7,6 +7,7 @@
   "scripts": {
     "start-web": "node ../node_modules/react-scripts/bin/react-scripts.js start",
     "build-web": "node ../node_modules/react-scripts/bin/react-scripts.js build",
+    "total-build": "node ../node_modules/react-scripts/bin/react-scripts.js build && node encrypt.js && node obfuscate-main.js",
     "test": "node ../node_modules/react-scripts/bin/react-scripts.js test",
     "eject": "node ../node_modules/react-scripts/bin/react-scripts.js eject",
     "package": "electron-builder",
@@ -25,7 +26,9 @@
   "devDependencies": {
     "@babel/plugin-syntax-object-rest-spread": "^7.8.3",
     "electron": "^30.0.9",
-    "electron-builder": "^24.13.3"
+    "electron-builder": "^24.13.3",
+    "javascript-obfuscator": "^4.1.1",
+    "uglify-js": "^3.18.0"
   },
   "eslintConfig": {
     "extends": "react-app"
@@ -43,7 +46,7 @@
       "output": "dist"
     },
     "files": [
-      "./electron.js",
+      "electron.js",
       "package.json",
       {
         "from": "build",

Some files were not shown because too many files changed in this diff