feat: add CF Turnstile when new address (#200)

This commit is contained in:
Dream Hunter
2024-05-04 23:14:23 +08:00
committed by GitHub
parent 26969bebb8
commit f63c4ebd9c
14 changed files with 154 additions and 17 deletions

View File

@@ -129,3 +129,24 @@ export const sendAdminInternalMail = async (c, toMail, subject, text) => {
return false;
}
};
export const checkCfTurnstile = async (c, token) => {
if (!c.env.CF_TURNSTILE_SITE_KEY) {
return;
}
const reqIp = c.req.raw.headers.get("cf-connecting-ip")
let formData = new FormData();
formData.append('secret', c.env.CF_TURNSTILE_SECRET_KEY);
formData.append('response', token);
formData.append('remoteip', reqIp);
const url = 'https://challenges.cloudflare.com/turnstile/v0/siteverify';
const result = await fetch(url, {
body: formData,
method: 'POST',
});
const captchaRes = await result.json();
if (!captchaRes.success) {
console.log("Captcha failed", captchaRes);
throw new Error("Captcha failed");
}
}