SelfDefendingUnicodeNode.ts 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  1. import { TNodeWithBlockStatement } from '../../types/TNodeWithBlockStatement';
  2. import { TStatement } from '../../types/TStatement';
  3. import { IOptions } from '../../interfaces/IOptions';
  4. import { IStackTraceData } from '../../interfaces/stack-trace-analyzer/IStackTraceData';
  5. import { AppendState } from '../../enums/AppendState';
  6. import { NO_CUSTOM_NODES_PRESET } from '../../preset-options/NoCustomNodesPreset';
  7. import { SelfDefendingTemplate } from '../../templates/custom-nodes/self-defending-nodes/self-defending-unicode-node/SelfDefendingTemplate';
  8. import { AbstractCustomNode } from '../AbstractCustomNode';
  9. import { NodeAppender } from '../../node/NodeAppender';
  10. import { JavaScriptObfuscator } from '../../JavaScriptObfuscator';
  11. import { NodeUtils } from '../../node/NodeUtils';
  12. import { Utils } from '../../Utils';
  13. export class SelfDefendingUnicodeNode extends AbstractCustomNode {
  14. /**
  15. * @type {AppendState}
  16. */
  17. protected appendState: AppendState = AppendState.AfterObfuscation;
  18. /**
  19. * @type {string}
  20. */
  21. protected callsControllerFunctionName: string;
  22. /**
  23. * @type {number}
  24. */
  25. protected randomStackTraceIndex: number;
  26. /**
  27. * @type {IStackTraceData[]}
  28. */
  29. protected stackTraceData: IStackTraceData[];
  30. /**
  31. * @param stackTraceData
  32. * @param callsControllerFunctionName
  33. * @param randomStackTraceIndex
  34. * @param options
  35. */
  36. constructor (
  37. stackTraceData: IStackTraceData[],
  38. callsControllerFunctionName: string,
  39. randomStackTraceIndex: number,
  40. options: IOptions
  41. ) {
  42. super(options);
  43. this.stackTraceData = stackTraceData;
  44. this.callsControllerFunctionName = callsControllerFunctionName;
  45. this.randomStackTraceIndex = randomStackTraceIndex;
  46. }
  47. /**
  48. * @param blockScopeNode
  49. */
  50. public appendNode (blockScopeNode: TNodeWithBlockStatement): void {
  51. NodeAppender.appendNodeToOptimalBlockScope(
  52. this.stackTraceData,
  53. blockScopeNode,
  54. this.getNode(),
  55. this.randomStackTraceIndex
  56. );
  57. }
  58. /**
  59. * @returns {TStatement[]}
  60. */
  61. protected getNodeStructure (): TStatement[] {
  62. return NodeUtils.convertCodeToStructure(
  63. JavaScriptObfuscator.obfuscate(
  64. SelfDefendingTemplate().formatUnicorn({
  65. selfDefendingFunctionName: Utils.getRandomVariableName(),
  66. singleNodeCallControllerFunctionName: this.callsControllerFunctionName
  67. }),
  68. Object.assign({}, NO_CUSTOM_NODES_PRESET, {
  69. seed: this.options.seed
  70. })
  71. ).getObfuscatedCode()
  72. );
  73. }
  74. }