UnicodeArrayNode.ts 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  1. import * as escodegen from 'escodegen';
  2. import * as estraverse from 'estraverse';
  3. import { INode } from '../../interfaces/nodes/INode';
  4. import { AppendState } from '../../enums/AppendState';
  5. import { NodeType } from "../../enums/NodeType";
  6. import { Node } from '../Node';
  7. import { NodeUtils } from "../../NodeUtils";
  8. import { Utils } from '../../Utils';
  9. export class UnicodeArrayNode extends Node {
  10. /**
  11. * @type {number}
  12. */
  13. public static UNICODE_ARRAY_RANDOM_LENGTH: number = 4;
  14. /**
  15. * @type {AppendState}
  16. */
  17. protected appendState: AppendState = AppendState.AfterObfuscation;
  18. /**
  19. * @type {string[]}
  20. */
  21. private unicodeArray: string[] = [];
  22. /**
  23. * @type {string}
  24. */
  25. private unicodeArrayName: string;
  26. /**
  27. * @type {number}
  28. */
  29. private unicodeArrayRotateValue: number;
  30. /**
  31. * @param unicodeArrayName
  32. * @param unicodeArrayRotateValue
  33. */
  34. constructor (unicodeArrayName: string, unicodeArrayRotateValue: number = 0) {
  35. super();
  36. this.unicodeArrayName = unicodeArrayName;
  37. this.unicodeArrayRotateValue = unicodeArrayRotateValue;
  38. this.node = this.getNodeStructure();
  39. }
  40. /**
  41. * @param astTree
  42. */
  43. public appendNode (astTree: INode): void {
  44. estraverse.replace(astTree, {
  45. leave: (node: INode, parent: INode): any => {
  46. if (NodeUtils.isProgramNode(node)) {
  47. NodeUtils.prependNode(node.body, this.getNode());
  48. return estraverse.VisitorOption.Break;
  49. }
  50. return estraverse.VisitorOption.Skip;
  51. }
  52. });
  53. }
  54. /**
  55. * @returns {string}
  56. */
  57. public getNodeIdentifier (): string {
  58. return this.unicodeArrayName;
  59. }
  60. /**
  61. * @returns {string[]}
  62. */
  63. public getNodeData (): string[] {
  64. return this.unicodeArray;
  65. }
  66. /**
  67. * @returns {INode}
  68. */
  69. public getNode (): INode {
  70. Utils.arrayRotate(this.unicodeArray, this.unicodeArrayRotateValue);
  71. this.updateNode();
  72. return super.getNode();
  73. }
  74. /**
  75. * @returns any
  76. */
  77. protected getNodeStructure (): any {
  78. return {
  79. 'type': NodeType.VariableDeclaration,
  80. 'declarations': [
  81. {
  82. 'type': NodeType.VariableDeclarator,
  83. 'id': {
  84. 'type': NodeType.Identifier,
  85. 'name': this.unicodeArrayName
  86. },
  87. 'init': {
  88. 'type': NodeType.ArrayExpression,
  89. 'elements': this.unicodeArray.map((value: string) => {
  90. return {
  91. 'type': NodeType.Literal,
  92. 'value': value,
  93. 'raw': `'${value}'`,
  94. 'x-verbatim-property': {
  95. 'content' : value,
  96. precedence: escodegen.Precedence.Primary
  97. }
  98. };
  99. })
  100. }
  101. }
  102. ],
  103. 'kind': 'var'
  104. };
  105. }
  106. }