bobocup commited on
Commit
fbc33ab
·
verified ·
1 Parent(s): e30c1ee

Update index.js

Browse files
Files changed (1) hide show
  1. index.js +91 -36
index.js CHANGED
@@ -97,7 +97,7 @@ class CapsolverClient {
97
  this.apiKey = apiKey;
98
  }
99
 
100
- async createTask(websiteURL, websiteKey) {
101
  const response = await fetch(`${CAPSOLVER_CONFIG.API_URL}/createTask`, {
102
  method: 'POST',
103
  headers: {
@@ -109,6 +109,9 @@ class CapsolverClient {
109
  type: "AwsWafCaptcha",
110
  websiteURL: websiteURL,
111
  websiteKey: websiteKey,
 
 
 
112
  proxy: {
113
  // 如果需要代理可以在这里配置
114
  }
@@ -152,26 +155,68 @@ class TokenManager {
152
  CONFIG.DEFAULT_HEADERS["request-id"] = `request-id-${Utils.uuidv4()}`;
153
  }
154
 
155
- async solveCaptcha(url) {
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
156
  console.log('开始处理验证码...');
157
- console.log('验证码URL:', url);
158
-
159
  const capsolverClient = new CapsolverClient(CAPSOLVER_CONFIG.API_KEY);
160
 
161
  try {
162
- const websiteKey = url.split('/')[2].split('.')[0];
163
  console.log('提取的websiteKey:', websiteKey);
164
 
165
  console.log('创建验证码任务...');
166
  const createTaskResponse = await capsolverClient.createTask(
167
  "partyrock.aws",
168
- websiteKey
 
 
 
 
 
169
  );
170
 
171
  console.log('创建任务响应:', JSON.stringify(createTaskResponse, null, 2));
172
 
173
  if (createTaskResponse.errorId > 0) {
174
- console.error('创建验证码任务失败:', createTaskResponse.errorDescription);
175
  throw new Error(`创建验证码任务失败: ${createTaskResponse.errorDescription}`);
176
  }
177
 
@@ -209,57 +254,67 @@ class TokenManager {
209
  try {
210
  console.log('Response headers:', JSON.stringify(Object.fromEntries([...response.headers]), null, 2));
211
 
212
- // 获取验证码URL
213
- const captchaUrl = response.headers.get('x-amz-waf-challenge-url');
214
- console.log('验证码URL:', captchaUrl);
215
-
216
- if (captchaUrl) {
217
- console.log('开始处理WAF验证码...');
218
- const solution = await this.solveCaptcha(captchaUrl);
219
-
220
- console.log('提交验证码解决方案...');
221
- const verifyResponse = await fetch(captchaUrl, {
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
222
  method: 'POST',
223
  headers: {
224
  'Content-Type': 'application/json',
225
- ...CONFIG.DEFAULT_HEADERS
 
226
  },
227
- body: JSON.stringify(solution)
 
 
228
  });
229
 
230
- console.log('验证响应状态:', verifyResponse.status);
231
- console.log('验证响应headers:', JSON.stringify(Object.fromEntries([...verifyResponse.headers]), null, 2));
232
-
233
- if (verifyResponse.ok) {
234
  console.log('验证成功,更新WAF token...');
235
  const wafToken = await Utils.extractWaf();
236
- console.log('提取的WAF token:', wafToken);
237
-
238
  if (wafToken) {
239
  Tokens[currentIndex].aws_waf_token = wafToken;
240
  await this.updateCacheTokens();
241
  this.updateRedisTokens();
242
  console.log("成功更新 aws-waf-token");
243
- } else {
244
- console.error('无法提取WAF token');
245
  }
246
- } else {
247
- console.error('验证响应不成功');
248
- const responseText = await verifyResponse.text();
249
- console.error('验证响应内容:', responseText);
250
  }
251
- } else {
252
- console.error('未找到验证码URL');
253
- console.log('完整响应:', await response.text());
254
  }
255
  } catch (error) {
256
  console.error('处理验证码过程中发生错误:', error);
257
  console.error('错误堆栈:', error.stack);
258
  }
259
 
260
- console.log(`切换到下一个token (当前索引: ${currentIndex})`);
261
  currentIndex = (currentIndex + 1) % Tokens.length;
262
- console.log(`新的token索引: ${currentIndex}`);
263
 
264
  } else {
265
  const newCsrfToken = response.headers.get('anti-csrftoken-a2z');
 
97
  this.apiKey = apiKey;
98
  }
99
 
100
+ async createTask(websiteURL, websiteKey, extraParams = {}) {
101
  const response = await fetch(`${CAPSOLVER_CONFIG.API_URL}/createTask`, {
102
  method: 'POST',
103
  headers: {
 
109
  type: "AwsWafCaptcha",
110
  websiteURL: websiteURL,
111
  websiteKey: websiteKey,
112
+ gokuProps: extraParams.gokuProps,
113
+ captchaUrl: extraParams.captchaUrl,
114
+ challengeUrl: extraParams.challengeUrl,
115
  proxy: {
116
  // 如果需要代理可以在这里配置
117
  }
 
155
  CONFIG.DEFAULT_HEADERS["request-id"] = `request-id-${Utils.uuidv4()}`;
156
  }
157
 
158
+ async extractChallengeInfo(html) {
159
+ console.log('开始提取验证码信息...');
160
+ try {
161
+ // 提取 gokuProps
162
+ const gokuPropsMatch = html.match(/window\.gokuProps\s*=\s*({[\s\S]*?});/);
163
+ if (!gokuPropsMatch) {
164
+ console.error('未找到 gokuProps');
165
+ return null;
166
+ }
167
+
168
+ // 提取验证码和challenge URL
169
+ const captchaUrlMatch = html.match(/src="(https:\/\/[^"]+\/captcha\.js)"/);
170
+ const challengeUrlMatch = html.match(/src="(https:\/\/[^"]+\/challenge\.js)"/);
171
+
172
+ if (!captchaUrlMatch || !challengeUrlMatch) {
173
+ console.error('未找到验证码或challenge URL');
174
+ return null;
175
+ }
176
+
177
+ const gokuProps = JSON.parse(gokuPropsMatch[1]);
178
+ const captchaUrl = captchaUrlMatch[1].replace('/captcha.js', '');
179
+ const challengeUrl = challengeUrlMatch[1].replace('/challenge.js', '');
180
+
181
+ console.log('提取的信息:', {
182
+ gokuProps,
183
+ captchaUrl,
184
+ challengeUrl
185
+ });
186
+
187
+ return {
188
+ gokuProps,
189
+ captchaUrl,
190
+ challengeUrl
191
+ };
192
+ } catch (error) {
193
+ console.error('提取验证码信息时出错:', error);
194
+ return null;
195
+ }
196
+ }
197
+
198
+ async solveCaptcha(challengeInfo) {
199
  console.log('开始处理验证码...');
 
 
200
  const capsolverClient = new CapsolverClient(CAPSOLVER_CONFIG.API_KEY);
201
 
202
  try {
203
+ const websiteKey = challengeInfo.captchaUrl.split('/')[2].split('.')[0];
204
  console.log('提取的websiteKey:', websiteKey);
205
 
206
  console.log('创建验证码任务...');
207
  const createTaskResponse = await capsolverClient.createTask(
208
  "partyrock.aws",
209
+ websiteKey,
210
+ {
211
+ gokuProps: challengeInfo.gokuProps,
212
+ captchaUrl: challengeInfo.captchaUrl,
213
+ challengeUrl: challengeInfo.challengeUrl
214
+ }
215
  );
216
 
217
  console.log('创建任务响应:', JSON.stringify(createTaskResponse, null, 2));
218
 
219
  if (createTaskResponse.errorId > 0) {
 
220
  throw new Error(`创建验证码任务失败: ${createTaskResponse.errorDescription}`);
221
  }
222
 
 
254
  try {
255
  console.log('Response headers:', JSON.stringify(Object.fromEntries([...response.headers]), null, 2));
256
 
257
+ // 获取HTML内容
258
+ const html = await response.text();
259
+ console.log('收到HTML响应');
260
+
261
+ // 提取验证码信息
262
+ const challengeInfo = await this.extractChallengeInfo(html);
263
+ if (!challengeInfo) {
264
+ throw new Error('无法提取验证码信息');
265
+ }
266
+
267
+ console.log('开始处理WAF验证码...');
268
+ const solution = await this.solveCaptcha(challengeInfo);
269
+
270
+ console.log('提交验证码解决方案...');
271
+ const verifyResponse = await fetch(`${challengeInfo.captchaUrl}/verify`, {
272
+ method: 'POST',
273
+ headers: {
274
+ 'Content-Type': 'application/json',
275
+ 'Referer': 'https://partyrock.aws/',
276
+ 'Origin': 'https://partyrock.aws'
277
+ },
278
+ body: JSON.stringify(solution)
279
+ });
280
+
281
+ console.log('验证响应状态:', verifyResponse.status);
282
+ console.log('验证响应headers:', JSON.stringify(Object.fromEntries([...verifyResponse.headers]), null, 2));
283
+
284
+ if (verifyResponse.ok) {
285
+ const voucherResponse = await verifyResponse.json();
286
+ console.log('获取到voucher:', voucherResponse);
287
+
288
+ // 提交voucher
289
+ const submitResponse = await fetch(`${challengeInfo.challengeUrl}/voucher`, {
290
  method: 'POST',
291
  headers: {
292
  'Content-Type': 'application/json',
293
+ 'Referer': 'https://partyrock.aws/',
294
+ 'Origin': 'https://partyrock.aws'
295
  },
296
+ body: JSON.stringify({
297
+ voucher: voucherResponse.voucher
298
+ })
299
  });
300
 
301
+ if (submitResponse.ok) {
 
 
 
302
  console.log('验证成功,更新WAF token...');
303
  const wafToken = await Utils.extractWaf();
 
 
304
  if (wafToken) {
305
  Tokens[currentIndex].aws_waf_token = wafToken;
306
  await this.updateCacheTokens();
307
  this.updateRedisTokens();
308
  console.log("成功更新 aws-waf-token");
 
 
309
  }
 
 
 
 
310
  }
 
 
 
311
  }
312
  } catch (error) {
313
  console.error('处理验证码过程中发生错误:', error);
314
  console.error('错误堆栈:', error.stack);
315
  }
316
 
 
317
  currentIndex = (currentIndex + 1) % Tokens.length;
 
318
 
319
  } else {
320
  const newCsrfToken = response.headers.get('anti-csrftoken-a2z');