Update index.js
Browse files
index.js
CHANGED
|
@@ -85,6 +85,56 @@ var RedisClient = class {
|
|
| 85 |
}
|
| 86 |
}
|
| 87 |
};
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 88 |
class TokenManager {
|
| 89 |
async updateRedisTokens() {
|
| 90 |
await redisClient.set(`tokens_${currentIndex}`, JSON.stringify(Tokens[currentIndex]));
|
|
@@ -102,20 +152,78 @@ class TokenManager {
|
|
| 102 |
CONFIG.DEFAULT_HEADERS["request-id"] = `request-id-${Utils.uuidv4()}`;
|
| 103 |
}
|
| 104 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 105 |
async updateTokens(response, isWaf = false) {
|
| 106 |
if (isWaf) {
|
| 107 |
-
|
| 108 |
-
|
| 109 |
-
|
| 110 |
-
|
| 111 |
-
|
| 112 |
-
|
| 113 |
-
|
| 114 |
-
|
| 115 |
-
|
| 116 |
-
|
| 117 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 118 |
}
|
|
|
|
|
|
|
|
|
|
| 119 |
} else {
|
| 120 |
const newCsrfToken = response.headers.get('anti-csrftoken-a2z');
|
| 121 |
const cookies = response.headers.get('set-cookie');
|
|
@@ -134,7 +242,6 @@ class TokenManager {
|
|
| 134 |
}
|
| 135 |
}
|
| 136 |
|
| 137 |
-
|
| 138 |
class Utils {
|
| 139 |
static async getRandomUserAgent() {
|
| 140 |
try {
|
|
|
|
| 85 |
}
|
| 86 |
}
|
| 87 |
};
|
| 88 |
+
|
| 89 |
+
// 添加 Capsolver 相关配置
|
| 90 |
+
const CAPSOLVER_CONFIG = {
|
| 91 |
+
API_KEY: process.env.CAPSOLVER_KEY || "YOUR_CAPSOLVER_KEY",
|
| 92 |
+
API_URL: "https://api.capsolver.com"
|
| 93 |
+
};
|
| 94 |
+
|
| 95 |
+
class CapsolverClient {
|
| 96 |
+
constructor(apiKey) {
|
| 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: {
|
| 104 |
+
'Content-Type': 'application/json'
|
| 105 |
+
},
|
| 106 |
+
body: JSON.stringify({
|
| 107 |
+
clientKey: this.apiKey,
|
| 108 |
+
task: {
|
| 109 |
+
type: "AwsWafCaptcha",
|
| 110 |
+
websiteURL: websiteURL,
|
| 111 |
+
websiteKey: websiteKey,
|
| 112 |
+
proxy: {
|
| 113 |
+
// 如果需要代理可以在这里配置
|
| 114 |
+
}
|
| 115 |
+
}
|
| 116 |
+
})
|
| 117 |
+
});
|
| 118 |
+
|
| 119 |
+
return await response.json();
|
| 120 |
+
}
|
| 121 |
+
|
| 122 |
+
async getTaskResult(taskId) {
|
| 123 |
+
const response = await fetch(`${CAPSOLVER_CONFIG.API_URL}/getTaskResult`, {
|
| 124 |
+
method: 'POST',
|
| 125 |
+
headers: {
|
| 126 |
+
'Content-Type': 'application/json'
|
| 127 |
+
},
|
| 128 |
+
body: JSON.stringify({
|
| 129 |
+
clientKey: this.apiKey,
|
| 130 |
+
taskId: taskId
|
| 131 |
+
})
|
| 132 |
+
});
|
| 133 |
+
|
| 134 |
+
return await response.json();
|
| 135 |
+
}
|
| 136 |
+
}
|
| 137 |
+
|
| 138 |
class TokenManager {
|
| 139 |
async updateRedisTokens() {
|
| 140 |
await redisClient.set(`tokens_${currentIndex}`, JSON.stringify(Tokens[currentIndex]));
|
|
|
|
| 152 |
CONFIG.DEFAULT_HEADERS["request-id"] = `request-id-${Utils.uuidv4()}`;
|
| 153 |
}
|
| 154 |
|
| 155 |
+
async solveCaptcha(url) {
|
| 156 |
+
const capsolverClient = new CapsolverClient(CAPSOLVER_CONFIG.API_KEY);
|
| 157 |
+
|
| 158 |
+
try {
|
| 159 |
+
// 创建验证码任务
|
| 160 |
+
const createTaskResponse = await capsolverClient.createTask(
|
| 161 |
+
"partyrock.aws",
|
| 162 |
+
// 从URL中提取websiteKey
|
| 163 |
+
url.split('/')[2].split('.')[0]
|
| 164 |
+
);
|
| 165 |
+
|
| 166 |
+
if (createTaskResponse.errorId > 0) {
|
| 167 |
+
throw new Error(`创建验证码任务失败: ${createTaskResponse.errorDescription}`);
|
| 168 |
+
}
|
| 169 |
+
|
| 170 |
+
// 轮询获取结果
|
| 171 |
+
let taskResult;
|
| 172 |
+
for (let i = 0; i < 30; i++) {
|
| 173 |
+
await new Promise(resolve => setTimeout(resolve, 2000)); // 等待2秒
|
| 174 |
+
taskResult = await capsolverClient.getTaskResult(createTaskResponse.taskId);
|
| 175 |
+
|
| 176 |
+
if (taskResult.status === 'ready') {
|
| 177 |
+
break;
|
| 178 |
+
}
|
| 179 |
+
}
|
| 180 |
+
|
| 181 |
+
if (!taskResult || taskResult.status !== 'ready') {
|
| 182 |
+
throw new Error('验证码解决超时');
|
| 183 |
+
}
|
| 184 |
+
|
| 185 |
+
return taskResult.solution;
|
| 186 |
+
|
| 187 |
+
} catch (error) {
|
| 188 |
+
console.error('验证码解决失败:', error);
|
| 189 |
+
throw error;
|
| 190 |
+
}
|
| 191 |
+
}
|
| 192 |
+
|
| 193 |
async updateTokens(response, isWaf = false) {
|
| 194 |
if (isWaf) {
|
| 195 |
+
try {
|
| 196 |
+
// 获取验证码URL
|
| 197 |
+
const captchaUrl = response.headers.get('x-amz-waf-challenge-url');
|
| 198 |
+
if (captchaUrl) {
|
| 199 |
+
const solution = await this.solveCaptcha(captchaUrl);
|
| 200 |
+
|
| 201 |
+
// 使用解决方案更新token
|
| 202 |
+
const verifyResponse = await fetch(captchaUrl, {
|
| 203 |
+
method: 'POST',
|
| 204 |
+
headers: {
|
| 205 |
+
'Content-Type': 'application/json',
|
| 206 |
+
...CONFIG.DEFAULT_HEADERS
|
| 207 |
+
},
|
| 208 |
+
body: JSON.stringify(solution)
|
| 209 |
+
});
|
| 210 |
+
|
| 211 |
+
if (verifyResponse.ok) {
|
| 212 |
+
const wafToken = await Utils.extractWaf();
|
| 213 |
+
if (wafToken) {
|
| 214 |
+
Tokens[currentIndex].aws_waf_token = wafToken;
|
| 215 |
+
await this.updateCacheTokens();
|
| 216 |
+
this.updateRedisTokens();
|
| 217 |
+
console.log("成功更新 aws-waf-token");
|
| 218 |
+
}
|
| 219 |
+
}
|
| 220 |
+
}
|
| 221 |
+
} catch (error) {
|
| 222 |
+
console.error('处理验证码失败:', error);
|
| 223 |
}
|
| 224 |
+
|
| 225 |
+
currentIndex = (currentIndex + 1) % Tokens.length;
|
| 226 |
+
|
| 227 |
} else {
|
| 228 |
const newCsrfToken = response.headers.get('anti-csrftoken-a2z');
|
| 229 |
const cookies = response.headers.get('set-cookie');
|
|
|
|
| 242 |
}
|
| 243 |
}
|
| 244 |
|
|
|
|
| 245 |
class Utils {
|
| 246 |
static async getRandomUserAgent() {
|
| 247 |
try {
|