Update index.js
Browse files
index.js
CHANGED
|
@@ -109,14 +109,15 @@ class TokenManager {
|
|
| 109 |
if (wafToken) {
|
| 110 |
Tokens[currentIndex].aws_waf_token = wafToken;
|
| 111 |
await this.updateCacheTokens();
|
| 112 |
-
this.updateRedisTokens();
|
|
|
|
| 113 |
currentIndex = (currentIndex + 1) % Tokens.length;
|
| 114 |
-
|
| 115 |
-
} else {
|
| 116 |
-
currentIndex = (currentIndex + 1) % Tokens.length;
|
| 117 |
-
await this.updateCacheTokens();
|
| 118 |
-
console.log("提取aws-waf-token失败");
|
| 119 |
}
|
|
|
|
|
|
|
|
|
|
|
|
|
| 120 |
} else {
|
| 121 |
const newCsrfToken = response.headers.get('anti-csrftoken-a2z');
|
| 122 |
const cookies = response.headers.get('set-cookie');
|
|
@@ -235,6 +236,10 @@ class Utils {
|
|
| 235 |
timeout: 30000
|
| 236 |
});
|
| 237 |
|
|
|
|
|
|
|
|
|
|
|
|
|
| 238 |
// 获取最终token
|
| 239 |
const awsWafToken = (await page.cookies()).find(
|
| 240 |
cookie => cookie.name.toLowerCase() === 'aws-waf-token'
|
|
@@ -618,10 +623,12 @@ app.post('/hf/v1/chat/completions', async (req, res) => {
|
|
| 618 |
await tokenManager.updateTokens(response);
|
| 619 |
throw new Error(`请求失败! status: ${response.statusText},已刷新验证信息,请重新请求`);
|
| 620 |
case 403:
|
| 621 |
-
|
| 622 |
-
|
| 623 |
-
|
| 624 |
-
|
|
|
|
|
|
|
| 625 |
default:
|
| 626 |
throw new Error(`请求失败! status: ${response.status}`);
|
| 627 |
}
|
|
|
|
| 109 |
if (wafToken) {
|
| 110 |
Tokens[currentIndex].aws_waf_token = wafToken;
|
| 111 |
await this.updateCacheTokens();
|
| 112 |
+
await this.updateRedisTokens();
|
| 113 |
+
console.log("✅ Capsolver方案更新token成功");
|
| 114 |
currentIndex = (currentIndex + 1) % Tokens.length;
|
| 115 |
+
return true;
|
|
|
|
|
|
|
|
|
|
|
|
|
| 116 |
}
|
| 117 |
+
console.log("❌ Capsolver方案失败,直接切换账号");
|
| 118 |
+
currentIndex = (currentIndex + 1) % Tokens.length;
|
| 119 |
+
await this.updateCacheTokens();
|
| 120 |
+
return false;
|
| 121 |
} else {
|
| 122 |
const newCsrfToken = response.headers.get('anti-csrftoken-a2z');
|
| 123 |
const cookies = response.headers.get('set-cookie');
|
|
|
|
| 236 |
timeout: 30000
|
| 237 |
});
|
| 238 |
|
| 239 |
+
// 新增:等待验证完成
|
| 240 |
+
await page.waitForTimeout(1000); // 等待1秒确保验证完成
|
| 241 |
+
await page.waitForSelector('.aws-waf-challenge', { hidden: true, timeout: 15000 });
|
| 242 |
+
|
| 243 |
// 获取最终token
|
| 244 |
const awsWafToken = (await page.cookies()).find(
|
| 245 |
cookie => cookie.name.toLowerCase() === 'aws-waf-token'
|
|
|
|
| 623 |
await tokenManager.updateTokens(response);
|
| 624 |
throw new Error(`请求失败! status: ${response.statusText},已刷新验证信息,请重新请求`);
|
| 625 |
case 403:
|
| 626 |
+
const success = await tokenManager.updateTokens(response, true);
|
| 627 |
+
if(success) {
|
| 628 |
+
console.log("✅ 验证成功,使用新token重试");
|
| 629 |
+
return retryRequest(); // 需要实现重试逻辑
|
| 630 |
+
}
|
| 631 |
+
throw new Error("验证失败,请更换账号");
|
| 632 |
default:
|
| 633 |
throw new Error(`请求失败! status: ${response.status}`);
|
| 634 |
}
|