DomainLockNode.ts 3.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  1. import { inject, injectable, } from 'inversify';
  2. import { ServiceIdentifiers } from '../../container/ServiceIdentifiers';
  3. import * as format from 'string-template';
  4. import { TIdentifierNamesGeneratorFactory } from '../../types/container/generators/TIdentifierNamesGeneratorFactory';
  5. import { TStatement } from '../../types/node/TStatement';
  6. import { ICryptUtils } from '../../interfaces/utils/ICryptUtils';
  7. import { IOptions } from '../../interfaces/options/IOptions';
  8. import { IRandomGenerator } from '../../interfaces/utils/IRandomGenerator';
  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 {IRandomGenerator} randomGenerator
  29. * @param {ICryptUtils} cryptUtils
  30. * @param {IOptions} options
  31. */
  32. constructor (
  33. @inject(ServiceIdentifiers.Factory__IIdentifierNamesGenerator)
  34. identifierNamesGeneratorFactory: TIdentifierNamesGeneratorFactory,
  35. @inject(ServiceIdentifiers.IRandomGenerator) randomGenerator: IRandomGenerator,
  36. @inject(ServiceIdentifiers.ICryptUtils) cryptUtils: ICryptUtils,
  37. @inject(ServiceIdentifiers.IOptions) options: IOptions
  38. ) {
  39. super(identifierNamesGeneratorFactory, randomGenerator, options);
  40. this.cryptUtils = cryptUtils;
  41. }
  42. /**
  43. * @param {string} callsControllerFunctionName
  44. */
  45. public initialize (callsControllerFunctionName: string): void {
  46. this.callsControllerFunctionName = callsControllerFunctionName;
  47. }
  48. /**
  49. * @returns {TStatement[]}
  50. */
  51. protected getNodeStructure (): TStatement[] {
  52. return NodeUtils.convertCodeToStructure(this.getTemplate());
  53. }
  54. /**
  55. * @returns {string}
  56. */
  57. protected getTemplate (): string {
  58. const domainsString: string = this.options.domainLock.join(';');
  59. const [hiddenDomainsString, diff]: string[] = this.cryptUtils.hideString(
  60. domainsString,
  61. domainsString.length * 3
  62. );
  63. const globalVariableTemplate: string = this.options.target !== ObfuscationTarget.Extension
  64. ? this.getGlobalVariableTemplate()
  65. : GlobalVariableNoEvalTemplate();
  66. return format(DomainLockNodeTemplate(), {
  67. domainLockFunctionName: this.identifierNamesGenerator.generate(6),
  68. diff: diff,
  69. domains: hiddenDomainsString,
  70. globalVariableTemplate,
  71. singleNodeCallControllerFunctionName: this.callsControllerFunctionName
  72. });
  73. }
  74. }