javascript-obfuscator.js 3.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  1. #!/usr/bin/env node
  2. var commands = require('commander'),
  3. fs = require('fs'),
  4. path = require('path'),
  5. JavaScriptObfuscator = require('../dist/index'),
  6. packageConfig = fs.readFileSync(path.join(path.dirname(fs.realpathSync(process.argv[1])), '../package.json')),
  7. buildVersion = JSON.parse(packageConfig).version,
  8. isWindows = process.platform == 'win32';
  9. // Specify commander options to parse command line params correctly
  10. commands
  11. .version(buildVersion, '-v, --version')
  12. .usage('[options] STDIN STDOUT')
  13. .option('--skip-compact', 'Disable one line output code compacting.')
  14. .option('--debugProtection', 'Disable browser Debug panel (can cause DevTools enabled browser freeze).')
  15. .option('--debugProtectionInterval', 'Disable browser Debug panel even after page was loaded (can cause DevTools enabled browser freeze).')
  16. .option('--skip-disableConsoleOutput', 'Allow console.log, console.info, console.error and console.warn messages output into browser console.')
  17. .option('--encodeUnicodeLiterals', 'All literals in Unicode array become encoded in Base64 (this option can slightly slow down your code speed).')
  18. .option('--reservedNames <list>', 'Disable obfuscation of variable names, function names and names of function parameters that match the passed RegExp patterns (comma separated).', (val) => val.split(','))
  19. .option('--skip-rotateUnicodeArray', 'Disable rotation of unicode array values during obfuscation.')
  20. .option('--skip-selfDefending', 'Disables self-defending for obfuscated code.')
  21. .option('--skip-unicodeArray', 'Disables gathering of all literal strings into an array and replacing every literal string with an array call.')
  22. .option('--unicodeArrayThreshold <threshold>', 'The probability that the literal string will be inserted into unicodeArray (Default: 0.8, Min: 0, Max: 1).', parseFloat)
  23. .option('--skip-wrapUnicodeArrayCalls', 'Disables usage of special access function instead of direct array call.')
  24. ;
  25. commands.on('--help', function () {
  26. console.log(' Examples:\n');
  27. console.log(' %> javascript-obfuscator < in.js > out.js');
  28. if (isWindows) {
  29. console.log(' %> type in1.js in2.js | javascript-obfuscator > out.js');
  30. } else {
  31. console.log(' %> cat in1.js in2.js | javascript-obfuscator > out.js');
  32. }
  33. console.log('');
  34. process.exit();
  35. });
  36. commands.parse(process.argv);
  37. // If no sensible data passed in just print help and exit
  38. var fromStdin = !process.env.__DIRECT__ && !process.stdin.isTTY;
  39. if (!fromStdin) {
  40. commands.outputHelp();
  41. return 0;
  42. }
  43. var encoding = 'utf-8',
  44. data = '';
  45. process.stdin.setEncoding(encoding);
  46. process.stdin.on('readable', function() {
  47. var chunk;
  48. while (chunk = process.stdin.read()) {
  49. data += chunk;
  50. }
  51. });
  52. process.stdin.on('end', function () {
  53. processData();
  54. });
  55. function processData () {
  56. var options = {
  57. compact : commands.skipCompact ? false : true,
  58. debugProtection : commands.debugProtection,
  59. debugProtectionInterval : commands.debugProtectionInterval,
  60. disableConsoleOutput : commands.skipDisableConsoleOutput ? false : true,
  61. encodeUnicodeLiterals: commands.encodeUnicodeLiterals,
  62. reservedNames: commands.reservedNames ? commands.reservedNames : [],
  63. rotateUnicodeArray : commands.skipRotateUnicodeArray ? false : true,
  64. selfDefending: commands.skipSelfDefending ? false : true,
  65. unicodeArray: commands.skipUnicodeArray ? false : true,
  66. unicodeArrayThreshold: commands.unicodeArrayThreshold ? commands.unicodeArrayThreshold : 0.8,
  67. wrapUnicodeArrayCalls: commands.skipWrapUnicodeArrayCalls ? false : true,
  68. };
  69. var obfuscatedCode = JavaScriptObfuscator.obfuscate(data, options);
  70. process.stdout.write(obfuscatedCode);
  71. }