ee-core 2.1.0-beta.2 → 2.1.0-beta.4

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/bin/tools.js CHANGED
@@ -16,3 +16,7 @@ if (cmd == 'rd') {
16
16
  if (cmd == 'encrypt') {
17
17
  encrypt.run();
18
18
  }
19
+
20
+ if (cmd == 'clean') {
21
+ encrypt.clean();
22
+ }
package/ee/application.js CHANGED
@@ -2,8 +2,9 @@ const Exception = require('../exception');
2
2
  const {app} = require('electron');
3
3
  const path = require('path');
4
4
  const debug = require('debug')('ee-core:Appliaction');
5
- const fs = require('fs');
6
5
  const EeApp = require('./eeApp');
6
+ const Utils = require('../utils');
7
+ const Ps = require('../ps');
7
8
 
8
9
  class Appliaction extends EeApp {
9
10
  constructor() {
@@ -43,10 +44,10 @@ class Appliaction extends EeApp {
43
44
  options.execDir = path.dirname(app.getPath('exe'));
44
45
  }
45
46
 
47
+ // Todo app.getAppPath() ??? process.cwd()
46
48
  // Use encryption, base directory is public/electron
47
- const encryptDir = path.join(app.getAppPath(), 'public', 'electron');
48
- if (options.env == 'prod' && fs.existsSync(encryptDir)) {
49
- options.baseDir = encryptDir;
49
+ if (options.env == 'prod' && Utils.isEncrypt(app.getAppPath())) {
50
+ options.baseDir = Ps.getEncryptDir(app.getAppPath());
50
51
  options.isEncrypted = true;
51
52
  }
52
53
 
package/ee/index.js CHANGED
@@ -1,3 +1,4 @@
1
+ const Utils = require('../utils');
1
2
  const EEApplication = Symbol('Ee#Application');
2
3
 
3
4
  const EE = {
@@ -24,7 +25,14 @@ const EE = {
24
25
  */
25
26
  get app() {
26
27
  return this[EEApplication] || null;
27
- },
28
+ },
29
+
30
+ /**
31
+ * 是否加密
32
+ */
33
+ isEncrypt(basePath) {
34
+ return Utils.isEncrypt(basePath);
35
+ },
28
36
  }
29
37
 
30
38
  module.exports = EE;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "ee-core",
3
- "version": "2.1.0-beta.2",
3
+ "version": "2.1.0-beta.4",
4
4
  "description": "ee core",
5
5
  "main": "index.js",
6
6
  "scripts": {
package/ps/index.js CHANGED
@@ -1,6 +1,5 @@
1
1
  const path = require('path');
2
2
  const eis = require('../utils/is');
3
- const Log = require('../log');
4
3
 
5
4
  /**
6
5
  * 当前进程的所有env
@@ -112,6 +111,15 @@ exports.getLogDir = function () {
112
111
  return dir;
113
112
  }
114
113
 
114
+ /**
115
+ * 获取加密文件路径
116
+ */
117
+ exports.getEncryptDir = function (basePath) {
118
+ const base = basePath || process.cwd();
119
+ const dir = path.join(base, 'public', 'electron');
120
+ return dir;
121
+ }
122
+
115
123
  /**
116
124
  * 获取root目录 (dev-项目根目录,pro-app user data目录)
117
125
  */
package/tools/encrypt.js CHANGED
@@ -7,6 +7,7 @@ const is = require('is-type-of');
7
7
  const bytenode = require('bytenode');
8
8
  const crypto = require('crypto');
9
9
  const JavaScriptObfuscator = require('javascript-obfuscator');
10
+ const globby = require('globby');
10
11
  const UtilsJson = require('../utils/json');
11
12
 
12
13
  class Encrypt {
@@ -16,11 +17,14 @@ class Encrypt {
16
17
  this.encryptCodeDir = path.join(this.basePath, 'public');
17
18
  this.config = this.loadConfig('encrypt.js');
18
19
  this.filesExt = this.config.fileExt || ['.js'];
19
- this.type = this.config.type || 'bytecode';
20
+ this.type = this.config.type || 'confusion';
20
21
  this.bOpt = this.config.bytecodeOptions || {};
21
22
  this.cOpt = this.config.confusionOptions || {};
23
+ this.cleanFiles = this.config.cleanFiles || ['electron'];
22
24
 
23
25
  const directory = this.config.directory || ['electron'];
26
+ this.patterns = this.config.files || null;
27
+ this.specificFiles = [ 'electron/preload/bridge.js' ];
24
28
  this.tmpFile = ''; // todo
25
29
  this.mapFile = ''; // todo
26
30
 
@@ -34,49 +38,80 @@ class Encrypt {
34
38
  }
35
39
  }
36
40
 
37
- // 检查存在的目录
38
41
  for (let i = 0; i < directory.length; i++) {
39
42
  let codeDirPath = path.join(this.basePath, directory[i]);
40
43
  if (fs.existsSync(codeDirPath)) {
41
44
  this.dirs.push(directory[i]);
42
45
  }
43
46
  }
44
- console.log('[ee-core] [tools/encrypt] dirs:', this.dirs);
47
+
48
+ this.codefiles = this._initCodeFiles();
49
+ //console.log('[ee-core] [tools/encrypt] codefiles:', this.codefiles);
45
50
  }
46
51
 
47
52
  /**
48
- * 备份 electron 目录代码
53
+ * 初始化需要加密的文件列表
49
54
  */
50
- backup () {
51
- console.log('[ee-core] [tools/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] [tools/encrypt] ERROR: backup %s is not exist', codeDirPath);
58
- return
59
- }
55
+ _initCodeFiles() {
56
+ if (!this.patterns) return;
60
57
 
61
- let targetDir = path.join(this.encryptCodeDir, this.dirs[i]);
58
+ const files = globby.sync(this.patterns, { cwd: this.basePath });
59
+ return files;
60
+ }
62
61
 
63
- // remove old
64
- this.rmBackup(targetDir);
62
+ /**
63
+ * 备份代码
64
+ */
65
+ backup() {
66
+ // clean
67
+ this.cleanCode();
65
68
 
66
- // copy
67
- console.log('[ee-core] [tools/encrypt] backup target Dir:', targetDir);
68
- if (!fs.existsSync(targetDir)) {
69
- this.mkdir(targetDir);
70
- this.chmodPath(targetDir, '777');
69
+ console.log('[ee-core] [tools/encrypt] backup start');
70
+ if (this.patterns) {
71
+ this.codefiles.forEach((filepath) => {
72
+ let source = path.join(this.basePath, filepath);
73
+ if (fs.existsSync(source)) {
74
+ let target = path.join(this.encryptCodeDir, filepath);
75
+ fsPro.copySync(source, target);
76
+ }
77
+ })
78
+ } else {
79
+ for (let i = 0; i < this.dirs.length; i++) {
80
+ // check code dir
81
+ let codeDirPath = path.join(this.basePath, this.dirs[i]);
82
+ if (!fs.existsSync(codeDirPath)) {
83
+ console.log('[ee-core] [tools/encrypt] ERROR: backup %s is not exist', codeDirPath);
84
+ return
85
+ }
86
+
87
+ // copy
88
+ let targetDir = path.join(this.encryptCodeDir, this.dirs[i]);
89
+ console.log('[ee-core] [tools/encrypt] backup target Dir:', targetDir);
90
+ if (!fs.existsSync(targetDir)) {
91
+ this.mkdir(targetDir);
92
+ this.chmodPath(targetDir, '777');
93
+ }
94
+
95
+ fsPro.copySync(codeDirPath, targetDir);
71
96
  }
72
-
73
- fsPro.copySync(codeDirPath, targetDir);
74
97
  }
98
+
75
99
  console.log('[ee-core] [tools/encrypt] backup end');
76
100
  return true;
77
101
  }
78
102
 
79
- prepare () {
103
+ /**
104
+ * 清除加密代码
105
+ */
106
+ cleanCode() {
107
+ this.cleanFiles.forEach((file) => {
108
+ let tmpFile = path.join(this.encryptCodeDir, file);
109
+ this.rmBackup(tmpFile);
110
+ console.log('[ee-core] [tools/encrypt] clean up tmp files:', tmpFile);
111
+ })
112
+ }
113
+
114
+ prepare() {
80
115
  if (this.type == 'bytecode') {
81
116
  let filename = this.config.mangle || this.config.mangle.file || null;
82
117
  if (filename) {
@@ -96,11 +131,28 @@ class Encrypt {
96
131
  /**
97
132
  * 加密代码
98
133
  */
99
- encrypt () {
134
+ encrypt() {
100
135
  console.log('[ee-core] [tools/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);
136
+ if (this.patterns) {
137
+ for (const file of this.codefiles) {
138
+ const fullpath = path.join(this.encryptCodeDir, file);
139
+ if (!fs.statSync(fullpath).isFile()) continue;
140
+
141
+ // 特殊文件处理
142
+ if (this.specificFiles.includes(file)) {
143
+ this.generate(fullpath, 'confusion');
144
+ continue;
145
+ }
146
+
147
+ this.generate(fullpath);
148
+ }
149
+ } else {
150
+ console.log('[ee-core] [tools/encrypt] !!!!!! please use the new encryption method !!!!!!');
151
+ for (let i = 0; i < this.dirs.length; i++) {
152
+ let codeDirPath = path.join(this.encryptCodeDir, this.dirs[i]);
153
+ this.loop(codeDirPath);
154
+ }
155
+ console.log('[ee-core] [tools/encrypt] !!!!!! please use the new encryption method !!!!!!');
104
156
  }
105
157
 
106
158
  console.log('[ee-core] [tools/encrypt] end ciphering');
@@ -109,7 +161,7 @@ class Encrypt {
109
161
  /**
110
162
  * 递归
111
163
  */
112
- loop (dirPath) {
164
+ loop(dirPath) {
113
165
  let files = [];
114
166
  if (fs.existsSync(dirPath)) {
115
167
  files = fs.readdirSync(dirPath);
@@ -130,10 +182,13 @@ class Encrypt {
130
182
  /**
131
183
  * 生成文件
132
184
  */
133
- generate (curPath) {
134
- if (this.type == 'bytecode') {
185
+ generate(curPath, type) {
186
+ let encryptType = type ? type : this.type;
187
+ console.log(`[ee-core] [tools/encrypt] file: ${curPath} (${encryptType})`);
188
+
189
+ if (encryptType == 'bytecode') {
135
190
  this.generateBytecodeFile(curPath);
136
- } else if (this.type == 'confusion') {
191
+ } else if (encryptType == 'confusion') {
137
192
  this.generateJSConfuseFile(curPath);
138
193
  } else {
139
194
  this.generateJSConfuseFile(curPath);
@@ -144,7 +199,7 @@ class Encrypt {
144
199
  /**
145
200
  * 使用 javascript-obfuscator 生成压缩/混淆文件
146
201
  */
147
- generateJSConfuseFile (file) {
202
+ generateJSConfuseFile(file) {
148
203
  let opt = Object.assign({
149
204
  compact: true,
150
205
  stringArray: true,
@@ -159,7 +214,7 @@ class Encrypt {
159
214
  /**
160
215
  * 生成字节码文件
161
216
  */
162
- generateBytecodeFile (curPath) {
217
+ generateBytecodeFile(curPath) {
163
218
  if (path.extname(curPath) !== '.js') {
164
219
  return
165
220
  }
@@ -181,10 +236,9 @@ class Encrypt {
181
236
  /**
182
237
  * 移除备份
183
238
  */
184
- rmBackup (dir) {
185
- if (fs.existsSync(dir)) {
186
- console.log('[ee-core] [tools/encrypt] clean old directory:', dir);
187
- fsPro.removeSync(dir);
239
+ rmBackup(file) {
240
+ if (fs.existsSync(file)) {
241
+ fsPro.removeSync(file);
188
242
  }
189
243
  return;
190
244
  }
@@ -192,7 +246,7 @@ class Encrypt {
192
246
  /**
193
247
  * 检查文件是否存在
194
248
  */
195
- fileExist (filePath) {
249
+ fileExist(filePath) {
196
250
  try {
197
251
  return fs.statSync(filePath).isFile();
198
252
  } catch (err) {
@@ -200,7 +254,7 @@ class Encrypt {
200
254
  }
201
255
  };
202
256
 
203
- mkdir (dirpath, dirname) {
257
+ mkdir(dirpath, dirname) {
204
258
  // 判断是否是第一次调用
205
259
  if (typeof dirname === 'undefined') {
206
260
  if (fs.existsSync(dirpath)) {
@@ -222,7 +276,7 @@ class Encrypt {
222
276
  }
223
277
  };
224
278
 
225
- chmodPath (path, mode) {
279
+ chmodPath(path, mode) {
226
280
  let files = [];
227
281
  if (fs.existsSync(path)) {
228
282
  files = fs.readdirSync(path);
@@ -238,7 +292,7 @@ class Encrypt {
238
292
  }
239
293
  };
240
294
 
241
- loadConfig (prop) {
295
+ loadConfig(prop) {
242
296
  const filepath = path.join(this.basePath, 'electron', 'config', prop);
243
297
  if (!fs.existsSync(filepath)) {
244
298
  return {};
@@ -254,7 +308,7 @@ class Encrypt {
254
308
  return ret || {};
255
309
  };
256
310
 
257
- md5 (file) {
311
+ md5(file) {
258
312
  const buffer = fs.readFileSync(file);
259
313
  const hash = crypto.createHash('md5');
260
314
  hash.update(buffer, 'utf8');
@@ -270,6 +324,12 @@ const run = () => {
270
324
  e.encrypt();
271
325
  }
272
326
 
327
+ const clean = () => {
328
+ const e = new Encrypt();
329
+ e.cleanCode();
330
+ }
331
+
273
332
  module.exports = {
274
- run
333
+ run,
334
+ clean,
275
335
  };
package/utils/index.js CHANGED
@@ -2,6 +2,7 @@
2
2
 
3
3
  const os = require("os");
4
4
  const path = require('path');
5
+ const fs = require('fs');
5
6
  const Ps = require('../ps');
6
7
  const UtilsJson = require('./json');
7
8
 
@@ -57,5 +58,15 @@ exports.isMAC = function(macAddress) {
57
58
  return macRegex.test(macAddress);
58
59
  }
59
60
 
61
+ /**
62
+ * is encrypt
63
+ */
64
+ exports.isEncrypt = function(basePath) {
65
+ const encryptDir = Ps.getEncryptDir(basePath);
66
+ if (fs.existsSync(encryptDir)) {
67
+ return true;
68
+ }
69
+ return false;
70
+ }
60
71
 
61
72