DebugProtectionFunctionNode.ts 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  1. import 'format-unicorn';
  2. import { TNodeWithBlockStatement } from '../../types/TNodeWithBlockStatement';
  3. import { IOptions } from '../../interfaces/IOptions';
  4. import { AppendState } from '../../enums/AppendState';
  5. import { DebugProtectionFunctionTemplate } from '../../templates/custom-nodes/debug-protection-nodes/debug-protection-function-node/DebugProtectionFunctionTemplate';
  6. import { AbstractCustomNode } from '../AbstractCustomNode';
  7. import { NodeAppender } from '../../node/NodeAppender';
  8. import { Utils } from '../../Utils';
  9. export class DebugProtectionFunctionNode extends AbstractCustomNode {
  10. /**
  11. * @type {AppendState}
  12. */
  13. protected appendState: AppendState = AppendState.BeforeObfuscation;
  14. /**
  15. * @type {string}
  16. */
  17. private debugProtectionFunctionName: string;
  18. /**
  19. * @param debugProtectionFunctionName
  20. * @param options
  21. */
  22. constructor (debugProtectionFunctionName: string, options: IOptions) {
  23. super(options);
  24. this.debugProtectionFunctionName = debugProtectionFunctionName;
  25. }
  26. /**
  27. * @param blockScopeNode
  28. */
  29. public appendNode (blockScopeNode: TNodeWithBlockStatement): void {
  30. let programBodyLength: number = blockScopeNode.body.length,
  31. randomIndex: number = Utils.getRandomGenerator().integer({
  32. min: 0,
  33. max: programBodyLength
  34. });
  35. NodeAppender.insertNodeAtIndex(blockScopeNode, this.getNode(), randomIndex);
  36. }
  37. /**
  38. * @returns {string}
  39. */
  40. public getCode (): string {
  41. return DebugProtectionFunctionTemplate().formatUnicorn({
  42. debugProtectionFunctionName: this.debugProtectionFunctionName
  43. });
  44. }
  45. /**
  46. * @returns {string}
  47. */
  48. public getNodeIdentifier (): string {
  49. return this.debugProtectionFunctionName;
  50. }
  51. }