JavaScriptObfuscatorCLI.ts 14 KB

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