* @function EncryptRequest.prototype.request * @description 加密请求地址。 * @param {Object} config - 加密请求参数。 * @param {string} config.method - 请求方法。 * @param {string} config.url - 请求地址。 * @param {string} config.params - 请求参数。 * @param {Object} config.options - 请求的配置属性。 * @returns {Pro
(options)
| 42 | * @returns {Promise} Promise 对象。 |
| 43 | */ |
| 44 | async request(options) { |
| 45 | if (!this.serverUrl) { |
| 46 | throw 'serverUrl can not be empty.'; |
| 47 | } |
| 48 | const config = Object.assign({ baseURL: '' }, options); |
| 49 | const tunnelUrl = await this._createTunnel(); |
| 50 | if (!tunnelUrl) { |
| 51 | return; |
| 52 | } |
| 53 | for (const pattern of this.blockedUrlRegex[config.method.toUpperCase()]) { |
| 54 | const regex = new RegExp(pattern); |
| 55 | if (regex.test(config.baseURL + config.url)) { |
| 56 | const data = { |
| 57 | url: config.baseURL + (config.url.startsWith('/') ? config.url.substring(1, config.url.length) : config.url), |
| 58 | method: config.method, |
| 59 | timeout: config.timeout, |
| 60 | headers: config.headers, |
| 61 | body: config.data |
| 62 | }; |
| 63 | // 替换请求 |
| 64 | config.method = 'post'; |
| 65 | config.data = AESGCMEncrypt(this.encryptAESKey, this.encryptAESIV, JSON.stringify(data)); |
| 66 | if (!config.data) { |
| 67 | throw 'encrypt failed'; |
| 68 | } |
| 69 | config.url = this.tunnelUrl; |
| 70 | break; |
| 71 | } |
| 72 | } |
| 73 | const response = await FetchRequest.commit(config.method, config.url, config.data, config.options); |
| 74 | if (config.url === this.tunnelUrl) { |
| 75 | const result = await response.text(); |
| 76 | const decryptResult = AESGCMDecrypt(this.encryptAESKey, this.encryptAESIV, result); |
| 77 | if (!decryptResult) { |
| 78 | console.debug('解密请求响应失败'); |
| 79 | return; |
| 80 | } |
| 81 | const resultData = JSON.parse(decryptResult); |
| 82 | const resData = Object.create({ |
| 83 | json: function () { |
| 84 | return Promise.resolve(resultData.data); |
| 85 | } |
| 86 | }); |
| 87 | return Object.assign(resData, resultData); |
| 88 | } |
| 89 | return response; |
| 90 | } |
| 91 | |
| 92 | /** |
| 93 | * @private |
no test coverage detected