JavaScriptObfuscatorInternal.ts 3.3 KB

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