|
| 1 | +import { FetchRequest } from './FetchRequest'; |
| 2 | +import { |
| 3 | + RSAEncrypt, |
| 4 | + AESGCMEncrypt, |
| 5 | + AESGCMDecrypt, |
| 6 | + generateAESRandomKey, |
| 7 | + generateAESRandomIV |
| 8 | +} from './RequestcryptUtil'; |
| 9 | +import URI from 'urijs'; |
| 10 | + |
| 11 | +/** |
| 12 | + * @name EncryptRequest |
| 13 | + * @version 11.2.0 |
| 14 | + * @namespace |
| 15 | + * @category BaseTypes Util |
| 16 | + * @classdesc 加密请求地址 |
| 17 | + * @param {string} serverUrl - 服务地址。 |
| 18 | + */ |
| 19 | +export class EncryptRequest { |
| 20 | + constructor(serverUrl) { |
| 21 | + this.serverUrl = serverUrl; |
| 22 | + this.tunnelUrl = undefined; |
| 23 | + this.blockedUrlRegex = { |
| 24 | + HEAD: [], |
| 25 | + POST: [], |
| 26 | + GET: [], |
| 27 | + PUT: [], |
| 28 | + DELETE: [] |
| 29 | + }; |
| 30 | + this.encryptAESKey = generateAESRandomKey(); |
| 31 | + this.encryptAESIV = generateAESRandomIV(); |
| 32 | + } |
| 33 | + |
| 34 | + /** |
| 35 | + * @function EncryptRequest.prototype.request |
| 36 | + * @description 加密请求地址。 |
| 37 | + * @param {Object} config - 加密请求参数。 |
| 38 | + * @param {string} config.method - 请求方法。 |
| 39 | + * @param {string} config.url - 请求地址。 |
| 40 | + * @param {string} config.params - 请求参数。 |
| 41 | + * @param {Object} config.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 |
| 94 | + * @description 获取RSA public key |
| 95 | + * @function EncryptRequest.prototype._getRSAPublicKey |
| 96 | + */ |
| 97 | + async _getRSAPublicKey() { |
| 98 | + try { |
| 99 | + const response = await FetchRequest.get(URI(this.serverUrl).segment('services/security/tunnel/v1/publickey').toString()); |
| 100 | + // 解析publicKey |
| 101 | + const publicKeyObj = await response.json(); |
| 102 | + // 生成AES密钥 |
| 103 | + const aesKeyObj = { |
| 104 | + key: this.encryptAESKey, |
| 105 | + iv: this.encryptAESIV, |
| 106 | + mode: 'GCM', |
| 107 | + padding: 'NoPadding' |
| 108 | + }; |
| 109 | + // 将AES密钥使用RSA公钥加密 |
| 110 | + const aesCipherText = RSAEncrypt(publicKeyObj.publicKey, aesKeyObj.key + aesKeyObj.iv); |
| 111 | + return aesCipherText; |
| 112 | + } catch (error) { |
| 113 | + console.debug('RSA公钥获取失败,错误详情:' + error); |
| 114 | + } |
| 115 | + } |
| 116 | + |
| 117 | + /** |
| 118 | + * @private |
| 119 | + * @description 创建隧道 |
| 120 | + * @function EncryptRequest.prototype._createTunnel |
| 121 | + */ |
| 122 | + async _createTunnel() { |
| 123 | + if (!this.tunnelUrl) { |
| 124 | + try { |
| 125 | + const data = await this._getRSAPublicKey(); |
| 126 | + if (!data) { |
| 127 | + throw 'fetch RSA publicKey failed'; |
| 128 | + } |
| 129 | + // 创建隧道 |
| 130 | + const response = await FetchRequest.post(URI(this.serverUrl).segment('services/security/tunnel/v1/tunnels').toString(), data); |
| 131 | + const result = await response.json(); |
| 132 | + Object.assign(this, { |
| 133 | + tunnelUrl: result.tunnelUrl, |
| 134 | + blockedUrlRegex: Object.assign({}, this.blockedUrlRegex, result.blockedUrlRegex) |
| 135 | + }); |
| 136 | + } catch (error) { |
| 137 | + console.debug('安全隧道创建失败,错误详情:' + error); |
| 138 | + } |
| 139 | + } |
| 140 | + return this.tunnelUrl; |
| 141 | + } |
| 142 | +} |
| 143 | + |
| 144 | +/** |
| 145 | + * @function getServiceKey |
| 146 | + * @version 11.2.0 |
| 147 | + * @category iServer |
| 148 | + * @description 获取矢量瓦片解密密钥 |
| 149 | + * @param {string} serviceUrl - iserver服务地址,例如: 'http://127.0.0.1:8090/iserver/services/xxx'、 'http://127.0.0.1:8090/iserver/services/xxx/rest/maps' 、 'http://127.0.0.1:8090/iserver/services/xxx/restjsr/v1/vectortile' |
| 150 | + * @return {Promise} key - 矢量瓦片密钥 |
| 151 | + */ |
| 152 | +export async function getServiceKey(serviceUrl) { |
| 153 | + try { |
| 154 | + const workspaceServerUrl = ((serviceUrl && |
| 155 | + serviceUrl.match(/.+(?=(\/restjsr\/v1\/vectortile\/|\/rest\/maps\/))/)) || |
| 156 | + [])[0]; |
| 157 | + if (!workspaceServerUrl) { |
| 158 | + return; |
| 159 | + } |
| 160 | + const servicesResponse = await FetchRequest.get(workspaceServerUrl); |
| 161 | + const servicesResult = await servicesResponse.json(); |
| 162 | + const matchRestData = (servicesResult || []).find( |
| 163 | + (item) => serviceUrl.includes(item.name) && item.serviceEncryptInfo |
| 164 | + ); |
| 165 | + if (!matchRestData) { |
| 166 | + return; |
| 167 | + } |
| 168 | + const iserverHost = workspaceServerUrl.split('/services/')[0]; |
| 169 | + const encryptRequest = new EncryptRequest(iserverHost); |
| 170 | + const svckeyUrl = |
| 171 | + matchRestData && `${iserverHost}/services/security/svckeys/${matchRestData.serviceEncryptInfo.encrptKeyID}.json`; |
| 172 | + const svcReponse = await encryptRequest.request({ |
| 173 | + method: 'get', |
| 174 | + url: svckeyUrl |
| 175 | + }); |
| 176 | + return svcReponse.json(); |
| 177 | + } catch (error) { |
| 178 | + console.error(error); |
| 179 | + } |
| 180 | +} |
0 commit comments