Spaces:
Paused
Paused
Update index.js
Browse files
index.js
CHANGED
|
@@ -20,10 +20,6 @@ const CONFIG = {
|
|
| 20 |
RedisUrl: process.env.RedisUrl,
|
| 21 |
RedisToken: process.env.RedisToken
|
| 22 |
},
|
| 23 |
-
RETRY: {
|
| 24 |
-
MAX_ATTEMPTS: 1,
|
| 25 |
-
DELAY_BASE: 1000
|
| 26 |
-
},
|
| 27 |
SERVER: {
|
| 28 |
PORT: process.env.PORT || 3000,
|
| 29 |
BODY_LIMIT: '5mb'
|
|
@@ -101,7 +97,7 @@ class TokenManager {
|
|
| 101 |
sessionId = Utils.uuidv4();
|
| 102 |
CONFIG.DEFAULT_HEADERS["anti-csrftoken-a2z"] = Tokens[currentIndex].anti_csrftoken_a2z;
|
| 103 |
CONFIG.DEFAULT_HEADERS.Cookie = `idToken=${Tokens[currentIndex].idToken}; pr_refresh_token=${Tokens[currentIndex].pr_refresh_token};aws-waf-token=${Tokens[currentIndex].aws_waf_token};cwr_s=${Tokens[currentIndex].cwr_s};cwr_u=${sessionId}`;
|
| 104 |
-
CONFIG.DEFAULT_HEADERS.referer = Tokens[currentIndex].
|
| 105 |
}
|
| 106 |
|
| 107 |
async updateTokens(response, isWaf = false) {
|
|
@@ -171,7 +167,7 @@ class Utils {
|
|
| 171 |
await page.setUserAgent(
|
| 172 |
CONFIG.DEFAULT_HEADERS["User-Agent"]
|
| 173 |
)
|
| 174 |
-
await page.goto(Tokens[currentIndex].
|
| 175 |
waitUntil: 'networkidle2',
|
| 176 |
timeout: 30000
|
| 177 |
});
|
|
@@ -256,7 +252,7 @@ async function initializeService() {
|
|
| 256 |
? JSON.parse(checkRedis)
|
| 257 |
: checkRedis;
|
| 258 |
Tokens.push({
|
| 259 |
-
|
| 260 |
anti_csrftoken_a2z: parsedRedis.anti_csrftoken_a2z,
|
| 261 |
pr_refresh_token: parsedRedis.pr_refresh_token,
|
| 262 |
aws_waf_token: parsedRedis.aws_waf_token,
|
|
@@ -269,18 +265,18 @@ async function initializeService() {
|
|
| 269 |
}
|
| 270 |
} else {
|
| 271 |
console.log(index, '没有缓存,开始提取环境变量');
|
| 272 |
-
const
|
| 273 |
const anti_csrftoken_a2z = process.env[`AUTH_TOKENS_${index}_ANTI_CSRF_TOKEN`];
|
| 274 |
const cookie = process.env[`AUTH_TOKENS_${index}_COOKIE`];
|
| 275 |
|
| 276 |
-
if (!
|
| 277 |
break;
|
| 278 |
}
|
| 279 |
const cookies = await Utils.extractTokens(cookie);
|
| 280 |
|
| 281 |
-
if (
|
| 282 |
Tokens.push({
|
| 283 |
-
|
| 284 |
anti_csrftoken_a2z,
|
| 285 |
pr_refresh_token: cookies["pr_refresh_token"],
|
| 286 |
aws_waf_token: cookies["aws-waf-token"],
|
|
@@ -530,7 +526,7 @@ app.post('/hf/v1/chat/completions', async (req, res) => {
|
|
| 530 |
try {
|
| 531 |
console.log("开始请求");
|
| 532 |
//发送请求
|
| 533 |
-
var response = await fetch(`https://
|
| 534 |
method: "POST",
|
| 535 |
headers: {
|
| 536 |
...CONFIG.DEFAULT_HEADERS
|
|
@@ -564,9 +560,7 @@ app.post('/hf/v1/chat/completions', async (req, res) => {
|
|
| 564 |
throw new Error(`请求失败! status: ${response.statusText},已刷新验证信息,请重新请求`);
|
| 565 |
case 403:
|
| 566 |
console.log("请求被阻止");
|
| 567 |
-
console.log(JSON.stringify(CONFIG.DEFAULT_HEADERS, null, 2));
|
| 568 |
CONFIG.DEFAULT_HEADERS["User-Agent"] = await Utils.getRandomUserAgent();
|
| 569 |
-
console.log(response.headers);
|
| 570 |
throw new Error(`请求失败! status: ${response.statusText},请重新请求,如果多次失败,请重新更换token`);
|
| 571 |
default:
|
| 572 |
throw new Error(`请求失败! status: ${response.status}`);
|
|
|
|
| 20 |
RedisUrl: process.env.RedisUrl,
|
| 21 |
RedisToken: process.env.RedisToken
|
| 22 |
},
|
|
|
|
|
|
|
|
|
|
|
|
|
| 23 |
SERVER: {
|
| 24 |
PORT: process.env.PORT || 3000,
|
| 25 |
BODY_LIMIT: '5mb'
|
|
|
|
| 97 |
sessionId = Utils.uuidv4();
|
| 98 |
CONFIG.DEFAULT_HEADERS["anti-csrftoken-a2z"] = Tokens[currentIndex].anti_csrftoken_a2z;
|
| 99 |
CONFIG.DEFAULT_HEADERS.Cookie = `idToken=${Tokens[currentIndex].idToken}; pr_refresh_token=${Tokens[currentIndex].pr_refresh_token};aws-waf-token=${Tokens[currentIndex].aws_waf_token};cwr_s=${Tokens[currentIndex].cwr_s};cwr_u=${sessionId}`;
|
| 100 |
+
CONFIG.DEFAULT_HEADERS.referer = Tokens[currentIndex].refererUrl;
|
| 101 |
}
|
| 102 |
|
| 103 |
async updateTokens(response, isWaf = false) {
|
|
|
|
| 167 |
await page.setUserAgent(
|
| 168 |
CONFIG.DEFAULT_HEADERS["User-Agent"]
|
| 169 |
)
|
| 170 |
+
await page.goto(Tokens[currentIndex].refererUrl, {
|
| 171 |
waitUntil: 'networkidle2',
|
| 172 |
timeout: 30000
|
| 173 |
});
|
|
|
|
| 252 |
? JSON.parse(checkRedis)
|
| 253 |
: checkRedis;
|
| 254 |
Tokens.push({
|
| 255 |
+
refererUrl: parsedRedis.refererUrl,
|
| 256 |
anti_csrftoken_a2z: parsedRedis.anti_csrftoken_a2z,
|
| 257 |
pr_refresh_token: parsedRedis.pr_refresh_token,
|
| 258 |
aws_waf_token: parsedRedis.aws_waf_token,
|
|
|
|
| 265 |
}
|
| 266 |
} else {
|
| 267 |
console.log(index, '没有缓存,开始提取环境变量');
|
| 268 |
+
const refererUrl = process.env[`AUTH_TOKENS_${index}_REFRESH_URL`];
|
| 269 |
const anti_csrftoken_a2z = process.env[`AUTH_TOKENS_${index}_ANTI_CSRF_TOKEN`];
|
| 270 |
const cookie = process.env[`AUTH_TOKENS_${index}_COOKIE`];
|
| 271 |
|
| 272 |
+
if (!refererUrl && !anti_csrftoken_a2z && !cookie) {
|
| 273 |
break;
|
| 274 |
}
|
| 275 |
const cookies = await Utils.extractTokens(cookie);
|
| 276 |
|
| 277 |
+
if (refererUrl && anti_csrftoken_a2z && cookie) {
|
| 278 |
Tokens.push({
|
| 279 |
+
refererUrl,
|
| 280 |
anti_csrftoken_a2z,
|
| 281 |
pr_refresh_token: cookies["pr_refresh_token"],
|
| 282 |
aws_waf_token: cookies["aws-waf-token"],
|
|
|
|
| 526 |
try {
|
| 527 |
console.log("开始请求");
|
| 528 |
//发送请求
|
| 529 |
+
var response = await fetch(`https://partyrock.aws/stream/getCompletion`, {
|
| 530 |
method: "POST",
|
| 531 |
headers: {
|
| 532 |
...CONFIG.DEFAULT_HEADERS
|
|
|
|
| 560 |
throw new Error(`请求失败! status: ${response.statusText},已刷新验证信息,请重新请求`);
|
| 561 |
case 403:
|
| 562 |
console.log("请求被阻止");
|
|
|
|
| 563 |
CONFIG.DEFAULT_HEADERS["User-Agent"] = await Utils.getRandomUserAgent();
|
|
|
|
| 564 |
throw new Error(`请求失败! status: ${response.statusText},请重新请求,如果多次失败,请重新更换token`);
|
| 565 |
default:
|
| 566 |
throw new Error(`请求失败! status: ${response.status}`);
|