mirror of
https://github.com/burrow-dev/burrow-server.git
synced 2026-11-19 09:37:50 -05:00
fix(captcha): cap Cloudflare checks in flight, and let a blocked widget be retried
- server: at most config.maxConcurrentCaptchaChecks (AJ_CAPTCHA_MAX_CONCURRENT, 24 by default, 1 to 100) Turnstile checks wait on Cloudflare at once; past that the answer is captcha_unavailable at once, so a slow Cloudflare cannot hold thousands of sockets open. The page's policy lets it connect to challenges.cloudflare.com too. - page: public/captcha.js gives up on a widget that has not loaded in 15 s, or that loaded without turnstile.render, instead of waiting for ever, and a render that throws counts as a failed answer. The game's captcha window says to check the connection or a content blocker, with a "Try again" button. Co-Authored-By: Claude Opus 5.5 <[email protected]>
This commit is contained in:
1 parent
43f2717b44
commit
974f68490e
4 files changed
+62
-19
No files matched your search
+7
-1
@@ -25,17 +25,21 @@
|
||||
-webkit-font-smoothing: antialiased;
|
||||
}
|
||||
#status { margin: 0; text-align: center; }
|
||||
button { display: none; border: 1px solid #8c8c8c; border-radius: 5px; padding: 6px 12px; background: transparent; color: #ddd; font: inherit; cursor: pointer; }
|
||||
button.show { display: block; }
|
||||
</style>
|
||||
</head>
|
||||
<body>
|
||||
<div id="widget"></div>
|
||||
<p id="status" role="status">Checking…</p>
|
||||
<button id="retry" type="button">Try again</button>
|
||||
<script src="/captcha.js"></script>
|
||||
<script>
|
||||
"use strict";
|
||||
|
||||
const action = new URLSearchParams(location.search).get("action") === "signup" ? "signup" : "login";
|
||||
const status = document.getElementById("status");
|
||||
const retry = document.getElementById("retry");
|
||||
const done = token => {
|
||||
if (window.burrowCaptcha) {
|
||||
window.burrowCaptcha.done(token);
|
||||
@@ -53,10 +57,12 @@
|
||||
}});
|
||||
check.answer().then(token => {
|
||||
if (!token) {
|
||||
status.textContent = "Captcha failed. Close this and try again.";
|
||||
status.textContent = "Captcha could not load. Check your connection or content blocker.";
|
||||
retry.classList.add("show");
|
||||
}
|
||||
});
|
||||
}
|
||||
retry.addEventListener("click", () => location.reload());
|
||||
</script>
|
||||
</body>
|
||||
</html>
|
||||
+38
-17
@@ -10,14 +10,29 @@
|
||||
// captcha.FAILED then), and check.reset() after it: an answer is good once.
|
||||
window.captcha = (() => {
|
||||
const siteKey = "{{siteKey}}";
|
||||
const LOAD_TIMEOUT_MS = 15000;
|
||||
let loading = null;
|
||||
|
||||
const load = () => {
|
||||
loading = loading || new Promise((resolve, reject) => {
|
||||
const script = document.createElement("script");
|
||||
script.src = "https://challenges.cloudflare.com/turnstile/v0/api.js?render=explicit";
|
||||
script.onload = () => resolve(window.turnstile);
|
||||
const timer = setTimeout(() => {
|
||||
loading = null;
|
||||
reject(new Error("the captcha took too long to load"));
|
||||
}, LOAD_TIMEOUT_MS);
|
||||
script.onload = () => {
|
||||
clearTimeout(timer);
|
||||
if (window.turnstile && typeof window.turnstile.render === "function") {
|
||||
resolve(window.turnstile);
|
||||
}
|
||||
else {
|
||||
loading = null;
|
||||
reject(new Error("the captcha did not start"));
|
||||
}
|
||||
};
|
||||
script.onerror = () => {
|
||||
clearTimeout(timer);
|
||||
loading = null;
|
||||
reject(new Error("the captcha did not load"));
|
||||
};
|
||||
@@ -44,21 +59,27 @@ window.captcha = (() => {
|
||||
};
|
||||
if (siteKey) {
|
||||
load().then(turnstile => {
|
||||
widget = turnstile.render(element, {
|
||||
sitekey: siteKey,
|
||||
action,
|
||||
theme: "dark",
|
||||
appearance: options.appearance || "interaction-only",
|
||||
callback: settle,
|
||||
"expired-callback": () => {
|
||||
answer = null;
|
||||
},
|
||||
// its code is cloudflare's (110200: this site's name is not on the widget)
|
||||
"error-callback": code => {
|
||||
console.log(`captcha error ${code}`);
|
||||
settle("");
|
||||
},
|
||||
});
|
||||
try {
|
||||
widget = turnstile.render(element, {
|
||||
sitekey: siteKey,
|
||||
action,
|
||||
theme: "dark",
|
||||
appearance: options.appearance || "interaction-only",
|
||||
callback: settle,
|
||||
"expired-callback": () => {
|
||||
answer = null;
|
||||
},
|
||||
// its code is cloudflare's (110200: this site's name is not on the widget)
|
||||
"error-callback": code => {
|
||||
console.log(`captcha error ${code}`);
|
||||
settle("");
|
||||
},
|
||||
});
|
||||
}
|
||||
catch (err) {
|
||||
console.log(`captcha could not start: ${err.message}`);
|
||||
settle("");
|
||||
}
|
||||
}, () => settle(""));
|
||||
}
|
||||
return {
|
||||
@@ -79,5 +100,5 @@ window.captcha = (() => {
|
||||
};
|
||||
};
|
||||
|
||||
return {on: Boolean(siteKey), mount, FAILED: "Captcha could not load. Reload the page."};
|
||||
return {on: Boolean(siteKey), mount, FAILED: "Captcha could not load. Check your connection or content blocker, then reload."};
|
||||
})();
|
||||
Reference in new issue
Block a user