DomainLockCodeHelper.ts 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  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 { ICustomCodeHelperFormatter } from '../../interfaces/custom-code-helpers/ICustomCodeHelperFormatter';
  6. import { ICustomCodeHelperObfuscator } from '../../interfaces/custom-code-helpers/ICustomCodeHelperObfuscator';
  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 { DomainLockTemplate } from './templates/DomainLockTemplate';
  13. import { GlobalVariableNoEvalTemplate } from '../common/templates/GlobalVariableNoEvalTemplate';
  14. import { AbstractCustomCodeHelper } from '../AbstractCustomCodeHelper';
  15. import { NodeUtils } from '../../node/NodeUtils';
  16. @injectable()
  17. export class DomainLockCodeHelper extends AbstractCustomCodeHelper {
  18. /**
  19. * @type {string}
  20. */
  21. @initializable()
  22. private callsControllerFunctionName!: string;
  23. /**
  24. * @type {string}
  25. */
  26. @initializable()
  27. private domainLockFunctionName!: string;
  28. /**
  29. * @type {ICryptUtils}
  30. */
  31. private readonly cryptUtils: ICryptUtils;
  32. /**
  33. * @param {TIdentifierNamesGeneratorFactory} identifierNamesGeneratorFactory
  34. * @param {ICustomCodeHelperFormatter} customCodeHelperFormatter
  35. * @param {ICustomCodeHelperObfuscator} customCodeHelperObfuscator
  36. * @param {IRandomGenerator} randomGenerator
  37. * @param {IOptions} options
  38. * @param {ICryptUtils} cryptUtils
  39. */
  40. public constructor (
  41. @inject(ServiceIdentifiers.Factory__IIdentifierNamesGenerator)
  42. identifierNamesGeneratorFactory: TIdentifierNamesGeneratorFactory,
  43. @inject(ServiceIdentifiers.ICustomCodeHelperFormatter) customCodeHelperFormatter: ICustomCodeHelperFormatter,
  44. @inject(ServiceIdentifiers.ICustomCodeHelperObfuscator) customCodeHelperObfuscator: ICustomCodeHelperObfuscator,
  45. @inject(ServiceIdentifiers.IRandomGenerator) randomGenerator: IRandomGenerator,
  46. @inject(ServiceIdentifiers.IOptions) options: IOptions,
  47. @inject(ServiceIdentifiers.ICryptUtils) cryptUtils: ICryptUtils
  48. ) {
  49. super(
  50. identifierNamesGeneratorFactory,
  51. customCodeHelperFormatter,
  52. customCodeHelperObfuscator,
  53. randomGenerator,
  54. options
  55. );
  56. this.cryptUtils = cryptUtils;
  57. }
  58. /**
  59. * @param {string} callsControllerFunctionName
  60. * @param {string} domainLockFunctionName
  61. */
  62. public initialize (callsControllerFunctionName: string, domainLockFunctionName: string): void {
  63. this.callsControllerFunctionName = callsControllerFunctionName;
  64. this.domainLockFunctionName = domainLockFunctionName;
  65. }
  66. /**
  67. * @param {string} codeHelperTemplate
  68. * @returns {TStatement[]}
  69. */
  70. protected getNodeStructure (codeHelperTemplate: string): TStatement[] {
  71. return NodeUtils.convertCodeToStructure(codeHelperTemplate);
  72. }
  73. /**
  74. * @returns {string}
  75. */
  76. protected override getCodeHelperTemplate (): string {
  77. const domainsString: string = this.options.domainLock.join(';');
  78. const [hiddenDomainsString, diff]: string[] = this.cryptUtils.hideString(
  79. domainsString,
  80. domainsString.length * 3
  81. );
  82. const globalVariableTemplate: string = this.options.target !== ObfuscationTarget.BrowserNoEval
  83. ? this.getGlobalVariableTemplate()
  84. : GlobalVariableNoEvalTemplate();
  85. return this.customCodeHelperFormatter.formatTemplate(DomainLockTemplate(), {
  86. callControllerFunctionName: this.callsControllerFunctionName,
  87. domainLockFunctionName: this.domainLockFunctionName,
  88. diff,
  89. domains: hiddenDomainsString,
  90. globalVariableTemplate
  91. });
  92. }
  93. }