@tmsfe/tms-core 0.0.144 → 0.0.146

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/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@tmsfe/tms-core",
3
- "version": "0.0.144",
3
+ "version": "0.0.146",
4
4
  "description": "tms运行时框架",
5
5
  "repository": {
6
6
  "type": "git",
package/src/encrypt.js CHANGED
@@ -56,6 +56,22 @@ function _getEccPublicKey() {
56
56
  function _getEccPublicId() {
57
57
  return wx.$_publicKey && wx.$_publicKey.id;
58
58
  }
59
+ // 检查path是否符合下发的路由前缀
60
+ function _checkPathInEnablePrefix(path) {
61
+ if (!_getEccPublicKey()) {
62
+ return false;
63
+ }
64
+ if (wx.$_publicKey && wx.$_publicKey.path === '*') {
65
+ return true;
66
+ }
67
+ const prefixArr = wx.$_publicKey && wx.$_publicKey.path ? wx.$_publicKey.path.split(',').map(item => item.trim()) : [];
68
+ for (let i = 0, len = prefixArr.length; i < len; i++) {
69
+ if (path.indexOf(prefixArr[i]) > -1) {
70
+ return true;
71
+ }
72
+ }
73
+ return false;
74
+ }
59
75
 
60
76
  // 获取私钥
61
77
  let privateKeyInfo = null;
@@ -212,12 +228,13 @@ const updateDecryptKey = (publicKeyInfo) => {
212
228
  wx.$_publicKey = null;
213
229
  return;
214
230
  }
215
- const { publicKey, expireDate, id } = publicKeyInfo;
231
+ const { publicKey, expireDate, id, path } = publicKeyInfo;
216
232
  // 1. 存储新的公钥
217
233
  wx.$_publicKey = {
218
234
  publicKey,
219
235
  expireDate,
220
236
  id,
237
+ path,
221
238
  };
222
239
  // 2. 生成新的私钥
223
240
  _getPrivateKeyInfo(true);
@@ -233,7 +250,9 @@ const encryptPathRule = {
233
250
  ],
234
251
  };
235
252
  function needsEncryption(url, params) {
236
- if (!_getEccPublicKey()) {
253
+ // 判断是否属于加密白名单, 如果不符合,直接返回false, 如果符合,则走下面的逻辑
254
+ const enablePathPrefix = _checkPathInEnablePrefix(url);
255
+ if (!enablePathPrefix) {
237
256
  return false;
238
257
  }
239
258
  // 如果是日志上报接口,需要过滤性能日志,不需要加密
package/src/request.js CHANGED
@@ -349,7 +349,8 @@ export default class Request {
349
349
  // wx请求封装成promise
350
350
  async wxRequest(path, method, header = {}, data, needReport, seqId) {
351
351
  if (needReport && path.indexOf('basic/event/upload') < 0) {
352
- reporter.reportPerformance(
352
+ logger.log(
353
+ 'tms-performance-log',
353
354
  'request_encrypt_log', 'main', 'send_unencrypt_request', seqId,
354
355
  !!wx.$_publicKey, Request.requestEncryptOpen, path,
355
356
  );
@@ -357,7 +358,7 @@ export default class Request {
357
358
  return new Promise((resolve, reject) => {
358
359
  wx.request({
359
360
  url: path,
360
- header: { ...header, 'X-Trace-Id': seqId },
361
+ header: { ...header, 'X-Seq-Id': seqId },
361
362
  method,
362
363
  data,
363
364
  enableHttp2: true,
@@ -380,7 +381,7 @@ export default class Request {
380
381
  encryptObj.updateDecryptKey(res.data.resData);
381
382
  } catch (e) {
382
383
  encryptObj.updateDecryptKey(null);
383
- reporter.reportPerformance('request_encrypt_log', 'main', 'refresh_encrypt_key_false', e);
384
+ logger.log('tms-performance-log', 'request_encrypt_log', 'main', 'refresh_encrypt_key_false', e);
384
385
  }
385
386
  }
386
387
 
@@ -477,7 +478,7 @@ export default class Request {
477
478
  header: encryptHeader, data: encryptData, aesKey,
478
479
  } = encryptObj.reqEncrypt(method, data, header, '');
479
480
  // 2. 发送请求
480
- reporter.reportPerformance('request_encrypt_log', 'main', 'send_encrypt_request', seqId);
481
+ logger.log('tms-performance-log', 'request_encrypt_log', 'main', 'send_encrypt_request', seqId, '', '', path);
481
482
  const result = await this.wxRequest(finalUrl, method, encryptHeader, encryptData, false, seqId);
482
483
  const { header: resHeader, data: resData } = result;
483
484
  // 3. 解密响应
@@ -486,12 +487,12 @@ export default class Request {
486
487
  reporter.reportPerformance('request_encrypt_log', 'main', 'local_response_decrypt_fail', seqId);
487
488
  return this.createRequestTask(path, param, method, header, false);
488
489
  }
489
- reporter.reportPerformance('request_encrypt_log', 'main', 'decrypt_response_success', seqId);
490
+ logger.log('tms-performance-log', 'request_encrypt_log', 'main', 'decrypt_response_success', seqId);
490
491
  // 4. 处理解密失败的响应
491
492
  const errCodeType = encryptObj.getErrcodeType(decryptData.errCode, decryptData.errMsg);
492
493
  if (errCodeType === encryptObj.reqErrType.pubKeyInvalid) { // 秘钥失效
493
- reporter.reportPerformance(
494
- 'request_encrypt_log', 'main', 'remote_response_decrypt_fail',
494
+ logger.log(
495
+ 'tms-performance-log', 'request_encrypt_log', 'main', 'remote_response_decrypt_fail',
495
496
  seqId, decryptData.errCode, retryTimes,
496
497
  );
497
498
  const encryptSwitch = await this.dealEncryptionSwitch(resHeader, true);
@@ -501,8 +502,8 @@ export default class Request {
501
502
  return this.createRequestTask(path, param, method, header, encryptSwitch, retryTimes + 1);
502
503
  }
503
504
  if (errCodeType === encryptObj.reqErrType.decryptError) { // 解密失败
504
- reporter.reportPerformance(
505
- 'request_encrypt_log', 'main', 'remote_response_decrypt_fail',
505
+ logger.log(
506
+ 'tms-performance-log', 'request_encrypt_log', 'main', 'remote_response_decrypt_fail',
506
507
  seqId, decryptData.errCode, retryTimes,
507
508
  );
508
509
  return this.createRequestTask(path, param, method, header, false);