StringArrayRotateFunctionNode.ts 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  1. import { injectable, inject } from 'inversify';
  2. import { ServiceIdentifiers } from '../../container/ServiceIdentifiers';
  3. import * as format from 'string-template';
  4. import { TNodeWithBlockStatement } from '../../types/node/TNodeWithBlockStatement';
  5. import { TObfuscationEvent } from '../../types/event-emitters/TObfuscationEvent';
  6. import { IOptions } from '../../interfaces/options/IOptions';
  7. import { IStackTraceData } from '../../interfaces/stack-trace-analyzer/IStackTraceData';
  8. import { IStorage } from '../../interfaces/storages/IStorage';
  9. import { ObfuscationEvents } from '../../enums/ObfuscationEvents';
  10. import { initializable } from '../../decorators/Initializable';
  11. import { NO_CUSTOM_NODES_PRESET } from '../../preset-options/NoCustomNodesPreset';
  12. import { SelfDefendingTemplate } from '../../templates/custom-nodes/string-array-nodes/string-array-rotate-function-node/SelfDefendingTemplate';
  13. import { StringArrayRotateFunctionTemplate } from '../../templates/custom-nodes/string-array-nodes/string-array-rotate-function-node/StringArrayRotateFunctionTemplate';
  14. import { AbstractCustomNode } from '../AbstractCustomNode';
  15. import { JavaScriptObfuscator } from '../../JavaScriptObfuscator';
  16. import { NodeAppender } from '../../node/NodeAppender';
  17. import { Utils } from '../../Utils';
  18. @injectable()
  19. export class StringArrayRotateFunctionNode extends AbstractCustomNode {
  20. /**
  21. * @type {TObfuscationEvent}
  22. */
  23. protected readonly appendEvent: TObfuscationEvent = ObfuscationEvents.AfterObfuscation;
  24. /**
  25. * @type {IStorage <string>}
  26. */
  27. @initializable()
  28. private stringArray: IStorage <string>;
  29. /**
  30. * @type {string}
  31. */
  32. @initializable()
  33. private stringArrayName: string;
  34. /**
  35. * @param {number}
  36. */
  37. @initializable()
  38. private stringArrayRotateValue: number;
  39. /**
  40. * @param options
  41. */
  42. constructor (
  43. @inject(ServiceIdentifiers.IOptions) options: IOptions
  44. ) {
  45. super(options);
  46. }
  47. /**
  48. * @param stringArray
  49. * @param stringArrayName
  50. * @param stringArrayRotateValue
  51. */
  52. public initialize (
  53. stringArray: IStorage <string>,
  54. stringArrayName: string,
  55. stringArrayRotateValue: number
  56. ): void {
  57. this.stringArray = stringArray;
  58. this.stringArrayName = stringArrayName;
  59. this.stringArrayRotateValue = stringArrayRotateValue;
  60. }
  61. /**
  62. * @param blockScopeNode
  63. * @param stackTraceData
  64. */
  65. public appendNode (blockScopeNode: TNodeWithBlockStatement, stackTraceData: IStackTraceData[]): void {
  66. if (!this.stringArray.getLength()) {
  67. return;
  68. }
  69. NodeAppender.insertNodeAtIndex(blockScopeNode, this.getNode(), 1);
  70. }
  71. /**
  72. * @returns {string}
  73. */
  74. public getCode (): string {
  75. let code: string = '',
  76. timesName: string = Utils.getRandomVariableName(),
  77. whileFunctionName: string = Utils.getRandomVariableName();
  78. if (this.options.selfDefending) {
  79. code = format(SelfDefendingTemplate(), {
  80. timesName,
  81. whileFunctionName
  82. });
  83. } else {
  84. code = `${whileFunctionName}(++${timesName})`;
  85. }
  86. return JavaScriptObfuscator.obfuscate(
  87. format(StringArrayRotateFunctionTemplate(), {
  88. code,
  89. timesName,
  90. stringArrayName: this.stringArrayName,
  91. stringArrayRotateValue: Utils.decToHex(this.stringArrayRotateValue),
  92. whileFunctionName
  93. }),
  94. Object.assign({}, NO_CUSTOM_NODES_PRESET, {
  95. seed: this.options.seed
  96. })
  97. ).getObfuscatedCode();
  98. }
  99. }