-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.js
More file actions
99 lines (80 loc) · 3.21 KB
/
main.js
File metadata and controls
99 lines (80 loc) · 3.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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
import 'dotenv/config';
import axios from 'axios';
// CapSolver API configuration
const CAPSOLVER_API_KEY = process.env.CAPSOLVER_API_KEY;
const CREATE_TASK_URL = 'https://api.capsolver.com/createTask';
const GET_RESULT_URL = 'https://api.capsolver.com/getTaskResult';
// Target website configuration
// Replace with your target site URL
const SITE_URL = 'https://example.com';
// IMPORTANT: Proxy is REQUIRED for Cloudflare Challenge
// Use Static or Sticky proxy only (rotating proxies not supported)
// Format: ip:port:user:pass
const PROXY = process.env.PROXY || 'ip:port:user:pass';
// User agent should match what you use in your requests
const USER_AGENT = 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/142.0.0.0 Safari/537.36';
async function createTask() {
const payload = {
clientKey: CAPSOLVER_API_KEY,
task: {
type: 'AntiCloudflareTask',
websiteURL: SITE_URL,
proxy: PROXY
// Optional parameters:
// userAgent: USER_AGENT,
// html: '<!DOCTYPE html>...' // The "Just a moment..." page HTML
}
};
const response = await axios.post(CREATE_TASK_URL, payload);
const result = response.data;
if (result.errorId !== 0) {
throw new Error(`Failed to create task: ${result.errorDescription}`);
}
return result.taskId;
}
async function getTaskResult(taskId) {
const payload = {
clientKey: CAPSOLVER_API_KEY,
taskId: taskId
};
while (true) {
const response = await axios.post(GET_RESULT_URL, payload);
const result = response.data;
if (result.errorId !== 0) {
throw new Error(`Failed to get result: ${result.errorDescription}`);
}
if (result.status === 'ready') {
return result.solution;
} else if (result.status === 'failed') {
throw new Error('Task failed');
} else if (result.status === 'processing') {
console.log('Task is still processing, waiting...');
await new Promise(resolve => setTimeout(resolve, 2000));
} else {
throw new Error(`Unknown status: ${result.status}`);
}
}
}
async function main() {
if (!CAPSOLVER_API_KEY) {
console.log('Error: CAPSOLVER_API_KEY not found in .env file');
console.log('Please create a .env file with your API key:');
console.log('CAPSOLVER_API_KEY=your_api_key_here');
return;
}
console.log('Creating Cloudflare Challenge solving task...');
console.log(`Site URL: ${SITE_URL}`);
console.log(`Proxy: ${PROXY}`);
const taskId = await createTask();
console.log(`Task created successfully! Task ID: ${taskId}`);
console.log('Waiting for solution...');
const solution = await getTaskResult(taskId);
console.log('\nSolution received!');
console.log(`cf_clearance: ${solution.cookies?.cf_clearance}`);
console.log(`Token: ${solution.token}`);
console.log(`User Agent: ${solution.userAgent}`);
// Use these values in your requests:
// const cookies = { cf_clearance: solution.cookies.cf_clearance };
// const headers = { 'User-Agent': solution.userAgent };
}
main().catch(console.error);