DomainLockNode.ts 3.8 KB

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