DomainLockCustomCodeHelperGroup.ts 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  1. import { inject, injectable, } from 'inversify';
  2. import { ServiceIdentifiers } from '../../../container/ServiceIdentifiers';
  3. import { TCustomCodeHelperFactory } from '../../../types/container/custom-code-helpers/TCustomCodeHelperFactory';
  4. import { TIdentifierNamesGeneratorFactory } from '../../../types/container/generators/TIdentifierNamesGeneratorFactory';
  5. import { TInitialData } from '../../../types/TInitialData';
  6. import { TNodeWithLexicalScope } from '../../../types/node/TNodeWithLexicalScope';
  7. import { TNodeWithStatements } from '../../../types/node/TNodeWithStatements';
  8. import { ICustomCodeHelper } from '../../../interfaces/custom-code-helpers/ICustomCodeHelper';
  9. import { IOptions } from '../../../interfaces/options/IOptions';
  10. import { IRandomGenerator } from '../../../interfaces/utils/IRandomGenerator';
  11. import { ICallsGraphData } from '../../../interfaces/analyzers/calls-graph-analyzer/ICallsGraphData';
  12. import { initializable } from '../../../decorators/Initializable';
  13. import { CustomCodeHelper } from '../../../enums/custom-code-helpers/CustomCodeHelper';
  14. import { ObfuscationEvent } from '../../../enums/event-emitters/ObfuscationEvent';
  15. import { AbstractCustomCodeHelperGroup } from '../../AbstractCustomCodeHelperGroup';
  16. import { CallsControllerFunctionCodeHelper } from '../../calls-controller/CallsControllerFunctionCodeHelper';
  17. import { DomainLockCodeHelper } from '../DomainLockCodeHelper';
  18. import { NodeAppender } from '../../../node/NodeAppender';
  19. import { NodeLexicalScopeUtils } from '../../../node/NodeLexicalScopeUtils';
  20. @injectable()
  21. export class DomainLockCustomCodeHelperGroup extends AbstractCustomCodeHelperGroup {
  22. /**
  23. * @type {Map<CustomCodeHelper, ICustomCodeHelper>}
  24. */
  25. @initializable()
  26. protected customCodeHelpers!: Map <CustomCodeHelper, ICustomCodeHelper>;
  27. /**
  28. * @type {ObfuscationEvent}
  29. */
  30. protected readonly appendEvent: ObfuscationEvent = ObfuscationEvent.BeforeObfuscation;
  31. /**
  32. * @type {TCustomCodeHelperFactory}
  33. */
  34. private readonly customCodeHelperFactory: TCustomCodeHelperFactory;
  35. /**
  36. * @param {TCustomCodeHelperFactory} customCodeHelperFactory
  37. * @param {TIdentifierNamesGeneratorFactory} identifierNamesGeneratorFactory
  38. * @param {IRandomGenerator} randomGenerator
  39. * @param {IOptions} options
  40. */
  41. public constructor (
  42. @inject(ServiceIdentifiers.Factory__ICustomCodeHelper) customCodeHelperFactory: TCustomCodeHelperFactory,
  43. @inject(ServiceIdentifiers.Factory__IIdentifierNamesGenerator)
  44. identifierNamesGeneratorFactory: TIdentifierNamesGeneratorFactory,
  45. @inject(ServiceIdentifiers.IRandomGenerator) randomGenerator: IRandomGenerator,
  46. @inject(ServiceIdentifiers.IOptions) options: IOptions
  47. ) {
  48. super(identifierNamesGeneratorFactory, randomGenerator, options);
  49. this.customCodeHelperFactory = customCodeHelperFactory;
  50. }
  51. /**
  52. * @param {TNodeWithStatements} nodeWithStatements
  53. * @param {ICallsGraphData[]} callsGraphData
  54. */
  55. public appendNodes (nodeWithStatements: TNodeWithStatements, callsGraphData: ICallsGraphData[]): void {
  56. if (!this.options.domainLock.length) {
  57. return;
  58. }
  59. const randomCallsGraphIndex: number = this.getRandomCallsGraphIndex(callsGraphData.length);
  60. const domainLockFunctionHostNode: TNodeWithStatements = callsGraphData.length
  61. ? NodeAppender.getOptimalBlockScope(callsGraphData, randomCallsGraphIndex)
  62. : nodeWithStatements;
  63. const callsControllerHostNode: TNodeWithStatements = callsGraphData.length
  64. ? NodeAppender.getOptimalBlockScope(callsGraphData, randomCallsGraphIndex, 1)
  65. : nodeWithStatements;
  66. const domainLockFunctionLexicalScopeNode: TNodeWithLexicalScope | null = NodeLexicalScopeUtils
  67. .getLexicalScope(domainLockFunctionHostNode) ?? null;
  68. const domainLockFunctionName: string = domainLockFunctionLexicalScopeNode
  69. ? this.identifierNamesGenerator.generate(domainLockFunctionLexicalScopeNode)
  70. : this.identifierNamesGenerator.generateNext();
  71. const callsControllerFunctionName: string = domainLockFunctionLexicalScopeNode
  72. ? this.identifierNamesGenerator.generate(domainLockFunctionLexicalScopeNode)
  73. : this.identifierNamesGenerator.generateNext();
  74. // domainLock helper nodes append
  75. this.appendCustomNodeIfExist(
  76. CustomCodeHelper.DomainLock,
  77. (customCodeHelper: ICustomCodeHelper<TInitialData<DomainLockCodeHelper>>) => {
  78. customCodeHelper.initialize(callsControllerFunctionName, domainLockFunctionName);
  79. NodeAppender.prepend(domainLockFunctionHostNode, customCodeHelper.getNode());
  80. }
  81. );
  82. // nodeCallsControllerFunction helper nodes append
  83. this.appendCustomNodeIfExist(
  84. CustomCodeHelper.CallsControllerFunction,
  85. (customCodeHelper: ICustomCodeHelper<TInitialData<CallsControllerFunctionCodeHelper>>) => {
  86. customCodeHelper.initialize(this.appendEvent, callsControllerFunctionName);
  87. NodeAppender.prepend(callsControllerHostNode, customCodeHelper.getNode());
  88. }
  89. );
  90. }
  91. public initialize (): void {
  92. this.customCodeHelpers = new Map <CustomCodeHelper, ICustomCodeHelper>();
  93. if (!this.options.domainLock.length) {
  94. return;
  95. }
  96. const domainLockCodeHelper: ICustomCodeHelper<TInitialData<DomainLockCodeHelper>> =
  97. this.customCodeHelperFactory(CustomCodeHelper.DomainLock);
  98. const callsControllerFunctionCodeHelper: ICustomCodeHelper<TInitialData<CallsControllerFunctionCodeHelper>> =
  99. this.customCodeHelperFactory(CustomCodeHelper.CallsControllerFunction);
  100. this.customCodeHelpers.set(CustomCodeHelper.DomainLock, domainLockCodeHelper);
  101. this.customCodeHelpers.set(CustomCodeHelper.CallsControllerFunction, callsControllerFunctionCodeHelper);
  102. }
  103. }