ConsoleOutputCustomNodeGroup.ts 4.3 KB

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