|
@@ -1,5 +1,26 @@
|
|
const express = require('express');
|
|
const express = require('express');
|
|
const { createProxyMiddleware } = require('http-proxy-middleware');
|
|
const { createProxyMiddleware } = require('http-proxy-middleware');
|
|
|
|
+const crypto = require('crypto');
|
|
|
|
+
|
|
|
|
+const algorithm = 'aes-256-cbc';
|
|
|
|
+const key = crypto.randomBytes(32);
|
|
|
|
+const iv = crypto.randomBytes(16);
|
|
|
|
+
|
|
|
|
+// 解密函数
|
|
|
|
+function decryptData(data, key, iv) {
|
|
|
|
+ const decipher = crypto.createDecipheriv(algorithm, key, iv);
|
|
|
|
+ let decrypted = decipher.update(data, 'hex', 'utf8');
|
|
|
|
+ decrypted += decipher.final('utf8');
|
|
|
|
+ return decrypted;
|
|
|
|
+}
|
|
|
|
+
|
|
|
|
+// 加密函数
|
|
|
|
+function encryptData(data, key, iv) {
|
|
|
|
+ const cipher = crypto.createCipheriv(algorithm, key, iv);
|
|
|
|
+ let encrypted = cipher.update(data, 'utf8', 'hex');
|
|
|
|
+ encrypted += cipher.final('hex');
|
|
|
|
+ return encrypted;
|
|
|
|
+}
|
|
|
|
|
|
const app = express();
|
|
const app = express();
|
|
|
|
|
|
@@ -10,8 +31,25 @@ const proxyOptions = {
|
|
pathRewrite: { '^/api': '' }
|
|
pathRewrite: { '^/api': '' }
|
|
};
|
|
};
|
|
|
|
|
|
|
|
+// 自定义中间件:代理前操作
|
|
|
|
+const preProxyMiddleware = (req, res, next) => {
|
|
|
|
+ console.log('代理请求前操作');
|
|
|
|
+ // 在这里可以执行一些操作,如记录请求信息、修改请求头等
|
|
|
|
+ req.body = decryptData(req.body, key, iv);
|
|
|
|
+ next();
|
|
|
|
+};
|
|
|
|
+
|
|
|
|
+// 自定义中间件:代理后操作
|
|
|
|
+const postProxyMiddleware = (req, res, next) => {
|
|
|
|
+ console.log('代理响应后操作');
|
|
|
|
+ // 在这里可以执行一些操作,如记录响应信息、修改响应数据等
|
|
|
|
+ req.body = encryptData(req.body, key, iv)
|
|
|
|
+ next();
|
|
|
|
+};
|
|
|
|
+
|
|
|
|
+
|
|
// 设置代理
|
|
// 设置代理
|
|
-app.use('/api', createProxyMiddleware(proxyOptions));
|
|
|
|
|
|
+app.use('/api', preProxyMiddleware, createProxyMiddleware(proxyOptions), postProxyMiddleware);
|
|
|
|
|
|
// 启动服务器
|
|
// 启动服务器
|
|
const PORT = process.env.PORT || 3001;
|
|
const PORT = process.env.PORT || 3001;
|