|
@@ -1,7 +1,15 @@
|
|
import { App, Config, Inject, InjectClient, Provide } from '@midwayjs/core';
|
|
import { App, Config, Inject, InjectClient, Provide } from '@midwayjs/core';
|
|
import { Application, Context } from '@midwayjs/koa';
|
|
import { Application, Context } from '@midwayjs/koa';
|
|
import { RequestBase } from '../interface/proxy.interface';
|
|
import { RequestBase } from '../interface/proxy.interface';
|
|
-import { get, isObject, isString, lowerCase, omit, pick } from 'lodash';
|
|
|
|
|
|
+import {
|
|
|
|
+ get,
|
|
|
|
+ isEqual,
|
|
|
|
+ isObject,
|
|
|
|
+ isString,
|
|
|
|
+ lowerCase,
|
|
|
|
+ omit,
|
|
|
|
+ pick,
|
|
|
|
+} from 'lodash';
|
|
import { HttpServiceFactory, HttpService } from '@midwayjs/axios';
|
|
import { HttpServiceFactory, HttpService } from '@midwayjs/axios';
|
|
import { PemService } from './pem.service';
|
|
import { PemService } from './pem.service';
|
|
@Provide()
|
|
@Provide()
|
|
@@ -43,7 +51,13 @@ export class ProxyService {
|
|
'ip',
|
|
'ip',
|
|
'originalUrl',
|
|
'originalUrl',
|
|
]) as RequestBase;
|
|
]) as RequestBase;
|
|
-
|
|
|
|
|
|
+ // 加密后的长度与解密后的长度不一致,会导致服务无法解析
|
|
|
|
+ delete result.header['content-length'];
|
|
|
|
+ if (this.useCrypto) {
|
|
|
|
+ if (result.body && result.rawBody) {
|
|
|
|
+ result.body = JSON.parse(result.rawBody);
|
|
|
|
+ }
|
|
|
|
+ }
|
|
return result;
|
|
return result;
|
|
}
|
|
}
|
|
/**
|
|
/**
|
|
@@ -70,7 +84,7 @@ export class ProxyService {
|
|
break;
|
|
break;
|
|
}
|
|
}
|
|
if (!url) return;
|
|
if (!url) return;
|
|
- const reqConfig: any = {
|
|
|
|
|
|
+ let reqConfig: any = {
|
|
url,
|
|
url,
|
|
method: rb.method,
|
|
method: rb.method,
|
|
headers: rb.header,
|
|
headers: rb.header,
|
|
@@ -78,15 +92,17 @@ export class ProxyService {
|
|
if (lowerCase(rb.method) === 'post') {
|
|
if (lowerCase(rb.method) === 'post') {
|
|
// 如果是post函数,且 不属于开发模式,则解密函数体.否则不用解密
|
|
// 如果是post函数,且 不属于开发模式,则解密函数体.否则不用解密
|
|
if (this.useCrypto) {
|
|
if (this.useCrypto) {
|
|
- let preBody;
|
|
|
|
- if (isString(rb.body)) preBody = rb.body;
|
|
|
|
- else if (isObject(rb.body)) preBody = JSON.stringify(rb.body);
|
|
|
|
|
|
+ const preBody = get(rb.body, 'data');
|
|
const deBody = this.pemService.decrypt(preBody);
|
|
const deBody = this.pemService.decrypt(preBody);
|
|
- reqConfig.data = deBody;
|
|
|
|
|
|
+ if (deBody) {
|
|
|
|
+ reqConfig.data = JSON.parse(deBody);
|
|
|
|
+ delete reqConfig.rawBody;
|
|
|
|
+ }
|
|
} else {
|
|
} else {
|
|
reqConfig.data = rb.body;
|
|
reqConfig.data = rb.body;
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
+ reqConfig = JSON.parse(JSON.stringify(reqConfig));
|
|
const res = await this.serviceAxios.request(reqConfig);
|
|
const res = await this.serviceAxios.request(reqConfig);
|
|
if (res.status !== 200) throw new Error('proxy service request error');
|
|
if (res.status !== 200) throw new Error('proxy service request error');
|
|
const result = res.data;
|
|
const result = res.data;
|