feat(CaptchaSolver): using lib node-capmonster

This commit is contained in:
Elysia 2023-04-22 00:00:55 +07:00
parent 2357170e0f
commit 8f43b9028e

View File

@ -1,6 +1,5 @@
'use strict'; 'use strict';
const { setTimeout } = require('node:timers');
const axios = require('axios');
module.exports = class CaptchaSolver { module.exports = class CaptchaSolver {
constructor(service, key, defaultCaptchaSolver) { constructor(service, key, defaultCaptchaSolver) {
this.service = 'custom'; this.service = 'custom';
@ -44,51 +43,30 @@ module.exports = class CaptchaSolver {
} }
case 'capmonster': { case 'capmonster': {
if (!key || typeof key !== 'string') throw new Error('Capmonster key is not provided'); if (!key || typeof key !== 'string') throw new Error('Capmonster key is not provided');
try {
const { HCaptchaTask } = require('node-capmonster');
this.service = 'capmonster'; this.service = 'capmonster';
this.key = key; this.key = key;
const client = new HCaptchaTask(this.key);
this.solve = (captchaData, userAgent) => this.solve = (captchaData, userAgent) =>
// https://github.com/aiko-chan-ai/discord.js-selfbot-v13/issues/548#issuecomment-1452091328 new Promise((resolve, reject) => {
// eslint-disable-next-line no-async-promise-executor if (userAgent) client.setUserAgent(userAgent);
new Promise(async (resolve, reject) => { client
try { .createWithTask(
const createTaskResponse = await axios.post( client.task({
'https://api.capmonster.cloud/createTask',
{
clientKey: this.key,
task: {
type: 'HCaptchaTask',
websiteURL: 'https://discord.com/channels/@me', websiteURL: 'https://discord.com/channels/@me',
websiteKey: captchaData.captcha_sitekey, websiteKey: captchaData.captcha_sitekey,
data: captchaData.captcha_rqdata,
isInvisible: !!captchaData.captcha_rqdata, isInvisible: !!captchaData.captcha_rqdata,
userAgent: userAgent, data: captchaData.captcha_rqdata,
}, }),
}, )
{ .then(id => client.joinTaskResult(id))
headers: { .then(result => resolve(result.gRecaptchaResponse))
'Content-Type': 'application/json', .catch(reject);
'user-agent': userAgent,
},
},
);
const taskId = createTaskResponse.data.taskId;
let getResults = { status: 'processing' };
while (getResults.status === 'processing') {
const getResultsResponse = await axios.post('https://api.capmonster.cloud/getTaskResult', {
clientKey: this.key,
taskId,
}); });
getResults = getResultsResponse.data;
await new Promise(resolve_ => setTimeout(resolve_, 1500).unref());
}
const solution = getResults.solution.gRecaptchaResponse;
return resolve(await solution);
} catch (e) { } catch (e) {
// !console.error(e); throw this._missingModule('node-capmonster');
reject(new Error(`Capmonster error: ${e.message}`, e?.response?.data));
} }
return true;
});
break; break;
} }
default: { default: {