DomainLockNode.ts 2.8 KB

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