ConsoleOutputCustomNodeGroup.ts 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  1. import { injectable, inject } from 'inversify';
  2. import { ServiceIdentifiers } from '../../../container/ServiceIdentifiers';
  3. import { TCustomNodeFactory } from '../../../types/container/TCustomNodeFactory';
  4. import { TNodeWithBlockStatement } from '../../../types/node/TNodeWithBlockStatement';
  5. import { TObfuscationEvent } from '../../../types/event-emitters/TObfuscationEvent';
  6. import { ICustomNode } from '../../../interfaces/custom-nodes/ICustomNode';
  7. import { IObfuscationEventEmitter } from '../../../interfaces/event-emitters/IObfuscationEventEmitter';
  8. import { IOptions } from '../../../interfaces/options/IOptions';
  9. import { IStackTraceData } from '../../../interfaces/stack-trace-analyzer/IStackTraceData';
  10. import { initializable } from '../../../decorators/Initializable';
  11. import { CustomNodes } from '../../../enums/container/CustomNodes';
  12. import { ObfuscationEvents } from '../../../enums/ObfuscationEvents';
  13. import { AbstractCustomNodeGroup } from '../../AbstractCustomNodeGroup';
  14. import { NodeAppender } from '../../../node/NodeAppender';
  15. import { RandomGeneratorUtils } from '../../../utils/RandomGeneratorUtils';
  16. @injectable()
  17. export class ConsoleOutputCustomNodeGroup extends AbstractCustomNodeGroup {
  18. /**
  19. * @type {TObfuscationEvent}
  20. */
  21. protected readonly appendEvent: TObfuscationEvent = ObfuscationEvents.BeforeObfuscation;
  22. /**
  23. * @type {Map<CustomNodes, ICustomNode>}
  24. */
  25. @initializable()
  26. protected customNodes: Map <CustomNodes, ICustomNode>;
  27. /**
  28. * @type {TCustomNodeFactory}
  29. */
  30. private readonly customNodeFactory: TCustomNodeFactory;
  31. /**
  32. * @type {IObfuscationEventEmitter}
  33. */
  34. private readonly obfuscationEventEmitter: IObfuscationEventEmitter;
  35. /**
  36. * @param customNodeFactory
  37. * @param obfuscationEventEmitter
  38. * @param options
  39. */
  40. constructor (
  41. @inject(ServiceIdentifiers.Factory__ICustomNode) customNodeFactory: TCustomNodeFactory,
  42. @inject(ServiceIdentifiers.IObfuscationEventEmitter) obfuscationEventEmitter: IObfuscationEventEmitter,
  43. @inject(ServiceIdentifiers.IOptions) options: IOptions
  44. ) {
  45. super(options);
  46. this.customNodeFactory = customNodeFactory;
  47. this.obfuscationEventEmitter = obfuscationEventEmitter;
  48. }
  49. /**
  50. * @param blockScopeNode
  51. * @param stackTraceData
  52. */
  53. public appendCustomNodes (blockScopeNode: TNodeWithBlockStatement, stackTraceData: IStackTraceData[]): void {
  54. const randomStackTraceIndex: number = NodeAppender.getRandomStackTraceIndex(stackTraceData.length);
  55. // consoleOutputDisableExpressionNode append
  56. this.appendCustomNodeIfExist(CustomNodes.ConsoleOutputDisableExpressionNode, (customNode: ICustomNode) => {
  57. NodeAppender.appendNodeToOptimalBlockScope(
  58. stackTraceData,
  59. blockScopeNode,
  60. customNode.getNode(),
  61. randomStackTraceIndex
  62. );
  63. });
  64. // nodeCallsControllerFunctionNode append
  65. this.appendCustomNodeIfExist(CustomNodes.NodeCallsControllerFunctionNode, (customNode: ICustomNode) => {
  66. let targetBlockScope: TNodeWithBlockStatement;
  67. if (stackTraceData.length) {
  68. targetBlockScope = NodeAppender.getOptimalBlockScope(stackTraceData, randomStackTraceIndex, 1);
  69. } else {
  70. targetBlockScope = blockScopeNode;
  71. }
  72. NodeAppender.prependNode(targetBlockScope, customNode.getNode());
  73. });
  74. }
  75. public initialize (): void {
  76. this.customNodes = new Map <CustomNodes, ICustomNode> ();
  77. if (!this.options.disableConsoleOutput) {
  78. return;
  79. }
  80. const callsControllerFunctionName: string = RandomGeneratorUtils.getRandomVariableName();
  81. const consoleOutputDisableExpressionNode: ICustomNode = this.customNodeFactory(CustomNodes.ConsoleOutputDisableExpressionNode);
  82. const nodeCallsControllerFunctionNode: ICustomNode = this.customNodeFactory(CustomNodes.NodeCallsControllerFunctionNode);
  83. consoleOutputDisableExpressionNode.initialize(callsControllerFunctionName);
  84. nodeCallsControllerFunctionNode.initialize(this.appendEvent, callsControllerFunctionName);
  85. this.customNodes.set(CustomNodes.ConsoleOutputDisableExpressionNode, consoleOutputDisableExpressionNode);
  86. this.customNodes.set(CustomNodes.NodeCallsControllerFunctionNode, nodeCallsControllerFunctionNode);
  87. }
  88. }