-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathcloudflare_challenge.js
More file actions
203 lines (162 loc) · 5.74 KB
/
cloudflare_challenge.js
File metadata and controls
203 lines (162 loc) · 5.74 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
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
/**
* Cloudflare Challenge Solver Demo (Node.js)
* Solves Cloudflare Challenge (5-second shield) using CapSolver API
*
* Requirements:
* - Static or Sticky proxy (MANDATORY - rotating proxies won't work)
* - TLS fingerprinting, matching headers, header order, and User-Agent
* - Chrome User-Agent recommended
*
* Returns: cf_clearance cookie and token
*/
const axios = require('axios');
// ============== CONFIGURATION ==============
const CAPSOLVER_API_KEY = 'YOUR_API_KEY_HERE';
const WEBSITE_URL = 'https://www.example.com';
const PROXY = 'http://user:pass@host:port';
const USER_AGENT = 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/131.0.0.0 Safari/537.36';
const HTML_CONTENT = '';
// ============================================
async function createTask(apiKey, taskData) {
const response = await axios.post('https://api.capsolver.com/createTask', {
clientKey: apiKey,
task: taskData
});
return response.data;
}
async function getTaskResult(apiKey, taskId) {
const response = await axios.post('https://api.capsolver.com/getTaskResult', {
clientKey: apiKey,
taskId: taskId
});
return response.data;
}
function sleep(ms) {
return new Promise(resolve => setTimeout(resolve, ms));
}
async function solveCloudflareChallenge(apiKey, websiteUrl, proxy, userAgent = null, html = null) {
console.log('='.repeat(70));
console.log('Cloudflare Challenge Solver (Node.js with tls-client)');
console.log('='.repeat(70));
if (!apiKey || apiKey === 'YOUR_API_KEY_HERE') {
throw new Error('Please set your CapSolver API key');
}
if (!websiteUrl || websiteUrl === 'https://www.example.com') {
throw new Error('Please set the target website URL');
}
if (!proxy || proxy === 'http://user:pass@host:port') {
throw new Error('Proxy is MANDATORY for Cloudflare Challenge. Use Static or Sticky proxy.');
}
const taskData = {
type: 'AntiCloudflareTask',
websiteURL: websiteUrl,
proxy: proxy
};
if (userAgent) {
taskData.userAgent = userAgent;
}
if (html) {
taskData.html = html;
}
console.log('\n[1/3] Creating task...');
console.log(` Website: ${websiteUrl}`);
console.log(` Proxy: ${proxy.length > 30 ? proxy.substring(0, 30) + '...' : proxy}`);
console.log(` User-Agent: ${userAgent ? 'Custom' : 'Default'}`);
console.log(` HTML provided: ${html ? 'Yes' : 'No'}`);
const createResult = await createTask(apiKey, taskData);
if (createResult.errorId !== 0) {
throw new Error(`Failed to create task: ${createResult.errorDescription || 'Unknown error'}`);
}
const taskId = createResult.taskId;
console.log(` ✓ Task created: ${taskId}`);
console.log('\n[2/3] Waiting for solution...');
console.log(' This may take 2-20 seconds depending on the website and proxy...');
const maxAttempts = 60;
let attempt = 0;
while (attempt < maxAttempts) {
await sleep(2000);
attempt++;
const result = await getTaskResult(apiKey, taskId);
const status = result.status;
if (status === 'ready') {
console.log(` ✓ Solution received after ${attempt * 2} seconds!`);
return result.solution;
} else if (status === 'failed') {
const errorDesc = result.errorDescription || 'Unknown error';
throw new Error(`Task failed: ${errorDesc}`);
} else if (status === 'processing') {
if (attempt % 5 === 0) {
console.log(` ... Processing (attempt ${attempt}/${maxAttempts})`);
}
}
}
throw new Error('Timeout: Failed to get solution within maximum time');
}
function printImportantNotes() {
console.log('\n' + '='.repeat(70));
console.log('IMPORTANT: Using cf_clearance Cookie');
console.log('='.repeat(70));
console.log(`
⚠️ CRITICAL REQUIREMENTS for using the cf_clearance cookie:
1. TLS Fingerprinting:
- Use TLS client libraries (tls-client, curl-impersonate, etc.)
- Match Chrome browser TLS fingerprint
- Standard HTTP libraries will likely fail
2. HTTP Headers:
- Use realistic browser headers
- Match the User-Agent returned by CapSolver (EXACT match)
- Include common headers: Accept, Accept-Language, Accept-Encoding, etc.
3. Header Order:
- Headers must be sent in browser-like order
- Cloudflare checks header ordering
- Use libraries that preserve header order
4. User-Agent Matching:
- MUST use the EXACT User-Agent returned in solution
- Don't modify or use a different User-Agent
- Case-sensitive match required
5. Cookie Format:
- Send as: Cookie: cf_clearance=VALUE
- Include other cookies if present
- Maintain cookie throughout session
Recommended Libraries:
Node.js: tls-client, node-curl-impersonate
Python: curl_cffi, tls-client
Go: http2, cycletls
Without proper TLS fingerprinting and header matching,
the cf_clearance cookie will be rejected by Cloudflare.
`);
}
async function main() {
try {
const solution = await solveCloudflareChallenge(
CAPSOLVER_API_KEY,
WEBSITE_URL,
PROXY,
USER_AGENT,
HTML_CONTENT || null
);
console.log('\n' + '='.repeat(70));
console.log('SOLUTION DETAILS');
console.log('='.repeat(70));
const cfClearance = solution.cookies?.cf_clearance;
const token = solution.token;
const userAgent = solution.userAgent;
console.log('\ncf_clearance cookie:');
console.log(` ${cfClearance}`);
console.log('\nToken:');
console.log(` ${token}`);
console.log('\nUser-Agent:');
console.log(` ${userAgent}`);
printImportantNotes();
console.log('\n' + '='.repeat(70));
console.log('COMPLETE');
console.log('='.repeat(70));
} catch (error) {
console.error(`\n✗ Error: ${error.message}`);
process.exit(1);
}
}
if (require.main === module) {
main();
}
module.exports = { solveCloudflareChallenge };