/** * 代码混淆配置 * 用于保护许可证验证相关的敏感代码 */ const JavaScriptObfuscator = require('javascript-obfuscator'); // 混淆配置选项 const obfuscatorOptions = { // 基础选项 compact: true, simplify: true, // 控制流扁平化 - 增加逆向难度 controlFlowFlattening: true, controlFlowFlatteningThreshold: 0.75, // 死代码注入 - 增加分析难度 deadCodeInjection: true, deadCodeInjectionThreshold: 0.4, // 字符串混淆 stringArray: true, stringArrayEncoding: ['base64'], stringArrayThreshold: 0.75, stringArrayRotate: true, stringArrayShuffle: true, // 标识符混淆 identifierNamesGenerator: 'hexadecimal', renameGlobals: false, // 不重命名全局变量,避免破坏模块导出 // 调试保护 debugProtection: false, // 生产环境可以开启 debugProtectionInterval: 0, // 禁用控制台输出(可选) disableConsoleOutput: false, // 自我保护 - 防止代码被格式化 selfDefending: true, // 分割字符串 splitStrings: true, splitStringsChunkLength: 10, // 转换对象键 transformObjectKeys: true, // Unicode 转义 unicodeEscapeSequence: false, // 保留名称(不混淆的标识符) reservedNames: [ // Next.js 相关 'getServerSideProps', 'getStaticProps', 'getStaticPaths', 'default', 'metadata', // React 相关 'useState', 'useEffect', 'useCallback', 'useMemo', 'useRef', // 导出的函数名 'activate', 'verify', 'heartbeat', 'getLicense', 'isFeatureEnabled', 'checkLimit', ], // 保留字符串(不混淆的字符串) reservedStrings: [ // API 路径 '/api/license/', // 环境变量名 'LICENSE_SERVER_URL', 'DATABASE_URL', ], }; // 需要混淆的文件模式 const filesToObfuscate = [ // 许可证相关核心文件 'lib/license/**/*.js', 'lib/license/**/*.ts', // 加密服务 'lib/license/crypto-service.js', 'lib/license/crypto-service.ts', // 指纹服务 'lib/license/fingerprint-service.js', 'lib/license/fingerprint-service.ts', // 许可证服务 'lib/license/license-service.js', 'lib/license/license-service.ts', ]; // 排除的文件 const excludeFiles = [ // 类型定义文件 '**/*.d.ts', // 测试文件 '**/__tests__/**', '**/*.test.ts', '**/*.test.tsx', '**/*.spec.ts', '**/*.spec.tsx', // node_modules '**/node_modules/**', ]; /** * 混淆单个文件内容 */ function obfuscateCode(code, filename = '') { try { const result = JavaScriptObfuscator.obfuscate(code, { ...obfuscatorOptions, sourceMap: true, sourceMapMode: 'separate', inputFileName: filename, }); return { code: result.getObfuscatedCode(), sourceMap: result.getSourceMap(), }; } catch (error) { console.error(`混淆失败 [${filename}]:`, error.message); return { code, sourceMap: null }; } } module.exports = { obfuscatorOptions, filesToObfuscate, excludeFiles, obfuscateCode, };