CLI tool for obfuscating URLs in JavaScript/TypeScript files
npm install output-obfuscator-pluginCLI 工具,用于混淆 JavaScript/TypeScript 文件中的 URL 字符串。
- 两种混淆模式:Simple(URL 混淆)和 Advanced(完整混淆)
- 支持 TypeScript 配置文件
- 零配置开箱即用
- 通过 npx 直接运行,无需安装
直接运行:
``bash`
npx -y output-obfuscator-plugin@latest ./dist
在 package.json 的 scripts 中添加 postbuild 脚本,构建完成后自动执行混淆:
`json`
{
"scripts": {
"build": "vite build",
"postbuild": "npx -y output-obfuscator-plugin@latest ./dist"
}
}
> 提示:-y 参数让 npx 自动同意安装,适用于 CI/CD 环境(GitHub Actions、GitLab CI 等)。
在项目根目录创建 output_obfuscator.ts 或 output_obfuscator.js:
`typescript
// output_obfuscator.ts
import type { CLIConfig } from 'output-obfuscator-plugin'
export default {
mode: 'simple',
extensions: ['.js'],
exclude: ['/vendor/'],
logLevel: 'info'
} satisfies CLIConfig
`
| 参数 | 类型 | 默认值 | 说明 |
|------|------|--------|------|
| mode | 'simple' \| 'advanced' | 'simple' | 混淆模式 |extensions
| | string[] | ['.js', '.ts', '.jsx', '.tsx'] | 处理的文件扩展名 |exclude
| | string[] | ['/node_modules/'] | 排除的文件 Glob 模式 |urlPattern
| | RegExp | 见下方 | URL 匹配正则 |obfuscatorOptions
| | ObfuscatorOptions | - | Advanced 模式配置 |logLevel
| | 'silent' \| 'info' \| 'debug' | 'info' | 日志级别 |
`javascript\)]+/g
/(?:https?|wss?):\/\/[^\s"'
``
专注于 URL 字符串混淆,随机使用以下三种方式之一:
| 方式 | 示例 |
|------|------|
| Base64 | atob("aHR0cHM6Ly9leGFtcGxlLmNvbQ==") |"\u0068\u0074\u0074\u0070\u0073..."
| Unicode | |String.fromCharCode(104, 116, 116, 112, ...)
| CharCodes | |
优点:轻量、快速、对代码体积影响小
基于 javascript-obfuscator,提供完整的代码混淆功能。
预设级别
| 预设 | 特性 |
|------|------|
| low | 字符串数组(50%),紧凑格式 |medium
| | 控制流扁平化(50%),字符串数组+Base64(75%) |high
| | 控制流扁平化(75%),死代码注入(40%),对象键转换,Unicode 转义 |
`typescript`
// output_obfuscator.ts
export default {
mode: 'simple',
extensions: ['.js'],
logLevel: 'info'
}
`typescript`
// output_obfuscator.ts
export default {
mode: 'advanced',
extensions: ['.js'],
exclude: ['/vendor/', '/lib/'],
obfuscatorOptions: {
preset: 'medium'
}
}
`typescript`
// output_obfuscator.ts
export default {
mode: 'advanced',
obfuscatorOptions: {
compact: true,
controlFlowFlattening: true,
controlFlowFlatteningThreshold: 0.5,
stringArray: true,
stringArrayEncoding: ['base64']
}
}
完整配置项参考 javascript-obfuscator 文档
`typescript`
// output_obfuscator.ts
export default {
mode: 'simple',
urlPattern: /https:\/\/api\.[^\s"']+/g // 只匹配 api. 开头的 URL
}
| 环境 | 版本要求 |
|------|----------|
| Node.js | >= 18 |
浏览器环境
- Simple 模式的 Base64 混淆需要 atob() 支持(IE10+)
- Unicode 和 CharCodes 方式兼容所有浏览器
1. 仅在生产环境使用 - 开发环境启用会影响调试体验
2. 性能影响 - Advanced 模式的 high 预设会显著增加代码体积和执行时间low
3. 建议从低级别开始 - 先用 或 medium` 测试,再根据需要提升
4. Source Map - 混淆后 Source Map 将失效
本工具仅供学习和测试使用,可能存在未知问题。
使用前请务必:
1. 在测试环境充分验证混淆后的代码功能
2. 确保混淆后的代码运行正常
3. 不要直接在生产环境使用未经验证的混淆代码
作者不对因使用本工具导致的任何问题承担责任。
MIT