JavaScriptObfuscatorCLI.ts 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428
  1. import * as commander from 'commander';
  2. import * as path from 'path';
  3. import { TInputCLIOptions } from '../types/options/TInputCLIOptions';
  4. import { TInputOptions } from '../types/options/TInputOptions';
  5. import { TSourceCodeData } from '../types/cli/TSourceCodeData';
  6. import { IFileData } from '../interfaces/cli/IFileData';
  7. import { IInitializable } from '../interfaces/IInitializable';
  8. import { IObfuscatedCode } from '../interfaces/source-code/IObfuscatedCode';
  9. import { initializable } from '../decorators/Initializable';
  10. import { DEFAULT_PRESET } from '../options/presets/Default';
  11. import { ArraySanitizer } from './sanitizers/ArraySanitizer';
  12. import { BooleanSanitizer } from './sanitizers/BooleanSanitizer';
  13. import { IdentifierNamesGeneratorSanitizer } from './sanitizers/IdentifierNamesGeneratorSanitizer';
  14. import { ObfuscationTargetSanitizer } from './sanitizers/ObfuscatingTargetSanitizer';
  15. import { SourceMapModeSanitizer } from './sanitizers/SourceMapModeSanitizer';
  16. import { StringArrayEncodingSanitizer } from './sanitizers/StringArrayEncodingSanitizer';
  17. import { CLIUtils } from './utils/CLIUtils';
  18. import { JavaScriptObfuscator } from '../JavaScriptObfuscatorFacade';
  19. import { SourceCodeReader } from './utils/SourceCodeReader';
  20. export class JavaScriptObfuscatorCLI implements IInitializable {
  21. /**
  22. * @type {BufferEncoding}
  23. */
  24. public static readonly encoding: BufferEncoding = 'utf8';
  25. /**
  26. * @type {string}
  27. */
  28. public static obfuscatedFilePrefix: string = '-obfuscated';
  29. /**
  30. * @type {string}
  31. */
  32. private static readonly baseIdentifiersPrefix: string = 'a';
  33. /**
  34. * @type {string[]}
  35. */
  36. private readonly arguments: string[];
  37. /**
  38. * @type {string[]}
  39. */
  40. private readonly rawArguments: string[];
  41. /**
  42. * @type {commander.CommanderStatic}
  43. */
  44. @initializable()
  45. private commands!: commander.CommanderStatic;
  46. /**
  47. * @type {TInputCLIOptions}
  48. */
  49. @initializable()
  50. private inputCLIOptions!: TInputCLIOptions;
  51. /**
  52. * @type {string}
  53. */
  54. @initializable()
  55. private inputPath!: string;
  56. /**
  57. * @param {string[]} argv
  58. */
  59. constructor (argv: string[]) {
  60. this.rawArguments = argv;
  61. this.arguments = argv.slice(2);
  62. }
  63. /**
  64. * @param {TObject} options
  65. * @returns {TInputOptions}
  66. */
  67. private static filterOptions (options: TInputCLIOptions): TInputOptions {
  68. const filteredOptions: TInputOptions = {};
  69. Object
  70. .keys(options)
  71. .forEach((option: keyof TInputCLIOptions) => {
  72. if (options[option] === undefined) {
  73. return;
  74. }
  75. filteredOptions[option] = options[option];
  76. });
  77. return filteredOptions;
  78. }
  79. /**
  80. * @param {string} sourceCode
  81. * @param {string} outputCodePath
  82. * @param {TInputOptions} options
  83. */
  84. private static processSourceCodeWithoutSourceMap (
  85. sourceCode: string,
  86. outputCodePath: string,
  87. options: TInputOptions
  88. ): void {
  89. const obfuscatedCode: string = JavaScriptObfuscator.obfuscate(sourceCode, options).getObfuscatedCode();
  90. CLIUtils.writeFile(outputCodePath, obfuscatedCode);
  91. }
  92. /**
  93. * @param {string} sourceCode
  94. * @param {string} outputCodePath
  95. * @param {TInputOptions} options
  96. */
  97. private static processSourceCodeWithSourceMap (
  98. sourceCode: string,
  99. outputCodePath: string,
  100. options: TInputOptions
  101. ): void {
  102. const outputSourceMapPath: string = CLIUtils.getOutputSourceMapPath(
  103. outputCodePath,
  104. options.sourceMapFileName || ''
  105. );
  106. options = {
  107. ...options,
  108. sourceMapFileName: path.basename(outputSourceMapPath)
  109. };
  110. const obfuscatedCode: IObfuscatedCode = JavaScriptObfuscator.obfuscate(sourceCode, options);
  111. CLIUtils.writeFile(outputCodePath, obfuscatedCode.getObfuscatedCode());
  112. if (options.sourceMapMode === 'separate' && obfuscatedCode.getSourceMap()) {
  113. CLIUtils.writeFile(outputSourceMapPath, obfuscatedCode.getSourceMap());
  114. }
  115. }
  116. public initialize (): void {
  117. this.inputPath = path.normalize(this.arguments[0] || '');
  118. this.commands = <commander.CommanderStatic>(new commander.Command());
  119. this.configureCommands();
  120. this.configureHelp();
  121. this.inputCLIOptions = this.commands.opts();
  122. }
  123. public run (): void {
  124. const canShowHelp: boolean = !this.arguments.length || this.arguments.includes('--help');
  125. if (canShowHelp) {
  126. this.commands.outputHelp();
  127. return;
  128. }
  129. const sourceCodeData: TSourceCodeData = new SourceCodeReader(this.inputCLIOptions)
  130. .readSourceCode(this.inputPath);
  131. this.processSourceCodeData(sourceCodeData);
  132. }
  133. /**
  134. * @returns {TInputOptions}
  135. */
  136. private buildOptions (): TInputOptions {
  137. const inputCLIOptions: TInputOptions = JavaScriptObfuscatorCLI.filterOptions(this.inputCLIOptions);
  138. const configFilePath: string | undefined = this.inputCLIOptions.config;
  139. const configFileLocation: string = configFilePath ? path.resolve(configFilePath, '.') : '';
  140. const configFileOptions: TInputOptions = configFileLocation ? CLIUtils.getUserConfig(configFileLocation) : {};
  141. const inputFileName: string = path.basename(this.inputPath);
  142. return {
  143. ...DEFAULT_PRESET,
  144. ...configFileOptions,
  145. ...inputCLIOptions,
  146. inputFileName
  147. };
  148. }
  149. private configureCommands (): void {
  150. this.commands
  151. .usage('<inputPath> [options]')
  152. .version(
  153. process.env.VERSION || 'unknown',
  154. '-v, --version'
  155. )
  156. .option(
  157. '-o, --output <path>',
  158. 'Output path for obfuscated code'
  159. )
  160. .option(
  161. '--compact <boolean>',
  162. 'Disable one line output code compacting',
  163. BooleanSanitizer
  164. )
  165. .option(
  166. '--config <boolean>',
  167. 'Name of js / json config file'
  168. )
  169. .option(
  170. '--control-flow-flattening <boolean>',
  171. 'Enables control flow flattening',
  172. BooleanSanitizer
  173. )
  174. .option(
  175. '--control-flow-flattening-threshold <number>',
  176. 'The probability that the control flow flattening transformation will be applied to the node',
  177. parseFloat
  178. )
  179. .option(
  180. '--dead-code-injection <boolean>',
  181. 'Enables dead code injection',
  182. BooleanSanitizer
  183. )
  184. .option(
  185. '--dead-code-injection-threshold <number>',
  186. 'The probability that the dead code injection transformation will be applied to the node',
  187. parseFloat
  188. )
  189. .option(
  190. '--debug-protection <boolean>',
  191. 'Disable browser Debug panel (can cause DevTools enabled browser freeze)',
  192. BooleanSanitizer
  193. )
  194. .option(
  195. '--debug-protection-interval <boolean>',
  196. 'Disable browser Debug panel even after page was loaded (can cause DevTools enabled browser freeze)',
  197. BooleanSanitizer
  198. )
  199. .option(
  200. '--disable-console-output <boolean>',
  201. 'Allow console.log, console.info, console.error and console.warn messages output into browser console',
  202. BooleanSanitizer
  203. )
  204. .option(
  205. '--domain-lock <list> (comma separated, without whitespaces)',
  206. 'Blocks the execution of the code in domains that do not match the passed RegExp patterns (comma separated)',
  207. ArraySanitizer
  208. )
  209. .option(
  210. '--exclude <list> (comma separated, without whitespaces)',
  211. 'A filename or glob which indicates files to exclude from obfuscation',
  212. ArraySanitizer
  213. )
  214. .option(
  215. '--identifier-names-generator <string>',
  216. 'Sets identifier names generator. ' +
  217. 'Values: hexadecimal, mangled, dictionary. ' +
  218. 'Default: hexadecimal',
  219. IdentifierNamesGeneratorSanitizer
  220. )
  221. .option(
  222. '--identifiers-prefix <string>',
  223. 'Sets prefix for all global identifiers.'
  224. )
  225. .option(
  226. '--identifiers-dictionary <list> (comma separated, without whitespaces)',
  227. 'Identifiers dictionary (comma separated) for `--identifier-names-generator dictionary` option',
  228. ArraySanitizer
  229. )
  230. .option(
  231. '--log <boolean>', 'Enables logging of the information to the console',
  232. BooleanSanitizer
  233. )
  234. .option(
  235. '--reserved-names <list> (comma separated, without whitespaces)',
  236. 'Disables obfuscation and generation of identifiers, which being matched by passed RegExp patterns (comma separated)',
  237. ArraySanitizer
  238. )
  239. .option(
  240. '--reserved-strings <list> (comma separated, without whitespaces)',
  241. 'Disables transformation of string literals, which being matched by passed RegExp patterns (comma separated)',
  242. ArraySanitizer
  243. )
  244. .option(
  245. '--rename-globals <boolean>', 'Allows to enable obfuscation of global variable and function names with declaration.',
  246. BooleanSanitizer
  247. )
  248. .option(
  249. '--rotate-string-array <boolean>', 'Disable rotation of unicode array values during obfuscation',
  250. BooleanSanitizer
  251. )
  252. .option(
  253. '--seed <string|number>',
  254. 'Sets seed for random generator. This is useful for creating repeatable results.',
  255. parseFloat
  256. )
  257. .option(
  258. '--self-defending <boolean>',
  259. 'Disables self-defending for obfuscated code',
  260. BooleanSanitizer
  261. )
  262. .option(
  263. '--source-map <boolean>',
  264. 'Enables source map generation',
  265. BooleanSanitizer
  266. )
  267. .option(
  268. '--source-map-base-url <string>',
  269. 'Sets base url to the source map import url when `--source-map-mode=separate`'
  270. )
  271. .option(
  272. '--source-map-file-name <string>',
  273. 'Sets file name for output source map when `--source-map-mode=separate`'
  274. )
  275. .option(
  276. '--source-map-mode <string>',
  277. 'Specify source map output mode. ' +
  278. 'Values: inline, separate. ' +
  279. 'Default: separate',
  280. SourceMapModeSanitizer
  281. )
  282. .option(
  283. '--split-strings <boolean>',
  284. 'Splits literal strings into chunks with length of `splitStringsChunkLength` option value',
  285. BooleanSanitizer
  286. )
  287. .option(
  288. '--split-strings-chunk-length <number>',
  289. 'Sets chunk length of `splitStrings` option',
  290. parseFloat
  291. )
  292. .option(
  293. '--string-array <boolean>',
  294. 'Disables gathering of all literal strings into an array and replacing every literal string with an array call',
  295. BooleanSanitizer
  296. )
  297. .option(
  298. '--string-array-encoding <string|boolean>',
  299. 'Encodes all strings in strings array using base64 or rc4 (this option can slow down your code speed. ' +
  300. 'Values: true, false, base64, rc4. ' +
  301. 'Default: false',
  302. StringArrayEncodingSanitizer
  303. )
  304. .option(
  305. '--string-array-threshold <number>',
  306. 'The probability that the literal string will be inserted into stringArray (Default: 0.8, Min: 0, Max: 1)',
  307. parseFloat
  308. )
  309. .option(
  310. '--target <string>',
  311. 'Allows to set target environment for obfuscated code. ' +
  312. 'Values: browser, browser-no-eval, node. ' +
  313. 'Default: browser',
  314. ObfuscationTargetSanitizer
  315. )
  316. .option(
  317. '--transform-object-keys <boolean>',
  318. 'Enables transformation of object keys',
  319. BooleanSanitizer
  320. )
  321. .option(
  322. '--unicode-escape-sequence <boolean>',
  323. 'Allows to enable/disable string conversion to unicode escape sequence',
  324. BooleanSanitizer
  325. )
  326. .parse(this.rawArguments);
  327. }
  328. private configureHelp (): void {
  329. this.commands.on('--help', () => {
  330. console.log(' Examples:\n');
  331. console.log(' %> javascript-obfuscator input_file_name.js --compact true --self-defending false');
  332. console.log(' %> javascript-obfuscator input_file_name.js --output output_file_name.js --compact true --self-defending false');
  333. console.log(' %> javascript-obfuscator input_directory_name --compact true --self-defending false');
  334. console.log('');
  335. });
  336. }
  337. /**
  338. * @param {TSourceCodeData} sourceCodeData
  339. */
  340. private processSourceCodeData (sourceCodeData: TSourceCodeData): void {
  341. const outputPath: string = this.inputCLIOptions.output
  342. ? path.normalize(this.inputCLIOptions.output)
  343. : '';
  344. if (!Array.isArray(sourceCodeData)) {
  345. const outputCodePath: string = outputPath || CLIUtils.getOutputCodePath(this.inputPath);
  346. this.processSourceCode(sourceCodeData, outputCodePath, null);
  347. } else {
  348. sourceCodeData.forEach(({ filePath, content }: IFileData, index: number) => {
  349. const outputCodePath: string = outputPath
  350. ? path.join(outputPath, filePath)
  351. : CLIUtils.getOutputCodePath(filePath);
  352. this.processSourceCode(content, outputCodePath, index);
  353. });
  354. }
  355. }
  356. /**
  357. * @param {string} sourceCode
  358. * @param {string} outputCodePath
  359. * @param {number | null} sourceCodeIndex
  360. */
  361. private processSourceCode (
  362. sourceCode: string,
  363. outputCodePath: string,
  364. sourceCodeIndex: number | null
  365. ): void {
  366. let options: TInputOptions = this.buildOptions();
  367. if (sourceCodeIndex !== null) {
  368. const baseIdentifiersPrefix: string = this.inputCLIOptions.identifiersPrefix
  369. || JavaScriptObfuscatorCLI.baseIdentifiersPrefix;
  370. const identifiersPrefix: string = `${baseIdentifiersPrefix}${sourceCodeIndex}`;
  371. options = {
  372. ...options,
  373. identifiersPrefix
  374. };
  375. }
  376. if (options.sourceMap) {
  377. JavaScriptObfuscatorCLI.processSourceCodeWithSourceMap(sourceCode, outputCodePath, options);
  378. } else {
  379. JavaScriptObfuscatorCLI.processSourceCodeWithoutSourceMap(sourceCode, outputCodePath, options);
  380. }
  381. }
  382. }