Spaces:
Paused
Paused
Update index.js
Browse files
index.js
CHANGED
|
@@ -4,6 +4,7 @@ import cors from 'cors';
|
|
| 4 |
import dotenv from 'dotenv';
|
| 5 |
import puppeteer from 'puppeteer-extra'
|
| 6 |
import StealthPlugin from 'puppeteer-extra-plugin-stealth'
|
|
|
|
| 7 |
|
| 8 |
dotenv.config();
|
| 9 |
|
|
@@ -20,7 +21,7 @@ const CONFIG = {
|
|
| 20 |
RedisToken: process.env.RedisToken
|
| 21 |
},
|
| 22 |
RETRY: {
|
| 23 |
-
MAX_ATTEMPTS:
|
| 24 |
DELAY_BASE: 1000
|
| 25 |
},
|
| 26 |
SERVER: {
|
|
@@ -100,6 +101,7 @@ class TokenManager {
|
|
| 100 |
CONFIG.DEFAULT_HEADERS["anti-csrftoken-a2z"] = Tokens[currentIndex].anti_csrftoken_a2z;
|
| 101 |
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}`;
|
| 102 |
CONFIG.DEFAULT_HEADERS.referer = Tokens[currentIndex].refreshUrl;
|
|
|
|
| 103 |
}
|
| 104 |
|
| 105 |
async updateTokens(response, isWaf = false) {
|
|
@@ -113,7 +115,7 @@ class TokenManager {
|
|
| 113 |
currentIndex = (currentIndex + 1) % Tokens.length;
|
| 114 |
await this.updateCacheTokens();
|
| 115 |
}
|
| 116 |
-
}else{
|
| 117 |
const newCsrfToken = response.headers.get('anti-csrftoken-a2z');
|
| 118 |
const cookies = response.headers.get('set-cookie');
|
| 119 |
if (newCsrfToken && cookies) {
|
|
@@ -133,6 +135,19 @@ class TokenManager {
|
|
| 133 |
|
| 134 |
|
| 135 |
class Utils {
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 136 |
static async extractWaf() {
|
| 137 |
const browser = await puppeteer.launch({
|
| 138 |
headless: true,
|
|
@@ -150,9 +165,9 @@ class Utils {
|
|
| 150 |
cookie: `pr_refresh_token=${Tokens[currentIndex].pr_refresh_token};idToken=${Tokens[currentIndex].idToken};aws-waf-token=${Tokens[currentIndex].aws_waf_token};cwr_s=${Tokens[currentIndex].cwr_s};cwr_u=${Utils.uuidv4()}`
|
| 151 |
});
|
| 152 |
await page.setUserAgent(
|
| 153 |
-
|
| 154 |
)
|
| 155 |
-
await page.goto(Tokens[currentIndex].refreshUrl, {
|
| 156 |
waitUntil: 'networkidle2',
|
| 157 |
timeout: 30000
|
| 158 |
});
|
|
@@ -345,7 +360,7 @@ class ApiClient {
|
|
| 345 |
temperature = 1;
|
| 346 |
}
|
| 347 |
const extractPartyRockId = url => url.match(/https:\/\/partyrock\.aws\/u\/[^/]+\/([^/]+)/)?.[1];
|
| 348 |
-
console.log("当前请求的是",CONFIG.DEFAULT_HEADERS.referer);
|
| 349 |
|
| 350 |
const requestPayload = {
|
| 351 |
"messages": mergedMessages,
|
|
@@ -413,10 +428,9 @@ class ResponseHandler {
|
|
| 413 |
buffer = lines.pop() || '';
|
| 414 |
|
| 415 |
for (const line of lines) {
|
| 416 |
-
if (!line) continue;
|
| 417 |
-
|
| 418 |
-
|
| 419 |
-
const data = trimmedLine.substring(6);
|
| 420 |
if (!data) continue;
|
| 421 |
if (data == "[DONE]") {
|
| 422 |
res.write('data: [DONE]\n\n');
|
|
@@ -501,7 +515,17 @@ app.get('/hf/v1/models', (req, res) => {
|
|
| 501 |
}))
|
| 502 |
});
|
| 503 |
});
|
| 504 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 505 |
app.post('/hf/v1/chat/completions', async (req, res) => {
|
| 506 |
var reqStatus = 500;
|
| 507 |
try {
|
|
@@ -524,7 +548,7 @@ app.post('/hf/v1/chat/completions', async (req, res) => {
|
|
| 524 |
...CONFIG.DEFAULT_HEADERS
|
| 525 |
},
|
| 526 |
body: JSON.stringify(requestPayload)
|
| 527 |
-
});
|
| 528 |
reqStatus = response.status;
|
| 529 |
switch (reqStatus) {
|
| 530 |
case 200:
|
|
@@ -547,13 +571,28 @@ app.post('/hf/v1/chat/completions', async (req, res) => {
|
|
| 547 |
await tokenManager.updateTokens(response, true);//常识获取waf,然后返回错误提示。
|
| 548 |
res.status(405).json({
|
| 549 |
error: {
|
| 550 |
-
message: "
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 551 |
type: 'server_error',
|
| 552 |
param: null,
|
| 553 |
code: null
|
| 554 |
}
|
| 555 |
});
|
| 556 |
return;
|
|
|
|
|
|
|
|
|
|
| 557 |
default:
|
| 558 |
throw new Error(`请求失败! status: ${response.status}`);
|
| 559 |
}
|
|
@@ -586,7 +625,7 @@ app.post('/hf/v1/chat/completions', async (req, res) => {
|
|
| 586 |
|
| 587 |
|
| 588 |
app.use((req, res) => {
|
| 589 |
-
res.status(404).send("API
|
| 590 |
});
|
| 591 |
|
| 592 |
// 启动服务器
|
|
|
|
| 4 |
import dotenv from 'dotenv';
|
| 5 |
import puppeteer from 'puppeteer-extra'
|
| 6 |
import StealthPlugin from 'puppeteer-extra-plugin-stealth'
|
| 7 |
+
import UserAgent from 'user-agents';
|
| 8 |
|
| 9 |
dotenv.config();
|
| 10 |
|
|
|
|
| 21 |
RedisToken: process.env.RedisToken
|
| 22 |
},
|
| 23 |
RETRY: {
|
| 24 |
+
MAX_ATTEMPTS: 1,
|
| 25 |
DELAY_BASE: 1000
|
| 26 |
},
|
| 27 |
SERVER: {
|
|
|
|
| 101 |
CONFIG.DEFAULT_HEADERS["anti-csrftoken-a2z"] = Tokens[currentIndex].anti_csrftoken_a2z;
|
| 102 |
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}`;
|
| 103 |
CONFIG.DEFAULT_HEADERS.referer = Tokens[currentIndex].refreshUrl;
|
| 104 |
+
CONFIG.DEFAULT_HEADERS["User-Agent"] = await Utils.getRandomUserAgent();
|
| 105 |
}
|
| 106 |
|
| 107 |
async updateTokens(response, isWaf = false) {
|
|
|
|
| 115 |
currentIndex = (currentIndex + 1) % Tokens.length;
|
| 116 |
await this.updateCacheTokens();
|
| 117 |
}
|
| 118 |
+
} else {
|
| 119 |
const newCsrfToken = response.headers.get('anti-csrftoken-a2z');
|
| 120 |
const cookies = response.headers.get('set-cookie');
|
| 121 |
if (newCsrfToken && cookies) {
|
|
|
|
| 135 |
|
| 136 |
|
| 137 |
class Utils {
|
| 138 |
+
static async getRandomUserAgent() {
|
| 139 |
+
try {
|
| 140 |
+
let type = ["Win32", "MacIntel", "Linux x86_64"]
|
| 141 |
+
const userAgent = new UserAgent({ platform: type[Math.floor(Math.random() * type.length)] });
|
| 142 |
+
return userAgent.random().toString();
|
| 143 |
+
} catch (error) {
|
| 144 |
+
let type = [
|
| 145 |
+
"Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/131.0.0.0 Safari/537.36",
|
| 146 |
+
"Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/16.0 Safari/605.1.15"
|
| 147 |
+
]
|
| 148 |
+
return type[Math.floor(Math.random() * type.length)]
|
| 149 |
+
}
|
| 150 |
+
}
|
| 151 |
static async extractWaf() {
|
| 152 |
const browser = await puppeteer.launch({
|
| 153 |
headless: true,
|
|
|
|
| 165 |
cookie: `pr_refresh_token=${Tokens[currentIndex].pr_refresh_token};idToken=${Tokens[currentIndex].idToken};aws-waf-token=${Tokens[currentIndex].aws_waf_token};cwr_s=${Tokens[currentIndex].cwr_s};cwr_u=${Utils.uuidv4()}`
|
| 166 |
});
|
| 167 |
await page.setUserAgent(
|
| 168 |
+
await Utils.getRandomUserAgent()
|
| 169 |
)
|
| 170 |
+
await page.goto(Tokens[currentIndex].refreshUrl, {
|
| 171 |
waitUntil: 'networkidle2',
|
| 172 |
timeout: 30000
|
| 173 |
});
|
|
|
|
| 360 |
temperature = 1;
|
| 361 |
}
|
| 362 |
const extractPartyRockId = url => url.match(/https:\/\/partyrock\.aws\/u\/[^/]+\/([^/]+)/)?.[1];
|
| 363 |
+
console.log("当前请求的是", CONFIG.DEFAULT_HEADERS.referer);
|
| 364 |
|
| 365 |
const requestPayload = {
|
| 366 |
"messages": mergedMessages,
|
|
|
|
| 428 |
buffer = lines.pop() || '';
|
| 429 |
|
| 430 |
for (const line of lines) {
|
| 431 |
+
if (!line.trim()) continue;
|
| 432 |
+
if (line.startsWith('data: ')) {
|
| 433 |
+
const data = line.substring(6);
|
|
|
|
| 434 |
if (!data) continue;
|
| 435 |
if (data == "[DONE]") {
|
| 436 |
res.write('data: [DONE]\n\n');
|
|
|
|
| 515 |
}))
|
| 516 |
});
|
| 517 |
});
|
| 518 |
+
app.post('/hf/delete/redis', (req, res) => {
|
| 519 |
+
res.json({
|
| 520 |
+
object: "list",
|
| 521 |
+
data: Object.keys(CONFIG.MODELS).map((model, index) => ({
|
| 522 |
+
id: model,
|
| 523 |
+
object: "model",
|
| 524 |
+
created: Math.floor(Date.now() / 1000),
|
| 525 |
+
owned_by: "partyrock",
|
| 526 |
+
}))
|
| 527 |
+
});
|
| 528 |
+
});
|
| 529 |
app.post('/hf/v1/chat/completions', async (req, res) => {
|
| 530 |
var reqStatus = 500;
|
| 531 |
try {
|
|
|
|
| 548 |
...CONFIG.DEFAULT_HEADERS
|
| 549 |
},
|
| 550 |
body: JSON.stringify(requestPayload)
|
| 551 |
+
});
|
| 552 |
reqStatus = response.status;
|
| 553 |
switch (reqStatus) {
|
| 554 |
case 200:
|
|
|
|
| 571 |
await tokenManager.updateTokens(response, true);//常识获取waf,然后返回错误提示。
|
| 572 |
res.status(405).json({
|
| 573 |
error: {
|
| 574 |
+
message: "人机验证,请重新请求,如果多次失败,请重新更换token",
|
| 575 |
+
type: 'server_error',
|
| 576 |
+
param: null,
|
| 577 |
+
code: null
|
| 578 |
+
}
|
| 579 |
+
});
|
| 580 |
+
return;
|
| 581 |
+
case 400:
|
| 582 |
+
console.log("信息过期,请��失败");
|
| 583 |
+
await tokenManager.updateTokens(response);
|
| 584 |
+
res.status(400).json({
|
| 585 |
+
error: {
|
| 586 |
+
message: "已刷新验证信息,请重新请求",
|
| 587 |
type: 'server_error',
|
| 588 |
param: null,
|
| 589 |
code: null
|
| 590 |
}
|
| 591 |
});
|
| 592 |
return;
|
| 593 |
+
case 403:
|
| 594 |
+
console.log("请求被阻止");
|
| 595 |
+
throw new Error(`请求失败! status: ${response.statusText},请重新请求,如果多次失败,请重新更换token`);
|
| 596 |
default:
|
| 597 |
throw new Error(`请求失败! status: ${response.status}`);
|
| 598 |
}
|
|
|
|
| 625 |
|
| 626 |
|
| 627 |
app.use((req, res) => {
|
| 628 |
+
res.status(404).send("API服务运行正常,,请使用正确请求路径");
|
| 629 |
});
|
| 630 |
|
| 631 |
// 启动服务器
|