DebugProtectionFunctionNode.ts 2.1 KB

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