mirror of
https://github.com/FoggedLens/deflock.git
synced 2026-04-27 05:46:00 +02:00
26 lines
812 B
TypeScript
26 lines
812 B
TypeScript
const TURNSTILE_SECRET_KEY = process.env.TURNSTILE_SECRET_KEY || '';
|
|
const SITEVERIFY_URL = 'https://challenges.cloudflare.com/turnstile/v0/siteverify';
|
|
|
|
export class TurnstileClient {
|
|
async verify(token: string, remoteIp?: string): Promise<boolean> {
|
|
const body = new URLSearchParams({
|
|
secret: TURNSTILE_SECRET_KEY,
|
|
response: token,
|
|
...(remoteIp ? { remoteip: remoteIp } : {}),
|
|
});
|
|
|
|
const response = await fetch(SITEVERIFY_URL, {
|
|
method: 'POST',
|
|
headers: { 'Content-Type': 'application/x-www-form-urlencoded' },
|
|
body: body.toString(),
|
|
});
|
|
|
|
if (!response.ok) {
|
|
throw new Error(`Turnstile siteverify request failed: ${response.status}`);
|
|
}
|
|
|
|
const json = await response.json() as { success: boolean };
|
|
return json.success === true;
|
|
}
|
|
}
|