Upload 2 files
Browse files- src/utils/cookieRefresher.js +77 -10
- src/utils/extractCookieFromCsv.js +140 -42
src/utils/cookieRefresher.js
CHANGED
|
@@ -480,15 +480,66 @@ async function extractCookiesFromCsvFile(csvFilePath) {
|
|
| 480 |
console.log(`尝试从CSV文件提取cookies (尝试 ${attempt}/${maxExtractAttempts})...`);
|
| 481 |
|
| 482 |
try {
|
| 483 |
-
//
|
| 484 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 485 |
|
| 486 |
-
|
| 487 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 488 |
return cookies;
|
| 489 |
-
} else {
|
| 490 |
-
console.warn(`未能从CSV文件提取到cookies (尝试 ${attempt}/${maxExtractAttempts})`);
|
| 491 |
}
|
|
|
|
|
|
|
| 492 |
} catch (error) {
|
| 493 |
console.error(`从CSV文件提取cookies时出错 (尝试 ${attempt}/${maxExtractAttempts}):`, error);
|
| 494 |
}
|
|
@@ -543,14 +594,24 @@ function addNewCookiesToSystem(apiKey, newCookies) {
|
|
| 543 |
|
| 544 |
// 验证cookie是否完整
|
| 545 |
const validatedCookies = newValidCookies.filter(cookie => {
|
| 546 |
-
// 检查
|
| 547 |
-
if (cookie.includes('
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 548 |
const parts = cookie.split('%3A%3A');
|
| 549 |
if (parts.length === 2) {
|
| 550 |
const jwt = parts[1];
|
| 551 |
// 检查JWT是否包含点(表示JWT的三个部分)
|
| 552 |
if (!jwt.includes('.') || jwt.split('.').length !== 3) {
|
| 553 |
-
console.warn(`跳过不完整的cookie: ${cookie}`);
|
| 554 |
return false;
|
| 555 |
}
|
| 556 |
}
|
|
@@ -704,6 +765,12 @@ async function autoRefreshCookies(apiKey, minCookieCount = config.refresh.minCoo
|
|
| 704 |
};
|
| 705 |
}
|
| 706 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 707 |
// 根据配置决定是否将现有cookie标记为无效
|
| 708 |
const refreshMode = process.env.COOKIE_REFRESH_MODE || 'append';
|
| 709 |
|
|
@@ -723,7 +790,7 @@ async function autoRefreshCookies(apiKey, minCookieCount = config.refresh.minCoo
|
|
| 723 |
|
| 724 |
return {
|
| 725 |
success: true,
|
| 726 |
-
message: `成功添加 ${addedCount} 个新 Cookie`,
|
| 727 |
refreshed: addedCount
|
| 728 |
};
|
| 729 |
} catch (error) {
|
|
|
|
| 480 |
console.log(`尝试从CSV文件提取cookies (尝试 ${attempt}/${maxExtractAttempts})...`);
|
| 481 |
|
| 482 |
try {
|
| 483 |
+
// 读取文件内容
|
| 484 |
+
if (!fs.existsSync(csvFilePath)) {
|
| 485 |
+
console.error(`CSV文件不存在: ${csvFilePath}`);
|
| 486 |
+
return [];
|
| 487 |
+
}
|
| 488 |
+
|
| 489 |
+
// 读取文件内容并处理可能的换行符
|
| 490 |
+
let fileContent = fs.readFileSync(csvFilePath, 'utf8');
|
| 491 |
+
fileContent = fileContent.replace(/\r\n/g, '\n').replace(/\r/g, '\n');
|
| 492 |
+
|
| 493 |
+
// 首先尝试直接从文件内容中提取所有可能的cookie
|
| 494 |
+
const cookies = [];
|
| 495 |
+
|
| 496 |
+
// 1. 检查是否有JWT格式的token (新格式)
|
| 497 |
+
const jwtRegex = /ey[A-Za-z0-9_-]+\.[A-Za-z0-9_-]+\.[A-Za-z0-9_-]+/g;
|
| 498 |
+
const jwtMatches = fileContent.match(jwtRegex);
|
| 499 |
+
|
| 500 |
+
if (jwtMatches && jwtMatches.length > 0) {
|
| 501 |
+
console.log(`直接从文件内容中提取到 ${jwtMatches.length} 个JWT token格式的Cookie`);
|
| 502 |
+
jwtMatches.forEach(match => {
|
| 503 |
+
if (!cookies.includes(match)) {
|
| 504 |
+
cookies.push(match);
|
| 505 |
+
}
|
| 506 |
+
});
|
| 507 |
+
}
|
| 508 |
+
|
| 509 |
+
// 2. 检查是否有旧格式的cookie
|
| 510 |
+
if (fileContent.includes('user_')) {
|
| 511 |
+
console.log('文件包含旧格式cookie标识"user_"');
|
| 512 |
+
|
| 513 |
+
// 使用旧的提取函数尝试提取
|
| 514 |
+
try {
|
| 515 |
+
const oldFormatCookies = await extractCookiesFromCsv(csvFilePath);
|
| 516 |
+
if (oldFormatCookies && oldFormatCookies.length > 0) {
|
| 517 |
+
console.log(`通过提取模块获取到 ${oldFormatCookies.length} 个cookie`);
|
| 518 |
+
oldFormatCookies.forEach(cookie => {
|
| 519 |
+
if (!cookies.includes(cookie)) {
|
| 520 |
+
cookies.push(cookie);
|
| 521 |
+
}
|
| 522 |
+
});
|
| 523 |
+
}
|
| 524 |
+
} catch (e) {
|
| 525 |
+
console.warn('通过提取模块获取cookie失败:', e.message);
|
| 526 |
+
}
|
| 527 |
+
}
|
| 528 |
|
| 529 |
+
// 3. 如果找到了cookie,返回结果
|
| 530 |
+
if (cookies.length > 0) {
|
| 531 |
+
const newFormatCount = cookies.filter(c => c.startsWith('ey')).length;
|
| 532 |
+
const oldFormatCount = cookies.filter(c => c.includes('%3A%3A')).length;
|
| 533 |
+
|
| 534 |
+
console.log(`总共找到 ${cookies.length} 个cookie`);
|
| 535 |
+
console.log(`新格式cookie(ey开头): ${newFormatCount}个`);
|
| 536 |
+
console.log(`旧格式cookie(包含%3A%3A): ${oldFormatCount}个`);
|
| 537 |
+
console.log(`其他格式cookie: ${cookies.length - newFormatCount - oldFormatCount}个`);
|
| 538 |
+
|
| 539 |
return cookies;
|
|
|
|
|
|
|
| 540 |
}
|
| 541 |
+
|
| 542 |
+
console.warn(`未能从文件中提取到任何cookie (尝试 ${attempt}/${maxExtractAttempts})`);
|
| 543 |
} catch (error) {
|
| 544 |
console.error(`从CSV文件提取cookies时出错 (尝试 ${attempt}/${maxExtractAttempts}):`, error);
|
| 545 |
}
|
|
|
|
| 594 |
|
| 595 |
// 验证cookie是否完整
|
| 596 |
const validatedCookies = newValidCookies.filter(cookie => {
|
| 597 |
+
// 检查是否是新格式的JWT token (ey开头)
|
| 598 |
+
if (cookie.startsWith('ey') && cookie.includes('.')) {
|
| 599 |
+
const parts = cookie.split('.');
|
| 600 |
+
// 检查JWT是否包含三个部分
|
| 601 |
+
if (parts.length !== 3) {
|
| 602 |
+
console.warn(`跳过不完整的JWT cookie (新格式): ${cookie}`);
|
| 603 |
+
return false;
|
| 604 |
+
}
|
| 605 |
+
return true;
|
| 606 |
+
}
|
| 607 |
+
// 检查旧格式cookie是否包含JWT的三个部分
|
| 608 |
+
else if (cookie.includes('%3A%3A')) {
|
| 609 |
const parts = cookie.split('%3A%3A');
|
| 610 |
if (parts.length === 2) {
|
| 611 |
const jwt = parts[1];
|
| 612 |
// 检查JWT是否包含点(表示JWT的三个部分)
|
| 613 |
if (!jwt.includes('.') || jwt.split('.').length !== 3) {
|
| 614 |
+
console.warn(`跳过不完整的cookie (旧格式): ${cookie}`);
|
| 615 |
return false;
|
| 616 |
}
|
| 617 |
}
|
|
|
|
| 765 |
};
|
| 766 |
}
|
| 767 |
|
| 768 |
+
// 分析提取到的cookie格式
|
| 769 |
+
const newFormatCookies = cookies.filter(cookie => cookie.startsWith('ey'));
|
| 770 |
+
const oldFormatCookies = cookies.filter(cookie => cookie.includes('%3A%3A'));
|
| 771 |
+
console.log(`提取到 ${newFormatCookies.length} 个新格式cookie(ey开头)`);
|
| 772 |
+
console.log(`提取到 ${oldFormatCookies.length} 个旧格式cookie(包含%3A%3A)`);
|
| 773 |
+
|
| 774 |
// 根据配置决定是否将现有cookie标记为无效
|
| 775 |
const refreshMode = process.env.COOKIE_REFRESH_MODE || 'append';
|
| 776 |
|
|
|
|
| 790 |
|
| 791 |
return {
|
| 792 |
success: true,
|
| 793 |
+
message: `成功添加 ${addedCount} 个新 Cookie (新格式: ${newFormatCookies.length}, 旧格式: ${oldFormatCookies.length})`,
|
| 794 |
refreshed: addedCount
|
| 795 |
};
|
| 796 |
} catch (error) {
|
src/utils/extractCookieFromCsv.js
CHANGED
|
@@ -26,42 +26,91 @@ async function extractCookiesFromCsv(csvFilePath) {
|
|
| 26 |
return resolve([]);
|
| 27 |
}
|
| 28 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 29 |
// 检查文件内容是否包含关键字
|
| 30 |
const hasTokenKeyword = fileContent.includes('token');
|
| 31 |
const hasUserPrefix = fileContent.includes('user_');
|
| 32 |
console.log(`文件包含"token"关键字: ${hasTokenKeyword}`);
|
| 33 |
console.log(`文件包含"user_"前缀: ${hasUserPrefix}`);
|
| 34 |
|
| 35 |
-
// 如果文件包含user_前缀,尝试
|
| 36 |
if (hasUserPrefix) {
|
| 37 |
-
const
|
| 38 |
-
if (
|
| 39 |
-
console.log(`
|
| 40 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 41 |
}
|
| 42 |
}
|
| 43 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 44 |
// 使用csv-parser解析CSV文件
|
| 45 |
-
const
|
| 46 |
-
const possibleTokenFields = ['token', 'cookie', 'value', 'Token', 'Cookie', 'Value'];
|
| 47 |
|
| 48 |
fs.createReadStream(csvFilePath)
|
| 49 |
.pipe(csv())
|
| 50 |
.on('data', (row) => {
|
| 51 |
// 检查所有可能的字段名
|
| 52 |
for (const field of possibleTokenFields) {
|
| 53 |
-
if (row[field]
|
| 54 |
-
|
| 55 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 56 |
}
|
| 57 |
}
|
| 58 |
|
| 59 |
// 如果没有找到预定义的字段,遍历所有字段
|
| 60 |
if (cookies.length === 0) {
|
| 61 |
for (const field in row) {
|
| 62 |
-
if (row[field] && typeof row[field] === 'string'
|
| 63 |
-
|
| 64 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 65 |
}
|
| 66 |
}
|
| 67 |
}
|
|
@@ -74,6 +123,19 @@ async function extractCookiesFromCsv(csvFilePath) {
|
|
| 74 |
console.log('尝试按行读取文件...');
|
| 75 |
const lines = fileContent.split('\n');
|
| 76 |
for (const line of lines) {
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 77 |
if (line.includes('user_')) {
|
| 78 |
const extractedCookies = extractCookiesFromText(line);
|
| 79 |
extractedCookies.forEach(cookie => {
|
|
@@ -86,18 +148,6 @@ async function extractCookiesFromCsv(csvFilePath) {
|
|
| 86 |
console.log(`按行读取后提取到 ${cookies.length} 个Cookie`);
|
| 87 |
}
|
| 88 |
|
| 89 |
-
// 如果仍然没有找到cookie,尝试从整个文件内容中提取
|
| 90 |
-
if (cookies.length === 0) {
|
| 91 |
-
console.log('尝试从整个文件内容中提取Cookie...');
|
| 92 |
-
const extractedCookies = extractCookiesFromText(fileContent);
|
| 93 |
-
extractedCookies.forEach(cookie => {
|
| 94 |
-
if (!cookies.includes(cookie)) {
|
| 95 |
-
cookies.push(cookie);
|
| 96 |
-
}
|
| 97 |
-
});
|
| 98 |
-
console.log(`从整个文件内容中提取到 ${cookies.length} 个Cookie`);
|
| 99 |
-
}
|
| 100 |
-
|
| 101 |
// 验证提取的cookie是否完整
|
| 102 |
const validatedCookies = validateCookies(cookies);
|
| 103 |
|
|
@@ -105,10 +155,36 @@ async function extractCookiesFromCsv(csvFilePath) {
|
|
| 105 |
})
|
| 106 |
.on('error', (error) => {
|
| 107 |
console.error('解析CSV文件时出错:', error);
|
| 108 |
-
|
| 109 |
-
|
| 110 |
-
|
| 111 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 112 |
});
|
| 113 |
} catch (error) {
|
| 114 |
console.error('提取Cookie时出错:', error);
|
|
@@ -125,12 +201,24 @@ async function extractCookiesFromCsv(csvFilePath) {
|
|
| 125 |
function extractCookiesFromText(text) {
|
| 126 |
const cookies = [];
|
| 127 |
|
| 128 |
-
// 使用正则表达式匹配user_开头的cookie
|
| 129 |
-
const
|
| 130 |
-
const
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 131 |
|
| 132 |
-
if (
|
| 133 |
-
|
| 134 |
if (!cookies.includes(match)) {
|
| 135 |
cookies.push(match);
|
| 136 |
}
|
|
@@ -146,23 +234,33 @@ function extractCookiesFromText(text) {
|
|
| 146 |
* @returns {string[]} - 验证后的cookie数组
|
| 147 |
*/
|
| 148 |
function validateCookies(cookies) {
|
| 149 |
-
return cookies.
|
| 150 |
-
// 检查
|
| 151 |
-
if (cookie.includes('
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 152 |
const parts = cookie.split('%3A%3A');
|
| 153 |
if (parts.length === 2) {
|
| 154 |
const jwt = parts[1];
|
| 155 |
// 检查JWT是否包含两个点(表示三个部分)
|
| 156 |
if (jwt.includes('.') && jwt.split('.').length === 3) {
|
| 157 |
-
return
|
| 158 |
} else {
|
| 159 |
-
console.warn(`检测到不完整的JWT: ${cookie}`);
|
| 160 |
-
|
| 161 |
-
return cookie;
|
| 162 |
}
|
| 163 |
}
|
| 164 |
}
|
| 165 |
-
return
|
| 166 |
});
|
| 167 |
}
|
| 168 |
|
|
|
|
| 26 |
return resolve([]);
|
| 27 |
}
|
| 28 |
|
| 29 |
+
// 首先尝试直接从文件内容中提取所有可能的cookie
|
| 30 |
+
const cookies = [];
|
| 31 |
+
|
| 32 |
+
// 检查是否有JWT格式的token (新格式)
|
| 33 |
+
const jwtRegex = /ey[A-Za-z0-9_-]+\.[A-Za-z0-9_-]+\.[A-Za-z0-9_-]+/g;
|
| 34 |
+
const jwtMatches = fileContent.match(jwtRegex);
|
| 35 |
+
|
| 36 |
+
if (jwtMatches && jwtMatches.length > 0) {
|
| 37 |
+
console.log(`直接从文件内容中提取到 ${jwtMatches.length} 个JWT token格式的Cookie`);
|
| 38 |
+
jwtMatches.forEach(match => {
|
| 39 |
+
if (!cookies.includes(match)) {
|
| 40 |
+
cookies.push(match);
|
| 41 |
+
}
|
| 42 |
+
});
|
| 43 |
+
}
|
| 44 |
+
|
| 45 |
// 检查文件内容是否包含关键字
|
| 46 |
const hasTokenKeyword = fileContent.includes('token');
|
| 47 |
const hasUserPrefix = fileContent.includes('user_');
|
| 48 |
console.log(`文件包含"token"关键字: ${hasTokenKeyword}`);
|
| 49 |
console.log(`文件包含"user_"前缀: ${hasUserPrefix}`);
|
| 50 |
|
| 51 |
+
// 如果文件包含user_前缀,尝试提取旧格式cookie
|
| 52 |
if (hasUserPrefix) {
|
| 53 |
+
const oldFormatCookies = extractCookiesFromText(fileContent);
|
| 54 |
+
if (oldFormatCookies.length > 0) {
|
| 55 |
+
console.log(`从文件内容中提取到 ${oldFormatCookies.length} 个旧格式Cookie`);
|
| 56 |
+
oldFormatCookies.forEach(cookie => {
|
| 57 |
+
if (!cookies.includes(cookie)) {
|
| 58 |
+
cookies.push(cookie);
|
| 59 |
+
}
|
| 60 |
+
});
|
| 61 |
}
|
| 62 |
}
|
| 63 |
|
| 64 |
+
// 如果已经找到cookie,返回结果
|
| 65 |
+
if (cookies.length > 0) {
|
| 66 |
+
console.log(`总共提取到 ${cookies.length} 个Cookie`);
|
| 67 |
+
return resolve(validateCookies(cookies));
|
| 68 |
+
}
|
| 69 |
+
|
| 70 |
// 使用csv-parser解析CSV文件
|
| 71 |
+
const possibleTokenFields = ['token', 'cookie', 'value', 'Token', 'Cookie', 'Value', 'jwt', 'JWT'];
|
|
|
|
| 72 |
|
| 73 |
fs.createReadStream(csvFilePath)
|
| 74 |
.pipe(csv())
|
| 75 |
.on('data', (row) => {
|
| 76 |
// 检查所有可能的字段名
|
| 77 |
for (const field of possibleTokenFields) {
|
| 78 |
+
if (row[field]) {
|
| 79 |
+
// 检查是否是JWT格式
|
| 80 |
+
if (row[field].startsWith('ey') && row[field].includes('.')) {
|
| 81 |
+
if (!cookies.includes(row[field])) {
|
| 82 |
+
cookies.push(row[field]);
|
| 83 |
+
}
|
| 84 |
+
break;
|
| 85 |
+
}
|
| 86 |
+
// 检查是否是旧格式
|
| 87 |
+
else if (row[field].includes('user_')) {
|
| 88 |
+
if (!cookies.includes(row[field])) {
|
| 89 |
+
cookies.push(row[field]);
|
| 90 |
+
}
|
| 91 |
+
break;
|
| 92 |
+
}
|
| 93 |
}
|
| 94 |
}
|
| 95 |
|
| 96 |
// 如果没有找到预定义的字段,遍历所有字段
|
| 97 |
if (cookies.length === 0) {
|
| 98 |
for (const field in row) {
|
| 99 |
+
if (row[field] && typeof row[field] === 'string') {
|
| 100 |
+
// 检查是否是JWT格式
|
| 101 |
+
if (row[field].startsWith('ey') && row[field].includes('.')) {
|
| 102 |
+
if (!cookies.includes(row[field])) {
|
| 103 |
+
cookies.push(row[field]);
|
| 104 |
+
}
|
| 105 |
+
break;
|
| 106 |
+
}
|
| 107 |
+
// 检查是否是旧格式
|
| 108 |
+
else if (row[field].includes('user_')) {
|
| 109 |
+
if (!cookies.includes(row[field])) {
|
| 110 |
+
cookies.push(row[field]);
|
| 111 |
+
}
|
| 112 |
+
break;
|
| 113 |
+
}
|
| 114 |
}
|
| 115 |
}
|
| 116 |
}
|
|
|
|
| 123 |
console.log('尝试按行读取文件...');
|
| 124 |
const lines = fileContent.split('\n');
|
| 125 |
for (const line of lines) {
|
| 126 |
+
// 检查是否有JWT格式token
|
| 127 |
+
if (line.includes('ey')) {
|
| 128 |
+
const jwtMatches = line.match(jwtRegex);
|
| 129 |
+
if (jwtMatches) {
|
| 130 |
+
jwtMatches.forEach(match => {
|
| 131 |
+
if (!cookies.includes(match)) {
|
| 132 |
+
cookies.push(match);
|
| 133 |
+
}
|
| 134 |
+
});
|
| 135 |
+
}
|
| 136 |
+
}
|
| 137 |
+
|
| 138 |
+
// 检查是否有旧格式cookie
|
| 139 |
if (line.includes('user_')) {
|
| 140 |
const extractedCookies = extractCookiesFromText(line);
|
| 141 |
extractedCookies.forEach(cookie => {
|
|
|
|
| 148 |
console.log(`按行读取后提取到 ${cookies.length} 个Cookie`);
|
| 149 |
}
|
| 150 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 151 |
// 验证提取的cookie是否完整
|
| 152 |
const validatedCookies = validateCookies(cookies);
|
| 153 |
|
|
|
|
| 155 |
})
|
| 156 |
.on('error', (error) => {
|
| 157 |
console.error('解析CSV文件时出错:', error);
|
| 158 |
+
|
| 159 |
+
// 如果已经提取到cookie,直接返回
|
| 160 |
+
if (cookies.length > 0) {
|
| 161 |
+
console.log(`解析出错但已提取到 ${cookies.length} 个Cookie,进行验证后返回`);
|
| 162 |
+
resolve(validateCookies(cookies));
|
| 163 |
+
} else {
|
| 164 |
+
// 否则尝试其他方法提取
|
| 165 |
+
console.log('尝试其他方法提取Cookie...');
|
| 166 |
+
|
| 167 |
+
// 尝试提取JWT格式token
|
| 168 |
+
const jwtMatches = fileContent.match(jwtRegex);
|
| 169 |
+
if (jwtMatches) {
|
| 170 |
+
jwtMatches.forEach(match => {
|
| 171 |
+
if (!cookies.includes(match)) {
|
| 172 |
+
cookies.push(match);
|
| 173 |
+
}
|
| 174 |
+
});
|
| 175 |
+
}
|
| 176 |
+
|
| 177 |
+
// 尝试提取旧格式cookie
|
| 178 |
+
const oldFormatCookies = extractCookiesFromText(fileContent);
|
| 179 |
+
oldFormatCookies.forEach(cookie => {
|
| 180 |
+
if (!cookies.includes(cookie)) {
|
| 181 |
+
cookies.push(cookie);
|
| 182 |
+
}
|
| 183 |
+
});
|
| 184 |
+
|
| 185 |
+
console.log(`通过其他方法提取到 ${cookies.length} 个Cookie`);
|
| 186 |
+
resolve(validateCookies(cookies));
|
| 187 |
+
}
|
| 188 |
});
|
| 189 |
} catch (error) {
|
| 190 |
console.error('提取Cookie时出错:', error);
|
|
|
|
| 201 |
function extractCookiesFromText(text) {
|
| 202 |
const cookies = [];
|
| 203 |
|
| 204 |
+
// 使用正则表达式匹配user_开头的cookie(旧格式)
|
| 205 |
+
const oldFormatRegex = /user_[a-zA-Z0-9%]+%3A%3A[a-zA-Z0-9%\.\_\-]+/g;
|
| 206 |
+
const oldFormatMatches = text.match(oldFormatRegex);
|
| 207 |
+
|
| 208 |
+
if (oldFormatMatches) {
|
| 209 |
+
oldFormatMatches.forEach(match => {
|
| 210 |
+
if (!cookies.includes(match)) {
|
| 211 |
+
cookies.push(match);
|
| 212 |
+
}
|
| 213 |
+
});
|
| 214 |
+
}
|
| 215 |
+
|
| 216 |
+
// 使用正则表达式匹配以ey开头的JWT格式cookie(新格式)
|
| 217 |
+
const jwtRegex = /ey[A-Za-z0-9_-]+\.[A-Za-z0-9_-]+\.[A-Za-z0-9_-]+/g;
|
| 218 |
+
const jwtMatches = text.match(jwtRegex);
|
| 219 |
|
| 220 |
+
if (jwtMatches) {
|
| 221 |
+
jwtMatches.forEach(match => {
|
| 222 |
if (!cookies.includes(match)) {
|
| 223 |
cookies.push(match);
|
| 224 |
}
|
|
|
|
| 234 |
* @returns {string[]} - 验证后的cookie数组
|
| 235 |
*/
|
| 236 |
function validateCookies(cookies) {
|
| 237 |
+
return cookies.filter(cookie => {
|
| 238 |
+
// 检查是否是新格式的JWT token (ey开头)
|
| 239 |
+
if (cookie.startsWith('ey') && cookie.includes('.')) {
|
| 240 |
+
const parts = cookie.split('.');
|
| 241 |
+
// 检查JWT是否包含三个部分
|
| 242 |
+
if (parts.length === 3) {
|
| 243 |
+
return true; // cookie有效
|
| 244 |
+
} else {
|
| 245 |
+
console.warn(`检测到不完整的JWT(新格式): ${cookie}`);
|
| 246 |
+
return false;
|
| 247 |
+
}
|
| 248 |
+
}
|
| 249 |
+
// 检查旧格式cookie是否完整
|
| 250 |
+
else if (cookie.includes('%3A%3A')) {
|
| 251 |
const parts = cookie.split('%3A%3A');
|
| 252 |
if (parts.length === 2) {
|
| 253 |
const jwt = parts[1];
|
| 254 |
// 检查JWT是否包含两个点(表示三个部分)
|
| 255 |
if (jwt.includes('.') && jwt.split('.').length === 3) {
|
| 256 |
+
return true; // cookie完整
|
| 257 |
} else {
|
| 258 |
+
console.warn(`检测到不完整的JWT(旧格式): ${cookie}`);
|
| 259 |
+
return false;
|
|
|
|
| 260 |
}
|
| 261 |
}
|
| 262 |
}
|
| 263 |
+
return true; // 对于无法识别的格式,默认保留
|
| 264 |
});
|
| 265 |
}
|
| 266 |
|