SelfDefendingUnicodeNode.ts 2.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  1. import { injectable, inject } from 'inversify';
  2. import { ServiceIdentifiers } from '../../container/ServiceIdentifiers';
  3. import * as format from 'string-template';
  4. import { TStatement } from '../../types/node/TStatement';
  5. import { IOptions } from '../../interfaces/options/IOptions';
  6. import { initializable } from '../../decorators/Initializable';
  7. import { NO_CUSTOM_NODES_PRESET } from '../../options/presets/NoCustomNodes';
  8. import { SelfDefendingTemplate } from '../../templates/custom-nodes/self-defending-nodes/self-defending-unicode-node/SelfDefendingTemplate';
  9. import { AbstractCustomNode } from '../AbstractCustomNode';
  10. import { JavaScriptObfuscator } from '../../JavaScriptObfuscator';
  11. import { NodeUtils } from '../../node/NodeUtils';
  12. import { RandomGeneratorUtils } from '../../utils/RandomGeneratorUtils';
  13. @injectable()
  14. export class SelfDefendingUnicodeNode extends AbstractCustomNode {
  15. /**
  16. * @type {string}
  17. */
  18. @initializable()
  19. protected callsControllerFunctionName: string;
  20. /**
  21. * @param options
  22. */
  23. constructor (
  24. @inject(ServiceIdentifiers.IOptions) options: IOptions
  25. ) {
  26. super(options);
  27. }
  28. /**
  29. * @param callsControllerFunctionName
  30. */
  31. public initialize (callsControllerFunctionName: string): void {
  32. this.callsControllerFunctionName = callsControllerFunctionName;
  33. }
  34. /**
  35. * @returns {TStatement[]}
  36. */
  37. protected getNodeStructure (): TStatement[] {
  38. return NodeUtils.convertCodeToStructure(this.getTemplate());
  39. }
  40. /**
  41. * @returns {string}
  42. */
  43. protected getTemplate (): string {
  44. return JavaScriptObfuscator.obfuscate(
  45. format(SelfDefendingTemplate(), {
  46. selfDefendingFunctionName: RandomGeneratorUtils.getRandomVariableName(6, true, true),
  47. singleNodeCallControllerFunctionName: this.callsControllerFunctionName
  48. }),
  49. {
  50. ...NO_CUSTOM_NODES_PRESET,
  51. seed: this.options.seed
  52. }
  53. ).getObfuscatedCode();
  54. }
  55. }