webpack打包使用eval加密混淆深度压缩
1.编写webpack自定义插件
const fs = require("fs");
const path = require("path");
const zlib = require("zlib");
class EvalCompressPlugin {
constructor({
file, packMinify = false, base62Encode = false, shrinkVariables = false,gzip = false }) {
if (!file) {
throw new Error("file 参数不能为空");
}
this.file = file;
this.packMinify = packMinify;
this.base62Encode = base62Encode;
this.shrinkVariables = shrinkVariables;
this.gzip = gzip;
}
apply(compiler) {
compiler.hooks.done.tap("EvalCompressPlugin", async stats => {
this.eval();
});
}
eval() {
let Packer = require("./JSPacker");
var packer = new Packer();
let fileContent = fs.readFileSync(this.file, "utf8");
var newFileContent = packer.pack(fileContent, this.packMinify,this.base62Encode, this.shrinkVariables);
fs.writeFileSync(this.file, newFileContent, "utf8");
if(this.gzip) this._gzip();
}
_gzip() {
let gzip = zlib.createGzip();
let rs = fs.createReadStream(this.file);
let ws = fs.createWriteStream(`${
this.file}.gz`);
rs.pipe(gzip).pipe(ws);
}
}
module.exports = EvalCompressPlugin;
2.webpack添加plugins
new EvalCompressPlugin({
file:path.resolve(__dirname, "build/BuildOutput.js"),
packMinify:true,
base62Encode:true,
shrinkVariables:true,
gzip:true
})
3.JSPacker加密混淆工具(packer在线地址)
var base2 = {
name: "base2",
version: "1.0.1(pre)",
exports: "Base, Package, Abstract, Module, Enumerable, Map, Collection, RegGrp, " + "assert, assertArity, assertType, " + "assignID, copy, counter, detect, extend, forEach, format, instanceOf, match, rescape, slice, trim, " + "I, K, Undefined, Null, True, False, bind, delegate, flip, not, partial, unbind",
global: this,
namespace: "var global=base2.global;function base(o,a){return o.base.apply(o,a)};",
detect: new (function (_) {
var global = _;
var jscript ;
if (_.navigator) {
var element = document.createElement("span");
var userAgent = navigator.platform + " " + navigator.userAgent;
if (!jscript) userAgent = userAgent.replace(/MSIE\s[\d.]+/, "");
userAgent = userAgent.replace(/([a-z])[\s\/](\d)/gi, "$1$2");
}
return function (a) {
var r = false;
var b = a.charAt(0) == "!";
if (b) a = a.slice(1);
if (a.charAt(0) == "(") {
try {
eval("r=!!" + a);
} catch (error) {
}
} else {
r = new RegExp("(" + a + ")", "i").test(userAgent);
}
return !!(b ^ r);
};
})(this)
};
new (function (_) {
var detect = base2.detect;
var slice =
Array.slice ||
(function (b) {
return function (a) {
return b.apply(a, b.call(arguments, 1));
};
})(Array.prototype.slice);
var Undefined = K(),
Null = K(null),
True = K(true),
False = K(false);
var _0 = /%([1-9])/g;
var _1 = /^\s\s*/;
var _2 = /\s\s*$/;
var _3 = /([\/()[\]{}|*+-.,^$?\\])/g;
var _4 = /eval/.test(detect) ? /\bbase\s*\(/ : /.*/;
var _5 = ["constructor", "toString", "valueOf"];
var _6 = String(new RegExp());
var _7 = 1;
_8();
eval(base2.namespace);
var _9 = function (a, b) {
base2.__prototyping = true;
var c = new this();
extend(c, a);
delete base2.__prototyping;
var d = c.constructor;
function _10() {
if (!base2.__prototyping) {
if (this.constructor == arguments.callee || this.__constructing) {
this.__constructing = true;
d.apply(this, arguments);
delete this.__constructing;
} else {
return extend(arguments[0], c);
}
}
return this;
}
c.constructor = _10;
for (var i in Base) _10[i] = this[i];
_10.toString = K(String(d));
_10.ancestor = this;
_10.base = Undefined;
_10.init = Undefined;
extend(_10, b);
_10.prototype = c;
_10.init();
return _10;
};
var Base = _9.call(
Object,
{
constructor: function () {
if (arguments.length > 0) {
this.extend(arguments[0]);
}
},
base: function () {
},
extend: delegate(extend)
},
(Base = {
ancestorOf: delegate(_11),
extend: _9,
forEach: delegate(_8),
implement: function (a) {
if (typeof a == "function") {
if (_11(Base, a)) {
a(this.prototype);
}
} else {
extend(this.prototype, a);
}
return this;
}
})
);
var Package = Base.extend({
constructor: function (d, e) {
this.extend(e);
if (this.init) this.init();
if (this.name != "base2") {
if (!this.parent) this.parent = base2;
this.parent.addName(this.name, this);
this.namespace = format("var %1=%2;", this.name, String(this).slice(1, -1));
}
var f = /[^\s,]+/g;
if (d) {
d.imports = Array2.reduce(
this.imports.match(f),
function (a, b) {
eval("var ns=base2." + b);
assert(ns, format("Package not found: '%1'.", b));
return (a += ns.namespace);
},
base2.namespace + JavaScript.namespace
);
d.exports = Array2.reduce(
this.exports.match(f),
function (a, b) {
var c = this.name + "." + b;
this.namespace += "var " + b + "=" + c + ";";
return (a += "if(!" + c + ")" + c + "=" + b + ";");
},
"",
this
);
}
},
exports: "",
imports: "",
name: "",
namespace: "",
parent: null,
addName: function (a, b) {
if (!this[a]) {
this[a] = b;
this.exports += ", " + a;
this.namespace += format("var %1=%2.%1;", a, this.name);
}
},
addPackage: function (a) {
this.addName(a, new Package(null, {
name: a, parent: this }));
},
toString: function () {
return format("[%1]", this.parent ? String(this.parent).slice(1, -1) + "." + this.name : this.name);
}
});
var Abstract = Base.extend({
constructor: function () {
throw new TypeError("Class cannot be instantiated.");
}
});
var Module = Abstract.extend(null, {
extend: function (a, b) {
var c = this.base();
_12(c, this);
c.implement(a);
extend(c, b);
c.init();
return c;
},
implement: function (c) {
var d = this;
if (typeof c == "function") {
d.base(c);
if (_11(Module, c)) {
_12(d, c);
}
} else {
var e = {
};
_8(Object, c, function (a, b) {
if (b.charAt(0) == "@") {
if (detect(b.slice(1))) {
forEach(a, arguments.callee);
}
} else if (!Module[b] && typeof a == "function" && a.call) {
function _13() {
return d[b].apply(d, [this].concat(slice(arguments)));
}
_13.__base = _4.test(a);
e[b] = _13;
}
});
extend(d.prototype, e);
extend(d, c);
}
return d;
}
});
function _12(a, b) {
for (var c in b) {
var method = b[c];
if (!Module[c] && typeof method == "function" && method.call) {
a[c] = method;
}
}
}
var Enumerable = Module.extend(
{
every: function (c, d, e) {
var f = true;
try {
this.forEach(c, function (a, b) {
f = d.call(e, a, b, c);
if (!f) throw