whistle.pastekitlab 1.7.3 → 1.7.5
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/index.js +54 -1
- package/package.json +1 -1
package/index.js
CHANGED
|
@@ -136,6 +136,52 @@ function broadcast(type, data) {
|
|
|
136
136
|
});
|
|
137
137
|
}
|
|
138
138
|
|
|
139
|
+
// ==================== 转发请求(不过滤、不通知)====================
|
|
140
|
+
async function forwardRequest(req, res, url) {
|
|
141
|
+
try {
|
|
142
|
+
// 收集请求体
|
|
143
|
+
let reqBody = '';
|
|
144
|
+
for await (const chunk of req) {
|
|
145
|
+
reqBody += chunk;
|
|
146
|
+
}
|
|
147
|
+
|
|
148
|
+
// 通过 axios 转发请求
|
|
149
|
+
const response = await axios({
|
|
150
|
+
method: req.method.toLowerCase(),
|
|
151
|
+
url: url,
|
|
152
|
+
headers: req.headers,
|
|
153
|
+
data: reqBody || undefined,
|
|
154
|
+
validateStatus: () => true
|
|
155
|
+
});
|
|
156
|
+
|
|
157
|
+
// 设置响应状态码和头
|
|
158
|
+
res.statusCode = response.status;
|
|
159
|
+
Object.keys(response.headers).forEach(key => {
|
|
160
|
+
res.setHeader(key, response.headers[key]);
|
|
161
|
+
});
|
|
162
|
+
|
|
163
|
+
// 转换响应数据
|
|
164
|
+
let responseBody;
|
|
165
|
+
if (typeof response.data === 'string') {
|
|
166
|
+
responseBody = response.data;
|
|
167
|
+
} else if (Buffer.isBuffer(response.data)) {
|
|
168
|
+
responseBody = response.data;
|
|
169
|
+
} else if (typeof response.data === 'object') {
|
|
170
|
+
responseBody = JSON.stringify(response.data);
|
|
171
|
+
} else {
|
|
172
|
+
responseBody = String(response.data);
|
|
173
|
+
}
|
|
174
|
+
|
|
175
|
+
res.end(responseBody);
|
|
176
|
+
log(`直接转发: ${req.method} ${url} -> ${response.status}`);
|
|
177
|
+
|
|
178
|
+
} catch (error) {
|
|
179
|
+
log(`直接转发失败: ${error.message}`);
|
|
180
|
+
res.statusCode = 500;
|
|
181
|
+
res.end(JSON.stringify({ error: error.message }));
|
|
182
|
+
}
|
|
183
|
+
}
|
|
184
|
+
|
|
139
185
|
// ==================== Whistle 插件主逻辑 ====================
|
|
140
186
|
module.exports = (server, options) => {
|
|
141
187
|
log('插件已启动(使用 axios 转发请求)');
|
|
@@ -146,6 +192,13 @@ module.exports = (server, options) => {
|
|
|
146
192
|
const url = req.fullUrl || req.url;
|
|
147
193
|
console.log('>>> [GLOBAL]', url);
|
|
148
194
|
|
|
195
|
+
// 域名过滤:如果不匹配则直接放行,不转发也不通知
|
|
196
|
+
if (!shouldIntercept(url)) {
|
|
197
|
+
log(`跳过未配置的域名: ${url}`);
|
|
198
|
+
// 不调用 next(),而是手动转发请求
|
|
199
|
+
return forwardRequest(req, res, url);
|
|
200
|
+
}
|
|
201
|
+
|
|
149
202
|
// 收集请求体
|
|
150
203
|
let reqBody = '';
|
|
151
204
|
for await (const chunk of req) {
|
|
@@ -157,7 +210,7 @@ module.exports = (server, options) => {
|
|
|
157
210
|
eventId: generateId(),
|
|
158
211
|
url,
|
|
159
212
|
method: req.method,
|
|
160
|
-
|
|
213
|
+
requestHeaders: {...req.headers},
|
|
161
214
|
body: bufferToString(Buffer.from(reqBody)),
|
|
162
215
|
bodyBase64: Buffer.from(reqBody).toString('base64'),
|
|
163
216
|
timestamp: Date.now()
|
package/package.json
CHANGED