Skip to content

Commit 1aa86af

Browse files
author
sanex3339
committed
Now using web workers to code obfuscation
1 parent e92db6e commit 1aa86af

File tree

6 files changed

+78
-34
lines changed

6 files changed

+78
-34
lines changed

App/actions/index.js

Lines changed: 47 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,32 +1,59 @@
11
import * as types from '../constants/ActionTypes';
22

3+
const obfuscationWorker = new Worker('../workers/obfuscation-worker.js');
4+
35
export const updateCode = (code) => ({
46
'type': types.UPDATE_CODE,
57
code
68
});
79

810
export const obfuscateCode = (code, options) => {
9-
10-
const body = {
11-
code,
12-
options
11+
return (dispatch) => {
12+
const message = {
13+
code,
14+
options
15+
};
16+
17+
if (!options.sourceMap) {
18+
delete options.sourceMapMode
19+
}
20+
21+
// options.stringArrayEncoding come from the client as strings, but the
22+
// obfuscator expects it to be a boolean or a string if 'base64'/'rc4'
23+
if (['false', 'true'].indexOf(options.stringArrayEncoding) !== -1) {
24+
options.stringArrayEncoding = options.stringArrayEncoding === 'true';
25+
}
26+
27+
obfuscationWorker.postMessage(message);
28+
29+
dispatch({
30+
type: types.OBFUSCATE,
31+
payload: new Promise((resolve) => {
32+
obfuscationWorker.onmessage = function (event) {
33+
const result = JSON.parse(event.data);
34+
35+
resolve(result);
36+
};
37+
}),
38+
});
39+
40+
/**
41+
const request = new Request('/obfuscate', {
42+
method: 'POST',
43+
credentials: 'same-origin',
44+
headers: {
45+
'Accept': 'application/json',
46+
'Content-Type': 'application/json'
47+
},
48+
body: JSON.stringify(body),
49+
});
50+
51+
return {
52+
type: types.OBFUSCATE,
53+
payload: fetch(request).then((response) => response.json()),
54+
}
55+
*/
1356
};
14-
15-
const request = new Request('/obfuscate', {
16-
method: 'POST',
17-
credentials: 'same-origin',
18-
headers: {
19-
'Accept': 'application/json',
20-
'Content-Type': 'application/json'
21-
},
22-
body: JSON.stringify(body),
23-
});
24-
25-
return {
26-
type: types.OBFUSCATE,
27-
payload: fetch(request).then((response) => response.json()),
28-
}
29-
3057
};
3158

3259
export const toggleOption = (optionType) => ({

App/workers/obfuscation-worker.js

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
self.window = self;
2+
3+
const JavaScriptObfuscator = require('javascript-obfuscator');
4+
5+
onmessage = function(event) {
6+
const {code, options} = event.data;
7+
8+
let result;
9+
10+
try {
11+
const obfuscationResult = JavaScriptObfuscator.obfuscate(code, options);
12+
13+
result = {
14+
code: obfuscationResult.getObfuscatedCode(),
15+
sourceMap: obfuscationResult.getSourceMap(),
16+
}
17+
} catch (error) {
18+
result = {
19+
code: error.toString(),
20+
sourceMap: '',
21+
}
22+
}
23+
24+
postMessage(JSON.stringify(result));
25+
};

package.json

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,12 @@
11
{
22
"name": "javascript-obfuscator-web",
3-
"version": "2.5.1",
3+
"version": "3.0.0",
44
"description": "",
55
"engines": {
66
"node": ">=12.13.1"
77
},
88
"scripts": {
99
"start": "node server.js",
10-
"stop": "pm2 delete ecosystem.config.js",
1110
"webpack:dev": "webpack --mode development --config webpack.conf.js --watch --color --progress",
1211
"webpack:prod": "NODE_ENV=production webpack --mode production --config webpack.conf.js --devtool source-map --progress --optimize-minimize",
1312
"postinstall": "npm run updatesemantic && npm run webpack:prod",

server.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ process.env.PWD = process.cwd();
1414
app.use(bodyParser.json({limit: '3mb'}));
1515

1616
app.use('/static/dist', express.static(__dirname + '/dist'));
17+
app.use('/workers', express.static(__dirname + '/dist/workers'));
1718
app.use('/static/images', express.static(__dirname + '/public/images'));
1819
app.use('/static/semantic', express.static(__dirname + '/public/semantic'));
1920

webpack.conf.js

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,13 @@ const HtmlWebpackPlugin = require('html-webpack-plugin');
66
module.exports = {
77
context: __dirname,
88
entry: {
9-
"bundle": "./App/index.js"
9+
"bundle": "./App/index.js",
10+
"workers/obfuscation-worker": "./App/workers/obfuscation-worker.js"
1011
},
1112
output: {
1213
path: __dirname + "/dist",
13-
filename: "bundle.js"
14+
filename: "[name].js",
15+
globalObject: "this"
1416
},
1517
module: {
1618
rules: [

workers/obfuscation-worker.js

Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -3,16 +3,6 @@ const JavaScriptObfuscator = require('javascript-obfuscator');
33

44
expose({
55
obfuscate: (code, options) => {
6-
if (!options.sourceMap) {
7-
delete options.sourceMapMode
8-
}
9-
10-
// options.stringArrayEncoding come from the client as strings, but the
11-
// obfuscator expects it to be a boolean or a string if 'base64'/'rc4'
12-
if (['false', 'true'].indexOf(options.stringArrayEncoding) !== -1) {
13-
options.stringArrayEncoding = options.stringArrayEncoding === 'true';
14-
}
15-
166
try {
177
const result = JavaScriptObfuscator.obfuscate(code, options);
188

0 commit comments

Comments
 (0)