NodeCallsControllerFunctionNode.ts 2.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  1. import { injectable, inject } from 'inversify';
  2. import { ServiceIdentifiers } from '../../container/ServiceIdentifiers';
  3. import * as format from 'string-template';
  4. import { TObfuscationEvent } from '../../types/event-emitters/TObfuscationEvent';
  5. import { TStatement } from '../../types/node/TStatement';
  6. import { IOptions } from '../../interfaces/options/IOptions';
  7. import { ObfuscationEvents } from '../../enums/ObfuscationEvents';
  8. import { initializable } from '../../decorators/Initializable';
  9. import { SingleNodeCallControllerTemplate } from '../../templates/custom-nodes/SingleNodeCallControllerTemplate';
  10. import { NO_CUSTOM_NODES_PRESET } from '../../options/presets/NoCustomNodes';
  11. import { AbstractCustomNode } from '../AbstractCustomNode';
  12. import { JavaScriptObfuscator } from '../../JavaScriptObfuscator';
  13. import { NodeUtils } from '../../node/NodeUtils';
  14. @injectable()
  15. export class NodeCallsControllerFunctionNode extends AbstractCustomNode {
  16. /**
  17. * @type {string}
  18. */
  19. @initializable()
  20. protected callsControllerFunctionName: string;
  21. /**
  22. * @type {TObfuscationEvent}
  23. */
  24. @initializable()
  25. private appendEvent: TObfuscationEvent;
  26. /**
  27. * @param options
  28. */
  29. constructor (
  30. @inject(ServiceIdentifiers.IOptions) options: IOptions
  31. ) {
  32. super(options);
  33. }
  34. /**
  35. * @param appendEvent
  36. * @param callsControllerFunctionName
  37. */
  38. public initialize (appendEvent: TObfuscationEvent, callsControllerFunctionName: string): void {
  39. this.appendEvent = appendEvent;
  40. this.callsControllerFunctionName = callsControllerFunctionName;
  41. }
  42. /**
  43. * @returns {TStatement[]}
  44. */
  45. protected getNodeStructure (): TStatement[] {
  46. return NodeUtils.convertCodeToStructure(this.getTemplate());
  47. }
  48. /**
  49. * @returns {string}
  50. */
  51. protected getTemplate (): string {
  52. if (this.appendEvent === ObfuscationEvents.AfterObfuscation) {
  53. return JavaScriptObfuscator.obfuscate(
  54. format(SingleNodeCallControllerTemplate(), {
  55. singleNodeCallControllerFunctionName: this.callsControllerFunctionName
  56. }),
  57. {
  58. ...NO_CUSTOM_NODES_PRESET,
  59. seed: this.options.seed
  60. }
  61. ).getObfuscatedCode();
  62. }
  63. return format(SingleNodeCallControllerTemplate(), {
  64. singleNodeCallControllerFunctionName: this.callsControllerFunctionName
  65. });
  66. }
  67. }