DomainLockNode.ts 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. import { inject, injectable, } from 'inversify';
  2. import { ServiceIdentifiers } from '../../container/ServiceIdentifiers';
  3. import { TIdentifierNamesGeneratorFactory } from '../../types/container/generators/TIdentifierNamesGeneratorFactory';
  4. import { TStatement } from '../../types/node/TStatement';
  5. import { ICryptUtils } from '../../interfaces/utils/ICryptUtils';
  6. import { IOptions } from '../../interfaces/options/IOptions';
  7. import { IRandomGenerator } from '../../interfaces/utils/IRandomGenerator';
  8. import { ICustomNodeFormatter } from '../../interfaces/custom-nodes/ICustomNodeFormatter';
  9. import { ObfuscationTarget } from '../../enums/ObfuscationTarget';
  10. import { initializable } from '../../decorators/Initializable';
  11. import { DomainLockNodeTemplate } from '../../templates/domain-lock-nodes/domain-lock-node/DomainLockNodeTemplate';
  12. import { GlobalVariableNoEvalTemplate } from '../../templates/GlobalVariableNoEvalTemplate';
  13. import { AbstractCustomNode } from '../AbstractCustomNode';
  14. import { NodeUtils } from '../../node/NodeUtils';
  15. @injectable()
  16. export class DomainLockNode extends AbstractCustomNode {
  17. /**
  18. * @type {string}
  19. */
  20. @initializable()
  21. protected callsControllerFunctionName!: string;
  22. /**
  23. * @type {ICryptUtils}
  24. */
  25. private readonly cryptUtils: ICryptUtils;
  26. /**
  27. * @param {TIdentifierNamesGeneratorFactory} identifierNamesGeneratorFactory
  28. * @param {ICustomNodeFormatter} customNodeFormatter
  29. * @param {IRandomGenerator} randomGenerator
  30. * @param {IOptions} options
  31. * @param {ICryptUtils} cryptUtils
  32. */
  33. constructor (
  34. @inject(ServiceIdentifiers.Factory__IIdentifierNamesGenerator)
  35. identifierNamesGeneratorFactory: TIdentifierNamesGeneratorFactory,
  36. @inject(ServiceIdentifiers.ICustomNodeFormatter) customNodeFormatter: ICustomNodeFormatter,
  37. @inject(ServiceIdentifiers.IRandomGenerator) randomGenerator: IRandomGenerator,
  38. @inject(ServiceIdentifiers.IOptions) options: IOptions,
  39. @inject(ServiceIdentifiers.ICryptUtils) cryptUtils: ICryptUtils
  40. ) {
  41. super(identifierNamesGeneratorFactory, customNodeFormatter, randomGenerator, options);
  42. this.cryptUtils = cryptUtils;
  43. }
  44. /**
  45. * @param {string} callsControllerFunctionName
  46. */
  47. public initialize (callsControllerFunctionName: string): void {
  48. this.callsControllerFunctionName = callsControllerFunctionName;
  49. }
  50. /**
  51. * @returns {TStatement[]}
  52. */
  53. protected getNodeStructure (): TStatement[] {
  54. return NodeUtils.convertCodeToStructure(this.getTemplate());
  55. }
  56. /**
  57. * @returns {string}
  58. */
  59. protected getTemplate (): string {
  60. const domainsString: string = this.options.domainLock.join(';');
  61. const [hiddenDomainsString, diff]: string[] = this.cryptUtils.hideString(
  62. domainsString,
  63. domainsString.length * 3
  64. );
  65. const globalVariableTemplate: string = this.options.target !== ObfuscationTarget.BrowserNoEval
  66. ? this.getGlobalVariableTemplate()
  67. : GlobalVariableNoEvalTemplate();
  68. return this.customNodeFormatter.formatTemplate(DomainLockNodeTemplate(), {
  69. domainLockFunctionName: this.identifierNamesGenerator.generate(),
  70. diff: diff,
  71. domains: hiddenDomainsString,
  72. globalVariableTemplate,
  73. singleNodeCallControllerFunctionName: this.callsControllerFunctionName
  74. });
  75. }
  76. }