ee-core 1.5.2-beta.1 → 2.0.0-beta.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +21 -21
- package/README.md +2 -2
- package/addon/window/index.js +91 -91
- package/bin/tools.js +18 -18
- package/config/config.default.js +287 -280
- package/core/index.js +12 -12
- package/core/lib/ee.js +218 -218
- package/core/lib/loader/context_loader.js +106 -106
- package/core/lib/loader/ee_loader.js +461 -457
- package/core/lib/loader/file_loader.js +325 -325
- package/core/lib/loader/mixin/addon.js +32 -32
- package/core/lib/loader/mixin/config.js +135 -135
- package/core/lib/loader/mixin/controller.js +125 -124
- package/core/lib/loader/mixin/service.js +28 -28
- package/core/lib/utils/base_context_class.js +34 -34
- package/core/lib/utils/function.js +30 -0
- package/core/lib/utils/index.js +127 -127
- package/core/lib/utils/sequencify.js +59 -59
- package/core/lib/utils/timing.js +77 -77
- package/index.js +49 -49
- package/lib/appLoader.js +48 -53
- package/lib/application.js +85 -84
- package/lib/baseApp.js +114 -131
- package/lib/eeApp.js +325 -359
- package/{lib/constant.js → module/const/index.js} +12 -9
- package/{lib/httpclient.js → module/httpclient/index.js} +170 -136
- package/module/jobs/child/forkProcess.js +99 -0
- package/module/jobs/child/index.js +33 -0
- package/module/jobs/index.js +55 -0
- package/module/jobs/renderer/index.js +140 -0
- package/module/jobs/renderer/loadView.js +40 -0
- package/module/loader/index.js +78 -0
- package/module/log/index.js +53 -0
- package/module/log/logger.js +61 -0
- package/module/message/index.js +13 -0
- package/module/message/ipcMain.js +160 -0
- package/module/message/ipcRender.js +0 -0
- package/{lib → module}/socket/httpServer.js +142 -142
- package/{lib → module}/socket/io.js +23 -23
- package/{lib → module}/socket/ipcServer.js +106 -108
- package/{lib → module}/socket/socketClient.js +51 -50
- package/{lib → module}/socket/socketServer.js +77 -76
- package/module/socket/start.js +22 -0
- package/{lib → module}/storage/index.js +35 -34
- package/module/storage/jsondb/adapters/Base.js +14 -0
- package/module/storage/jsondb/adapters/FileSync.js +32 -0
- package/{lib/storage/lowdb → module/storage/jsondb}/main.js +42 -46
- package/{lib/storage/lowdbStorage.js → module/storage/jsondbStorage.js} +98 -99
- package/{lib → module}/storage/sqliteStorage.js +123 -127
- package/module/utils/copyto.js +161 -0
- package/module/utils/helper.js +117 -0
- package/module/utils/index.js +120 -0
- package/module/utils/json.js +72 -0
- package/module/utils/ps.js +175 -0
- package/{utils → module/utils}/wrap.js +35 -37
- package/package.json +44 -48
- package/tools/encrypt.js +274 -274
- package/tools/replaceDist.js +61 -61
- package/utils/index.js +128 -246
- package/lib/logger.js +0 -47
- package/lib/socket/start.js +0 -22
- package/lib/storage/lowdb/adapters/Base.js +0 -15
- package/lib/storage/lowdb/adapters/FileAsync.js +0 -41
- package/lib/storage/lowdb/adapters/FileSync.js +0 -39
- package/lib/storage/lowdb/adapters/LocalStorage.js +0 -20
- package/lib/storage/lowdb/adapters/Memory.js +0 -8
- package/lib/storage/lowdb/adapters/_stringify.js +0 -4
- package/lib/storage/lowdb/common.js +0 -33
- package/lib/storage/lowdb/fp.js +0 -23
- package/lib/storage/lowdb/is-promise.js +0 -6
- package/lib/storage/lowdb/nano.js +0 -5
- package/utils/common.js +0 -91
package/package.json
CHANGED
|
@@ -1,48 +1,44 @@
|
|
|
1
|
-
{
|
|
2
|
-
"name": "ee-core",
|
|
3
|
-
"version": "
|
|
4
|
-
"description": "ee core",
|
|
5
|
-
"main": "index.js",
|
|
6
|
-
"scripts": {
|
|
7
|
-
"test": "echo \"Error: no test specified\" && exit 1"
|
|
8
|
-
},
|
|
9
|
-
"author": "",
|
|
10
|
-
"license": "ISC",
|
|
11
|
-
"bin": {
|
|
12
|
-
"ee-core": "./bin/tools.js"
|
|
13
|
-
},
|
|
14
|
-
"dependencies": {
|
|
15
|
-
"agentkeepalive": "^4.2.0",
|
|
16
|
-
"bytenode": "^1.3.6",
|
|
17
|
-
"co": "^4.6.0",
|
|
18
|
-
"debug": "^4.3.3",
|
|
19
|
-
"depd": "^2.0.0",
|
|
20
|
-
"egg-errors": "^2.3.0",
|
|
21
|
-
"egg-logger": "^2.7.1",
|
|
22
|
-
"electron-is": "^3.0.0",
|
|
23
|
-
"electron-updater": "^4.6.1",
|
|
24
|
-
"extend2": "^1.0.1",
|
|
25
|
-
"fs-extra": "^10.0.0",
|
|
26
|
-
"get-port": "^5.1.1",
|
|
27
|
-
"globby": "^10.0.0",
|
|
28
|
-
"
|
|
29
|
-
"
|
|
30
|
-
"
|
|
31
|
-
"
|
|
32
|
-
"koa": "^
|
|
33
|
-
"koa-
|
|
34
|
-
"koa-
|
|
35
|
-
"
|
|
36
|
-
"
|
|
37
|
-
"
|
|
38
|
-
"
|
|
39
|
-
"
|
|
40
|
-
"
|
|
41
|
-
"
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
"utility": "^1.17.0"
|
|
46
|
-
},
|
|
47
|
-
"devDependencies": {}
|
|
48
|
-
}
|
|
1
|
+
{
|
|
2
|
+
"name": "ee-core",
|
|
3
|
+
"version": "2.0.0-beta.1",
|
|
4
|
+
"description": "ee core",
|
|
5
|
+
"main": "index.js",
|
|
6
|
+
"scripts": {
|
|
7
|
+
"test": "echo \"Error: no test specified\" && exit 1"
|
|
8
|
+
},
|
|
9
|
+
"author": "",
|
|
10
|
+
"license": "ISC",
|
|
11
|
+
"bin": {
|
|
12
|
+
"ee-core": "./bin/tools.js"
|
|
13
|
+
},
|
|
14
|
+
"dependencies": {
|
|
15
|
+
"agentkeepalive": "^4.2.0",
|
|
16
|
+
"bytenode": "^1.3.6",
|
|
17
|
+
"co": "^4.6.0",
|
|
18
|
+
"debug": "^4.3.3",
|
|
19
|
+
"depd": "^2.0.0",
|
|
20
|
+
"egg-errors": "^2.3.0",
|
|
21
|
+
"egg-logger": "^2.7.1",
|
|
22
|
+
"electron-is": "^3.0.0",
|
|
23
|
+
"electron-updater": "^4.6.1",
|
|
24
|
+
"extend2": "^1.0.1",
|
|
25
|
+
"fs-extra": "^10.0.0",
|
|
26
|
+
"get-port": "^5.1.1",
|
|
27
|
+
"globby": "^10.0.0",
|
|
28
|
+
"humanize-ms": "^1.2.1",
|
|
29
|
+
"is-type-of": "^1.2.1",
|
|
30
|
+
"javascript-obfuscator": "^4.0.0",
|
|
31
|
+
"koa": "^2.13.4",
|
|
32
|
+
"koa-body": "^5.0.0",
|
|
33
|
+
"koa-convert": "^2.0.0",
|
|
34
|
+
"koa-static": "^5.0.0",
|
|
35
|
+
"koa2-cors": "^2.0.6",
|
|
36
|
+
"lodash": "^4.17.21",
|
|
37
|
+
"mkdirp": "^2.1.3",
|
|
38
|
+
"path-to-regexp": "^6.2.0",
|
|
39
|
+
"socket.io": "^4.4.1",
|
|
40
|
+
"socket.io-client": "^4.4.1",
|
|
41
|
+
"urllib": "^2.38.0"
|
|
42
|
+
},
|
|
43
|
+
"devDependencies": {}
|
|
44
|
+
}
|
package/tools/encrypt.js
CHANGED
|
@@ -1,275 +1,275 @@
|
|
|
1
|
-
'use strict';
|
|
2
|
-
|
|
3
|
-
const path = require('path');
|
|
4
|
-
const fs = require('fs');
|
|
5
|
-
const fsPro = require('fs-extra');
|
|
6
|
-
const is = require('is-type-of');
|
|
7
|
-
const bytenode = require('bytenode');
|
|
8
|
-
const
|
|
9
|
-
const
|
|
10
|
-
const
|
|
11
|
-
|
|
12
|
-
class Encrypt {
|
|
13
|
-
constructor() {
|
|
14
|
-
this.basePath = process.cwd();
|
|
15
|
-
this.dirs = [];
|
|
16
|
-
this.encryptCodeDir = path.join(this.basePath, 'public');
|
|
17
|
-
this.config = this.loadConfig('encrypt.js');
|
|
18
|
-
this.filesExt = this.config.fileExt || ['.js'];
|
|
19
|
-
this.type = this.config.type || 'bytecode';
|
|
20
|
-
this.bOpt = this.config.bytecodeOptions || {};
|
|
21
|
-
this.cOpt = this.config.confusionOptions || {};
|
|
22
|
-
|
|
23
|
-
const directory = this.config.directory || ['electron'];
|
|
24
|
-
this.tmpFile = ''; // todo
|
|
25
|
-
this.mapFile = ''; // todo
|
|
26
|
-
|
|
27
|
-
// cli
|
|
28
|
-
if (Object.keys(this.config).length == 0) {
|
|
29
|
-
for (let i = 0; i < process.argv.length; i++) {
|
|
30
|
-
let tmpArgv = process.argv[i];
|
|
31
|
-
if (tmpArgv.indexOf('--type=') !== -1) {
|
|
32
|
-
this.type = tmpArgv.substring(7);
|
|
33
|
-
}
|
|
34
|
-
}
|
|
35
|
-
}
|
|
36
|
-
|
|
37
|
-
// 检查存在的目录
|
|
38
|
-
for (let i = 0; i < directory.length; i++) {
|
|
39
|
-
let codeDirPath = path.join(this.basePath, directory[i]);
|
|
40
|
-
if (fs.existsSync(codeDirPath)) {
|
|
41
|
-
this.dirs.push(directory[i]);
|
|
42
|
-
}
|
|
43
|
-
}
|
|
44
|
-
console.log('[ee-core] [encrypt] dirs:', this.dirs);
|
|
45
|
-
}
|
|
46
|
-
|
|
47
|
-
/**
|
|
48
|
-
* 备份 electron 目录代码
|
|
49
|
-
*/
|
|
50
|
-
backup () {
|
|
51
|
-
console.log('[ee-core] [encrypt] backup start');
|
|
52
|
-
|
|
53
|
-
for (let i = 0; i < this.dirs.length; i++) {
|
|
54
|
-
// check code dir
|
|
55
|
-
let codeDirPath = path.join(this.basePath, this.dirs[i]);
|
|
56
|
-
if (!fs.existsSync(codeDirPath)) {
|
|
57
|
-
console.log('[ee-core] [encrypt] ERROR: backup %s is not exist', codeDirPath);
|
|
58
|
-
return
|
|
59
|
-
}
|
|
60
|
-
|
|
61
|
-
let targetDir = path.join(this.encryptCodeDir, this.dirs[i]);
|
|
62
|
-
|
|
63
|
-
// remove old
|
|
64
|
-
this.rmBackup(targetDir);
|
|
65
|
-
|
|
66
|
-
// copy
|
|
67
|
-
console.log('[ee-core] [encrypt] backup target Dir:', targetDir);
|
|
68
|
-
if (!fs.existsSync(targetDir)) {
|
|
69
|
-
this.mkdir(targetDir);
|
|
70
|
-
this.chmodPath(targetDir, '777');
|
|
71
|
-
}
|
|
72
|
-
|
|
73
|
-
fsPro.copySync(codeDirPath, targetDir);
|
|
74
|
-
}
|
|
75
|
-
console.log('[ee-core] [encrypt] backup end');
|
|
76
|
-
return true;
|
|
77
|
-
}
|
|
78
|
-
|
|
79
|
-
prepare () {
|
|
80
|
-
if (this.type == 'bytecode') {
|
|
81
|
-
let filename = this.config.mangle || this.config.mangle.file || null;
|
|
82
|
-
if (filename) {
|
|
83
|
-
this.tmpFile = path.join(this.encryptCodeDir, 'electron', 'tmp.json');
|
|
84
|
-
this.mapFile = path.join(this.encryptCodeDir, 'electron', filename);
|
|
85
|
-
fs.writeFileSync(this.mapFile, '');
|
|
86
|
-
const content = {
|
|
87
|
-
nameMap: {}
|
|
88
|
-
};
|
|
89
|
-
|
|
90
|
-
}
|
|
91
|
-
}
|
|
92
|
-
|
|
93
|
-
return true;
|
|
94
|
-
}
|
|
95
|
-
|
|
96
|
-
/**
|
|
97
|
-
* 加密代码
|
|
98
|
-
*/
|
|
99
|
-
encrypt () {
|
|
100
|
-
console.log('[ee-core] [encrypt] start ciphering');
|
|
101
|
-
for (let i = 0; i < this.dirs.length; i++) {
|
|
102
|
-
let codeDirPath = path.join(this.encryptCodeDir, this.dirs[i]);
|
|
103
|
-
this.loop(codeDirPath);
|
|
104
|
-
}
|
|
105
|
-
|
|
106
|
-
console.log('[ee-core] [encrypt] end ciphering');
|
|
107
|
-
};
|
|
108
|
-
|
|
109
|
-
/**
|
|
110
|
-
* 递归
|
|
111
|
-
*/
|
|
112
|
-
loop (dirPath) {
|
|
113
|
-
let files = [];
|
|
114
|
-
if (fs.existsSync(dirPath)) {
|
|
115
|
-
files = fs.readdirSync(dirPath);
|
|
116
|
-
files.forEach((file, index) => {
|
|
117
|
-
let curPath = dirPath + '/' + file;
|
|
118
|
-
if (fs.statSync(curPath).isDirectory()) {
|
|
119
|
-
this.loop(curPath);
|
|
120
|
-
} else {
|
|
121
|
-
const extname = path.extname(curPath);
|
|
122
|
-
if (this.filesExt.indexOf(extname) !== -1) {
|
|
123
|
-
this.generate(curPath);
|
|
124
|
-
}
|
|
125
|
-
}
|
|
126
|
-
});
|
|
127
|
-
}
|
|
128
|
-
}
|
|
129
|
-
|
|
130
|
-
/**
|
|
131
|
-
* 生成文件
|
|
132
|
-
*/
|
|
133
|
-
generate (curPath) {
|
|
134
|
-
if (this.type == 'bytecode') {
|
|
135
|
-
this.generateBytecodeFile(curPath);
|
|
136
|
-
} else if (this.type == 'confusion') {
|
|
137
|
-
this.generateJSConfuseFile(curPath);
|
|
138
|
-
} else {
|
|
139
|
-
this.generateJSConfuseFile(curPath);
|
|
140
|
-
this.generateBytecodeFile(curPath);
|
|
141
|
-
}
|
|
142
|
-
}
|
|
143
|
-
|
|
144
|
-
/**
|
|
145
|
-
* 使用 javascript-obfuscator 生成压缩/混淆文件
|
|
146
|
-
*/
|
|
147
|
-
generateJSConfuseFile (file) {
|
|
148
|
-
let opt = Object.assign({
|
|
149
|
-
compact: true,
|
|
150
|
-
stringArray: true,
|
|
151
|
-
stringArrayThreshold: 1,
|
|
152
|
-
}, this.cOpt);
|
|
153
|
-
|
|
154
|
-
let code = fs.readFileSync(file, "utf8");
|
|
155
|
-
let result = JavaScriptObfuscator.obfuscate(code, opt);
|
|
156
|
-
fs.writeFileSync(file, result.getObfuscatedCode(), "utf8");
|
|
157
|
-
}
|
|
158
|
-
|
|
159
|
-
/**
|
|
160
|
-
* 生成字节码文件
|
|
161
|
-
*/
|
|
162
|
-
generateBytecodeFile (curPath) {
|
|
163
|
-
if (path.extname(curPath) !== '.js') {
|
|
164
|
-
return
|
|
165
|
-
}
|
|
166
|
-
//let jscFile = curPath.replace(/.js/g, '.jsc');
|
|
167
|
-
let jscFile = curPath + 'c';
|
|
168
|
-
let opt = Object.assign({
|
|
169
|
-
filename: curPath,
|
|
170
|
-
output: jscFile,
|
|
171
|
-
electron: true
|
|
172
|
-
}, this.bOpt);
|
|
173
|
-
|
|
174
|
-
bytenode.compileFile(opt);
|
|
175
|
-
|
|
176
|
-
//fs.writeFileSync(curPath, 'require("bytenode");module.exports = require("./'+path.basename(jscFile)+'");', 'utf8');
|
|
177
|
-
|
|
178
|
-
fsPro.removeSync(curPath);
|
|
179
|
-
}
|
|
180
|
-
|
|
181
|
-
/**
|
|
182
|
-
* 移除备份
|
|
183
|
-
*/
|
|
184
|
-
rmBackup (dir) {
|
|
185
|
-
if (fs.existsSync(dir)) {
|
|
186
|
-
console.log('[ee-core] [encrypt] clean old directory:', dir);
|
|
187
|
-
fsPro.removeSync(dir);
|
|
188
|
-
}
|
|
189
|
-
return;
|
|
190
|
-
}
|
|
191
|
-
|
|
192
|
-
/**
|
|
193
|
-
* 检查文件是否存在
|
|
194
|
-
*/
|
|
195
|
-
fileExist (filePath) {
|
|
196
|
-
try {
|
|
197
|
-
return fs.statSync(filePath).isFile();
|
|
198
|
-
} catch (err) {
|
|
199
|
-
return false;
|
|
200
|
-
}
|
|
201
|
-
};
|
|
202
|
-
|
|
203
|
-
mkdir (dirpath, dirname) {
|
|
204
|
-
// 判断是否是第一次调用
|
|
205
|
-
if (typeof dirname === 'undefined') {
|
|
206
|
-
if (fs.existsSync(dirpath)) {
|
|
207
|
-
return;
|
|
208
|
-
}
|
|
209
|
-
this.mkdir(dirpath, path.dirname(dirpath));
|
|
210
|
-
} else {
|
|
211
|
-
// 判断第二个参数是否正常,避免调用时传入错误参数
|
|
212
|
-
if (dirname !== path.dirname(dirpath)) {
|
|
213
|
-
this.mkdir(dirpath);
|
|
214
|
-
return;
|
|
215
|
-
}
|
|
216
|
-
if (fs.existsSync(dirname)) {
|
|
217
|
-
fs.mkdirSync(dirpath);
|
|
218
|
-
} else {
|
|
219
|
-
this.mkdir(dirname, path.dirname(dirname));
|
|
220
|
-
fs.mkdirSync(dirpath);
|
|
221
|
-
}
|
|
222
|
-
}
|
|
223
|
-
};
|
|
224
|
-
|
|
225
|
-
chmodPath (path, mode) {
|
|
226
|
-
let files = [];
|
|
227
|
-
if (fs.existsSync(path)) {
|
|
228
|
-
files = fs.readdirSync(path);
|
|
229
|
-
files.forEach((file, index) => {
|
|
230
|
-
const curPath = path + '/' + file;
|
|
231
|
-
if (fs.statSync(curPath).isDirectory()) {
|
|
232
|
-
this.chmodPath(curPath, mode); // 递归删除文件夹
|
|
233
|
-
} else {
|
|
234
|
-
fs.chmodSync(curPath, mode);
|
|
235
|
-
}
|
|
236
|
-
});
|
|
237
|
-
fs.chmodSync(path, mode);
|
|
238
|
-
}
|
|
239
|
-
};
|
|
240
|
-
|
|
241
|
-
loadConfig (prop) {
|
|
242
|
-
const filepath = path.join(this.basePath, 'electron', 'config', prop);
|
|
243
|
-
if (!fs.existsSync(filepath)) {
|
|
244
|
-
return {};
|
|
245
|
-
}
|
|
246
|
-
const obj = require(filepath);
|
|
247
|
-
if (!obj) return obj;
|
|
248
|
-
|
|
249
|
-
let ret = obj;
|
|
250
|
-
if (is.function(obj) && !is.class(obj)) {
|
|
251
|
-
ret = obj();
|
|
252
|
-
}
|
|
253
|
-
|
|
254
|
-
return ret || {};
|
|
255
|
-
};
|
|
256
|
-
|
|
257
|
-
md5 (file) {
|
|
258
|
-
const buffer = fs.readFileSync(file);
|
|
259
|
-
const hash = crypto.createHash('md5');
|
|
260
|
-
hash.update(buffer, 'utf8');
|
|
261
|
-
const str = hash.digest('hex');
|
|
262
|
-
return str;
|
|
263
|
-
}
|
|
264
|
-
}
|
|
265
|
-
|
|
266
|
-
const run = () => {
|
|
267
|
-
const e = new Encrypt();
|
|
268
|
-
if (!e.backup()) return;
|
|
269
|
-
//if (!e.prepare()) return;
|
|
270
|
-
e.encrypt();
|
|
271
|
-
}
|
|
272
|
-
|
|
273
|
-
module.exports = {
|
|
274
|
-
run
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
const path = require('path');
|
|
4
|
+
const fs = require('fs');
|
|
5
|
+
const fsPro = require('fs-extra');
|
|
6
|
+
const is = require('is-type-of');
|
|
7
|
+
const bytenode = require('bytenode');
|
|
8
|
+
const crypto = require('crypto');
|
|
9
|
+
const JavaScriptObfuscator = require('javascript-obfuscator');
|
|
10
|
+
const utilsJson = require('../module/utils/json');
|
|
11
|
+
|
|
12
|
+
class Encrypt {
|
|
13
|
+
constructor() {
|
|
14
|
+
this.basePath = process.cwd();
|
|
15
|
+
this.dirs = [];
|
|
16
|
+
this.encryptCodeDir = path.join(this.basePath, 'public');
|
|
17
|
+
this.config = this.loadConfig('encrypt.js');
|
|
18
|
+
this.filesExt = this.config.fileExt || ['.js'];
|
|
19
|
+
this.type = this.config.type || 'bytecode';
|
|
20
|
+
this.bOpt = this.config.bytecodeOptions || {};
|
|
21
|
+
this.cOpt = this.config.confusionOptions || {};
|
|
22
|
+
|
|
23
|
+
const directory = this.config.directory || ['electron'];
|
|
24
|
+
this.tmpFile = ''; // todo
|
|
25
|
+
this.mapFile = ''; // todo
|
|
26
|
+
|
|
27
|
+
// cli
|
|
28
|
+
if (Object.keys(this.config).length == 0) {
|
|
29
|
+
for (let i = 0; i < process.argv.length; i++) {
|
|
30
|
+
let tmpArgv = process.argv[i];
|
|
31
|
+
if (tmpArgv.indexOf('--type=') !== -1) {
|
|
32
|
+
this.type = tmpArgv.substring(7);
|
|
33
|
+
}
|
|
34
|
+
}
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
// 检查存在的目录
|
|
38
|
+
for (let i = 0; i < directory.length; i++) {
|
|
39
|
+
let codeDirPath = path.join(this.basePath, directory[i]);
|
|
40
|
+
if (fs.existsSync(codeDirPath)) {
|
|
41
|
+
this.dirs.push(directory[i]);
|
|
42
|
+
}
|
|
43
|
+
}
|
|
44
|
+
console.log('[ee-core] [encrypt] dirs:', this.dirs);
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
/**
|
|
48
|
+
* 备份 electron 目录代码
|
|
49
|
+
*/
|
|
50
|
+
backup () {
|
|
51
|
+
console.log('[ee-core] [encrypt] backup start');
|
|
52
|
+
|
|
53
|
+
for (let i = 0; i < this.dirs.length; i++) {
|
|
54
|
+
// check code dir
|
|
55
|
+
let codeDirPath = path.join(this.basePath, this.dirs[i]);
|
|
56
|
+
if (!fs.existsSync(codeDirPath)) {
|
|
57
|
+
console.log('[ee-core] [encrypt] ERROR: backup %s is not exist', codeDirPath);
|
|
58
|
+
return
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
let targetDir = path.join(this.encryptCodeDir, this.dirs[i]);
|
|
62
|
+
|
|
63
|
+
// remove old
|
|
64
|
+
this.rmBackup(targetDir);
|
|
65
|
+
|
|
66
|
+
// copy
|
|
67
|
+
console.log('[ee-core] [encrypt] backup target Dir:', targetDir);
|
|
68
|
+
if (!fs.existsSync(targetDir)) {
|
|
69
|
+
this.mkdir(targetDir);
|
|
70
|
+
this.chmodPath(targetDir, '777');
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
fsPro.copySync(codeDirPath, targetDir);
|
|
74
|
+
}
|
|
75
|
+
console.log('[ee-core] [encrypt] backup end');
|
|
76
|
+
return true;
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
prepare () {
|
|
80
|
+
if (this.type == 'bytecode') {
|
|
81
|
+
let filename = this.config.mangle || this.config.mangle.file || null;
|
|
82
|
+
if (filename) {
|
|
83
|
+
this.tmpFile = path.join(this.encryptCodeDir, 'electron', 'tmp.json');
|
|
84
|
+
this.mapFile = path.join(this.encryptCodeDir, 'electron', filename);
|
|
85
|
+
fs.writeFileSync(this.mapFile, '');
|
|
86
|
+
const content = {
|
|
87
|
+
nameMap: {}
|
|
88
|
+
};
|
|
89
|
+
utilsJson.writeSync(this.tmpFile, content);
|
|
90
|
+
}
|
|
91
|
+
}
|
|
92
|
+
|
|
93
|
+
return true;
|
|
94
|
+
}
|
|
95
|
+
|
|
96
|
+
/**
|
|
97
|
+
* 加密代码
|
|
98
|
+
*/
|
|
99
|
+
encrypt () {
|
|
100
|
+
console.log('[ee-core] [encrypt] start ciphering');
|
|
101
|
+
for (let i = 0; i < this.dirs.length; i++) {
|
|
102
|
+
let codeDirPath = path.join(this.encryptCodeDir, this.dirs[i]);
|
|
103
|
+
this.loop(codeDirPath);
|
|
104
|
+
}
|
|
105
|
+
|
|
106
|
+
console.log('[ee-core] [encrypt] end ciphering');
|
|
107
|
+
};
|
|
108
|
+
|
|
109
|
+
/**
|
|
110
|
+
* 递归
|
|
111
|
+
*/
|
|
112
|
+
loop (dirPath) {
|
|
113
|
+
let files = [];
|
|
114
|
+
if (fs.existsSync(dirPath)) {
|
|
115
|
+
files = fs.readdirSync(dirPath);
|
|
116
|
+
files.forEach((file, index) => {
|
|
117
|
+
let curPath = dirPath + '/' + file;
|
|
118
|
+
if (fs.statSync(curPath).isDirectory()) {
|
|
119
|
+
this.loop(curPath);
|
|
120
|
+
} else {
|
|
121
|
+
const extname = path.extname(curPath);
|
|
122
|
+
if (this.filesExt.indexOf(extname) !== -1) {
|
|
123
|
+
this.generate(curPath);
|
|
124
|
+
}
|
|
125
|
+
}
|
|
126
|
+
});
|
|
127
|
+
}
|
|
128
|
+
}
|
|
129
|
+
|
|
130
|
+
/**
|
|
131
|
+
* 生成文件
|
|
132
|
+
*/
|
|
133
|
+
generate (curPath) {
|
|
134
|
+
if (this.type == 'bytecode') {
|
|
135
|
+
this.generateBytecodeFile(curPath);
|
|
136
|
+
} else if (this.type == 'confusion') {
|
|
137
|
+
this.generateJSConfuseFile(curPath);
|
|
138
|
+
} else {
|
|
139
|
+
this.generateJSConfuseFile(curPath);
|
|
140
|
+
this.generateBytecodeFile(curPath);
|
|
141
|
+
}
|
|
142
|
+
}
|
|
143
|
+
|
|
144
|
+
/**
|
|
145
|
+
* 使用 javascript-obfuscator 生成压缩/混淆文件
|
|
146
|
+
*/
|
|
147
|
+
generateJSConfuseFile (file) {
|
|
148
|
+
let opt = Object.assign({
|
|
149
|
+
compact: true,
|
|
150
|
+
stringArray: true,
|
|
151
|
+
stringArrayThreshold: 1,
|
|
152
|
+
}, this.cOpt);
|
|
153
|
+
|
|
154
|
+
let code = fs.readFileSync(file, "utf8");
|
|
155
|
+
let result = JavaScriptObfuscator.obfuscate(code, opt);
|
|
156
|
+
fs.writeFileSync(file, result.getObfuscatedCode(), "utf8");
|
|
157
|
+
}
|
|
158
|
+
|
|
159
|
+
/**
|
|
160
|
+
* 生成字节码文件
|
|
161
|
+
*/
|
|
162
|
+
generateBytecodeFile (curPath) {
|
|
163
|
+
if (path.extname(curPath) !== '.js') {
|
|
164
|
+
return
|
|
165
|
+
}
|
|
166
|
+
//let jscFile = curPath.replace(/.js/g, '.jsc');
|
|
167
|
+
let jscFile = curPath + 'c';
|
|
168
|
+
let opt = Object.assign({
|
|
169
|
+
filename: curPath,
|
|
170
|
+
output: jscFile,
|
|
171
|
+
electron: true
|
|
172
|
+
}, this.bOpt);
|
|
173
|
+
|
|
174
|
+
bytenode.compileFile(opt);
|
|
175
|
+
|
|
176
|
+
//fs.writeFileSync(curPath, 'require("bytenode");module.exports = require("./'+path.basename(jscFile)+'");', 'utf8');
|
|
177
|
+
|
|
178
|
+
fsPro.removeSync(curPath);
|
|
179
|
+
}
|
|
180
|
+
|
|
181
|
+
/**
|
|
182
|
+
* 移除备份
|
|
183
|
+
*/
|
|
184
|
+
rmBackup (dir) {
|
|
185
|
+
if (fs.existsSync(dir)) {
|
|
186
|
+
console.log('[ee-core] [encrypt] clean old directory:', dir);
|
|
187
|
+
fsPro.removeSync(dir);
|
|
188
|
+
}
|
|
189
|
+
return;
|
|
190
|
+
}
|
|
191
|
+
|
|
192
|
+
/**
|
|
193
|
+
* 检查文件是否存在
|
|
194
|
+
*/
|
|
195
|
+
fileExist (filePath) {
|
|
196
|
+
try {
|
|
197
|
+
return fs.statSync(filePath).isFile();
|
|
198
|
+
} catch (err) {
|
|
199
|
+
return false;
|
|
200
|
+
}
|
|
201
|
+
};
|
|
202
|
+
|
|
203
|
+
mkdir (dirpath, dirname) {
|
|
204
|
+
// 判断是否是第一次调用
|
|
205
|
+
if (typeof dirname === 'undefined') {
|
|
206
|
+
if (fs.existsSync(dirpath)) {
|
|
207
|
+
return;
|
|
208
|
+
}
|
|
209
|
+
this.mkdir(dirpath, path.dirname(dirpath));
|
|
210
|
+
} else {
|
|
211
|
+
// 判断第二个参数是否正常,避免调用时传入错误参数
|
|
212
|
+
if (dirname !== path.dirname(dirpath)) {
|
|
213
|
+
this.mkdir(dirpath);
|
|
214
|
+
return;
|
|
215
|
+
}
|
|
216
|
+
if (fs.existsSync(dirname)) {
|
|
217
|
+
fs.mkdirSync(dirpath);
|
|
218
|
+
} else {
|
|
219
|
+
this.mkdir(dirname, path.dirname(dirname));
|
|
220
|
+
fs.mkdirSync(dirpath);
|
|
221
|
+
}
|
|
222
|
+
}
|
|
223
|
+
};
|
|
224
|
+
|
|
225
|
+
chmodPath (path, mode) {
|
|
226
|
+
let files = [];
|
|
227
|
+
if (fs.existsSync(path)) {
|
|
228
|
+
files = fs.readdirSync(path);
|
|
229
|
+
files.forEach((file, index) => {
|
|
230
|
+
const curPath = path + '/' + file;
|
|
231
|
+
if (fs.statSync(curPath).isDirectory()) {
|
|
232
|
+
this.chmodPath(curPath, mode); // 递归删除文件夹
|
|
233
|
+
} else {
|
|
234
|
+
fs.chmodSync(curPath, mode);
|
|
235
|
+
}
|
|
236
|
+
});
|
|
237
|
+
fs.chmodSync(path, mode);
|
|
238
|
+
}
|
|
239
|
+
};
|
|
240
|
+
|
|
241
|
+
loadConfig (prop) {
|
|
242
|
+
const filepath = path.join(this.basePath, 'electron', 'config', prop);
|
|
243
|
+
if (!fs.existsSync(filepath)) {
|
|
244
|
+
return {};
|
|
245
|
+
}
|
|
246
|
+
const obj = require(filepath);
|
|
247
|
+
if (!obj) return obj;
|
|
248
|
+
|
|
249
|
+
let ret = obj;
|
|
250
|
+
if (is.function(obj) && !is.class(obj)) {
|
|
251
|
+
ret = obj();
|
|
252
|
+
}
|
|
253
|
+
|
|
254
|
+
return ret || {};
|
|
255
|
+
};
|
|
256
|
+
|
|
257
|
+
md5 (file) {
|
|
258
|
+
const buffer = fs.readFileSync(file);
|
|
259
|
+
const hash = crypto.createHash('md5');
|
|
260
|
+
hash.update(buffer, 'utf8');
|
|
261
|
+
const str = hash.digest('hex');
|
|
262
|
+
return str;
|
|
263
|
+
}
|
|
264
|
+
}
|
|
265
|
+
|
|
266
|
+
const run = () => {
|
|
267
|
+
const e = new Encrypt();
|
|
268
|
+
if (!e.backup()) return;
|
|
269
|
+
//if (!e.prepare()) return;
|
|
270
|
+
e.encrypt();
|
|
271
|
+
}
|
|
272
|
+
|
|
273
|
+
module.exports = {
|
|
274
|
+
run
|
|
275
275
|
};
|