UnicodeArrayCallsWrapper.ts 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. import * as esprima from 'esprima';
  2. import { INode } from "../../interfaces/nodes/INode";
  3. import { IOptions } from "../../interfaces/IOptions";
  4. import { TBlockScopeNode } from "../../types/TBlockScopeNode";
  5. import { AppendState } from "../../enums/AppendState";
  6. import { Node } from '../Node';
  7. import { NodeUtils } from "../../NodeUtils";
  8. import { Utils } from "../../Utils";
  9. export class UnicodeArrayCallsWrapper extends Node {
  10. /**
  11. * @type {AppendState}
  12. */
  13. protected appendState: AppendState = AppendState.AfterObfuscation;
  14. /**
  15. * @type {string[]}
  16. */
  17. private unicodeArray: string[];
  18. /**
  19. * @type {string}
  20. */
  21. private unicodeArrayName: string;
  22. /**
  23. * @type {string}
  24. */
  25. private unicodeArrayCallsWrapperName: string;
  26. /**
  27. * @param unicodeArrayCallsWrapperName
  28. * @param unicodeArrayName
  29. * @param unicodeArray
  30. * @param options
  31. */
  32. constructor (
  33. unicodeArrayCallsWrapperName: string,
  34. unicodeArrayName: string,
  35. unicodeArray: string[],
  36. options: IOptions = {}
  37. ) {
  38. super(options);
  39. this.unicodeArrayCallsWrapperName = unicodeArrayCallsWrapperName;
  40. this.unicodeArrayName = unicodeArrayName;
  41. this.unicodeArray = unicodeArray;
  42. this.node = this.getNodeStructure();
  43. }
  44. /**
  45. * @param blockScopeNode
  46. */
  47. public appendNode (blockScopeNode: TBlockScopeNode): void {
  48. NodeUtils.insertNodeAtIndex(blockScopeNode.body, this.getNode(), 1);
  49. }
  50. /**
  51. * @returns {string}
  52. */
  53. public getNodeIdentifier (): string {
  54. return this.unicodeArrayCallsWrapperName;
  55. };
  56. /**
  57. * @returns {INode}
  58. */
  59. public getNode (): INode {
  60. if (!this.unicodeArray.length) {
  61. return;
  62. }
  63. this.updateNode();
  64. return super.getNode();
  65. }
  66. /**
  67. * @returns {INode}
  68. */
  69. protected getNodeStructure (): INode {
  70. let code: string = '',
  71. environmentName: string = Utils.getRandomVariableName(),
  72. keyName: string = Utils.getRandomVariableName(),
  73. node: INode;
  74. if (this.options['selfDefending']) {
  75. code = `
  76. var ${environmentName} = function(){return ${Utils.stringToUnicode('production')};};
  77. if (
  78. ${keyName} % ${Utils.getRandomInteger(this.unicodeArray.length / 8, this.unicodeArray.length / 2)} === 0 &&
  79. (
  80. /\\w+ *\\(\\) *{\\w+ *['|"].+['|"];? *}/.test(
  81. ${environmentName}[${Utils.stringToUnicode('toString')}]()
  82. ) === true || ${keyName}++
  83. )
  84. );
  85. return ${this.unicodeArrayName}[parseInt(${keyName}, 16)];
  86. `;
  87. } else {
  88. code = `return ${this.unicodeArrayName}[parseInt(${keyName}, 16)]`;
  89. }
  90. node = esprima.parse(`
  91. var ${this.unicodeArrayCallsWrapperName} = function (${keyName}) {
  92. ${code}
  93. };
  94. `);
  95. NodeUtils.addXVerbatimPropertyToLiterals(node);
  96. return NodeUtils.getBlockScopeNodeByIndex(node);
  97. }
  98. }