Spaces:
Paused
Paused
Update index.js
Browse files
index.js
CHANGED
|
@@ -47,7 +47,7 @@ const CONFIG = {
|
|
| 47 |
SSO_INDEX: 0,//sso的索引
|
| 48 |
TEMP_COOKIE_INDEX: 0,//临时cookie的下标
|
| 49 |
ISSHOW_SEARCH_RESULTS: process.env.ISSHOW_SEARCH_RESULTS == undefined ? true : process.env.ISSHOW_SEARCH_RESULTS == 'true',//是否显示搜索结果
|
| 50 |
-
CHROME_PATH: null
|
| 51 |
};
|
| 52 |
puppeteer.use(StealthPlugin())
|
| 53 |
|
|
@@ -72,14 +72,16 @@ const DEFAULT_HEADERS = {
|
|
| 72 |
|
| 73 |
|
| 74 |
async function initialization() {
|
| 75 |
-
|
| 76 |
-
|
| 77 |
-
|
| 78 |
-
|
|
|
|
|
|
|
| 79 |
}
|
| 80 |
Logger.info(`CHROME_PATH: ${CONFIG.CHROME_PATH}`, 'Server');
|
| 81 |
if (CONFIG.API.IS_CUSTOM_SSO) {
|
| 82 |
-
if(CONFIG.API.IS_TEMP_GROK2){
|
| 83 |
await tempCookieManager.ensureCookies();
|
| 84 |
}
|
| 85 |
return;
|
|
@@ -89,7 +91,8 @@ async function initialization() {
|
|
| 89 |
tokenManager.addToken(`sso-rw=${sso};sso=${sso}`);
|
| 90 |
});
|
| 91 |
console.log(JSON.stringify(tokenManager.getActiveTokens(), null, 2));
|
| 92 |
-
|
|
|
|
| 93 |
await tempCookieManager.ensureCookies();
|
| 94 |
CONFIG.API.TEMP_COOKIE = tempCookieManager.cookies[tempCookieManager.currentIndex];
|
| 95 |
}
|
|
@@ -102,18 +105,18 @@ class AuthTokenManager {
|
|
| 102 |
this.activeTokens = [];
|
| 103 |
this.expiredTokens = new Map();
|
| 104 |
this.tokenModelFrequency = new Map();
|
| 105 |
-
if(CONFIG.API.IS_TEMP_GROK2){
|
| 106 |
this.modelRateLimit = {
|
| 107 |
"grok-3": { RequestFrequency: 20 },
|
| 108 |
-
"grok-3-deepsearch": { RequestFrequency:
|
| 109 |
-
"grok-3-reasoning": { RequestFrequency:
|
| 110 |
};
|
| 111 |
this.modelInitFrequency = {
|
| 112 |
"grok-3": 0,
|
| 113 |
"grok-3-deepsearch": 0,
|
| 114 |
"grok-3-reasoning": 0
|
| 115 |
};
|
| 116 |
-
}else{
|
| 117 |
this.modelRateLimit = {
|
| 118 |
"grok-2": { RequestFrequency: 20 },
|
| 119 |
"grok-3": { RequestFrequency: 20 },
|
|
@@ -340,33 +343,33 @@ class GrokTempCookieManager {
|
|
| 340 |
// 获取新的 cookies
|
| 341 |
let retryCount = 0;
|
| 342 |
let remainingCount = this.initialCookieCount - this.cookies.length;
|
| 343 |
-
|
| 344 |
while (retryCount < CONFIG.RETRY.MAX_ATTEMPTS) {
|
| 345 |
await this.initializeTempCookies(remainingCount);
|
| 346 |
-
if(this.extractCount != remainingCount){
|
| 347 |
-
if(this.extractCount == 0){
|
| 348 |
Logger.error(`无法获取足够的有效 TempCookies,可能网络存在问题,当前数量:${this.cookies.length}`);
|
| 349 |
-
}else if(this.extractCount < remainingCount){
|
| 350 |
-
remainingCount-=this.extractCount;
|
| 351 |
this.extractCount = 0;
|
| 352 |
retryCount++;
|
| 353 |
-
await Utils.delay(1000 * retryCount);
|
| 354 |
-
}else{
|
| 355 |
break;
|
| 356 |
}
|
| 357 |
-
}else {
|
| 358 |
break;
|
| 359 |
}
|
| 360 |
}
|
| 361 |
if (this.currentIndex >= this.cookies.length) {
|
| 362 |
this.currentIndex = 0;
|
| 363 |
}
|
| 364 |
-
|
| 365 |
if (this.cookies.length < this.initialCookieCount) {
|
| 366 |
-
if(this.cookies.length !== 0){
|
| 367 |
// 如果已经获取到一些 TempCookies,则只提示警告错误
|
| 368 |
Logger.error(`无法获取足够的有效 TempCookies,可能网络存在问题,当前数量:${this.cookies.length}`);
|
| 369 |
-
}else{
|
| 370 |
// 如果未获取到任何 TempCookies,则抛出错误
|
| 371 |
throw new Error(`无法获取足够的有效 TempCookies,可能网络存在问题,当前数量:${this.cookies.length}`);
|
| 372 |
}
|
|
@@ -375,7 +378,7 @@ class GrokTempCookieManager {
|
|
| 375 |
Logger.error('刷新 cookies 失败:', error);
|
| 376 |
} finally {
|
| 377 |
Logger.info(`已提取${this.cookies.length}个TempCookies`, 'Server');
|
| 378 |
-
Logger.info(`提取的TempCookies为${JSON.stringify(this.cookies,null,2)}`, 'Server');
|
| 379 |
this.isRefreshing = false;
|
| 380 |
}
|
| 381 |
}
|
|
@@ -942,17 +945,17 @@ app.post('/hf/v1/chat/completions', async (req, res) => {
|
|
| 942 |
if (isTempCookie) {
|
| 943 |
// 移除当前失效的 cookie
|
| 944 |
tempCookieManager.cookies.splice(tempCookieManager.currentIndex, 1);
|
| 945 |
-
if(tempCookieManager.cookies.length != 0){
|
| 946 |
tempCookieManager.currentIndex = tempCookieManager.currentIndex % tempCookieManager.cookies.length;
|
| 947 |
CONFIG.API.TEMP_COOKIE = tempCookieManager.cookies[tempCookieManager.currentIndex];
|
| 948 |
tempCookieManager.ensureCookies()
|
| 949 |
-
}else{
|
| 950 |
try {
|
| 951 |
await tempCookieManager.ensureCookies();
|
| 952 |
tempCookieManager.currentIndex = tempCookieManager.currentIndex % tempCookieManager.cookies.length;
|
| 953 |
CONFIG.API.TEMP_COOKIE = tempCookieManager.cookies[tempCookieManager.currentIndex];
|
| 954 |
} catch (error) {
|
| 955 |
-
throw error;
|
| 956 |
}
|
| 957 |
}
|
| 958 |
} else {
|
|
@@ -972,17 +975,17 @@ app.post('/hf/v1/chat/completions', async (req, res) => {
|
|
| 972 |
if (isTempCookie) {
|
| 973 |
// 移除当前失效的 cookie
|
| 974 |
tempCookieManager.cookies.splice(tempCookieManager.currentIndex, 1);
|
| 975 |
-
if(tempCookieManager.cookies.length != 0){
|
| 976 |
tempCookieManager.currentIndex = tempCookieManager.currentIndex % tempCookieManager.cookies.length;
|
| 977 |
CONFIG.API.TEMP_COOKIE = tempCookieManager.cookies[tempCookieManager.currentIndex];
|
| 978 |
tempCookieManager.ensureCookies()
|
| 979 |
-
}else{
|
| 980 |
try {
|
| 981 |
await tempCookieManager.ensureCookies();
|
| 982 |
tempCookieManager.currentIndex = tempCookieManager.currentIndex % tempCookieManager.cookies.length;
|
| 983 |
CONFIG.API.TEMP_COOKIE = tempCookieManager.cookies[tempCookieManager.currentIndex];
|
| 984 |
} catch (error) {
|
| 985 |
-
throw error;
|
| 986 |
}
|
| 987 |
}
|
| 988 |
} else {
|
|
@@ -1001,17 +1004,17 @@ app.post('/hf/v1/chat/completions', async (req, res) => {
|
|
| 1001 |
if (isTempCookie) {
|
| 1002 |
// 移除当前失效的 cookie
|
| 1003 |
tempCookieManager.cookies.splice(tempCookieManager.currentIndex, 1);
|
| 1004 |
-
if(tempCookieManager.cookies.length != 0){
|
| 1005 |
tempCookieManager.currentIndex = tempCookieManager.currentIndex % tempCookieManager.cookies.length;
|
| 1006 |
CONFIG.API.TEMP_COOKIE = tempCookieManager.cookies[tempCookieManager.currentIndex];
|
| 1007 |
tempCookieManager.ensureCookies()
|
| 1008 |
-
}else{
|
| 1009 |
try {
|
| 1010 |
await tempCookieManager.ensureCookies();
|
| 1011 |
tempCookieManager.currentIndex = tempCookieManager.currentIndex % tempCookieManager.cookies.length;
|
| 1012 |
CONFIG.API.TEMP_COOKIE = tempCookieManager.cookies[tempCookieManager.currentIndex];
|
| 1013 |
} catch (error) {
|
| 1014 |
-
throw error;
|
| 1015 |
}
|
| 1016 |
}
|
| 1017 |
} else {
|
|
|
|
| 47 |
SSO_INDEX: 0,//sso的索引
|
| 48 |
TEMP_COOKIE_INDEX: 0,//临时cookie的下标
|
| 49 |
ISSHOW_SEARCH_RESULTS: process.env.ISSHOW_SEARCH_RESULTS == undefined ? true : process.env.ISSHOW_SEARCH_RESULTS == 'true',//是否显示搜索结果
|
| 50 |
+
CHROME_PATH: process.env.CHROME_PATH || null
|
| 51 |
};
|
| 52 |
puppeteer.use(StealthPlugin())
|
| 53 |
|
|
|
|
| 72 |
|
| 73 |
|
| 74 |
async function initialization() {
|
| 75 |
+
if (CONFIG.CHROME_PATH == null) {
|
| 76 |
+
try {
|
| 77 |
+
CONFIG.CHROME_PATH = puppeteer.executablePath();
|
| 78 |
+
} catch (error) {
|
| 79 |
+
CONFIG.CHROME_PATH = "/usr/bin/chromium";
|
| 80 |
+
}
|
| 81 |
}
|
| 82 |
Logger.info(`CHROME_PATH: ${CONFIG.CHROME_PATH}`, 'Server');
|
| 83 |
if (CONFIG.API.IS_CUSTOM_SSO) {
|
| 84 |
+
if (CONFIG.API.IS_TEMP_GROK2) {
|
| 85 |
await tempCookieManager.ensureCookies();
|
| 86 |
}
|
| 87 |
return;
|
|
|
|
| 91 |
tokenManager.addToken(`sso-rw=${sso};sso=${sso}`);
|
| 92 |
});
|
| 93 |
console.log(JSON.stringify(tokenManager.getActiveTokens(), null, 2));
|
| 94 |
+
Logger.info(`令牌加载完成,共加载: ${tokenManager.getTokenCount()}个令牌`, 'Server');
|
| 95 |
+
if (CONFIG.API.IS_TEMP_GROK2) {
|
| 96 |
await tempCookieManager.ensureCookies();
|
| 97 |
CONFIG.API.TEMP_COOKIE = tempCookieManager.cookies[tempCookieManager.currentIndex];
|
| 98 |
}
|
|
|
|
| 105 |
this.activeTokens = [];
|
| 106 |
this.expiredTokens = new Map();
|
| 107 |
this.tokenModelFrequency = new Map();
|
| 108 |
+
if (CONFIG.API.IS_TEMP_GROK2) {
|
| 109 |
this.modelRateLimit = {
|
| 110 |
"grok-3": { RequestFrequency: 20 },
|
| 111 |
+
"grok-3-deepsearch": { RequestFrequency: 10 },
|
| 112 |
+
"grok-3-reasoning": { RequestFrequency: 10 }
|
| 113 |
};
|
| 114 |
this.modelInitFrequency = {
|
| 115 |
"grok-3": 0,
|
| 116 |
"grok-3-deepsearch": 0,
|
| 117 |
"grok-3-reasoning": 0
|
| 118 |
};
|
| 119 |
+
} else {
|
| 120 |
this.modelRateLimit = {
|
| 121 |
"grok-2": { RequestFrequency: 20 },
|
| 122 |
"grok-3": { RequestFrequency: 20 },
|
|
|
|
| 343 |
// 获取新的 cookies
|
| 344 |
let retryCount = 0;
|
| 345 |
let remainingCount = this.initialCookieCount - this.cookies.length;
|
| 346 |
+
|
| 347 |
while (retryCount < CONFIG.RETRY.MAX_ATTEMPTS) {
|
| 348 |
await this.initializeTempCookies(remainingCount);
|
| 349 |
+
if (this.extractCount != remainingCount) {
|
| 350 |
+
if (this.extractCount == 0) {
|
| 351 |
Logger.error(`无法获取足够的有效 TempCookies,可能网络存在问题,当前数量:${this.cookies.length}`);
|
| 352 |
+
} else if (this.extractCount < remainingCount) {
|
| 353 |
+
remainingCount -= this.extractCount;
|
| 354 |
this.extractCount = 0;
|
| 355 |
retryCount++;
|
| 356 |
+
await Utils.delay(1000 * retryCount);
|
| 357 |
+
} else {
|
| 358 |
break;
|
| 359 |
}
|
| 360 |
+
} else {
|
| 361 |
break;
|
| 362 |
}
|
| 363 |
}
|
| 364 |
if (this.currentIndex >= this.cookies.length) {
|
| 365 |
this.currentIndex = 0;
|
| 366 |
}
|
| 367 |
+
|
| 368 |
if (this.cookies.length < this.initialCookieCount) {
|
| 369 |
+
if (this.cookies.length !== 0) {
|
| 370 |
// 如果已经获取到一些 TempCookies,则只提示警告错误
|
| 371 |
Logger.error(`无法获取足够的有效 TempCookies,可能网络存在问题,当前数量:${this.cookies.length}`);
|
| 372 |
+
} else {
|
| 373 |
// 如果未获取到任何 TempCookies,则抛出错误
|
| 374 |
throw new Error(`无法获取足够的有效 TempCookies,可能网络存在问题,当前数量:${this.cookies.length}`);
|
| 375 |
}
|
|
|
|
| 378 |
Logger.error('刷新 cookies 失败:', error);
|
| 379 |
} finally {
|
| 380 |
Logger.info(`已提取${this.cookies.length}个TempCookies`, 'Server');
|
| 381 |
+
Logger.info(`提取的TempCookies为${JSON.stringify(this.cookies, null, 2)}`, 'Server');
|
| 382 |
this.isRefreshing = false;
|
| 383 |
}
|
| 384 |
}
|
|
|
|
| 945 |
if (isTempCookie) {
|
| 946 |
// 移除当前失效的 cookie
|
| 947 |
tempCookieManager.cookies.splice(tempCookieManager.currentIndex, 1);
|
| 948 |
+
if (tempCookieManager.cookies.length != 0) {
|
| 949 |
tempCookieManager.currentIndex = tempCookieManager.currentIndex % tempCookieManager.cookies.length;
|
| 950 |
CONFIG.API.TEMP_COOKIE = tempCookieManager.cookies[tempCookieManager.currentIndex];
|
| 951 |
tempCookieManager.ensureCookies()
|
| 952 |
+
} else {
|
| 953 |
try {
|
| 954 |
await tempCookieManager.ensureCookies();
|
| 955 |
tempCookieManager.currentIndex = tempCookieManager.currentIndex % tempCookieManager.cookies.length;
|
| 956 |
CONFIG.API.TEMP_COOKIE = tempCookieManager.cookies[tempCookieManager.currentIndex];
|
| 957 |
} catch (error) {
|
| 958 |
+
throw error;
|
| 959 |
}
|
| 960 |
}
|
| 961 |
} else {
|
|
|
|
| 975 |
if (isTempCookie) {
|
| 976 |
// 移除当前失效的 cookie
|
| 977 |
tempCookieManager.cookies.splice(tempCookieManager.currentIndex, 1);
|
| 978 |
+
if (tempCookieManager.cookies.length != 0) {
|
| 979 |
tempCookieManager.currentIndex = tempCookieManager.currentIndex % tempCookieManager.cookies.length;
|
| 980 |
CONFIG.API.TEMP_COOKIE = tempCookieManager.cookies[tempCookieManager.currentIndex];
|
| 981 |
tempCookieManager.ensureCookies()
|
| 982 |
+
} else {
|
| 983 |
try {
|
| 984 |
await tempCookieManager.ensureCookies();
|
| 985 |
tempCookieManager.currentIndex = tempCookieManager.currentIndex % tempCookieManager.cookies.length;
|
| 986 |
CONFIG.API.TEMP_COOKIE = tempCookieManager.cookies[tempCookieManager.currentIndex];
|
| 987 |
} catch (error) {
|
| 988 |
+
throw error;
|
| 989 |
}
|
| 990 |
}
|
| 991 |
} else {
|
|
|
|
| 1004 |
if (isTempCookie) {
|
| 1005 |
// 移除当前失效的 cookie
|
| 1006 |
tempCookieManager.cookies.splice(tempCookieManager.currentIndex, 1);
|
| 1007 |
+
if (tempCookieManager.cookies.length != 0) {
|
| 1008 |
tempCookieManager.currentIndex = tempCookieManager.currentIndex % tempCookieManager.cookies.length;
|
| 1009 |
CONFIG.API.TEMP_COOKIE = tempCookieManager.cookies[tempCookieManager.currentIndex];
|
| 1010 |
tempCookieManager.ensureCookies()
|
| 1011 |
+
} else {
|
| 1012 |
try {
|
| 1013 |
await tempCookieManager.ensureCookies();
|
| 1014 |
tempCookieManager.currentIndex = tempCookieManager.currentIndex % tempCookieManager.cookies.length;
|
| 1015 |
CONFIG.API.TEMP_COOKIE = tempCookieManager.cookies[tempCookieManager.currentIndex];
|
| 1016 |
} catch (error) {
|
| 1017 |
+
throw error;
|
| 1018 |
}
|
| 1019 |
}
|
| 1020 |
} else {
|