2021-02-06 02:46:47 +00:00
|
|
|
import fetch from 'node-fetch';
|
|
|
|
import { URLSearchParams } from 'url';
|
2021-08-19 12:55:45 +00:00
|
|
|
import { getAgentByUrl } from './fetch';
|
|
|
|
import config from '@/config/index';
|
2021-02-06 02:46:47 +00:00
|
|
|
|
|
|
|
export async function verifyRecaptcha(secret: string, response: string) {
|
|
|
|
const result = await getCaptchaResponse('https://www.recaptcha.net/recaptcha/api/siteverify', secret, response).catch(e => {
|
|
|
|
throw `recaptcha-request-failed: ${e}`;
|
|
|
|
});
|
|
|
|
|
|
|
|
if (result.success !== true) {
|
|
|
|
const errorCodes = result['error-codes'] ? result['error-codes']?.join(', ') : '';
|
|
|
|
throw `recaptcha-failed: ${errorCodes}`;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
export async function verifyHcaptcha(secret: string, response: string) {
|
|
|
|
const result = await getCaptchaResponse('https://hcaptcha.com/siteverify', secret, response).catch(e => {
|
|
|
|
throw `hcaptcha-request-failed: ${e}`;
|
|
|
|
});
|
|
|
|
|
|
|
|
if (result.success !== true) {
|
|
|
|
const errorCodes = result['error-codes'] ? result['error-codes']?.join(', ') : '';
|
|
|
|
throw `hcaptcha-failed: ${errorCodes}`;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
type CaptchaResponse = {
|
|
|
|
success: boolean;
|
|
|
|
'error-codes'?: string[];
|
|
|
|
};
|
|
|
|
|
|
|
|
async function getCaptchaResponse(url: string, secret: string, response: string): Promise<CaptchaResponse> {
|
|
|
|
const params = new URLSearchParams({
|
|
|
|
secret,
|
2021-12-09 14:58:30 +00:00
|
|
|
response,
|
2021-02-06 02:46:47 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
const res = await fetch(url, {
|
|
|
|
method: 'POST',
|
|
|
|
body: params,
|
|
|
|
headers: {
|
2021-12-09 14:58:30 +00:00
|
|
|
'User-Agent': config.userAgent,
|
2021-02-06 02:46:47 +00:00
|
|
|
},
|
|
|
|
timeout: 10 * 1000,
|
2021-12-09 14:58:30 +00:00
|
|
|
agent: getAgentByUrl,
|
2021-02-06 02:46:47 +00:00
|
|
|
}).catch(e => {
|
|
|
|
throw `${e.message || e}`;
|
|
|
|
});
|
|
|
|
|
|
|
|
if (!res.ok) {
|
|
|
|
throw `${res.status}`;
|
|
|
|
}
|
|
|
|
|
|
|
|
return await res.json() as CaptchaResponse;
|
|
|
|
}
|