NodeObfuscator.js 2.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  1. "use strict";
  2. const JSFuck_1 = require("../enums/JSFuck");
  3. const NodeUtils_1 = require("../NodeUtils");
  4. const Utils_1 = require('../Utils');
  5. class NodeObfuscator {
  6. constructor(nodes, options = {}) {
  7. this.nodes = nodes;
  8. this.options = options;
  9. }
  10. replaceNodeIdentifierByNewValue(node, parentNode, namesMap) {
  11. if (NodeUtils_1.NodeUtils.isIdentifierNode(node) && namesMap.has(node.name)) {
  12. const parentNodeIsAPropertyNode = (NodeUtils_1.NodeUtils.isPropertyNode(parentNode) &&
  13. parentNode.key === node), parentNodeIsAMemberExpressionNode = (NodeUtils_1.NodeUtils.isMemberExpressionNode(parentNode) &&
  14. parentNode.computed === false &&
  15. parentNode.property === node);
  16. if (parentNodeIsAPropertyNode || parentNodeIsAMemberExpressionNode) {
  17. return;
  18. }
  19. node.name = namesMap.get(node.name);
  20. }
  21. }
  22. replaceLiteralBooleanByJSFuck(nodeValue) {
  23. return nodeValue ? JSFuck_1.JSFuck.True : JSFuck_1.JSFuck.False;
  24. }
  25. replaceLiteralNumberByHexadecimalValue(nodeValue) {
  26. const prefix = '0x';
  27. if (!Utils_1.Utils.isInteger(nodeValue)) {
  28. return String(nodeValue);
  29. }
  30. return `${prefix}${Utils_1.Utils.decToHex(nodeValue)}`;
  31. }
  32. replaceLiteralValueByUnicodeValue(nodeValue) {
  33. let value = nodeValue;
  34. if (this.options['unicodeArray'] && this.options['encodeUnicodeArray']) {
  35. value = new Buffer(encodeURI(value)).toString('base64');
  36. }
  37. value = Utils_1.Utils.stringToUnicode(value);
  38. if (!this.options['unicodeArray']) {
  39. return value;
  40. }
  41. return this.replaceLiteralValueByUnicodeArrayCall(value);
  42. }
  43. replaceLiteralValueByUnicodeArrayCall(value) {
  44. let unicodeArray = this.nodes.get('unicodeArrayNode').getNodeData(), sameIndex = unicodeArray.indexOf(value), index, hexadecimalIndex;
  45. if (sameIndex < 0) {
  46. index = unicodeArray.length;
  47. unicodeArray.push(value);
  48. }
  49. else {
  50. index = sameIndex;
  51. }
  52. hexadecimalIndex = this.replaceLiteralNumberByHexadecimalValue(index);
  53. if (this.options['wrapUnicodeArrayCalls']) {
  54. return `${this.nodes.get('unicodeArrayCallsWrapper').getNodeIdentifier()}('${hexadecimalIndex}')`;
  55. }
  56. return `${this.nodes.get('unicodeArrayNode').getNodeIdentifier()}[${hexadecimalIndex}]`;
  57. }
  58. }
  59. exports.NodeObfuscator = NodeObfuscator;