Skip to content

obfuscation

什么是 obfuscation

obfuscation 是代码加密的意思,虽说 js 都是明文存放,但是通过加密后的代码变得更加难以阅读,甚至难以调试从而达到保护核心代码不外泄. 我们采用 javascript-obfuscator 这个库进行代码加密. 它内置的配置为

typescript
let obfuscationResult = JavaScriptObfuscator.obfuscate(content, {
  compact: true,
  controlFlowFlattening: true,
  controlFlowFlatteningThreshold: 1,
  numbersToExpressions: true,
  simplify: true,

  stringArray: true,
  stringArrayCallsTransform: true,
  stringArrayEncoding: ["base64", "rc4"],
  stringArrayIndexShift: true,
  stringArrayRotate: true,
  stringArrayShuffle: true,
  stringArrayWrappersCount: 3,
  stringArrayWrappersChainedCalls: true,
  stringArrayWrappersParametersMaxCount: 2,
  stringArrayWrappersType: "variable",
  stringArrayThreshold: 1,

  splitStrings: true,
  unicodeEscapeSequence: true,
});
let obfuscationResult = JavaScriptObfuscator.obfuscate(content, {
  compact: true,
  controlFlowFlattening: true,
  controlFlowFlatteningThreshold: 1,
  numbersToExpressions: true,
  simplify: true,

  stringArray: true,
  stringArrayCallsTransform: true,
  stringArrayEncoding: ["base64", "rc4"],
  stringArrayIndexShift: true,
  stringArrayRotate: true,
  stringArrayShuffle: true,
  stringArrayWrappersCount: 3,
  stringArrayWrappersChainedCalls: true,
  stringArrayWrappersParametersMaxCount: 2,
  stringArrayWrappersType: "variable",
  stringArrayThreshold: 1,

  splitStrings: true,
  unicodeEscapeSequence: true,
});

让我们看看一段代码加密前后的区别

加密前

img_1.png

加密后(你可以直接放弃阅读了)

img.png

如何配置

传入 obfuscation 它的类型为 Array<"injects" | "transformers">,也就是说它只支持对于 injectScript 和 transformers 的加密,因为我们的核心代码只存在于这两种方式的文件中.

typescript
import { defineConfig } from "vite";

export default defineConfig({
  //  ...
  plugins: [
    //    ...
    //    在这里你可以配置所有 webextkit 提供的 vite 插件
    ViteWebExtKits({
      extensionId: extId,
      obfuscation: ["injects", "transformers"],
    }),
  ],
});
import { defineConfig } from "vite";

export default defineConfig({
  //  ...
  plugins: [
    //    ...
    //    在这里你可以配置所有 webextkit 提供的 vite 插件
    ViteWebExtKits({
      extensionId: extId,
      obfuscation: ["injects", "transformers"],
    }),
  ],
});

WARNING

加密行为只有在打包时 pnpm run build 才会生效,开发环境中是不会加密的(提高编译速度)

Powered by Vitepress