-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
394 lines (333 loc) · 10.9 KB
/
Copy pathscript.js
File metadata and controls
394 lines (333 loc) · 10.9 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
const captcha = document.querySelector(".captcha-card");
const checkButton = document.querySelector(".check-control");
const statusLine = document.querySelector(".status-line");
const resetButton = document.querySelector("#resetBtn");
const continueButton = document.querySelector("#continueBtn");
const requestState = document.querySelector("#requestState");
const rayId = document.querySelector("#rayId");
const traceCanvas = document.querySelector("#traceCanvas");
const traceCount = document.querySelector("#traceCount");
const traceDuration = document.querySelector("#traceDuration");
const traceOutput = document.querySelector("#traceOutput");
const copyTraceButton = document.querySelector("#copyTraceBtn");
const traceContext = traceCanvas.getContext("2d");
const delbotModelState = document.querySelector("#delbotModelState");
const humanScore = document.querySelector("#humanScore");
const botProbability = document.querySelector("#botProbability");
const delbotDecision = document.querySelector("#delbotDecision");
let timer = null;
let isRecordingTrace = true;
let traceStart = null;
let lastTraceAt = 0;
let tracePoints = [];
let delbotRecorder = createDelbotRecorder();
let delbotRnn1Model = null;
const delbotRnn1ModelUrl =
"./models/rnn1/model-rnn1-features2.json";
const traceSampleIntervalMs = 10;
const shouldLogDelbotDebug = false;
function createDelbotRecorder() {
if (!window.delbot?.Recorder) return null;
return new window.delbot.Recorder(window.innerWidth, window.innerHeight);
}
function getDelbotRnn1Model() {
if (delbotRnn1Model) return delbotRnn1Model;
if (!window.delbot?.Model || !window.delbot?.data?.DataFeatures2) return null;
delbotRnn1Model = new window.delbot.Model(
delbotRnn1ModelUrl,
new window.delbot.data.DataFeatures2({
numClasses: 1,
xSize: 24,
shouldCompleteXSize: false,
}),
);
return delbotRnn1Model;
}
function getTracePayload() {
return {
capturedBefore: "verify-click",
delbot: getDelbotPayload(),
pointCount: tracePoints.length,
viewport: {
width: window.innerWidth,
height: window.innerHeight,
},
points: tracePoints,
};
}
function getDelbotPayload() {
return {
model: "DELBOT-Mouse rnn1",
modelState: delbotModelState.textContent,
humanScore: humanScore.textContent,
botProbability: botProbability.textContent,
decision: delbotDecision.textContent,
};
}
function setDelbotResult({ modelState, score, botRate, decision }) {
if (modelState) {
delbotModelState.textContent = modelState;
}
humanScore.textContent = Number.isFinite(score) ? `${score}/100` : "--";
botProbability.textContent = Number.isFinite(botRate)
? `${(botRate * 100).toFixed(1)}%`
: "--";
delbotDecision.textContent = decision;
traceOutput.textContent = JSON.stringify(getTracePayload(), null, 2);
}
function average(values) {
if (!values.length) return 0;
return values.reduce((sum, value) => sum + value, 0) / values.length;
}
async function scoreTraceWithDelbot() {
const rnn1Model = getDelbotRnn1Model();
if (!rnn1Model || !window.tf) {
setDelbotResult({
modelState: "未加载",
score: NaN,
botRate: NaN,
decision: "请检查脚本 CDN",
});
return;
}
if (!delbotRecorder) {
delbotRecorder = createDelbotRecorder();
}
if (!delbotRecorder || tracePoints.length < 24) {
setDelbotResult({
modelState: "数据不足",
score: NaN,
botRate: NaN,
decision: "至少需要 24 个移动点",
});
return;
}
setDelbotResult({
modelState: "rnn1 推理中",
score: NaN,
botRate: NaN,
decision: "计算中",
});
try {
const loadedModel = await rnn1Model.getModel();
if (shouldLogDelbotDebug) {
const rawRecords = delbotRecorder.getRecords();
console.log("[DEBUG] Total records in recorder:", rawRecords.length);
console.log("[DEBUG] First record:", JSON.stringify(rawRecords[0]));
console.log("[DEBUG] Last record:", JSON.stringify(rawRecords.at(-1)));
console.log("[DEBUG] tf version:", window.tf?.version);
console.log("[DEBUG] Model loadingPath:", rnn1Model.loadingPath);
console.log("[DEBUG] Model input shape:", loadedModel.inputs[0].shape);
console.log("[DEBUG] Model output shape:", loadedModel.outputs[0].shape);
}
const predictions = await delbotRecorder.getPrediction(rnn1Model);
if (shouldLogDelbotDebug) {
console.log("[DEBUG] Raw predictions array:", predictions);
console.log("[DEBUG] Predictions count:", predictions.length);
console.log(
"[DEBUG] Each prediction:",
...predictions.map((p, i) => `[${i}]=${p.toFixed(6)}`),
);
}
if (!predictions.length) {
setDelbotResult({
modelState: "数据不足",
score: NaN,
botRate: NaN,
decision: "模型未生成预测",
});
return;
}
const botRate = Math.max(0, Math.min(1, average(predictions)));
const score = Math.round((1 - botRate) * 100);
const decision = botRate <= 0.2 ? "偏真人" : "偏机器";
setDelbotResult({
modelState: "rnn1 已完成",
score,
botRate,
decision,
});
} catch (error) {
setDelbotResult({
modelState: "推理失败",
score: NaN,
botRate: NaN,
decision:
error.message === "Failed to fetch"
? "模型文件下载失败"
: error.message || "模型调用异常",
});
}
}
function drawTrace() {
const rect = traceCanvas.getBoundingClientRect();
const ratio = window.devicePixelRatio || 1;
const width = Math.max(1, Math.round(rect.width * ratio));
const height = Math.max(1, Math.round(rect.height * ratio));
if (traceCanvas.width !== width || traceCanvas.height !== height) {
traceCanvas.width = width;
traceCanvas.height = height;
}
traceContext.clearRect(0, 0, width, height);
if (tracePoints.length === 0) {
traceContext.fillStyle = "#7a828c";
traceContext.font = `${12 * ratio}px sans-serif`;
traceContext.fillText("移动鼠标开始采集", 16 * ratio, 26 * ratio);
return;
}
traceContext.lineWidth = 2 * ratio;
traceContext.strokeStyle = "#f48120";
traceContext.lineCap = "round";
traceContext.lineJoin = "round";
traceContext.beginPath();
tracePoints.forEach((point, index) => {
const x = (point.x / window.innerWidth) * width;
const y = (point.y / window.innerHeight) * height;
if (index === 0) {
traceContext.moveTo(x, y);
} else {
traceContext.lineTo(x, y);
}
});
traceContext.stroke();
const first = tracePoints[0];
const last = tracePoints[tracePoints.length - 1];
const startX = (first.x / window.innerWidth) * width;
const startY = (first.y / window.innerHeight) * height;
const endX = (last.x / window.innerWidth) * width;
const endY = (last.y / window.innerHeight) * height;
traceContext.fillStyle = "#2f6fed";
traceContext.beginPath();
traceContext.arc(startX, startY, 4 * ratio, 0, Math.PI * 2);
traceContext.fill();
traceContext.fillStyle = "#118a45";
traceContext.beginPath();
traceContext.arc(endX, endY, 5 * ratio, 0, Math.PI * 2);
traceContext.fill();
}
function updateTracePanel() {
const duration = tracePoints.length
? tracePoints[tracePoints.length - 1].t - tracePoints[0].t
: 0;
traceCount.textContent = String(tracePoints.length);
traceDuration.textContent = `${duration} ms`;
traceOutput.textContent = JSON.stringify(getTracePayload(), null, 2);
drawTrace();
}
function recordTracePoint(event) {
if (!isRecordingTrace || captcha.dataset.state !== "idle") return;
if (event.pointerType && event.pointerType !== "mouse") return;
if (event.timeStamp - lastTraceAt < traceSampleIntervalMs) return;
if (traceStart === null) {
traceStart = Math.round(event.timeStamp);
}
lastTraceAt = event.timeStamp;
const point = {
t: Math.round(event.timeStamp - traceStart),
x: Math.round(event.clientX),
y: Math.round(event.clientY),
pageX: Math.round(event.pageX),
pageY: Math.round(event.pageY),
};
tracePoints.push(point);
if (delbotRecorder) {
delbotRecorder.addRecord({
time: point.t,
type: "Move",
x: point.x,
y: point.y,
});
}
if (tracePoints.length > 500) {
tracePoints = tracePoints.slice(-500);
}
updateTracePanel();
}
function resetTrace() {
isRecordingTrace = true;
traceStart = null;
lastTraceAt = 0;
tracePoints = [];
delbotRecorder = createDelbotRecorder();
setDelbotResult({
modelState: getDelbotRnn1Model() ? "rnn1 就绪" : "等待加载",
score: NaN,
botRate: NaN,
decision: "等待验证",
});
updateTracePanel();
}
function freezeTrace() {
isRecordingTrace = false;
updateTracePanel();
}
function setState(state) {
captcha.dataset.state = state;
if (state === "idle") {
statusLine.textContent = "等待用户操作";
requestState.textContent = "Challenge pending";
continueButton.disabled = true;
checkButton.disabled = false;
checkButton.setAttribute("aria-label", "开始验证");
}
if (state === "loading") {
statusLine.textContent = "正在分析浏览器环境...";
requestState.textContent = "Running checks";
continueButton.disabled = true;
checkButton.disabled = true;
checkButton.setAttribute("aria-label", "正在验证");
}
if (state === "done") {
statusLine.textContent = "验证通过,可以继续访问";
requestState.textContent = "Access granted";
continueButton.disabled = false;
checkButton.disabled = true;
checkButton.setAttribute("aria-label", "验证成功");
}
}
function randomRayId() {
const chars = "0123456789abcdef";
let id = "";
for (let index = 0; index < 16; index += 1) {
id += chars[Math.floor(Math.random() * chars.length)];
}
return id;
}
checkButton.addEventListener("click", () => {
if (captcha.dataset.state !== "idle") return;
freezeTrace();
scoreTraceWithDelbot();
setState("loading");
window.clearTimeout(timer);
timer = window.setTimeout(() => setState("done"), 1650);
});
resetButton.addEventListener("click", () => {
window.clearTimeout(timer);
rayId.textContent = randomRayId();
resetTrace();
setState("idle");
});
continueButton.addEventListener("click", () => {
statusLine.textContent = "演示完成:真实项目中这里会跳转到受保护页面";
});
copyTraceButton.addEventListener("click", async () => {
const json = JSON.stringify(getTracePayload(), null, 2);
try {
await navigator.clipboard.writeText(json);
copyTraceButton.textContent = "已复制";
window.setTimeout(() => {
copyTraceButton.textContent = "复制 JSON";
}, 1200);
} catch {
traceOutput.focus();
}
});
window.addEventListener("pointermove", recordTracePoint);
window.addEventListener("resize", drawTrace);
setDelbotResult({
modelState: getDelbotRnn1Model() ? "rnn1 就绪" : "等待加载",
score: NaN,
botRate: NaN,
decision: "等待验证",
});
updateTracePanel();