whistle.mock-plugins 1.4.0-beta.4 → 1.4.0-beta.6
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/VERSION.md +16 -0
- package/app/public/js/247.index.js +2 -0
- package/app/public/js/574.index.js +1 -1
- package/app/public/js/580.index.js +2 -0
- package/app/public/js/682.index.js +1 -1
- package/app/public/js/index.js +1 -1
- package/app/public/whistle-certificate.html +24 -0
- package/lib/replay-assets.js +142 -0
- package/lib/replay-cache.js +49 -0
- package/lib/replay-launch.js +236 -0
- package/lib/replay-match.js +182 -0
- package/lib/replay-session.js +181 -0
- package/lib/ui-server.js +32 -1
- package/package.json +7 -2
- package/rules.txt +2 -0
- package/scripts/replay-session.js +233 -0
- package/app/public/js/198.index.js +0 -2
- package/app/public/js/26.index.js +0 -2
- /package/app/public/js/{26.index.js.LICENSE.txt → 247.index.js.LICENSE.txt} +0 -0
- /package/app/public/js/{198.index.js.LICENSE.txt → 580.index.js.LICENSE.txt} +0 -0
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
<!doctype html>
|
|
2
|
+
<html lang="zh-CN">
|
|
3
|
+
<head>
|
|
4
|
+
<meta charset="utf-8">
|
|
5
|
+
<meta name="viewport" content="width=device-width, initial-scale=1">
|
|
6
|
+
<title>下载 Whistle HTTPS 根证书</title>
|
|
7
|
+
<style>
|
|
8
|
+
body { margin: 0; background: #f4f6fb; color: #1e2638; font: 16px/1.6 system-ui, sans-serif; }
|
|
9
|
+
main { box-sizing: border-box; max-width: 460px; margin: 32px auto; padding: 24px; }
|
|
10
|
+
h1 { margin: 0 0 12px; font-size: 25px; line-height: 1.3; }
|
|
11
|
+
p { margin: 0 0 18px; }
|
|
12
|
+
.download { display: block; padding: 12px; border-radius: 8px; background: #364dcc; color: #fff; text-align: center; font-weight: 600; text-decoration: none; }
|
|
13
|
+
.instructions { margin-top: 18px; }
|
|
14
|
+
</style>
|
|
15
|
+
</head>
|
|
16
|
+
<body>
|
|
17
|
+
<main>
|
|
18
|
+
<h1>下载 Whistle HTTPS 根证书</h1>
|
|
19
|
+
<p>如果这个页面在微信或其他应用内打开,请先从应用菜单选择「在浏览器打开」,再下载证书。</p>
|
|
20
|
+
<a class="download" href="/whistle/cgi-bin/rootca?type=crt">下载证书(rootCA.crt)</a>
|
|
21
|
+
<p class="instructions"><a href="https://wproxy.org/docs/mobile.html">查看 iOS 和 Android 的证书安装与信任步骤</a></p>
|
|
22
|
+
</main>
|
|
23
|
+
</body>
|
|
24
|
+
</html>
|
|
@@ -0,0 +1,142 @@
|
|
|
1
|
+
const axios = require('axios');
|
|
2
|
+
const dns = require('dns').promises;
|
|
3
|
+
const http = require('http');
|
|
4
|
+
const https = require('https');
|
|
5
|
+
const { BlockList, isIP } = require('net');
|
|
6
|
+
const { randomUUID } = require('crypto');
|
|
7
|
+
const { replayHeaders, replayResourceType } = require('./replay-session');
|
|
8
|
+
|
|
9
|
+
const reserved = new BlockList();
|
|
10
|
+
for (const [address, bits] of [
|
|
11
|
+
['0.0.0.0', 8], ['10.0.0.0', 8], ['100.64.0.0', 10], ['127.0.0.0', 8], ['169.254.0.0', 16],
|
|
12
|
+
['172.16.0.0', 12], ['192.0.0.0', 24], ['192.0.2.0', 24], ['192.88.99.0', 24], ['192.168.0.0', 16],
|
|
13
|
+
['198.18.0.0', 15], ['198.51.100.0', 24], ['203.0.113.0', 24], ['224.0.0.0', 3]
|
|
14
|
+
]) reserved.addSubnet(address, bits);
|
|
15
|
+
for (const [address, bits] of [
|
|
16
|
+
['2001::', 23], ['2001:db8::', 32], ['2002::', 16], ['3fff::', 20]
|
|
17
|
+
]) reserved.addSubnet(address, bits, 'ipv6');
|
|
18
|
+
|
|
19
|
+
const globalV6 = new BlockList();
|
|
20
|
+
globalV6.addSubnet('2000::', 3, 'ipv6');
|
|
21
|
+
|
|
22
|
+
const syntheticAddresses = new BlockList();
|
|
23
|
+
syntheticAddresses.addSubnet('198.18.0.0', 15);
|
|
24
|
+
const publicResolver = new dns.Resolver({ timeout: 1500, tries: 1 });
|
|
25
|
+
publicResolver.setServers(['223.5.5.5', '1.1.1.1']);
|
|
26
|
+
const pendingLookups = new Map();
|
|
27
|
+
|
|
28
|
+
function isPublicAddress({ address, family }) {
|
|
29
|
+
return family === 4 ? !reserved.check(address) :
|
|
30
|
+
family === 6 && globalV6.check(address, 'ipv6') && !reserved.check(address, 'ipv6');
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
function resolvePublicDns(hostname) {
|
|
34
|
+
if (!pendingLookups.has(hostname)) {
|
|
35
|
+
const lookup = Promise.allSettled([publicResolver.resolve4(hostname), publicResolver.resolve6(hostname)])
|
|
36
|
+
.then(results => results.flatMap((result, index) => result.status === 'fulfilled'
|
|
37
|
+
? result.value.map(address => ({ address, family: index === 0 ? 4 : 6 })) : []))
|
|
38
|
+
.finally(() => pendingLookups.delete(hostname));
|
|
39
|
+
pendingLookups.set(hostname, lookup);
|
|
40
|
+
}
|
|
41
|
+
return pendingLookups.get(hostname);
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
async function publicLookup(hostname) {
|
|
45
|
+
const addresses = await dns.lookup(hostname, { all: true });
|
|
46
|
+
const publicAddresses = addresses.filter(isPublicAddress);
|
|
47
|
+
if (publicAddresses.length) return publicAddresses;
|
|
48
|
+
// Some system proxies return synthetic addresses in the benchmarking range.
|
|
49
|
+
if (addresses.some(({ address, family }) => syntheticAddresses.check(address, family === 6 ? 'ipv6' : 'ipv4'))) {
|
|
50
|
+
const fallback = (await resolvePublicDns(hostname)).filter(isPublicAddress);
|
|
51
|
+
if (fallback.length) return fallback;
|
|
52
|
+
}
|
|
53
|
+
throw new Error('资源域名未解析到公网地址');
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
function isReplayAssetUrl(url) {
|
|
57
|
+
return ['http:', 'https:'].includes(url.protocol) && !url.username && !url.password && !url.port &&
|
|
58
|
+
!isIP(url.hostname.replace(/^\[|\]$/g, '')) && url.href.length <= 4096;
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
function replayAssetType(url, type) {
|
|
62
|
+
return isReplayAssetUrl(url) && (type === 'script' && /\.m?js$/i.test(url.pathname) ||
|
|
63
|
+
type === 'stylesheet' && /\.css$/i.test(url.pathname));
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
const direct = axios.create({
|
|
67
|
+
proxy: false,
|
|
68
|
+
httpAgent: new http.Agent({ maxSockets: 6, maxTotalSockets: 12 }),
|
|
69
|
+
httpsAgent: new https.Agent({ maxSockets: 6, maxTotalSockets: 12 }),
|
|
70
|
+
maxRedirects: 0,
|
|
71
|
+
timeout: 10000,
|
|
72
|
+
maxContentLength: 5 * 1024 * 1024,
|
|
73
|
+
responseType: 'arraybuffer',
|
|
74
|
+
validateStatus: () => true
|
|
75
|
+
});
|
|
76
|
+
|
|
77
|
+
const retryable = new Set(['ECONNRESET', 'ECONNREFUSED', 'EHOSTUNREACH', 'ENETUNREACH', 'EPIPE', 'ETIMEDOUT']);
|
|
78
|
+
|
|
79
|
+
async function requestAsset(url, origin, signal) {
|
|
80
|
+
let lookupResult;
|
|
81
|
+
for (let attempt = 0; ; attempt++) {
|
|
82
|
+
try {
|
|
83
|
+
return await direct.get(url.href, {
|
|
84
|
+
signal,
|
|
85
|
+
lookup(hostname, options, callback) {
|
|
86
|
+
lookupResult ||= publicLookup(hostname);
|
|
87
|
+
lookupResult.then(addresses => {
|
|
88
|
+
const offset = attempt % addresses.length;
|
|
89
|
+
callback(null, [...addresses.slice(offset), ...addresses.slice(0, offset)]);
|
|
90
|
+
}, callback);
|
|
91
|
+
},
|
|
92
|
+
headers: { 'Cache-Control': 'no-cache', 'Accept-Encoding': 'identity', ...(origin ? { Origin: origin } : {}) }
|
|
93
|
+
});
|
|
94
|
+
} catch (error) {
|
|
95
|
+
if (signal.aborted || attempt >= 2 || !retryable.has(error.code)) throw error;
|
|
96
|
+
}
|
|
97
|
+
}
|
|
98
|
+
}
|
|
99
|
+
|
|
100
|
+
const mimeByType = {
|
|
101
|
+
png: /^image\/png$/,
|
|
102
|
+
jpg: /^image\/jpeg$/,
|
|
103
|
+
gif: /^image\/gif$/,
|
|
104
|
+
webp: /^image\/webp$/,
|
|
105
|
+
svg: /^image\/svg\+xml$/,
|
|
106
|
+
js: /^(?:(?:text|application)\/(?:x-)?(?:javascript|ecmascript)(?:1\.[0-5])?|text\/jscript)$/,
|
|
107
|
+
css: /^text\/css$/,
|
|
108
|
+
font: /^(?:font\/[a-z0-9.+-]+|application\/(?:font-[a-z0-9-]+|x-font-[a-z0-9-]+|vnd\.ms-fontobject|octet-stream))$/
|
|
109
|
+
};
|
|
110
|
+
|
|
111
|
+
async function fetchReplayResource(asset, signal) {
|
|
112
|
+
const url = new URL(asset.url);
|
|
113
|
+
const type = replayResourceType(asset.type, url);
|
|
114
|
+
if (!isReplayAssetUrl(url) || !type) throw new Error('该地址不支持静态资源直连');
|
|
115
|
+
let response;
|
|
116
|
+
try {
|
|
117
|
+
response = await requestAsset(url, asset.origin, signal);
|
|
118
|
+
} catch (error) {
|
|
119
|
+
if (signal.aborted || error.code === 'ECONNABORTED') throw new Error('下载超时');
|
|
120
|
+
if (error.code === 'ERR_BAD_RESPONSE') throw new Error('正文不完整或超过 5 MB');
|
|
121
|
+
throw new Error(error.message === '资源域名未解析到公网地址' ? error.message : '直连下载失败');
|
|
122
|
+
}
|
|
123
|
+
if (response.status !== 200) throw new Error('直连返回 HTTP ' + response.status);
|
|
124
|
+
const mime = String(response.headers['content-type'] || '').split(';')[0].trim().toLowerCase();
|
|
125
|
+
if (!mimeByType[type].test(mime)) throw new Error('返回内容与请求的资源类型不符');
|
|
126
|
+
return {
|
|
127
|
+
status: 200, outcome: 'response',
|
|
128
|
+
response: { headers: replayHeaders(response.headers), body: Buffer.from(response.data).toString('base64') }
|
|
129
|
+
};
|
|
130
|
+
}
|
|
131
|
+
|
|
132
|
+
async function downloadReplayAsset(asset, signal) {
|
|
133
|
+
const url = new URL(asset.url);
|
|
134
|
+
if (!replayAssetType(url, asset.type)) throw new Error('该地址不支持直连补录');
|
|
135
|
+
return {
|
|
136
|
+
...await fetchReplayResource(asset, signal),
|
|
137
|
+
id: 'direct-' + randomUUID(), method: 'GET', url: url.href, requestBody: '',
|
|
138
|
+
startTime: Date.now(), duration: 0, source: 'direct'
|
|
139
|
+
};
|
|
140
|
+
}
|
|
141
|
+
|
|
142
|
+
module.exports = { replayAssetType, downloadReplayAsset, fetchReplayResource };
|
|
@@ -0,0 +1,49 @@
|
|
|
1
|
+
const { isIP } = require('net');
|
|
2
|
+
|
|
3
|
+
let activeIp;
|
|
4
|
+
let pending;
|
|
5
|
+
|
|
6
|
+
function cacheRule(ip) {
|
|
7
|
+
return ip ? '* disable://cache includeFilter://clientIp:/^' + ip.replace(/\./g, '\\.') + '$/' : '';
|
|
8
|
+
}
|
|
9
|
+
|
|
10
|
+
function serveReplayCacheRules(req, res) {
|
|
11
|
+
const change = pending;
|
|
12
|
+
if (change) res.once('finish', change.published);
|
|
13
|
+
res.type('text').send(cacheRule(change ? change.ip : activeIp));
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
async function replayCache(req, res) {
|
|
17
|
+
const { action, ip } = req.body || {};
|
|
18
|
+
if (action === 'status') return res.json({ active: activeIp ? { ip: activeIp } : null });
|
|
19
|
+
if (action !== 'enable' && action !== 'disable') return res.sendStatus(400);
|
|
20
|
+
if (action === 'enable' && !isIP(ip)) {
|
|
21
|
+
return res.status(400).json({ message: '请先选择有效的设备 IP。' });
|
|
22
|
+
}
|
|
23
|
+
if (pending) return res.status(409).json({ message: 'Whistle 正在同步设置,请稍后重试。' });
|
|
24
|
+
|
|
25
|
+
const updateRules = req.app.get('replayCacheUpdateRules');
|
|
26
|
+
if (!updateRules) return res.status(503).json({ message: '请从 Whistle 插件页面打开后重试。' });
|
|
27
|
+
const nextIp = action === 'enable' ? ip : undefined;
|
|
28
|
+
if (nextIp === activeIp) return res.json({ active: activeIp ? { ip: activeIp } : null });
|
|
29
|
+
|
|
30
|
+
let timer;
|
|
31
|
+
try {
|
|
32
|
+
await new Promise((resolve, reject) => {
|
|
33
|
+
pending = { ip: nextIp, published: resolve };
|
|
34
|
+
timer = setTimeout(() => reject(new Error('Whistle 未同步设备缓存规则,请检查插件是否已启用。')), 5000);
|
|
35
|
+
updateRules();
|
|
36
|
+
});
|
|
37
|
+
activeIp = nextIp;
|
|
38
|
+
return res.json({ active: activeIp ? { ip: activeIp } : null });
|
|
39
|
+
} catch (error) {
|
|
40
|
+
pending = undefined;
|
|
41
|
+
updateRules();
|
|
42
|
+
return res.status(503).json({ message: error.message });
|
|
43
|
+
} finally {
|
|
44
|
+
pending = undefined;
|
|
45
|
+
clearTimeout(timer);
|
|
46
|
+
}
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
module.exports = { replayCache, serveReplayCacheRules };
|
|
@@ -0,0 +1,236 @@
|
|
|
1
|
+
const { fork } = require('child_process');
|
|
2
|
+
const { randomUUID } = require('crypto');
|
|
3
|
+
const fs = require('fs');
|
|
4
|
+
const path = require('path');
|
|
5
|
+
const { replayMatchScope } = require('./replay-match');
|
|
6
|
+
const { assertReplayEntry, httpUrl, parseSession, parseDirectReplayTypes, replayCookies, BODY_BUDGET, MAX_RECORDS } = require('./replay-session');
|
|
7
|
+
const { replayAssetType, downloadReplayAsset } = require('./replay-assets');
|
|
8
|
+
|
|
9
|
+
let activeWorker;
|
|
10
|
+
let lastReplay;
|
|
11
|
+
|
|
12
|
+
function isLoopback(address) {
|
|
13
|
+
return /^(127\.0\.0\.1|::1|::ffff:127\.0\.0\.1)$/.test(address);
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
function isLocalReplayRequest(req) {
|
|
17
|
+
const { host, origin } = req.headers;
|
|
18
|
+
const remote = req.headers['x-whistle-remote-address'];
|
|
19
|
+
const client = req.headers['x-forwarded-for'];
|
|
20
|
+
return /^(localhost|127\.0\.0\.1|\[::1\])(:\d+)?$/i.test(host) &&
|
|
21
|
+
(origin === 'http://' + host || origin === 'https://' + host) &&
|
|
22
|
+
isLoopback(req.socket.remoteAddress) &&
|
|
23
|
+
(remote === undefined || isLoopback(remote)) &&
|
|
24
|
+
(client === undefined || isLoopback(client));
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
function launchReplay(req, res) {
|
|
28
|
+
let session, cookies, directTypes;
|
|
29
|
+
try {
|
|
30
|
+
const payload = JSON.parse(req.body);
|
|
31
|
+
session = parseSession(payload?.session ?? payload);
|
|
32
|
+
directTypes = parseDirectReplayTypes(payload?.session ? payload.directTypes : undefined);
|
|
33
|
+
assertReplayEntry(session);
|
|
34
|
+
cookies = replayCookies(payload?.session ? payload.cookieHeader : undefined, session.entryUrl);
|
|
35
|
+
} catch (error) {
|
|
36
|
+
return res.status(400).json({ message: error.message });
|
|
37
|
+
}
|
|
38
|
+
if (activeWorker) return res.status(409).json({ message: '请先关闭已打开的复现窗口,再开始新的复现。' });
|
|
39
|
+
|
|
40
|
+
const localNode = path.join(__dirname, '../node_modules/node/bin/node');
|
|
41
|
+
const execPath = process.env.MOCKMASTER_REPLAY_NODE ||
|
|
42
|
+
(Number(process.versions.node.split('.')[0]) >= 20 ? process.execPath : fs.existsSync(localNode) ? localNode : null);
|
|
43
|
+
if (!execPath) {
|
|
44
|
+
return res.status(503).json({ message: '复现需要 Node.js 20 或更新版本。请用新版 Node.js 启动 Whistle。' });
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
const child = fork(path.join(__dirname, '../scripts/replay-session.js'), [], {
|
|
48
|
+
execPath,
|
|
49
|
+
execArgv: [],
|
|
50
|
+
stdio: ['ignore', 'ignore', 'ignore', 'ipc']
|
|
51
|
+
});
|
|
52
|
+
activeWorker = child;
|
|
53
|
+
const replay = lastReplay = {
|
|
54
|
+
id: randomUUID(), running: true, directTypes, blocked: [], liveResources: {}, overrides: new Map(), missingAssets: new Map(), additions: [],
|
|
55
|
+
entryUrl: session.entryUrl, records: session.records,
|
|
56
|
+
recordCount: session.records.length,
|
|
57
|
+
directCount: session.records.filter(row => row.source === 'direct').length,
|
|
58
|
+
bodySize: session.records.reduce((size, row) => size + (row.requestBody?.length || 0) + (row.response?.body.length || 0), 0)
|
|
59
|
+
};
|
|
60
|
+
let replied = false;
|
|
61
|
+
const reply = (status, message) => {
|
|
62
|
+
if (replied) return;
|
|
63
|
+
replied = true;
|
|
64
|
+
clearTimeout(timer);
|
|
65
|
+
res.status(status).json({ ok: status === 200, message, id: replay.id, supportsBatchOverrides: true });
|
|
66
|
+
};
|
|
67
|
+
const fail = message => {
|
|
68
|
+
reply(503, message);
|
|
69
|
+
child.kill();
|
|
70
|
+
};
|
|
71
|
+
const timer = setTimeout(() => fail('打开 Chrome 超时,请重试。'), 20000);
|
|
72
|
+
child.on('message', result => {
|
|
73
|
+
if (result.ready) reply(200, '独立 Chrome 窗口已打开');
|
|
74
|
+
else if (result.error) reply(503, result.error);
|
|
75
|
+
else if (result.reset) {
|
|
76
|
+
replay.blocked = [];
|
|
77
|
+
replay.liveResources = {};
|
|
78
|
+
replay.missingAssets.clear();
|
|
79
|
+
if (result.directTypes) replay.directTypes = result.directTypes;
|
|
80
|
+
if (result.entryUrl) replay.entryUrl = result.entryUrl;
|
|
81
|
+
} else if (result.liveResource && replay.directTypes.includes(result.liveResource)) {
|
|
82
|
+
replay.liveResources[result.liveResource] = (replay.liveResources[result.liveResource] || 0) + 1;
|
|
83
|
+
} else if (result.blocked && replay.blocked.length < 60) {
|
|
84
|
+
const asset = result.asset;
|
|
85
|
+
if (asset && replayAssetType(new URL(asset.url), asset.type)) replay.missingAssets.set(asset.url, asset);
|
|
86
|
+
const item = result.blocked;
|
|
87
|
+
if (item.candidateIds?.length) item.scope = replayMatchScope(item.method, item.url, item.type);
|
|
88
|
+
if (!replay.blocked.some(previous => JSON.stringify(previous) === JSON.stringify(item))) {
|
|
89
|
+
replay.blocked.push(item);
|
|
90
|
+
}
|
|
91
|
+
}
|
|
92
|
+
});
|
|
93
|
+
child.once('error', () => {
|
|
94
|
+
replay.running = false;
|
|
95
|
+
replay.records = null;
|
|
96
|
+
if (activeWorker === child) activeWorker = null;
|
|
97
|
+
reply(503, '无法启动本地复现进程,请检查 Node.js 路径。');
|
|
98
|
+
});
|
|
99
|
+
child.once('exit', () => {
|
|
100
|
+
replay.running = false;
|
|
101
|
+
replay.records = null;
|
|
102
|
+
if (activeWorker === child) activeWorker = null;
|
|
103
|
+
reply(503, 'Chrome 未能启动,请检查 Chrome 和回放组件是否已安装。');
|
|
104
|
+
});
|
|
105
|
+
child.send({ session, cookies, directTypes }, error => {
|
|
106
|
+
if (error) fail('无法将会话交给本地复现进程。');
|
|
107
|
+
});
|
|
108
|
+
}
|
|
109
|
+
|
|
110
|
+
function replayStatus(req, res) {
|
|
111
|
+
if (!isLocalReplayRequest(req)) return res.sendStatus(403);
|
|
112
|
+
if (!lastReplay || req.query.id !== lastReplay.id) return res.sendStatus(404);
|
|
113
|
+
const { id, running, entryUrl, directTypes, blocked, liveResources, overrides } = lastReplay;
|
|
114
|
+
res.set('Cache-Control', 'no-store').json({ id, running, entryUrl, directTypes, blocked, liveResources, overrides: [...overrides.values()],
|
|
115
|
+
missingAssets: lastReplay.missingAssets.size, directCount: lastReplay.directCount, supportsBatchOverrides: true });
|
|
116
|
+
}
|
|
117
|
+
|
|
118
|
+
async function replaySupplement(req, res) {
|
|
119
|
+
if (!isLocalReplayRequest(req)) return res.sendStatus(403);
|
|
120
|
+
const replay = lastReplay;
|
|
121
|
+
const worker = activeWorker;
|
|
122
|
+
if (!replay?.running || req.query.id !== replay.id || !worker?.connected) return res.sendStatus(404);
|
|
123
|
+
if (replay.supplementing) return res.status(409).json({ message: '正在补录静态资源,请稍候。' });
|
|
124
|
+
res.set('Cache-Control', 'no-store');
|
|
125
|
+
const limit = Math.min(12, MAX_RECORDS - replay.recordCount);
|
|
126
|
+
if (limit <= 0 || replay.bodySize >= BODY_BUDGET) {
|
|
127
|
+
if (replay.additions.length) return res.json({ records: replay.additions, added: 0, failures: [], notice: '会话已达到容量上限。' });
|
|
128
|
+
return res.status(400).json({ message: '会话已达到容量上限。' });
|
|
129
|
+
}
|
|
130
|
+
const assets = [...replay.missingAssets.values()].slice(0, limit);
|
|
131
|
+
const controller = new AbortController();
|
|
132
|
+
const timer = setTimeout(() => controller.abort(), 12000);
|
|
133
|
+
replay.supplementing = true;
|
|
134
|
+
try {
|
|
135
|
+
const results = await Promise.allSettled(assets.map(asset => downloadReplayAsset(asset, controller.signal)));
|
|
136
|
+
const records = [];
|
|
137
|
+
const failures = [];
|
|
138
|
+
let size = replay.bodySize;
|
|
139
|
+
results.forEach((result, index) => {
|
|
140
|
+
if (result.status === 'fulfilled' && size + result.value.response.body.length <= BODY_BUDGET) {
|
|
141
|
+
size += result.value.response.body.length;
|
|
142
|
+
records.push(result.value);
|
|
143
|
+
} else {
|
|
144
|
+
const url = new URL(assets[index].url);
|
|
145
|
+
failures.push({ url: url.origin + url.pathname,
|
|
146
|
+
reason: result.status === 'rejected' ? result.reason.message : '会话正文已达到容量上限' });
|
|
147
|
+
}
|
|
148
|
+
});
|
|
149
|
+
if (records.length) {
|
|
150
|
+
if (lastReplay !== replay || !worker.connected) return res.status(409).json({ message: '复现已结束,请重新开始后补录。' });
|
|
151
|
+
await new Promise((resolve, reject) => worker.send({ supplement: records }, error => error ? reject(error) : resolve()));
|
|
152
|
+
replay.bodySize = size;
|
|
153
|
+
replay.recordCount += records.length;
|
|
154
|
+
replay.directCount += records.length;
|
|
155
|
+
replay.additions.push(...records);
|
|
156
|
+
for (const row of records) replay.missingAssets.delete(row.url);
|
|
157
|
+
}
|
|
158
|
+
res.json({ records: replay.additions, added: records.length, failures });
|
|
159
|
+
} catch {
|
|
160
|
+
res.status(503).json({ message: '无法把补录数据交给复现窗口,请重试。' });
|
|
161
|
+
} finally {
|
|
162
|
+
clearTimeout(timer);
|
|
163
|
+
replay.supplementing = false;
|
|
164
|
+
}
|
|
165
|
+
}
|
|
166
|
+
|
|
167
|
+
function replayControl(req, res) {
|
|
168
|
+
if (!isLocalReplayRequest(req)) return res.sendStatus(403);
|
|
169
|
+
const replay = lastReplay;
|
|
170
|
+
const worker = activeWorker;
|
|
171
|
+
if (!replay?.running || req.query.id !== replay.id || !worker?.connected) return res.sendStatus(404);
|
|
172
|
+
|
|
173
|
+
const { type, scope, recordId } = req.body || {};
|
|
174
|
+
if (type === 'restart' || type === 'stop') {
|
|
175
|
+
const action = { type };
|
|
176
|
+
if (type === 'restart') {
|
|
177
|
+
try {
|
|
178
|
+
if (req.body.directTypes !== undefined) action.directTypes = parseDirectReplayTypes(req.body.directTypes);
|
|
179
|
+
if (req.body.entryUrl !== undefined) {
|
|
180
|
+
if (typeof req.body.entryUrl !== 'string') throw new Error('请输入有效的复现地址');
|
|
181
|
+
const entry = httpUrl(req.body.entryUrl);
|
|
182
|
+
if (entry.origin !== httpUrl(replay.entryUrl).origin) {
|
|
183
|
+
throw new Error('跨站点修改请先结束复现,再用新地址启动 Chrome。');
|
|
184
|
+
}
|
|
185
|
+
assertReplayEntry({ entryUrl: entry.href, records: replay.records });
|
|
186
|
+
action.entryUrl = entry.href;
|
|
187
|
+
}
|
|
188
|
+
} catch (error) {
|
|
189
|
+
return res.status(400).json({ message: error.message });
|
|
190
|
+
}
|
|
191
|
+
}
|
|
192
|
+
return worker.send({ action }, error => res.sendStatus(error ? 503 : 204));
|
|
193
|
+
}
|
|
194
|
+
if (replay.changingOverrides) return res.status(409).json({ message: '正在应用参数例外,请稍候。' });
|
|
195
|
+
let changes;
|
|
196
|
+
if (type === 'override' || type === 'apply-overrides') {
|
|
197
|
+
const selections = type === 'override' ? [{ scope, recordId }] : req.body.selections;
|
|
198
|
+
if (!Array.isArray(selections) || !selections.length || selections.length > 60 ||
|
|
199
|
+
selections.some(item => typeof item?.scope !== 'string' || typeof item.recordId !== 'string') ||
|
|
200
|
+
new Set(selections.map(item => item.scope)).size !== selections.length) {
|
|
201
|
+
return res.status(400).json({ message: '参数例外的选择有误,请重新选择后再应用。' });
|
|
202
|
+
}
|
|
203
|
+
|
|
204
|
+
const diagnostics = new Map(replay.blocked.filter(item => item.scope).map(item => [item.scope, item]));
|
|
205
|
+
changes = [];
|
|
206
|
+
for (const selection of selections) {
|
|
207
|
+
const item = diagnostics.get(selection.scope);
|
|
208
|
+
if (!item?.candidateIds.includes(selection.recordId)) {
|
|
209
|
+
return res.status(409).json({ message: '部分诊断已变化,请检查选择后再应用。' });
|
|
210
|
+
}
|
|
211
|
+
changes.push({ scope: item.scope, method: item.method, url: item.url, type: item.type, recordId: selection.recordId });
|
|
212
|
+
}
|
|
213
|
+
const additions = changes.filter(item => !replay.overrides.has(item.scope)).length;
|
|
214
|
+
if (replay.overrides.size + additions > MAX_RECORDS) {
|
|
215
|
+
return res.status(409).json({ message: '本次复现的例外将超过上限,请先撤销部分例外。' });
|
|
216
|
+
}
|
|
217
|
+
} else if (type === 'remove-override' && typeof scope === 'string') {
|
|
218
|
+
if (!replay.overrides.has(scope)) return res.status(409).json({ message: '这条例外已撤销。' });
|
|
219
|
+
changes = [{ scope }];
|
|
220
|
+
} else {
|
|
221
|
+
return res.sendStatus(400);
|
|
222
|
+
}
|
|
223
|
+
|
|
224
|
+
replay.changingOverrides = true;
|
|
225
|
+
worker.send({ overrideChanges: changes }, error => {
|
|
226
|
+
replay.changingOverrides = false;
|
|
227
|
+
if (error) return res.sendStatus(503);
|
|
228
|
+
for (const change of changes) {
|
|
229
|
+
if (change.recordId !== undefined) replay.overrides.set(change.scope, change);
|
|
230
|
+
else replay.overrides.delete(change.scope);
|
|
231
|
+
}
|
|
232
|
+
res.sendStatus(204);
|
|
233
|
+
});
|
|
234
|
+
}
|
|
235
|
+
|
|
236
|
+
module.exports = { isLocalReplayRequest, launchReplay, replayStatus, replaySupplement, replayControl };
|
|
@@ -0,0 +1,182 @@
|
|
|
1
|
+
const { requestKey } = require('./replay-session');
|
|
2
|
+
|
|
3
|
+
const TRACE_ID = /^T\d{15,17}$/;
|
|
4
|
+
const CALLBACK = /^[a-z_$][\w$]*$/i;
|
|
5
|
+
const utf8 = new TextDecoder('utf-8', { fatal: true });
|
|
6
|
+
const PARAMETER_MISMATCH = '已录到同一接口,但请求参数不同';
|
|
7
|
+
const MISSING_REQUEST = '会话里没有录到这条请求';
|
|
8
|
+
|
|
9
|
+
function endpointKey(method, url) {
|
|
10
|
+
return method + ' ' + url.origin + url.pathname;
|
|
11
|
+
}
|
|
12
|
+
|
|
13
|
+
function replayMatchScope(method, address, type) {
|
|
14
|
+
return JSON.stringify([endpointKey(method, new URL(address)), type]);
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
// Keep the exact JSON tokens for business fields, including large numbers.
|
|
18
|
+
function bodyWithoutTrace(body) {
|
|
19
|
+
if (!body || body.length > 350000) return null;
|
|
20
|
+
try {
|
|
21
|
+
const text = utf8.decode(Buffer.from(body, 'base64'));
|
|
22
|
+
if (!/^\s*\{/.test(text) || !text.includes('traceId')) return null;
|
|
23
|
+
JSON.parse(text);
|
|
24
|
+
const tokens = text.match(/"(?:\\.|[^"\\])*"|[{}\[\],:]|[^\s{}\[\],:"]+/g);
|
|
25
|
+
let depth = 0;
|
|
26
|
+
let traceIndex = -1;
|
|
27
|
+
for (let i = 0; i < tokens.length; i++) {
|
|
28
|
+
const token = tokens[i];
|
|
29
|
+
if (token === '{' || token === '[') depth++;
|
|
30
|
+
else if (token === '}' || token === ']') depth--;
|
|
31
|
+
else if (depth === 1 && token.startsWith('"') && tokens[i + 1] === ':' &&
|
|
32
|
+
JSON.parse(token) === 'traceId') {
|
|
33
|
+
if (traceIndex !== -1 || !/^"T\d{15,17}"$/.test(tokens[i + 2])) return null;
|
|
34
|
+
traceIndex = i + 2;
|
|
35
|
+
}
|
|
36
|
+
}
|
|
37
|
+
if (traceIndex === -1) return null;
|
|
38
|
+
// An empty token is distinct from every valid JSON value.
|
|
39
|
+
tokens[traceIndex] = '';
|
|
40
|
+
return tokens.join('');
|
|
41
|
+
} catch {
|
|
42
|
+
return null;
|
|
43
|
+
}
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
function normalize(method, url, body, jsonp) {
|
|
47
|
+
let changed = false;
|
|
48
|
+
const traces = url.searchParams.getAll('traceId');
|
|
49
|
+
const callbacks = url.searchParams.getAll('callback');
|
|
50
|
+
const callback = jsonp && callbacks.length === 1 && CALLBACK.test(callbacks[0]) ? callbacks[0] : null;
|
|
51
|
+
const query = url.search.slice(1).split('&').map(part => {
|
|
52
|
+
const key = part.split('=', 1)[0];
|
|
53
|
+
if ((key === 'traceId' && traces.length === 1 && TRACE_ID.test(traces[0])) ||
|
|
54
|
+
(key === 'callback' && callback)) {
|
|
55
|
+
changed = true;
|
|
56
|
+
return [key];
|
|
57
|
+
}
|
|
58
|
+
return part;
|
|
59
|
+
});
|
|
60
|
+
const normalizedBody = method === 'POST' ? bodyWithoutTrace(body) : null;
|
|
61
|
+
if (!changed && normalizedBody === null) return null;
|
|
62
|
+
return {
|
|
63
|
+
key: JSON.stringify([method, url.origin, url.pathname, query, normalizedBody === null ? body : [normalizedBody]]),
|
|
64
|
+
callback
|
|
65
|
+
};
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
function readJsonp(row, url) {
|
|
69
|
+
const callbacks = url.searchParams.getAll('callback');
|
|
70
|
+
const callback = callbacks[0];
|
|
71
|
+
if (row.method !== 'GET' || row.requestBody !== '' || row.outcome !== 'response' || row.status !== 200 ||
|
|
72
|
+
!row.response || callbacks.length !== 1 || !CALLBACK.test(callback)) return null;
|
|
73
|
+
try {
|
|
74
|
+
const source = utf8.decode(Buffer.from(row.response.body, 'base64'));
|
|
75
|
+
const wrapper = /^(\s*)([a-z_$][\w$]*)(\s*\()/i.exec(source);
|
|
76
|
+
return wrapper?.[2] === callback ? { source, callback, offset: wrapper[1].length } : null;
|
|
77
|
+
} catch {
|
|
78
|
+
return null;
|
|
79
|
+
}
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
function describeFields(label, names) {
|
|
83
|
+
const visible = names.filter(name => /^[\w.-]{1,64}$/.test(name)).slice(0, 6);
|
|
84
|
+
return visible.length ? `${label}字段 ${visible.join('、')}${visible.length < names.length ? ' 等' : ''} 不同` : `${label}内容不同`;
|
|
85
|
+
}
|
|
86
|
+
|
|
87
|
+
function describeMismatch(record, url, body) {
|
|
88
|
+
const details = [];
|
|
89
|
+
const recorded = new URL(record.url);
|
|
90
|
+
if (recorded.search !== url.search) {
|
|
91
|
+
const names = [...new Set([...recorded.searchParams.keys(), ...url.searchParams.keys()])];
|
|
92
|
+
details.push(describeFields('查询参数', names.filter(name =>
|
|
93
|
+
JSON.stringify(recorded.searchParams.getAll(name)) !== JSON.stringify(url.searchParams.getAll(name)))));
|
|
94
|
+
}
|
|
95
|
+
if (record.requestBody !== body) {
|
|
96
|
+
let names = [];
|
|
97
|
+
if (record.requestBody.length <= 350000 && body.length <= 350000) {
|
|
98
|
+
try {
|
|
99
|
+
const previous = JSON.parse(utf8.decode(Buffer.from(record.requestBody, 'base64')));
|
|
100
|
+
const current = JSON.parse(utf8.decode(Buffer.from(body, 'base64')));
|
|
101
|
+
if (previous && current && typeof previous === 'object' && typeof current === 'object' &&
|
|
102
|
+
!Array.isArray(previous) && !Array.isArray(current)) {
|
|
103
|
+
names = [...new Set([...Object.keys(previous), ...Object.keys(current)])].filter(name =>
|
|
104
|
+
JSON.stringify(previous[name]) !== JSON.stringify(current[name]));
|
|
105
|
+
}
|
|
106
|
+
} catch {
|
|
107
|
+
// Other body formats are reported without exposing their contents.
|
|
108
|
+
}
|
|
109
|
+
}
|
|
110
|
+
details.push(describeFields('请求体', names));
|
|
111
|
+
}
|
|
112
|
+
return details.join(',');
|
|
113
|
+
}
|
|
114
|
+
|
|
115
|
+
function withJsonpCallback({ row, jsonp }, newCallback) {
|
|
116
|
+
if (!newCallback || !jsonp || newCallback === jsonp.callback) return row;
|
|
117
|
+
const { source, callback, offset } = jsonp;
|
|
118
|
+
const text = source.slice(0, offset) + newCallback + source.slice(offset + callback.length);
|
|
119
|
+
return { ...row, response: { ...row.response, body: Buffer.from(text).toString('base64') } };
|
|
120
|
+
}
|
|
121
|
+
|
|
122
|
+
function createReplayMatcher(records, overrides = new Map()) {
|
|
123
|
+
const exact = new Map();
|
|
124
|
+
const normalized = new Map();
|
|
125
|
+
const paths = new Map();
|
|
126
|
+
const usableResponses = new Map();
|
|
127
|
+
const ordered = [...records].sort((a, b) => a.startTime - b.startTime ||
|
|
128
|
+
a.id.localeCompare(b.id, undefined, { numeric: true }));
|
|
129
|
+
function add(map, key, item) {
|
|
130
|
+
if (!map.has(key)) map.set(key, []);
|
|
131
|
+
map.get(key).push(item);
|
|
132
|
+
}
|
|
133
|
+
for (const row of ordered) {
|
|
134
|
+
const url = new URL(row.url);
|
|
135
|
+
const path = endpointKey(row.method, url);
|
|
136
|
+
if (paths.get(path)?.requestBody == null) paths.set(path, row);
|
|
137
|
+
const jsonp = readJsonp(row, url);
|
|
138
|
+
const item = { row, jsonp, used: false };
|
|
139
|
+
if (row.outcome === 'response' && row.response && row.status !== 304) add(usableResponses, path, item);
|
|
140
|
+
if (row.requestBody === null) continue;
|
|
141
|
+
add(exact, requestKey(row.method, row.url, row.requestBody), item);
|
|
142
|
+
const key = normalize(row.method, url, row.requestBody, !!jsonp);
|
|
143
|
+
if (key) add(normalized, key.key, item);
|
|
144
|
+
}
|
|
145
|
+
return (method, url, body, type) => {
|
|
146
|
+
const literal = exact.get(requestKey(method, url.href, body));
|
|
147
|
+
const item = literal?.find(candidate => !candidate.used);
|
|
148
|
+
if (item) {
|
|
149
|
+
item.used = true;
|
|
150
|
+
return { entry: item.row };
|
|
151
|
+
}
|
|
152
|
+
// A callback can only be renamed for a script load and a known JSONP wrapper.
|
|
153
|
+
const keys = [normalize(method, url, body, false)];
|
|
154
|
+
if (type === 'script' && method === 'GET') keys.unshift(normalize(method, url, body, true));
|
|
155
|
+
let exhausted = !!literal;
|
|
156
|
+
for (const key of keys) {
|
|
157
|
+
if (!key) continue;
|
|
158
|
+
const candidates = normalized.get(key.key);
|
|
159
|
+
exhausted ||= !!candidates;
|
|
160
|
+
const match = candidates?.find(candidate => !candidate.used);
|
|
161
|
+
if (!match) continue;
|
|
162
|
+
match.used = true;
|
|
163
|
+
return { entry: withJsonpCallback(match, key.callback) };
|
|
164
|
+
}
|
|
165
|
+
if (exhausted) return { reason: '录制的响应已用完' };
|
|
166
|
+
const path = endpointKey(method, url);
|
|
167
|
+
const recorded = paths.get(path);
|
|
168
|
+
if (!recorded) return { reason: MISSING_REQUEST };
|
|
169
|
+
if (recorded.requestBody === null) return { reason: '已录到同一接口,但请求体未完整录到' };
|
|
170
|
+
const candidates = usableResponses.get(path) || [];
|
|
171
|
+
const selection = overrides.get(replayMatchScope(method, url, type));
|
|
172
|
+
const approved = candidates.find(candidate => candidate.row.id === selection);
|
|
173
|
+
if (approved) {
|
|
174
|
+
const callback = type === 'script' ? normalize(method, url, body, true)?.callback : null;
|
|
175
|
+
return { entry: withJsonpCallback(approved, callback) };
|
|
176
|
+
}
|
|
177
|
+
return { reason: PARAMETER_MISMATCH, detail: describeMismatch(recorded, url, body),
|
|
178
|
+
candidateIds: candidates.map(candidate => candidate.row.id) };
|
|
179
|
+
};
|
|
180
|
+
}
|
|
181
|
+
|
|
182
|
+
module.exports = { createReplayMatcher, replayMatchScope, PARAMETER_MISMATCH, MISSING_REQUEST };
|