├── .gitignore ├── app.js ├── command-tool.js ├── package.json ├── readme.md └── test ├── run.js └── sample-codebase └── start.js /.gitignore: -------------------------------------------------------------------------------- 1 | node_modules 2 | .idea -------------------------------------------------------------------------------- /app.js: -------------------------------------------------------------------------------- 1 | var esprima = require('esprima'); 2 | var fs = require('fs'); 3 | var toString = require('escodegen').generate; 4 | var confusion = require('confusion'); 5 | var path = require('path'); 6 | var async = require('async'); 7 | var mkpath = require('mkpath'); 8 | var uglifyJS = require("uglify-js"); 9 | 10 | var walk = function (dir, done) { 11 | var results = []; 12 | fs.readdir(dir, function (err, list) { 13 | if (err) return done(err); 14 | var i = 0; 15 | (function next() { 16 | var file = list[i++]; 17 | if (!file) return done(null, results); 18 | file = dir + '/' + file; 19 | fs.stat(file, function (err, stat) { 20 | if (stat && stat.isDirectory()) { 21 | walk(file, function (err, res) { 22 | results = results.concat(res); 23 | next(); 24 | }); 25 | } else { 26 | results.push(file); 27 | next(); 28 | } 29 | }); 30 | })(); 31 | }); 32 | }; 33 | 34 | var onlyJsFile = function (dirPath, callback) { 35 | var filesArr = []; 36 | walk(dirPath, function (err, data) { 37 | if (err) { 38 | callback({FilePath: dirPath, err: err}); 39 | } else { 40 | data.forEach(function (filePath) { 41 | if (path.extname(filePath) === '.js') { 42 | filesArr.push(filePath); 43 | } 44 | }); 45 | callback(null, filesArr); 46 | } 47 | 48 | }); 49 | }; 50 | 51 | var init = function (paramObj, callback) { 52 | if (!paramObj.sourceDir) { 53 | callback({errorMessage: 'sourceDir field is a mandatory option, can not be blank.'}, null); 54 | return; 55 | } 56 | onlyJsFile(paramObj.sourceDir, function (err, dataList) { 57 | var tasks = []; 58 | 59 | if (err) { 60 | throw err; 61 | } else { 62 | dataList.forEach(function (filePath) { 63 | tasks.push(function (done) { 64 | var destinationPath = filePath; 65 | if (paramObj.destinationDir) { 66 | destinationPath = path.join(paramObj.destinationDir, filePath); 67 | } 68 | fs.readFile(filePath, function (err, data) { 69 | var ast = esprima.parse(data); 70 | var obfuscated = confusion.transformAst(ast, confusion.createVariableName); 71 | var unreadCode = ''; 72 | if (err) { 73 | done(err); 74 | } else { 75 | if (!fs.existsSync(destinationPath)) { 76 | mkpath.sync(path.dirname(destinationPath)); 77 | } 78 | unreadCode = toString(obfuscated); 79 | if (paramObj.uglify) { 80 | unreadCode = uglifyJS.minify(unreadCode, {fromString: true}).code; 81 | } 82 | fs.writeFile(destinationPath, unreadCode, function (err) { 83 | var status = filePath + ' -> ' + destinationPath; 84 | if (err) { 85 | done(err); 86 | } else { 87 | if (paramObj.debug) { 88 | console.log(status); 89 | } 90 | done(null, status); 91 | } 92 | }); 93 | } 94 | }); 95 | } 96 | ); 97 | }); 98 | 99 | async.series(tasks, callback); 100 | } 101 | }); 102 | }; 103 | 104 | module.exports = init; 105 | 106 | 107 | 108 | -------------------------------------------------------------------------------- /command-tool.js: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env node 2 | 3 | var program = require('commander'); 4 | var secureCode = require('.'); 5 | var package = require('./package.json'); 6 | var option = { 7 | sourceDir: '', 8 | destinationDir: '', 9 | uglify:false, 10 | debug: false 11 | }; 12 | 13 | program 14 | .version(package.version) 15 | .usage('[options] -s Warning: By Default destination directory set as source directory path.') 16 | .option('-s, --source ', 'mention the source directory path.') 17 | .option('-d, --destination ', 'mention destination directory path.') 18 | .option('-u, --uglify ', 'uglification on files') 19 | .option('-x, --debug ', 'debug mode') 20 | .parse(process.argv); 21 | 22 | option.sourceDir = program.source; 23 | option.destinationDir = program.destination; 24 | option.uglify = program.uglify; 25 | option.debug = program.debug; 26 | 27 | if(program.source){ 28 | secureCode(option,function(err,result){ 29 | if(err){ 30 | throw err; 31 | }else{ 32 | console.log(result); 33 | } 34 | }) 35 | }else{ 36 | console.log('\nArgument missing, Try command: `code-protect -h` to get more help\n'); 37 | } -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "code-protect", 3 | "version": "1.0.3", 4 | "description": "It's a module which convert your JavaScript code into some not understandable format and still it's executable.This module can be used to protect the code base which run's on browser's client side or we can protect the nodejs code base as well.", 5 | "main": "app.js", 6 | "scripts": { 7 | "test": "echo \"Error: no test specified\" && exit 1" 8 | }, 9 | "author": "Kashish Gupta", 10 | "license": "ISC", 11 | "keywords": [ 12 | "protect", 13 | "secure", 14 | "uglify", 15 | "obfuscation", 16 | "code-secure", 17 | "confuse" 18 | ], 19 | "repository": { 20 | "type": "git", 21 | "url": "git+https://github.com/kashishgupta1990/code-protect" 22 | }, 23 | "bugs": { 24 | "url": "https://github.com/kashishgupta1990/code-protect/issues" 25 | }, 26 | "bin": { 27 | "code-protect": "./command-tool.js" 28 | }, 29 | "dependencies": { 30 | "async": "^1.5.2", 31 | "commander": "^2.9.0", 32 | "confusion": "^0.1.1", 33 | "escodegen": "^1.8.0", 34 | "esprima": "^2.7.2", 35 | "mkpath": "^1.0.0", 36 | "uglify-js": "^2.6.2" 37 | } 38 | } 39 | -------------------------------------------------------------------------------- /readme.md: -------------------------------------------------------------------------------- 1 | # Code Protect 2 | 3 | It's a module which convert your `JavaScript` code into some not understandable format and still it's executable. 4 | This module can be used to protect the code base which run's on browser's client side or we can protect the NodeJs code base as well. 5 | We have to mention the few options provided where `sourceDir` is mandatory field. Just mention the root path in this field and `code-protect` converts all the `.js` files present in sub-folder. 6 | 7 | ## Module Usage 8 | 9 | ### Install 10 | `npm install code-protect --save` 11 | 12 | ### Usage 13 | > Module Require: 14 | ```javascript 15 | var codeProtect = require('code-protect'); 16 | ``` 17 | > Options: `sourceDir` is a mandatory field 18 | ```javascript 19 | var option = { 20 | sourceDir: './sample-codebase', 21 | destinationDir: './new-sample-codebase', 22 | uglify:true, 23 | debug: true 24 | }; 25 | ``` 26 | > Method Use: 27 | ```javascript 28 | codeProtect(option, function (err, data) { 29 | if (err) { 30 | throw err; 31 | } else { 32 | doSomething...... 33 | } 34 | }); 35 | ``` 36 | 37 | ### `JavaScript` Sample Code: 38 | ```javascript 39 | var codeProtect = require('code-protect'); 40 | var option = { 41 | sourceDir: './sample-codebase', 42 | destinationDir: './new-sample-codebase', 43 | uglify:true, 44 | debug: true 45 | }; 46 | 47 | codeProtect(option, function (err, data) { 48 | if (err) { 49 | throw err; 50 | } else { 51 | console.log(data); 52 | } 53 | }); 54 | ``` 55 | 56 | ## Command Usage 57 | 58 | ### Install 59 | `npm install code-protect -g` 60 | 61 | ### Usage 62 | `Usage:` code-protect [options] -s 63 | `Warning: By Default destination directory set as source directory path.` 64 | 65 | #### `Options:` 66 | - -h, --help, output usage information 67 | - -V, --version, output the version number 68 | - -s, --source , mention the source directory path. 69 | - -d, --destination , mention destination directory path. 70 | - -u, --uglify , uglification on files 71 | - -x, --debug , debug mode 72 | 73 | 74 | -------------------------------------------------------------------------------- /test/run.js: -------------------------------------------------------------------------------- 1 | var codeProtect = require('../'); 2 | var option = { 3 | sourceDir: './sample-codebase', 4 | destinationDir: './new-sample-codebase', 5 | uglify:true, 6 | debug: true 7 | }; 8 | 9 | codeProtect(option, function (err, data) { 10 | if (err) { 11 | console.log(JSON.stringify(err)); 12 | throw err; 13 | } else { 14 | console.log(data); 15 | } 16 | }); -------------------------------------------------------------------------------- /test/sample-codebase/start.js: -------------------------------------------------------------------------------- 1 | //Lets require/import the HTTP module 2 | var http = require('http'); 3 | 4 | //Lets define a port we want to listen to 5 | const PORT=8080; 6 | 7 | //We need a function which handles requests and send response 8 | function handleRequest(request, response){ 9 | response.end('It Works!! Path Hit: ' + request.url); 10 | } 11 | 12 | //Create a server 13 | var server = http.createServer(handleRequest); 14 | 15 | //Lets start our server 16 | server.listen(PORT, function(){ 17 | //Callback triggered when server is successfully listening. Hurray! 18 | console.log("Server listening on: http://localhost:%s", PORT); 19 | }); --------------------------------------------------------------------------------