Skip to content

Commit

Permalink
Now using web workers to code obfuscation
Browse files Browse the repository at this point in the history
  • Loading branch information
sanex3339 committed Jan 27, 2020
1 parent e92db6e commit 1aa86af
Show file tree
Hide file tree
Showing 6 changed files with 78 additions and 34 deletions.
67 changes: 47 additions & 20 deletions App/actions/index.js
@@ -1,32 +1,59 @@
import * as types from '../constants/ActionTypes';

const obfuscationWorker = new Worker('../workers/obfuscation-worker.js');

export const updateCode = (code) => ({
'type': types.UPDATE_CODE,
code
});

export const obfuscateCode = (code, options) => {

const body = {
code,
options
return (dispatch) => {
const message = {
code,
options
};

if (!options.sourceMap) {
delete options.sourceMapMode
}

// options.stringArrayEncoding come from the client as strings, but the
// obfuscator expects it to be a boolean or a string if 'base64'/'rc4'
if (['false', 'true'].indexOf(options.stringArrayEncoding) !== -1) {
options.stringArrayEncoding = options.stringArrayEncoding === 'true';
}

obfuscationWorker.postMessage(message);

dispatch({
type: types.OBFUSCATE,
payload: new Promise((resolve) => {
obfuscationWorker.onmessage = function (event) {
const result = JSON.parse(event.data);

resolve(result);
};
}),
});

/**
const request = new Request('/obfuscate', {
method: 'POST',
credentials: 'same-origin',
headers: {
'Accept': 'application/json',
'Content-Type': 'application/json'
},
body: JSON.stringify(body),
});
return {
type: types.OBFUSCATE,
payload: fetch(request).then((response) => response.json()),
}
*/
};

const request = new Request('/obfuscate', {
method: 'POST',
credentials: 'same-origin',
headers: {
'Accept': 'application/json',
'Content-Type': 'application/json'
},
body: JSON.stringify(body),
});

return {
type: types.OBFUSCATE,
payload: fetch(request).then((response) => response.json()),
}

};

export const toggleOption = (optionType) => ({
Expand Down
25 changes: 25 additions & 0 deletions App/workers/obfuscation-worker.js
@@ -0,0 +1,25 @@
self.window = self;

const JavaScriptObfuscator = require('javascript-obfuscator');

onmessage = function(event) {
const {code, options} = event.data;

let result;

try {
const obfuscationResult = JavaScriptObfuscator.obfuscate(code, options);

result = {
code: obfuscationResult.getObfuscatedCode(),
sourceMap: obfuscationResult.getSourceMap(),
}
} catch (error) {
result = {
code: error.toString(),
sourceMap: '',
}
}

postMessage(JSON.stringify(result));
};
3 changes: 1 addition & 2 deletions package.json
@@ -1,13 +1,12 @@
{
"name": "javascript-obfuscator-web",
"version": "2.5.1",
"version": "3.0.0",
"description": "",
"engines": {
"node": ">=12.13.1"
},
"scripts": {
"start": "node server.js",
"stop": "pm2 delete ecosystem.config.js",
"webpack:dev": "webpack --mode development --config webpack.conf.js --watch --color --progress",
"webpack:prod": "NODE_ENV=production webpack --mode production --config webpack.conf.js --devtool source-map --progress --optimize-minimize",
"postinstall": "npm run updatesemantic && npm run webpack:prod",
Expand Down
1 change: 1 addition & 0 deletions server.js
Expand Up @@ -14,6 +14,7 @@ process.env.PWD = process.cwd();
app.use(bodyParser.json({limit: '3mb'}));

app.use('/static/dist', express.static(__dirname + '/dist'));
app.use('/workers', express.static(__dirname + '/dist/workers'));
app.use('/static/images', express.static(__dirname + '/public/images'));
app.use('/static/semantic', express.static(__dirname + '/public/semantic'));

Expand Down
6 changes: 4 additions & 2 deletions webpack.conf.js
Expand Up @@ -6,11 +6,13 @@ const HtmlWebpackPlugin = require('html-webpack-plugin');
module.exports = {
context: __dirname,
entry: {
"bundle": "./App/index.js"
"bundle": "./App/index.js",
"workers/obfuscation-worker": "./App/workers/obfuscation-worker.js"
},
output: {
path: __dirname + "/dist",
filename: "bundle.js"
filename: "[name].js",
globalObject: "this"
},
module: {
rules: [
Expand Down
10 changes: 0 additions & 10 deletions workers/obfuscation-worker.js
Expand Up @@ -3,16 +3,6 @@ const JavaScriptObfuscator = require('javascript-obfuscator');

expose({
obfuscate: (code, options) => {
if (!options.sourceMap) {
delete options.sourceMapMode
}

// options.stringArrayEncoding come from the client as strings, but the
// obfuscator expects it to be a boolean or a string if 'base64'/'rc4'
if (['false', 'true'].indexOf(options.stringArrayEncoding) !== -1) {
options.stringArrayEncoding = options.stringArrayEncoding === 'true';
}

try {
const result = JavaScriptObfuscator.obfuscate(code, options);

Expand Down

0 comments on commit 1aa86af

Please sign in to comment.