javascript-obfuscator.js 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  1. #!/usr/bin/env node
  2. var commands = require('commander'),
  3. fs = require('fs'),
  4. path = require('path'),
  5. JavaScriptObfuscator = require('../dist/index'),
  6. data = '',
  7. defaultOptions = {
  8. compact: true,
  9. debugProtection: false,
  10. debugProtectionInterval: false,
  11. disableConsoleOutput: true,
  12. encodeUnicodeLiterals: false,
  13. reservedNames: [],
  14. rotateUnicodeArray: true,
  15. selfDefending: true,
  16. unicodeArray: true,
  17. unicodeArrayThreshold: 0.8,
  18. wrapUnicodeArrayCalls: true
  19. };
  20. configureProcess();
  21. configureCommands();
  22. commands.parse(process.argv);
  23. if (!isDataExist()) {
  24. commands.outputHelp();
  25. return 0;
  26. }
  27. function buildOptions () {
  28. var options = {},
  29. availableOptions = Object.keys(defaultOptions);
  30. for (var option in commands) {
  31. if (availableOptions.indexOf(option) === -1) {
  32. continue;
  33. }
  34. options[option] = commands[option];
  35. }
  36. return Object.assign({}, defaultOptions, options);
  37. }
  38. function configureCommands () {
  39. commands
  40. .version(getBuildVersion(), '-v, --version')
  41. .usage('[options] STDIN STDOUT')
  42. .option('--compact <boolean>', 'Disable one line output code compacting.', parseBoolean)
  43. .option('--debugProtection <boolean>', 'Disable browser Debug panel (can cause DevTools enabled browser freeze).', parseBoolean)
  44. .option('--debugProtectionInterval <boolean>', 'Disable browser Debug panel even after page was loaded (can cause DevTools enabled browser freeze).', parseBoolean)
  45. .option('--disableConsoleOutput <boolean>', 'Allow console.log, console.info, console.error and console.warn messages output into browser console.', parseBoolean)
  46. .option('--encodeUnicodeLiterals <boolean>', 'All literals in Unicode array become encoded in Base64 (this option can slightly slow down your code speed).', parseBoolean)
  47. .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(','))
  48. .option('--rotateUnicodeArray <boolean>', 'Disable rotation of unicode array values during obfuscation.', parseBoolean)
  49. .option('--selfDefending <boolean>', 'Disables self-defending for obfuscated code.', parseBoolean)
  50. .option('--unicodeArray <boolean>', 'Disables gathering of all literal strings into an array and replacing every literal string with an array call.', parseBoolean)
  51. .option('--unicodeArrayThreshold <number>', 'The probability that the literal string will be inserted into unicodeArray (Default: 0.8, Min: 0, Max: 1).', parseFloat)
  52. .option('--wrapUnicodeArrayCalls <boolean>', 'Disables usage of special access function instead of direct array call.', parseBoolean)
  53. ;
  54. commands.on('--help', function () {
  55. var isWindows = process.platform == 'win32';
  56. console.log(' Examples:\n');
  57. console.log(' %> javascript-obfuscator < in.js > out.js');
  58. if (isWindows) {
  59. console.log(' %> type in1.js in2.js | javascript-obfuscator > out.js');
  60. } else {
  61. console.log(' %> cat in1.js in2.js | javascript-obfuscator > out.js');
  62. }
  63. console.log('');
  64. process.exit();
  65. });
  66. }
  67. function configureProcess () {
  68. process.stdin.setEncoding('utf-8');
  69. process.stdin.on('readable', function () {
  70. var chunk;
  71. while (chunk = process.stdin.read()) {
  72. data += chunk;
  73. }
  74. });
  75. process.stdin.on('end', function () {
  76. processData();
  77. });
  78. }
  79. function getBuildVersion () {
  80. var packageConfig = fs.readFileSync(
  81. path.join(
  82. path.dirname(
  83. fs.realpathSync(process.argv[1])
  84. ),
  85. '../package.json'
  86. )
  87. );
  88. return JSON.parse(packageConfig).version;
  89. }
  90. function isDataExist () {
  91. return !process.env.__DIRECT__ && !process.stdin.isTTY;
  92. }
  93. function parseBoolean (value) {
  94. return value === 'true' || value === '1';
  95. }
  96. function processData() {
  97. process.stdout.write(
  98. JavaScriptObfuscator.obfuscate(data, buildOptions())
  99. );
  100. }