JavaScriptObfuscatorInternal.ts 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. import { injectable, inject } from 'inversify';
  2. import { ServiceIdentifiers } from './container/ServiceIdentifiers';
  3. import * as esprima from 'esprima';
  4. import * as escodegen from 'escodegen';
  5. import * as ESTree from 'estree';
  6. import { IJavaScriptObfuscator } from './interfaces/IJavaScriptObfsucator';
  7. import { IObfuscationResult } from './interfaces/IObfuscationResult';
  8. import { IObfuscator } from './interfaces/IObfuscator';
  9. import { IGeneratorOutput } from './interfaces/IGeneratorOutput';
  10. import { IOptions } from './interfaces/options/IOptions';
  11. import { ISourceMapCorrector } from './interfaces/ISourceMapCorrector';
  12. import { RandomGeneratorUtils } from './utils/RandomGeneratorUtils';
  13. @injectable()
  14. export class JavaScriptObfuscatorInternal implements IJavaScriptObfuscator {
  15. /**
  16. * @type {GenerateOptions}
  17. */
  18. private static readonly escodegenParams: escodegen.GenerateOptions = {
  19. verbatim: 'x-verbatim-property',
  20. sourceMapWithCode: true
  21. };
  22. /**
  23. * @type {esprima.Options}
  24. */
  25. private static readonly esprimaParams: esprima.Options = {
  26. loc: true
  27. };
  28. /**
  29. * @type {IObfuscator}
  30. */
  31. private readonly obfuscator: IObfuscator;
  32. /**
  33. * @type {IOptions}
  34. */
  35. private readonly options: IOptions;
  36. /**
  37. * @type {ISourceMapCorrector}
  38. */
  39. private readonly sourceMapCorrector: ISourceMapCorrector;
  40. /**
  41. * @param obfuscator
  42. * @param sourceMapCorrector
  43. * @param options
  44. */
  45. constructor (
  46. @inject(ServiceIdentifiers.IObfuscator) obfuscator: IObfuscator,
  47. @inject(ServiceIdentifiers.ISourceMapCorrector) sourceMapCorrector: ISourceMapCorrector,
  48. @inject(ServiceIdentifiers.IOptions) options: IOptions
  49. ) {
  50. this.obfuscator = obfuscator;
  51. this.sourceMapCorrector = sourceMapCorrector;
  52. this.options = options;
  53. }
  54. /**
  55. * @param sourceCode
  56. * @param astTree
  57. */
  58. private generateCode (sourceCode: string, astTree: ESTree.Program): IGeneratorOutput {
  59. const escodegenParams: escodegen.GenerateOptions = {
  60. ...JavaScriptObfuscatorInternal.escodegenParams
  61. };
  62. if (this.options.sourceMap) {
  63. escodegenParams.sourceMap = 'sourceMap';
  64. escodegenParams.sourceContent = sourceCode;
  65. }
  66. escodegenParams.format = {
  67. compact: this.options.compact
  68. };
  69. const generatorOutput: IGeneratorOutput = escodegen.generate(astTree, escodegenParams);
  70. generatorOutput.map = generatorOutput.map ? generatorOutput.map.toString() : '';
  71. return generatorOutput;
  72. }
  73. /**
  74. * @param generatorOutput
  75. * @returns {IObfuscationResult}
  76. */
  77. private getObfuscationResult (generatorOutput: IGeneratorOutput): IObfuscationResult {
  78. return this.sourceMapCorrector.correct(
  79. generatorOutput.code,
  80. generatorOutput.map
  81. );
  82. }
  83. /**
  84. * @param sourceCode
  85. * @returns {IObfuscationResult}
  86. */
  87. public obfuscate (sourceCode: string): IObfuscationResult {
  88. if (this.options.seed !== 0) {
  89. RandomGeneratorUtils.setRandomGeneratorSeed(this.options.seed);
  90. }
  91. // parse AST tree
  92. const astTree: ESTree.Program = esprima.parse(sourceCode, JavaScriptObfuscatorInternal.esprimaParams);
  93. // obfuscate AST tree
  94. const obfuscatedAstTree: ESTree.Program = this.obfuscator.obfuscateAstTree(astTree);
  95. // generate code
  96. const generatorOutput: IGeneratorOutput = this.generateCode(sourceCode, obfuscatedAstTree);
  97. return this.getObfuscationResult(generatorOutput);
  98. }
  99. }