whistle.mock-plugins 1.0.6 → 1.1.0
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/app/cgi-bin/interfaces.js +35 -0
- package/app/cgi-bin/test-interface.js +260 -262
- package/app/public/js/index.js +1 -1
- package/app/public/js/index.js.LICENSE.txt +8 -2
- package/lib/models.js +12 -1
- package/lib/routes/interfaces.js +274 -56
- package/lib/ruleManager.js +41 -2
- package/lib/rules-server.js +246 -139
- package/package.json +1 -1
- package/ui/src/components/interface-management/InterfaceTestModal.js +313 -0
- package/ui/src/components/interface-management/PreviewModal.js +50 -0
- package/ui/src/components/interface-management/README.md +184 -0
- package/ui/src/components/interface-management/ResponseContentEditor.js +427 -0
- package/ui/src/components/interface-management/constants.js +36 -0
- package/ui/src/components/interface-management/index.js +6 -0
- package/ui/src/components/interface-management/utils.js +103 -0
- package/ui/src/pages/InterfaceManagement.js +360 -653
- package/ui/src/pages/MockData.js +32 -0
package/lib/rules-server.js
CHANGED
|
@@ -10,149 +10,207 @@ const INTERFACES_FILE = path.join(DATA_DIR, 'interfaces.json');
|
|
|
10
10
|
// 添加功能模块配置文件
|
|
11
11
|
const FEATURES_FILE = path.join(DATA_DIR, 'features.json');
|
|
12
12
|
|
|
13
|
+
// 配置项
|
|
14
|
+
const CONFIG = {
|
|
15
|
+
// 日志级别:0=NONE, 1=ERROR, 2=WARN, 3=INFO, 4=DEBUG
|
|
16
|
+
LOG_LEVEL: process.env.MOCK_PLUGIN_LOG_LEVEL || 3,
|
|
17
|
+
// 缓存有效期(毫秒),默认 60 秒
|
|
18
|
+
CACHE_INTERVAL: process.env.MOCK_PLUGIN_CACHE_INTERVAL || 60000,
|
|
19
|
+
// 是否在控制台输出详细日志
|
|
20
|
+
VERBOSE_CONSOLE: process.env.MOCK_PLUGIN_VERBOSE === 'true'
|
|
21
|
+
};
|
|
22
|
+
|
|
23
|
+
// 缓存控制变量
|
|
24
|
+
let enabledInterfacesCache = null; // 仅缓存启用状态的接口
|
|
25
|
+
let cacheTime = 0;
|
|
26
|
+
let cacheVersion = 1;
|
|
27
|
+
|
|
28
|
+
// 正则表达式缓存
|
|
29
|
+
const regexCache = new Map();
|
|
30
|
+
|
|
13
31
|
// 记录日志
|
|
14
|
-
const log = (message) => {
|
|
15
|
-
|
|
32
|
+
const log = (message, level = 3) => {
|
|
33
|
+
// 只记录小于等于当前日志级别的日志
|
|
34
|
+
if (level <= CONFIG.LOG_LEVEL) {
|
|
35
|
+
const prefix = level === 1 ? '[ERROR] ' :
|
|
36
|
+
level === 2 ? '[WARN] ' :
|
|
37
|
+
level === 4 ? '[DEBUG] ' : '';
|
|
38
|
+
|
|
39
|
+
console.log(`[mock-plugin:rulesServer] ${prefix}${message}`);
|
|
40
|
+
}
|
|
16
41
|
};
|
|
17
42
|
|
|
18
|
-
//
|
|
19
|
-
const
|
|
43
|
+
// 错误日志
|
|
44
|
+
const logError = (message) => log(message, 1);
|
|
45
|
+
|
|
46
|
+
// 警告日志
|
|
47
|
+
const logWarn = (message) => log(message, 2);
|
|
48
|
+
|
|
49
|
+
// 信息日志
|
|
50
|
+
const logInfo = (message) => log(message, 3);
|
|
51
|
+
|
|
52
|
+
// 调试日志
|
|
53
|
+
const logDebug = (message) => log(message, 4);
|
|
54
|
+
|
|
55
|
+
// 获取文件最后修改时间
|
|
56
|
+
const getFileModTime = (filePath) => {
|
|
20
57
|
try {
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
return [];
|
|
24
|
-
}
|
|
25
|
-
|
|
26
|
-
const data = fs.readJsonSync(FEATURES_FILE);
|
|
27
|
-
|
|
28
|
-
// 处理不同的数据结构
|
|
29
|
-
let features = [];
|
|
30
|
-
|
|
31
|
-
if (data && Array.isArray(data)) {
|
|
32
|
-
// 如果直接是数组
|
|
33
|
-
features = data;
|
|
34
|
-
} else if (data && data.features && Array.isArray(data.features)) {
|
|
35
|
-
// 如果是 {features: [...]} 格式
|
|
36
|
-
features = data.features;
|
|
37
|
-
} else {
|
|
38
|
-
log('未能识别功能模块数据结构');
|
|
39
|
-
return [];
|
|
40
|
-
}
|
|
41
|
-
|
|
42
|
-
log(`总共发现 ${features.length} 个功能模块配置`);
|
|
43
|
-
|
|
44
|
-
// 过滤出启用状态的功能模块
|
|
45
|
-
const enabledFeatures = features.filter(feature => feature.active === true);
|
|
46
|
-
|
|
47
|
-
log(`找到 ${enabledFeatures.length} 个启用的功能模块`);
|
|
48
|
-
return enabledFeatures;
|
|
58
|
+
const stats = fs.statSync(filePath);
|
|
59
|
+
return stats.mtimeMs;
|
|
49
60
|
} catch (err) {
|
|
50
|
-
|
|
51
|
-
return [];
|
|
61
|
+
return 0;
|
|
52
62
|
}
|
|
53
63
|
};
|
|
54
64
|
|
|
55
|
-
//
|
|
56
|
-
const
|
|
65
|
+
// 加载所有启用的接口(整合了功能模块和接口的加载逻辑)
|
|
66
|
+
const loadEnabledInterfaces = () => {
|
|
57
67
|
try {
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
}
|
|
62
|
-
|
|
63
|
-
const data = fs.readJsonSync(INTERFACES_FILE);
|
|
64
|
-
// 为了避免日志过长,只打印前100个字符
|
|
65
|
-
const dataPreview = JSON.stringify(data).substring(0, 100) + (JSON.stringify(data).length > 100 ? '...' : '');
|
|
66
|
-
log(`读取到接口配置数据: ${dataPreview}`);
|
|
68
|
+
const currentTime = Date.now();
|
|
69
|
+
const interfacesFileModTime = getFileModTime(INTERFACES_FILE);
|
|
70
|
+
const featuresFileModTime = getFileModTime(FEATURES_FILE);
|
|
67
71
|
|
|
68
|
-
//
|
|
69
|
-
|
|
72
|
+
// 取两个文件的最后修改时间的最大值
|
|
73
|
+
const lastModTime = Math.max(interfacesFileModTime, featuresFileModTime);
|
|
70
74
|
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
} else {
|
|
78
|
-
log('未能识别接口数据结构');
|
|
79
|
-
return [];
|
|
75
|
+
// 如果缓存有效且文件未被修改,直接返回缓存
|
|
76
|
+
if (enabledInterfacesCache &&
|
|
77
|
+
currentTime - cacheTime < CONFIG.CACHE_INTERVAL &&
|
|
78
|
+
lastModTime <= cacheTime) {
|
|
79
|
+
logDebug('使用已启用接口的缓存');
|
|
80
|
+
return enabledInterfacesCache;
|
|
80
81
|
}
|
|
81
82
|
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
// 加载功能模块配置
|
|
85
|
-
const enabledFeatures = loadFeatures();
|
|
86
|
-
const enabledFeatureIds = enabledFeatures.map(feature => feature.id);
|
|
83
|
+
logDebug('缓存无效或文件已修改,重新加载接口');
|
|
87
84
|
|
|
88
|
-
//
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
85
|
+
// 1. 读取并解析所有功能模块
|
|
86
|
+
let enabledFeatureIds = [];
|
|
87
|
+
if (fs.existsSync(FEATURES_FILE)) {
|
|
88
|
+
try {
|
|
89
|
+
const featuresData = fs.readJsonSync(FEATURES_FILE);
|
|
90
|
+
let features = [];
|
|
91
|
+
|
|
92
|
+
if (featuresData && Array.isArray(featuresData)) {
|
|
93
|
+
features = featuresData;
|
|
94
|
+
} else if (featuresData && featuresData.features && Array.isArray(featuresData.features)) {
|
|
95
|
+
features = featuresData.features;
|
|
96
|
+
}
|
|
97
|
+
|
|
98
|
+
// 过滤出启用状态的功能ID
|
|
99
|
+
enabledFeatureIds = features
|
|
100
|
+
.filter(feature => feature.active === true)
|
|
101
|
+
.map(feature => feature.id);
|
|
102
|
+
|
|
103
|
+
logDebug(`找到 ${enabledFeatureIds.length} 个启用的功能模块`);
|
|
104
|
+
} catch (err) {
|
|
105
|
+
logError(`读取功能模块失败: ${err.message}`);
|
|
97
106
|
}
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
107
|
+
}
|
|
108
|
+
|
|
109
|
+
// 2. 读取并解析所有接口
|
|
110
|
+
let enabledInterfaces = [];
|
|
111
|
+
if (fs.existsSync(INTERFACES_FILE)) {
|
|
112
|
+
try {
|
|
113
|
+
const interfacesData = fs.readJsonSync(INTERFACES_FILE);
|
|
114
|
+
let interfaces = [];
|
|
115
|
+
|
|
116
|
+
if (interfacesData && Array.isArray(interfacesData)) {
|
|
117
|
+
interfaces = interfacesData;
|
|
118
|
+
} else if (interfacesData && interfacesData.interfaces && Array.isArray(interfacesData.interfaces)) {
|
|
119
|
+
interfaces = interfacesData.interfaces;
|
|
120
|
+
}
|
|
121
|
+
|
|
122
|
+
// 过滤出启用状态的接口
|
|
123
|
+
enabledInterfaces = interfaces.filter(intf => {
|
|
124
|
+
// 首先检查接口本身是否启用
|
|
125
|
+
const isInterfaceEnabled = intf.active !== false; // 默认为true
|
|
126
|
+
|
|
127
|
+
if (!isInterfaceEnabled) {
|
|
128
|
+
return false;
|
|
129
|
+
}
|
|
130
|
+
|
|
131
|
+
// 如果接口有关联功能,检查功能是否启用
|
|
132
|
+
if (intf.featureId) {
|
|
133
|
+
return enabledFeatureIds.includes(intf.featureId);
|
|
134
|
+
}
|
|
135
|
+
|
|
136
|
+
// 如果接口没有关联功能,只要接口本身启用就可以
|
|
137
|
+
return true;
|
|
138
|
+
});
|
|
139
|
+
|
|
140
|
+
logInfo(`找到 ${enabledInterfaces.length} 个启用的接口`);
|
|
141
|
+
} catch (err) {
|
|
142
|
+
logError(`读取接口配置失败: ${err.message}`);
|
|
103
143
|
}
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
144
|
+
}
|
|
145
|
+
|
|
146
|
+
// 更新缓存
|
|
147
|
+
enabledInterfacesCache = enabledInterfaces;
|
|
148
|
+
cacheTime = currentTime;
|
|
108
149
|
|
|
109
|
-
log(`找到 ${enabledInterfaces.length} 个启用的接口配置`);
|
|
110
150
|
return enabledInterfaces;
|
|
111
151
|
} catch (err) {
|
|
112
|
-
|
|
113
|
-
return [];
|
|
152
|
+
logError(`加载启用接口失败: ${err.message}`);
|
|
153
|
+
return enabledInterfacesCache || [];
|
|
114
154
|
}
|
|
115
155
|
};
|
|
116
156
|
|
|
117
157
|
// 匹配URL是否符合模式
|
|
118
158
|
const isUrlMatch = (url, pattern, proxyType, fullUrl) => {
|
|
119
159
|
if (!pattern) return false;
|
|
160
|
+
|
|
120
161
|
try {
|
|
121
162
|
// 针对不同的代理类型采用不同的匹配策略
|
|
122
163
|
|
|
123
164
|
// 对于url_redirect类型,需要完全匹配fullPath才命中
|
|
124
165
|
if (proxyType === 'url_redirect') {
|
|
125
|
-
//
|
|
166
|
+
// 精确匹配完整路径,这是最快的比较方式
|
|
126
167
|
return pattern === fullUrl;
|
|
127
168
|
}
|
|
128
169
|
|
|
129
170
|
// 对于redirect类型,只要url以pattern开头即可命中(前缀匹配)
|
|
130
171
|
if (proxyType === 'redirect') {
|
|
131
|
-
|
|
172
|
+
// 使用字符串原生的startsWith方法更高效
|
|
173
|
+
return fullUrl.startsWith(pattern);
|
|
132
174
|
}
|
|
133
175
|
|
|
134
176
|
// 以下是默认的匹配逻辑(用于response类型等)
|
|
135
177
|
|
|
136
|
-
// 精确匹配
|
|
178
|
+
// 精确匹配 - 最高效的匹配方式
|
|
137
179
|
if (pattern === url) {
|
|
138
180
|
return true;
|
|
139
181
|
}
|
|
140
182
|
|
|
141
183
|
// 通配符匹配
|
|
142
184
|
if (pattern.includes('*')) {
|
|
143
|
-
//
|
|
144
|
-
const
|
|
145
|
-
|
|
146
|
-
|
|
185
|
+
// 使用缓存的正则表达式
|
|
186
|
+
const cacheKey = `wildcard:${pattern}`;
|
|
187
|
+
let regex = regexCache.get(cacheKey);
|
|
188
|
+
|
|
189
|
+
if (!regex) {
|
|
190
|
+
// 需要转换为正则表达式,处理特殊字符
|
|
191
|
+
const regexPattern = pattern
|
|
192
|
+
.replace(/[-\/\\^$+?.()|[\]{}]/g, '\\$&') // 转义特殊字符
|
|
193
|
+
.replace(/\*/g, '.*'); // 将 * 替换为 .*
|
|
194
|
+
|
|
195
|
+
regex = new RegExp('^' + regexPattern + '$');
|
|
196
|
+
regexCache.set(cacheKey, regex);
|
|
197
|
+
}
|
|
147
198
|
|
|
148
|
-
const regex = new RegExp('^' + regexPattern + '$');
|
|
149
199
|
return regex.test(url);
|
|
150
200
|
}
|
|
151
201
|
|
|
152
202
|
// 正则表达式匹配
|
|
153
|
-
if (pattern.startsWith('/')) {
|
|
154
|
-
|
|
155
|
-
const
|
|
203
|
+
if (pattern.startsWith('/') && pattern.length > 2 && pattern.endsWith('/')) {
|
|
204
|
+
// 使用缓存的正则表达式
|
|
205
|
+
const cacheKey = `regex:${pattern}`;
|
|
206
|
+
let regex = regexCache.get(cacheKey);
|
|
207
|
+
|
|
208
|
+
if (!regex) {
|
|
209
|
+
const regexStr = pattern.slice(1, -1);
|
|
210
|
+
regex = new RegExp(regexStr);
|
|
211
|
+
regexCache.set(cacheKey, regex);
|
|
212
|
+
}
|
|
213
|
+
|
|
156
214
|
return regex.test(url);
|
|
157
215
|
}
|
|
158
216
|
} catch (e) {
|
|
@@ -199,8 +257,9 @@ const generateRandomValue = (pattern) => {
|
|
|
199
257
|
}
|
|
200
258
|
|
|
201
259
|
const formatPattern = pattern.substring(1); // 去掉@前缀
|
|
202
|
-
//
|
|
260
|
+
// 使用单次正则替换代替每个字符的遍历
|
|
203
261
|
return formatPattern.replace(/x/g, () => {
|
|
262
|
+
// 使用一个固定的字符集,避免生成不兼容的字符
|
|
204
263
|
const chars = 'abcdefghijklmnopqrstuvwxyz0123456789';
|
|
205
264
|
return chars.charAt(Math.floor(Math.random() * chars.length));
|
|
206
265
|
});
|
|
@@ -208,44 +267,93 @@ const generateRandomValue = (pattern) => {
|
|
|
208
267
|
|
|
209
268
|
// 构建重定向规则
|
|
210
269
|
const buildRedirectRule = (fullUrl, targetUrl, customHeaders, pattern, isUrlRedirect = 0) => {
|
|
270
|
+
// 构建目标URL
|
|
271
|
+
let finalTargetUrl;
|
|
211
272
|
if(!isUrlRedirect){
|
|
212
|
-
|
|
273
|
+
finalTargetUrl = fullUrl.replace(pattern, targetUrl);
|
|
213
274
|
}else{
|
|
214
|
-
|
|
275
|
+
finalTargetUrl = `redirect://${targetUrl}`;
|
|
215
276
|
}
|
|
216
277
|
|
|
217
|
-
|
|
278
|
+
// 定义结果数组,避免多次字符串拼接
|
|
279
|
+
const rules = [`${fullUrl} ${finalTargetUrl}`];
|
|
218
280
|
|
|
219
281
|
// 如果有自定义请求头,添加到规则中
|
|
220
282
|
if (customHeaders && Object.keys(customHeaders).length > 0) {
|
|
221
|
-
//
|
|
222
|
-
|
|
283
|
+
// 为每个请求头创建一个规则行
|
|
284
|
+
Object.entries(customHeaders).forEach(([key, value]) => {
|
|
223
285
|
// 处理随机值
|
|
224
286
|
const processedValue = generateRandomValue(value);
|
|
225
287
|
|
|
226
|
-
|
|
288
|
+
// 添加到规则数组
|
|
289
|
+
rules.push(`${fullUrl} headerReplace://req.${key}:/(.*)/=${processedValue}`);
|
|
227
290
|
});
|
|
228
|
-
|
|
229
|
-
if (headerRuleLines.length > 0) {
|
|
230
|
-
// 将所有规则合并为一个多行规则字符串
|
|
231
|
-
rule = `${rule}\n${headerRuleLines.join('\n')}`;
|
|
232
|
-
}
|
|
233
291
|
}
|
|
234
292
|
|
|
235
|
-
|
|
293
|
+
// 最后才将所有规则合并为一个字符串,减少字符串操作
|
|
294
|
+
return rules.join('\n');
|
|
295
|
+
};
|
|
296
|
+
|
|
297
|
+
// 缓存管理接口 - 简化版本
|
|
298
|
+
const cacheManager = {
|
|
299
|
+
// 清除缓存
|
|
300
|
+
clearCache: () => {
|
|
301
|
+
logInfo('主动清除接口缓存');
|
|
302
|
+
enabledInterfacesCache = null;
|
|
303
|
+
cacheTime = 0;
|
|
304
|
+
cacheVersion++;
|
|
305
|
+
},
|
|
306
|
+
|
|
307
|
+
// 获取当前缓存状态
|
|
308
|
+
getStatus: () => {
|
|
309
|
+
return {
|
|
310
|
+
enabled: {
|
|
311
|
+
cached: !!enabledInterfacesCache,
|
|
312
|
+
cacheTime: cacheTime,
|
|
313
|
+
itemCount: enabledInterfacesCache ? enabledInterfacesCache.length : 0
|
|
314
|
+
},
|
|
315
|
+
cacheVersion,
|
|
316
|
+
configInterval: CONFIG.CACHE_INTERVAL
|
|
317
|
+
};
|
|
318
|
+
}
|
|
236
319
|
};
|
|
237
320
|
|
|
238
321
|
// Whistle 规则服务器实现
|
|
239
322
|
module.exports = (server, options) => {
|
|
323
|
+
// 优化:缓存常用路径匹配
|
|
324
|
+
const debugConfigPath = '/_debug/config';
|
|
325
|
+
const debugEnabledInterfacesPath = '/_debug/enabled-interfaces';
|
|
326
|
+
const clearCachePath = '/_flush_cache';
|
|
327
|
+
|
|
240
328
|
server.on('request', (req, res) => {
|
|
241
329
|
const oReq = req.originalReq;
|
|
242
330
|
|
|
243
331
|
// 提取请求相关信息
|
|
244
332
|
const fullUrl = oReq.url;
|
|
245
|
-
|
|
333
|
+
logDebug(`原始URL: ${fullUrl}`);
|
|
334
|
+
|
|
335
|
+
// 处理缓存刷新请求
|
|
336
|
+
if (fullUrl.startsWith(clearCachePath)) {
|
|
337
|
+
cacheManager.clearCache();
|
|
338
|
+
|
|
339
|
+
res.writeHead(200, { 'Content-Type': 'application/json' });
|
|
340
|
+
res.end(JSON.stringify({
|
|
341
|
+
success: true,
|
|
342
|
+
message: '缓存已刷新',
|
|
343
|
+
cacheStatus: cacheManager.getStatus()
|
|
344
|
+
}));
|
|
345
|
+
return;
|
|
346
|
+
}
|
|
347
|
+
|
|
348
|
+
// 特殊调试路径,查看缓存状态
|
|
349
|
+
if (fullUrl.endsWith('/_cache_status')) {
|
|
350
|
+
res.writeHead(200, { 'Content-Type': 'application/json' });
|
|
351
|
+
res.end(JSON.stringify(cacheManager.getStatus()));
|
|
352
|
+
return;
|
|
353
|
+
}
|
|
246
354
|
|
|
247
|
-
//
|
|
248
|
-
if (fullUrl.endsWith(
|
|
355
|
+
// 特殊调试路径,使用更高效的匹配
|
|
356
|
+
if (fullUrl.endsWith(debugConfigPath)) {
|
|
249
357
|
try {
|
|
250
358
|
const configs = {
|
|
251
359
|
interfaces: fs.existsSync(INTERFACES_FILE) ? fs.readJsonSync(INTERFACES_FILE) : null,
|
|
@@ -254,23 +362,23 @@ module.exports = (server, options) => {
|
|
|
254
362
|
res.end(JSON.stringify(configs, null, 2));
|
|
255
363
|
return;
|
|
256
364
|
} catch (err) {
|
|
257
|
-
|
|
365
|
+
logError(`读取配置文件失败: ${err.message}`);
|
|
258
366
|
res.end(`读取配置文件失败: ${err.message}`);
|
|
259
367
|
return;
|
|
260
368
|
}
|
|
261
369
|
}
|
|
262
370
|
|
|
263
371
|
// 调试路径,查看启用的接口
|
|
264
|
-
if (fullUrl.endsWith(
|
|
372
|
+
if (fullUrl.endsWith(debugEnabledInterfacesPath)) {
|
|
265
373
|
try {
|
|
266
|
-
const enabledInterfaces =
|
|
374
|
+
const enabledInterfaces = loadEnabledInterfaces();
|
|
267
375
|
res.end(JSON.stringify({
|
|
268
376
|
count: enabledInterfaces.length,
|
|
269
377
|
interfaces: enabledInterfaces
|
|
270
378
|
}, null, 2));
|
|
271
379
|
return;
|
|
272
380
|
} catch (err) {
|
|
273
|
-
|
|
381
|
+
logError(`获取启用接口失败: ${err.message}`);
|
|
274
382
|
res.end(`获取启用接口失败: ${err.message}`);
|
|
275
383
|
return;
|
|
276
384
|
}
|
|
@@ -281,7 +389,7 @@ module.exports = (server, options) => {
|
|
|
281
389
|
try {
|
|
282
390
|
parsedUrl = url.parse(fullUrl);
|
|
283
391
|
} catch (err) {
|
|
284
|
-
|
|
392
|
+
logError(`解析URL失败: ${err.message}`);
|
|
285
393
|
// 解析失败时,不处理请求
|
|
286
394
|
res.end('');
|
|
287
395
|
return;
|
|
@@ -291,26 +399,25 @@ module.exports = (server, options) => {
|
|
|
291
399
|
const method = oReq.method;
|
|
292
400
|
const path = parsedUrl.pathname || '';
|
|
293
401
|
|
|
294
|
-
|
|
402
|
+
logDebug(`请求方法: ${method}, 请求路径: ${path}`);
|
|
295
403
|
|
|
296
|
-
//
|
|
297
|
-
const enabledInterfaces =
|
|
298
|
-
log(`已加载 ${enabledInterfaces.length} 个接口配置`);
|
|
404
|
+
// 加载启用的接口配置(使用缓存优化)
|
|
405
|
+
const enabledInterfaces = loadEnabledInterfaces();
|
|
299
406
|
|
|
300
407
|
if (enabledInterfaces.length === 0) {
|
|
301
|
-
|
|
408
|
+
logDebug('没有找到启用的接口配置,跳过处理');
|
|
302
409
|
res.end('');
|
|
303
410
|
return;
|
|
304
411
|
}
|
|
305
412
|
|
|
306
|
-
//
|
|
413
|
+
// 优化:使用 find 查找匹配的接口,只遍历一次数组
|
|
307
414
|
const matchedInterface = enabledInterfaces.find(intf => {
|
|
308
415
|
// 获取URL模式
|
|
309
416
|
const pattern = intf.urlPattern || '';
|
|
310
417
|
const proxyType = intf.proxyType || 'response';
|
|
311
418
|
|
|
312
419
|
if (!pattern) {
|
|
313
|
-
|
|
420
|
+
logDebug(`接口 ${intf.id || intf.name || '未知'} 缺少URL模式`);
|
|
314
421
|
return false;
|
|
315
422
|
}
|
|
316
423
|
|
|
@@ -322,7 +429,7 @@ module.exports = (server, options) => {
|
|
|
322
429
|
return false;
|
|
323
430
|
}
|
|
324
431
|
|
|
325
|
-
|
|
432
|
+
logDebug(`URL模式匹配成功: ${fullUrl} 匹配 ${pattern} (${proxyType}模式)`);
|
|
326
433
|
|
|
327
434
|
// 检查HTTP方法匹配
|
|
328
435
|
const methodField = intf.httpMethod || intf.method || '';
|
|
@@ -333,18 +440,18 @@ module.exports = (server, options) => {
|
|
|
333
440
|
methodValue === method;
|
|
334
441
|
|
|
335
442
|
if (!methodMatches) {
|
|
336
|
-
|
|
443
|
+
logDebug(`方法不匹配: 请求方法 ${method}, 接口方法 ${methodValue}`);
|
|
337
444
|
return false;
|
|
338
445
|
}
|
|
339
446
|
|
|
340
|
-
|
|
447
|
+
logDebug(`方法匹配成功: ${method} 匹配 ${methodValue || 'ALL'}`);
|
|
341
448
|
|
|
342
449
|
return true;
|
|
343
450
|
});
|
|
344
451
|
|
|
345
452
|
// 如果找到匹配的接口,处理对应的规则
|
|
346
453
|
if (matchedInterface) {
|
|
347
|
-
|
|
454
|
+
logInfo(`找到匹配接口 "${matchedInterface.name || matchedInterface.id || '未知'}" 用于 ${method} ${path}`);
|
|
348
455
|
|
|
349
456
|
// 处理不同的代理类型
|
|
350
457
|
const proxyType = matchedInterface.proxyType || 'response';
|
|
@@ -352,14 +459,14 @@ module.exports = (server, options) => {
|
|
|
352
459
|
// 优先级判断:首先处理 response 类型
|
|
353
460
|
if (proxyType === 'response') {
|
|
354
461
|
// 返回指向插件服务的规则
|
|
355
|
-
|
|
462
|
+
logDebug(`接口 "${matchedInterface.name}" 使用模拟响应模式`);
|
|
356
463
|
res.end(`${fullUrl} mock-plugins://`);
|
|
357
464
|
return;
|
|
358
465
|
}
|
|
359
466
|
|
|
360
467
|
// 对于redirect和url_redirect类型,验证规则有效性
|
|
361
468
|
if ((proxyType === 'redirect' || proxyType === 'url_redirect') && !validateRedirectRule(matchedInterface)) {
|
|
362
|
-
|
|
469
|
+
logWarn(`接口 "${matchedInterface.name}" 的重定向规则无效,跳过处理`);
|
|
363
470
|
res.end('');
|
|
364
471
|
return;
|
|
365
472
|
}
|
|
@@ -368,18 +475,18 @@ module.exports = (server, options) => {
|
|
|
368
475
|
if (proxyType === 'redirect') {
|
|
369
476
|
const targetUrl = matchedInterface.targetUrl.trim();
|
|
370
477
|
const pattern = matchedInterface.urlPattern.trim();
|
|
371
|
-
|
|
478
|
+
logDebug(`接口 "${matchedInterface.name}" 使用重定向模式,模式: ${pattern}, 目标URL: ${targetUrl}`);
|
|
372
479
|
|
|
373
480
|
// 确保目标URL合法
|
|
374
481
|
if (!isValidTargetUrl(targetUrl)) {
|
|
375
|
-
|
|
482
|
+
logWarn(`目标URL不合法: ${targetUrl},跳过处理`);
|
|
376
483
|
res.end('');
|
|
377
484
|
return;
|
|
378
485
|
}
|
|
379
486
|
|
|
380
487
|
// 执行重定向:将fullUrl中匹配的pattern替换为targetUrl
|
|
381
|
-
//
|
|
382
|
-
if (fullUrl.
|
|
488
|
+
// 使用startsWith优化匹配效率
|
|
489
|
+
if (fullUrl.startsWith(pattern)) {
|
|
383
490
|
// 获取自定义请求头
|
|
384
491
|
const customHeaders = matchedInterface.customHeaders || {};
|
|
385
492
|
|
|
@@ -388,10 +495,10 @@ module.exports = (server, options) => {
|
|
|
388
495
|
|
|
389
496
|
// 返回规则
|
|
390
497
|
res.end(redirectRule);
|
|
391
|
-
|
|
498
|
+
logDebug(`重定向规则: ${redirectRule}`);
|
|
392
499
|
return;
|
|
393
500
|
} else {
|
|
394
|
-
|
|
501
|
+
logDebug(`URL ${fullUrl} 不以 ${pattern} 开头,跳过处理`);
|
|
395
502
|
res.end('');
|
|
396
503
|
return;
|
|
397
504
|
}
|
|
@@ -400,11 +507,11 @@ module.exports = (server, options) => {
|
|
|
400
507
|
// 处理 url_redirect 类型:完全匹配 fullPath 并直接返回目标URL
|
|
401
508
|
if (proxyType === 'url_redirect') {
|
|
402
509
|
const targetUrl = matchedInterface.targetUrl.trim();
|
|
403
|
-
|
|
510
|
+
logDebug(`接口 "${matchedInterface.name}" 使用URL重定向模式,目标URL: ${targetUrl}`);
|
|
404
511
|
|
|
405
512
|
// 确保目标URL合法
|
|
406
513
|
if (!isValidTargetUrl(targetUrl)) {
|
|
407
|
-
|
|
514
|
+
logWarn(`目标URL不合法: ${targetUrl},跳过处理`);
|
|
408
515
|
res.end('');
|
|
409
516
|
return;
|
|
410
517
|
}
|
|
@@ -419,21 +526,21 @@ module.exports = (server, options) => {
|
|
|
419
526
|
|
|
420
527
|
// 返回规则
|
|
421
528
|
res.end(redirectRule);
|
|
422
|
-
|
|
529
|
+
logDebug(`URL重定向规则: ${redirectRule}`);
|
|
423
530
|
return;
|
|
424
531
|
} else {
|
|
425
|
-
|
|
532
|
+
logDebug(`URL ${fullUrl} 不完全匹配 ${matchedInterface.urlPattern},跳过处理`);
|
|
426
533
|
res.end('');
|
|
427
534
|
return;
|
|
428
535
|
}
|
|
429
536
|
}
|
|
430
537
|
|
|
431
538
|
// 默认情况下使用mock-plugins处理
|
|
432
|
-
|
|
539
|
+
logDebug(`接口 "${matchedInterface.name}" 使用默认处理模式`);
|
|
433
540
|
res.end(`${fullUrl} mock-plugins://`);
|
|
434
541
|
} else {
|
|
435
542
|
// 如果没有匹配的接口,返回空字符串,Whistle 会继续处理下一个规则
|
|
436
|
-
|
|
543
|
+
logDebug(`未找到匹配接口,跳过处理 ${method} ${path}`);
|
|
437
544
|
res.end('');
|
|
438
545
|
}
|
|
439
546
|
});
|