-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathcaptcha-solver.js
More file actions
78 lines (63 loc) · 2.21 KB
/
captcha-solver.js
File metadata and controls
78 lines (63 loc) · 2.21 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
import axios from 'axios';
import { config } from './config.js';
export class CaptchaSolver {
constructor() {
this.apiKey = config.nextCaptcha.apiKey;
this.apiUrl = config.nextCaptcha.apiUrl;
}
async solveCaptcha(siteKey, pageUrl, captchaType = 'recaptcha_v2') {
if (!this.apiKey) {
throw new Error('NextCaptcha API key not configured');
}
console.log('Submitting captcha to NextCaptcha...');
const taskId = await this.createTask(siteKey, pageUrl, captchaType);
const solution = await this.getTaskResult(taskId);
return solution;
}
async createTask(siteKey, pageUrl, captchaType) {
try {
const response = await axios.post(`${this.apiUrl}/createTask`, {
clientKey: this.apiKey,
task: {
type: captchaType === 'recaptcha_v3' ? 'RecaptchaV3TaskProxyless' : 'RecaptchaV2TaskProxyless',
websiteURL: pageUrl,
websiteKey: siteKey,
...(captchaType === 'recaptcha_v3' && { minScore: 0.7 })
}
});
if (response.data.errorId !== 0) {
throw new Error(`NextCaptcha error: ${response.data.errorDescription}`);
}
return response.data.taskId;
} catch (error) {
throw new Error(`Failed to create captcha task: ${error.message}`);
}
}
async getTaskResult(taskId, maxAttempts = 60) {
for (let i = 0; i < maxAttempts; i++) {
await this.sleep(3000);
try {
const response = await axios.post(`${this.apiUrl}/getTaskResult`, {
clientKey: this.apiKey,
taskId: taskId
});
if (response.data.status === 'ready') {
console.log('Captcha solved successfully!');
return response.data.solution.gRecaptchaResponse;
}
if (response.data.status === 'failed') {
throw new Error('Captcha solving failed');
}
console.log(`Waiting for captcha solution... (${i + 1}/${maxAttempts})`);
} catch (error) {
if (i === maxAttempts - 1) {
throw new Error(`Failed to get captcha result: ${error.message}`);
}
}
}
throw new Error('Captcha solving timeout');
}
sleep(ms) {
return new Promise(resolve => setTimeout(resolve, ms));
}
}