File size: 82,761 Bytes
f505714 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 1413 1414 1415 1416 1417 1418 1419 1420 1421 1422 1423 1424 1425 1426 1427 1428 1429 1430 1431 1432 1433 1434 1435 1436 1437 1438 1439 1440 1441 1442 1443 1444 1445 1446 1447 1448 1449 1450 1451 1452 1453 1454 1455 1456 1457 1458 1459 1460 1461 1462 1463 1464 1465 1466 1467 1468 1469 1470 1471 1472 1473 1474 1475 1476 1477 1478 1479 1480 1481 1482 1483 1484 1485 1486 1487 1488 1489 1490 1491 1492 1493 1494 1495 1496 1497 1498 1499 1500 1501 1502 1503 1504 1505 1506 1507 1508 1509 1510 1511 1512 1513 1514 1515 1516 1517 1518 1519 1520 1521 1522 1523 1524 1525 1526 1527 1528 1529 1530 1531 1532 1533 1534 1535 1536 1537 1538 1539 1540 1541 1542 1543 1544 1545 1546 1547 1548 1549 1550 1551 1552 1553 1554 1555 1556 1557 1558 1559 1560 1561 1562 1563 1564 1565 1566 1567 1568 1569 1570 1571 1572 1573 1574 1575 1576 1577 1578 1579 1580 1581 1582 1583 1584 1585 1586 1587 1588 1589 1590 1591 1592 1593 1594 1595 1596 1597 1598 1599 1600 1601 1602 1603 1604 1605 1606 1607 1608 1609 1610 1611 1612 1613 1614 1615 1616 1617 1618 1619 1620 1621 1622 1623 1624 1625 1626 1627 1628 1629 1630 1631 1632 1633 1634 1635 1636 1637 1638 1639 1640 1641 1642 1643 1644 1645 1646 1647 1648 1649 1650 1651 1652 1653 1654 1655 1656 1657 1658 1659 1660 1661 1662 1663 1664 1665 1666 1667 1668 1669 1670 1671 1672 1673 1674 1675 1676 1677 1678 1679 1680 1681 1682 1683 1684 1685 1686 1687 1688 1689 1690 1691 1692 1693 1694 1695 1696 1697 1698 1699 1700 1701 1702 1703 1704 1705 1706 1707 1708 1709 1710 1711 1712 1713 1714 1715 1716 1717 1718 1719 1720 1721 1722 1723 1724 1725 1726 1727 1728 1729 1730 1731 1732 1733 1734 1735 1736 1737 1738 1739 1740 1741 1742 1743 1744 1745 1746 1747 1748 1749 1750 1751 1752 1753 1754 1755 1756 1757 1758 1759 1760 1761 1762 1763 1764 1765 1766 1767 1768 1769 1770 1771 1772 1773 1774 1775 1776 1777 1778 1779 1780 1781 1782 1783 1784 1785 1786 1787 1788 1789 1790 1791 1792 1793 1794 1795 1796 1797 1798 1799 1800 1801 1802 1803 1804 1805 1806 1807 1808 1809 1810 1811 1812 1813 1814 1815 1816 1817 1818 1819 1820 1821 1822 1823 1824 1825 1826 1827 1828 1829 1830 1831 1832 1833 1834 1835 1836 1837 1838 | const express = require('express');
const WebSocket = require('ws');
const http = require('http');
const { EventEmitter } = require('events');
const fs = require('fs');
const path = require('path');
const { firefox } = require('playwright');
const os = require('os');
// ===================================================================================
// 认证源管理模块 (已升级以支持动态管理)
// ===================================================================================
class AuthSource {
constructor(logger) {
this.logger = logger;
this.authMode = 'file'; // 默认模式
this.initialIndices = []; // 启动时发现的索引
this.runtimeAuths = new Map(); // 用于动态添加的账号
if (process.env.AUTH_JSON_1) {
this.authMode = 'env';
this.logger.info('[认证] 检测到 AUTH_JSON_1 环境变量,切换到环境变量认证模式。');
} else {
this.logger.info('[认证] 未检测到环境变量认证,将使用 "auth/" 目录下的文件。');
}
this._discoverAvailableIndices();
if (this.getAvailableIndices().length === 0) {
this.logger.error(`[认证] 致命错误:在 '${this.authMode}' 模式下未找到任何有效的认证源。`);
throw new Error("未找到有效的认证源。");
}
}
_discoverAvailableIndices() {
let indices = [];
if (this.authMode === 'env') {
const regex = /^AUTH_JSON_(\d+)$/;
for (const key in process.env) {
const match = key.match(regex);
// 修正:正确解析捕获组 (match[1]) 而不是整个匹配对象
if (match && match[1]) {
indices.push(parseInt(match[1], 10));
}
}
} else { // 'file' 模式
const authDir = path.join(__dirname, 'auth');
if (!fs.existsSync(authDir)) {
this.logger.warn('[认证] "auth/" 目录不存在。');
this.initialIndices = [];
return;
}
try {
const files = fs.readdirSync(authDir);
const authFiles = files.filter(file => /^auth-\d+\.json$/.test(file));
// 修正:正确解析文件名中的捕获组 (match[1])
indices = authFiles.map(file => {
const match = file.match(/^auth-(\d+)\.json$/);
return parseInt(match[1], 10);
});
} catch (error) {
this.logger.error(`[认证] 扫描 "auth/" 目录失败: ${error.message}`);
this.initialIndices = [];
return;
}
}
this.initialIndices = [...new Set(indices)].sort((a, b) => a - b);
this.logger.info(`[认证] 在 '${this.authMode}' 模式下,检测到 ${this.initialIndices.length} 个认证源。`);
if (this.initialIndices.length > 0) {
this.logger.info(`[认证] 可用初始索引: [${this.initialIndices.join(', ')}]`);
}
}
getAvailableIndices() {
const runtimeIndices = Array.from(this.runtimeAuths.keys());
const allIndices = [...new Set([...this.initialIndices, ...runtimeIndices])].sort((a, b) => a - b);
return allIndices;
}
// 新增方法:为仪表盘获取详细信息
getAccountDetails() {
const allIndices = this.getAvailableIndices();
return allIndices.map(index => ({
index,
source: this.runtimeAuths.has(index) ? 'temporary' : this.authMode
}));
}
getFirstAvailableIndex() {
const indices = this.getAvailableIndices();
return indices.length > 0 ? indices[0] : null;
}
getAuth(index) {
if (!this.getAvailableIndices().includes(index)) {
this.logger.error(`[认证] 请求了无效或不存在的认证索引: ${index}`);
return null;
}
// 优先使用运行时(临时)的认证信息
if (this.runtimeAuths.has(index)) {
this.logger.info(`[认证] 使用索引 ${index} 的临时认证源。`);
return this.runtimeAuths.get(index);
}
let jsonString;
let sourceDescription;
if (this.authMode === 'env') {
jsonString = process.env[`AUTH_JSON_${index}`];
sourceDescription = `环境变量 AUTH_JSON_${index}`;
} else {
const authFilePath = path.join(__dirname, 'auth', `auth-${index}.json`);
sourceDescription = `文件 ${authFilePath}`;
if (!fs.existsSync(authFilePath)) {
this.logger.error(`[认证] ${sourceDescription} 在读取时突然消失。`);
return null;
}
try {
jsonString = fs.readFileSync(authFilePath, 'utf-8');
} catch (e) {
this.logger.error(`[认证] 读取 ${sourceDescription} 失败: ${e.message}`);
return null;
}
}
try {
return JSON.parse(jsonString);
} catch (e) {
this.logger.error(`[认证] 解析来自 ${sourceDescription} 的JSON内容失败: ${e.message}`);
return null;
}
}
// 新增方法:动态添加账号
addAccount(index, authData) {
if (typeof index !== 'number' || index <= 0) {
return { success: false, message: "索引必须是一个正数。" };
}
if (this.initialIndices.includes(index)) {
return { success: false, message: `索引 ${index} 已作为永久账号存在。` };
}
try {
// 验证 authData 是否为有效的JSON对象
if (typeof authData !== 'object' || authData === null) {
throw new Error("提供的数据不是一个有效的对象。");
}
this.runtimeAuths.set(index, authData);
this.logger.info(`[认证] 成功添加索引为 ${index} 的临时账号。`);
return { success: true, message: `账号 ${index} 已临时添加。` };
} catch (e) {
this.logger.error(`[认证] 添加临时账号 ${index} 失败: ${e.message}`);
return { success: false, message: `添加账号失败: ${e.message}` };
}
}
// 新增方法:动态删除账号
removeAccount(index) {
if (!this.runtimeAuths.has(index)) {
return { success: false, message: `索引 ${index} 不是一个临时账号,无法移除。` };
}
this.runtimeAuths.delete(index);
this.logger.info(`[认证] 成功移除索引为 ${index} 的临时账号。`);
return { success: true, message: `账号 ${index} 已移除。` };
}
}
// ===================================================================================
// 浏览器管理模块
// ===================================================================================
class BrowserManager {
constructor(logger, config, authSource) {
this.logger = logger;
this.config = config;
this.authSource = authSource;
this.browser = null;
this.context = null;
this.page = null;
this.currentAuthIndex = 0;
this.scriptFileName = 'dark-browser.js';
if (this.config.browserExecutablePath) {
this.browserExecutablePath = this.config.browserExecutablePath;
this.logger.info(`[系统] 使用环境变量 CAMOUFOX_EXECUTABLE_PATH 指定的浏览器路径。`);
} else {
const platform = os.platform();
if (platform === 'win32') {
this.browserExecutablePath = path.join(__dirname, 'camoufox', 'camoufox.exe');
this.logger.info(`[系统] 检测到操作系统: Windows. 将使用 'camoufox' 目录下的浏览器。`);
} else if (platform === 'linux') {
this.browserExecutablePath = path.join(__dirname, 'camoufox-linux', 'camoufox');
this.logger.info(`[系统] 检测到操作系统: Linux. 将使用 'camoufox-linux' 目录下的浏览器。`);
} else {
this.logger.error(`[系统] 不支持的操作系统: ${platform}.`);
throw new Error(`不支持的操作系统: ${platform}`);
}
}
}
async launchBrowser(authIndex) {
if (this.browser) {
this.logger.warn('尝试启动一个已在运行的浏览器实例,操作已取消。');
return;
}
const sourceDescription = this.authSource.authMode === 'env' ? `环境变量 AUTH_JSON_${authIndex}` : `文件 auth-${authIndex}.json`;
this.logger.info('==================================================');
this.logger.info(`🚀 [浏览器] 准备启动浏览器`);
this.logger.info(` • 认证源: ${sourceDescription}`);
this.logger.info(` • 浏览器路径: ${this.browserExecutablePath}`);
this.logger.info('==================================================');
if (!fs.existsSync(this.browserExecutablePath)) {
this.logger.error(`❌ [浏览器] 找不到浏览器可执行文件: ${this.browserExecutablePath}`);
throw new Error(`找不到浏览器可执行文件路径: ${this.browserExecutablePath}`);
}
const storageStateObject = this.authSource.getAuth(authIndex);
if (!storageStateObject) {
this.logger.error(`❌ [浏览器] 无法获取或解析索引为 ${authIndex} 的认证信息。`);
throw new Error(`获取或解析索引 ${authIndex} 的认证源失败。`);
}
if (storageStateObject.cookies && Array.isArray(storageStateObject.cookies)) {
let fixedCount = 0;
const validSameSiteValues = ['Lax', 'Strict', 'None'];
storageStateObject.cookies.forEach(cookie => {
if (!validSameSiteValues.includes(cookie.sameSite)) {
this.logger.warn(`[认证] 发现无效的 sameSite 值: '${cookie.sameSite}',正在自动修正为 'None'。`);
cookie.sameSite = 'None';
fixedCount++;
}
});
if (fixedCount > 0) {
this.logger.info(`[认证] 自动修正了 ${fixedCount} 个无效的 Cookie 'sameSite' 属性。`);
}
}
let buildScriptContent;
try {
const scriptFilePath = path.join(__dirname, this.scriptFileName);
if (fs.existsSync(scriptFilePath)) {
buildScriptContent = fs.readFileSync(scriptFilePath, 'utf-8');
this.logger.info(`✅ [浏览器] 成功读取注入脚本 "${this.scriptFileName}"`);
} else {
this.logger.warn(`[浏览器] 未找到注入脚本 "${this.scriptFileName}"。将无注入继续运行。`);
buildScriptContent = "console.log('dark-browser.js not found, running without injection.');";
}
} catch (error) {
this.logger.error(`❌ [浏览器] 无法读取注入脚本 "${this.scriptFileName}"!`);
throw error;
}
try {
this.browser = await firefox.launch({
headless: true,
executablePath: this.browserExecutablePath,
});
this.browser.on('disconnected', () => {
this.logger.error('❌ [浏览器] 浏览器意外断开连接!服务器可能需要重启。');
this.browser = null; this.context = null; this.page = null;
});
this.context = await this.browser.newContext({
storageState: storageStateObject,
viewport: { width: 1280, height: 720 },
});
this.page = await this.context.newPage();
this.logger.info(`[浏览器] 正在加载账号 ${authIndex} 并访问目标网页...`);
const targetUrl = 'https://aistudio.google.com/u/0/apps/bundled/blank?showPreview=true&showCode=true&showAssistant=true';
await this.page.goto(targetUrl, { timeout: 120000, waitUntil: 'networkidle' });
this.logger.info('[浏览器] 网页加载完成,正在注入客户端脚本...');
const editorContainerLocator = this.page.locator('div.monaco-editor').first();
this.logger.info('[浏览器] 等待编辑器出现,最长120秒...');
await editorContainerLocator.waitFor({ state: 'visible', timeout: 120000 });
this.logger.info('[浏览器] 编辑器已出现,准备粘贴脚本。');
this.logger.info('[浏览器] 等待5秒,之后将在页面下方执行一次模拟点击以确保页面激活...');
await this.page.waitForTimeout(5000);
const viewport = this.page.viewportSize();
if (viewport) {
const clickX = viewport.width / 2;
const clickY = viewport.height - 120;
this.logger.info(`[浏览器] 在页面底部中心位置 (x≈${Math.round(clickX)}, y=${clickY}) 执行点击。`);
await this.page.mouse.click(clickX, clickY);
} else {
this.logger.warn('[浏览器] 无法获取视窗大小,跳过页面底部模拟点击。');
}
await editorContainerLocator.click({ timeout: 120000 });
await this.page.evaluate(text => navigator.clipboard.writeText(text), buildScriptContent);
const isMac = os.platform() === 'darwin';
const pasteKey = isMac ? 'Meta+V' : 'Control+V';
await this.page.keyboard.press(pasteKey);
this.logger.info('[浏览器] 脚本已粘贴。浏览器端初始化完成。');
this.currentAuthIndex = authIndex;
this.logger.info('==================================================');
this.logger.info(`✅ [浏览器] 账号 ${authIndex} 初始化成功!`);
this.logger.info('✅ [浏览器] 浏览器客户端已准备就绪。');
this.logger.info('==================================================');
} catch (error) {
this.logger.error(`❌ [浏览器] 账号 ${authIndex} 初始化失败: ${error.message}`);
if (this.browser) {
await this.browser.close();
this.browser = null;
}
throw error;
}
}
async closeBrowser() {
if (this.browser) {
this.logger.info('[浏览器] 正在关闭当前浏览器实例...');
await this.browser.close();
this.browser = null; this.context = null; this.page = null;
this.logger.info('[浏览器] 浏览器已关闭。');
}
}
async switchAccount(newAuthIndex) {
this.logger.info(`🔄 [浏览器] 开始账号切换: 从 ${this.currentAuthIndex} 到 ${newAuthIndex}`);
await this.closeBrowser();
await this.launchBrowser(newAuthIndex);
this.logger.info(`✅ [浏览器] 账号切换完成,当前账号: ${this.currentAuthIndex}`);
}
}
// ===================================================================================
// 代理服务模块
// ===================================================================================
class LoggingService {
constructor(serviceName = 'ProxyServer') {
this.serviceName = serviceName;
}
_getFormattedTime() {
// 使用 toLocaleTimeString 并指定 en-GB 区域来保证输出为 HH:mm:ss 格式
return new Date().toLocaleTimeString('en-GB', { hour12: false });
}
// 用于 ERROR, WARN, DEBUG 等带有级别标签的日志
_formatMessage(level, message) {
const time = this._getFormattedTime();
return `[${level}] ${time} [${this.serviceName}] - ${message}`;
}
// info 级别使用特殊格式,不显示 [INFO]
info(message) {
const time = this._getFormattedTime();
console.log(`${time} [${this.serviceName}] - ${message}`);
}
error(message) {
console.error(this._formatMessage('ERROR', message));
}
warn(message) {
console.warn(this._formatMessage('WARN', message));
}
debug(message) {
// 修正:移除内部对环境变量的检查。
// 现在,只要调用此方法,就会打印日志。
// 是否调用取决于程序其他部分的 this.config.debugMode 判断。
console.debug(this._formatMessage('DEBUG', message));
}
}
class MessageQueue extends EventEmitter {
constructor(timeoutMs = 1200000) {
super();
this.messages = [];
this.waitingResolvers = [];
this.defaultTimeout = timeoutMs;
this.closed = false;
}
enqueue(message) {
if (this.closed) return;
if (this.waitingResolvers.length > 0) {
const resolver = this.waitingResolvers.shift();
resolver.resolve(message);
} else {
this.messages.push(message);
}
}
async dequeue(timeoutMs = this.defaultTimeout) {
if (this.closed) {
throw new Error('队列已关闭');
}
return new Promise((resolve, reject) => {
if (this.messages.length > 0) {
resolve(this.messages.shift());
return;
}
const resolver = { resolve, reject };
this.waitingResolvers.push(resolver);
const timeoutId = setTimeout(() => {
const index = this.waitingResolvers.indexOf(resolver);
if (index !== -1) {
this.waitingResolvers.splice(index, 1);
reject(new Error('队列超时'));
}
}, timeoutMs);
resolver.timeoutId = timeoutId;
});
}
close() {
this.closed = true;
this.waitingResolvers.forEach(resolver => {
clearTimeout(resolver.timeoutId);
resolver.reject(new Error('队列已关闭'));
});
this.waitingResolvers = [];
this.messages = [];
}
}
class ConnectionRegistry extends EventEmitter {
constructor(logger) {
super();
this.logger = logger;
this.connections = new Set();
this.messageQueues = new Map();
}
addConnection(websocket, clientInfo) {
this.connections.add(websocket);
this.logger.info(`[服务器] 内部WebSocket客户端已连接 (来自: ${clientInfo.address})`);
websocket.on('message', (data) => this._handleIncomingMessage(data.toString()));
websocket.on('close', () => this._removeConnection(websocket));
websocket.on('error', (error) => this.logger.error(`[服务器] 内部WebSocket连接错误: ${error.message}`));
this.emit('connectionAdded', websocket);
}
_removeConnection(websocket) {
this.connections.delete(websocket);
this.logger.warn('[服务器] 内部WebSocket客户端连接断开');
this.messageQueues.forEach(queue => queue.close());
this.messageQueues.clear();
this.emit('connectionRemoved', websocket);
}
_handleIncomingMessage(messageData) {
try {
const parsedMessage = JSON.parse(messageData);
const requestId = parsedMessage.request_id;
if (!requestId) {
this.logger.warn('[服务器] 收到无效消息:缺少request_id');
return;
}
const queue = this.messageQueues.get(requestId);
if (queue) {
this._routeMessage(parsedMessage, queue);
}
} catch (error) {
this.logger.error('[服务器] 解析内部WebSocket消息失败');
}
}
_routeMessage(message, queue) {
const { event_type } = message;
switch (event_type) {
case 'response_headers': case 'chunk': case 'error':
queue.enqueue(message);
break;
case 'stream_close':
queue.enqueue({ type: 'STREAM_END' });
break;
default:
this.logger.warn(`[服务器] 未知的内部事件类型: ${event_type}`);
}
}
hasActiveConnections() { return this.connections.size > 0; }
getFirstConnection() { return this.connections.values().next().value; }
createMessageQueue(requestId) {
const queue = new MessageQueue();
this.messageQueues.set(requestId, queue);
return queue;
}
removeMessageQueue(requestId) {
const queue = this.messageQueues.get(requestId);
if (queue) {
queue.close();
this.messageQueues.delete(requestId);
}
}
}
class RequestHandler {
constructor(serverSystem, connectionRegistry, logger, browserManager, config, authSource) {
this.serverSystem = serverSystem;
this.connectionRegistry = connectionRegistry;
this.logger = logger;
this.browserManager = browserManager;
this.config = config;
this.authSource = authSource;
this.maxRetries = this.config.maxRetries;
this.retryDelay = this.config.retryDelay;
this.failureCount = 0;
this.isAuthSwitching = false;
}
get currentAuthIndex() {
return this.browserManager.currentAuthIndex;
}
_getNextAuthIndex() {
const available = this.authSource.getAvailableIndices();
if (available.length === 0) return null;
if (available.length === 1) return available[0];
const currentIndexInArray = available.indexOf(this.currentAuthIndex);
if (currentIndexInArray === -1) {
this.logger.warn(`[认证] 当前索引 ${this.currentAuthIndex} 不在可用列表中,将切换到第一个可用索引。`);
return available[0];
}
const nextIndexInArray = (currentIndexInArray + 1) % available.length;
return available[nextIndexInArray];
}
async _switchToNextAuth() {
if (this.isAuthSwitching) {
this.logger.info('🔄 [认证] 正在切换账号,跳过重复切换');
return;
}
this.isAuthSwitching = true;
const nextAuthIndex = this._getNextAuthIndex();
const totalAuthCount = this.authSource.getAvailableIndices().length;
if (nextAuthIndex === null) {
this.logger.error('🔴 [认证] 无法切换账号,因为没有可用的认证源!');
this.isAuthSwitching = false;
throw new Error('没有可用的认证源可以切换。');
}
this.logger.info('==================================================');
this.logger.info(`🔄 [认证] 开始账号切换流程`);
this.logger.info(` • 失败次数: ${this.failureCount}/${this.config.failureThreshold > 0 ? this.config.failureThreshold : 'N/A'}`);
this.logger.info(` • 当前账号索引: ${this.currentAuthIndex}`);
this.logger.info(` • 目标账号索引: ${nextAuthIndex}`);
this.logger.info(` • 可用账号总数: ${totalAuthCount}`);
this.logger.info('==================================================');
try {
await this.browserManager.switchAccount(nextAuthIndex);
this.failureCount = 0;
this.logger.info('==================================================');
this.logger.info(`✅ [认证] 成功切换到账号索引 ${this.currentAuthIndex}`);
this.logger.info(`✅ [认证] 失败计数已重置为0`);
this.logger.info('==================================================');
} catch (error) {
this.logger.error('==================================================');
this.logger.error(`❌ [认证] 切换账号失败: ${error.message}`);
this.logger.error('==================================================');
throw error;
} finally {
this.isAuthSwitching = false;
}
}
_parseAndCorrectErrorDetails(errorDetails) {
const correctedDetails = { ...errorDetails };
this.logger.debug(`[错误解析器] 原始错误详情: status=${correctedDetails.status}, message="${correctedDetails.message}"`);
if (correctedDetails.message && typeof correctedDetails.message === 'string') {
const regex = /(?:HTTP|status code)\s+(\d{3})/;
const match = correctedDetails.message.match(regex);
if (match && match[1]) {
const parsedStatus = parseInt(match[1], 10);
if (parsedStatus >= 400 && parsedStatus <= 599) {
if (correctedDetails.status !== parsedStatus) {
this.logger.warn(`[错误解析器] 修正了错误状态码!原始: ${correctedDetails.status}, 从消息中解析得到: ${parsedStatus}`);
correctedDetails.status = parsedStatus;
} else {
this.logger.debug(`[错误解析器] 解析的状态码 (${parsedStatus}) 与原始状态码一致,无需修正。`);
}
}
}
}
return correctedDetails;
}
async _handleRequestFailureAndSwitch(errorDetails, res) {
// 新增:在调试模式下打印完整的原始错误信息
if (this.config.debugMode) {
this.logger.debug(`[认证][调试] 收到来自浏览器的完整错误详情:\n${JSON.stringify(errorDetails, null, 2)}`);
}
const correctedDetails = { ...errorDetails };
if (correctedDetails.message && typeof correctedDetails.message === 'string') {
const regex = /(?:HTTP|status code)\s*(\d{3})|"code"\s*:\s*(\d{3})/;
const match = correctedDetails.message.match(regex);
const parsedStatusString = match ? (match[1] || match[2]) : null;
if (parsedStatusString) {
const parsedStatus = parseInt(parsedStatusString, 10);
if (parsedStatus >= 400 && parsedStatus <= 599 && correctedDetails.status !== parsedStatus) {
this.logger.warn(`[认证] 修正了错误状态码!原始: ${correctedDetails.status}, 从消息中解析得到: ${parsedStatus}`);
correctedDetails.status = parsedStatus;
}
}
}
const isImmediateSwitch = this.config.immediateSwitchStatusCodes.includes(correctedDetails.status);
if (isImmediateSwitch) {
this.logger.warn(`🔴 [认证] 收到状态码 ${correctedDetails.status} (已修正),触发立即切换账号...`);
if (res) this._sendErrorChunkToClient(res, `收到状态码 ${correctedDetails.status},正在尝试切换账号...`);
try {
await this._switchToNextAuth();
if (res) this._sendErrorChunkToClient(res, `已切换到账号索引 ${this.currentAuthIndex},请重试`);
} catch (switchError) {
this.logger.error(`🔴 [认证] 账号切换失败: ${switchError.message}`);
if (res) this._sendErrorChunkToClient(res, `切换账号失败: ${switchError.message}`);
}
return;
}
if (this.config.failureThreshold > 0) {
this.failureCount++;
this.logger.warn(`⚠️ [认证] 请求失败 - 失败计数: ${this.failureCount}/${this.config.failureThreshold} (当前账号索引: ${this.currentAuthIndex}, 状态码: ${correctedDetails.status})`);
if (this.failureCount >= this.config.failureThreshold) {
this.logger.warn(`🔴 [认证] 达到失败阈值!准备切换账号...`);
if (res) this._sendErrorChunkToClient(res, `连续失败${this.failureCount}次,正在尝试切换账号...`);
try {
await this._switchToNextAuth();
if (res) this._sendErrorChunkToClient(res, `已切换到账号索引 ${this.currentAuthIndex},请重试`);
} catch (switchError) {
this.logger.error(`🔴 [认证] 账号切换失败: ${switchError.message}`);
if (res) this._sendErrorChunkToClient(res, `切换账号失败: ${switchError.message}`);
}
}
} else {
this.logger.warn(`[认证] 请求失败 (状态码: ${correctedDetails.status})。基于计数的自动切换已禁用 (failureThreshold=0)`);
}
}
_getModelFromRequest(req) {
let body = req.body;
if (Buffer.isBuffer(body)) {
try {
body = JSON.parse(body.toString('utf-8'));
} catch (e) { body = {}; }
} else if (typeof body === 'string') {
try {
body = JSON.parse(body);
} catch (e) { body = {}; }
}
if (body && typeof body === 'object') {
if (body.model) return body.model;
if (body.generation_config && body.generation_config.model) return body.generation_config.model;
}
const match = req.path.match(/\/models\/([^/:]+)/);
if (match && match[1]) {
return match[1];
}
return 'unknown_model';
}
async processRequest(req, res) {
// 关键修复 (V2): 使用 hasOwnProperty 来准确判断 'key' 参数是否存在,
// 无论其值是空字符串还是有内容。
if ((!this.config.apiKeys || this.config.apiKeys.length === 0) && req.query && req.query.hasOwnProperty('key')) {
if (this.config.debugMode) {
this.logger.debug(`[请求预处理] 服务器API密钥认证已禁用。检测到并移除了来自客户端的 'key' 查询参数 (值为: '${req.query.key}')。`);
}
delete req.query.key;
}
// 提前获取模型名称和当前账号
const modelName = this._getModelFromRequest(req);
const currentAccount = this.currentAuthIndex;
// 新增的合并日志行,报告路径、账号和模型
this.logger.info(`[请求] ${req.method} ${req.path} | 账号: ${currentAccount} | 模型: 🤖 ${modelName}`);
// --- 升级的统计逻辑 ---
this.serverSystem.stats.totalCalls++;
if (this.serverSystem.stats.accountCalls[currentAccount]) {
this.serverSystem.stats.accountCalls[currentAccount].total = (this.serverSystem.stats.accountCalls[currentAccount].total || 0) + 1;
this.serverSystem.stats.accountCalls[currentAccount].models[modelName] = (this.serverSystem.stats.accountCalls[currentAccount].models[modelName] || 0) + 1;
} else {
this.serverSystem.stats.accountCalls[currentAccount] = {
total: 1,
models: { [modelName]: 1 }
};
}
if (!this.connectionRegistry.hasActiveConnections()) {
return this._sendErrorResponse(res, 503, '没有可用的浏览器连接');
}
const requestId = this._generateRequestId();
const proxyRequest = this._buildProxyRequest(req, requestId);
const messageQueue = this.connectionRegistry.createMessageQueue(requestId);
try {
if (this.serverSystem.streamingMode === 'fake') {
await this._handlePseudoStreamResponse(proxyRequest, messageQueue, req, res);
} else {
await this._handleRealStreamResponse(proxyRequest, messageQueue, res);
}
} catch (error) {
this._handleRequestError(error, res);
} finally {
this.connectionRegistry.removeMessageQueue(requestId);
}
}
_generateRequestId() { return `${Date.now()}_${Math.random().toString(36).substring(2, 11)}`; }
_buildProxyRequest(req, requestId) {
const proxyRequest = {
path: req.path,
method: req.method,
headers: req.headers,
query_params: req.query,
request_id: requestId,
streaming_mode: this.serverSystem.streamingMode
};
// 关键修正:只在允许有请求体的HTTP方法中添加body字段
if (req.method !== 'GET' && req.method !== 'HEAD') {
let requestBodyString;
if (typeof req.body === 'object' && req.body !== null) {
requestBodyString = JSON.stringify(req.body);
} else if (typeof req.body === 'string') {
requestBodyString = req.body;
} else if (Buffer.isBuffer(req.body)) {
requestBodyString = req.body.toString('utf-8');
} else {
requestBodyString = '';
}
proxyRequest.body = requestBodyString;
}
return proxyRequest;
}
_forwardRequest(proxyRequest) {
const connection = this.connectionRegistry.getFirstConnection();
if (connection) {
connection.send(JSON.stringify(proxyRequest));
} else {
throw new Error("无法转发请求:没有可用的WebSocket连接。");
}
}
_sendErrorChunkToClient(res, errorMessage) {
const errorPayload = {
error: { message: `[代理系统提示] ${errorMessage}`, type: 'proxy_error', code: 'proxy_error' }
};
const chunk = `data: ${JSON.stringify(errorPayload)}\n\n`;
if (res && !res.writableEnded) {
res.write(chunk);
this.logger.info(`[请求] 已向客户端发送标准错误信号: ${errorMessage}`);
}
}
_getKeepAliveChunk(req) {
if (req.path.includes('chat/completions')) {
const payload = { id: `chatcmpl-${this._generateRequestId()}`, object: "chat.completion.chunk", created: Math.floor(Date.now() / 1000), model: "gpt-4", choices: [{ index: 0, delta: {}, finish_reason: null }] };
return `data: ${JSON.stringify(payload)}\n\n`;
}
if (req.path.includes('generateContent') || req.path.includes('streamGenerateContent')) {
const payload = { candidates: [{ content: { parts: [{ text: "" }], role: "model" }, finishReason: null, index: 0, safetyRatings: [] }] };
return `data: ${JSON.stringify(payload)}\n\n`;
}
return 'data: {}\n\n';
}
async _handlePseudoStreamResponse(proxyRequest, messageQueue, req, res) {
const originalPath = req.path;
const isStreamRequest = originalPath.includes(':stream');
this.logger.info(`[请求] 假流式处理流程启动,路径: "${originalPath}",判定为: ${isStreamRequest ? '流式请求' : '非流式请求'}`);
let connectionMaintainer = null;
if (isStreamRequest) {
res.status(200).set({
'Content-Type': 'text/event-stream',
'Cache-Control': 'no-cache',
'Connection': 'keep-alive'
});
const keepAliveChunk = this._getKeepAliveChunk(req);
connectionMaintainer = setInterval(() => { if (!res.writableEnded) res.write(keepAliveChunk); }, 2000);
}
try {
let lastMessage, requestFailed = false;
for (let attempt = 1; attempt <= this.maxRetries; attempt++) {
this.logger.info(`[请求] 请求尝试 #${attempt}/${this.maxRetries}...`);
this._forwardRequest(proxyRequest);
lastMessage = await messageQueue.dequeue();
if (lastMessage.event_type === 'error' && lastMessage.status >= 400 && lastMessage.status <= 599) {
const correctedMessage = this._parseAndCorrectErrorDetails(lastMessage);
await this._handleRequestFailureAndSwitch(correctedMessage, isStreamRequest ? res : null);
const errorText = `收到 ${correctedMessage.status} 错误。${attempt < this.maxRetries ? `将在 ${this.retryDelay / 1000}秒后重试...` : '已达到最大重试次数。'}`;
this.logger.warn(`[请求] ${errorText}`);
if (isStreamRequest) {
this._sendErrorChunkToClient(res, errorText);
}
if (attempt < this.maxRetries) {
await new Promise(resolve => setTimeout(resolve, this.retryDelay));
continue;
}
requestFailed = true;
}
break;
}
if (lastMessage.event_type === 'error' || requestFailed) {
const finalError = this._parseAndCorrectErrorDetails(lastMessage);
if (!res.headersSent) {
this._sendErrorResponse(res, finalError.status, `请求失败: ${finalError.message}`);
} else {
this._sendErrorChunkToClient(res, `请求最终失败 (状态码: ${finalError.status}): ${finalError.message}`);
}
return;
}
if (this.failureCount > 0) {
this.logger.info(`✅ [认证] 请求成功 - 失败计数已从 ${this.failureCount} 重置为 0`);
}
this.failureCount = 0;
const dataMessage = await messageQueue.dequeue();
const endMessage = await messageQueue.dequeue();
if (endMessage.type !== 'STREAM_END') this.logger.warn('[请求] 未收到预期的流结束信号。');
if (isStreamRequest) {
if (dataMessage.data) {
res.write(`data: ${dataMessage.data}\n\n`);
}
res.write('data: [DONE]\n\n');
this.logger.info('[请求] 已将完整响应作为模拟SSE事件发送。');
} else {
this.logger.info('[请求] 准备发送 application/json 响应。');
if (dataMessage.data) {
try {
const jsonData = JSON.parse(dataMessage.data);
res.status(200).json(jsonData);
} catch (e) {
this.logger.error(`[请求] 无法将来自浏览器的响应解析为JSON: ${e.message}`);
this._sendErrorResponse(res, 500, '代理内部错误:无法解析来自后端的响应。');
}
} else {
this._sendErrorResponse(res, 500, '代理内部错误:后端未返回有效数据。');
}
}
} catch (error) {
this.logger.error(`[请求] 假流式处理期间发生意外错误: ${error.message}`);
if (!res.headersSent) {
this._handleRequestError(error, res);
} else {
this._sendErrorChunkToClient(res, `处理失败: ${error.message}`);
}
} finally {
if (connectionMaintainer) clearInterval(connectionMaintainer);
if (!res.writableEnded) res.end();
this.logger.info('[请求] 假流式响应处理结束。');
}
}
async _handleRealStreamResponse(proxyRequest, messageQueue, res) {
let headerMessage, requestFailed = false;
for (let attempt = 1; attempt <= this.maxRetries; attempt++) {
this.logger.info(`[请求] 请求尝试 #${attempt}/${this.maxRetries}...`);
this._forwardRequest(proxyRequest);
headerMessage = await messageQueue.dequeue();
if (headerMessage.event_type === 'error' && headerMessage.status >= 400 && headerMessage.status <= 599) {
const correctedMessage = this._parseAndCorrectErrorDetails(headerMessage);
await this._handleRequestFailureAndSwitch(correctedMessage, null);
this.logger.warn(`[请求] 收到 ${correctedMessage.status} 错误,将在 ${this.retryDelay / 1000}秒后重试...`);
if (attempt < this.maxRetries) {
await new Promise(resolve => setTimeout(resolve, this.retryDelay));
continue;
}
requestFailed = true;
}
break;
}
if (headerMessage.event_type === 'error' || requestFailed) {
const finalError = this._parseAndCorrectErrorDetails(headerMessage);
return this._sendErrorResponse(res, finalError.status, finalError.message);
}
if (this.failureCount > 0) {
this.logger.info(`✅ [认证] 请求成功 - 失败计数已从 ${this.failureCount} 重置为 0`);
}
this.failureCount = 0;
this._setResponseHeaders(res, headerMessage);
this.logger.info('[请求] 已向客户端发送真实响应头,开始流式传输...');
try {
while (true) {
const dataMessage = await messageQueue.dequeue(30000);
if (dataMessage.type === 'STREAM_END') { this.logger.info('[请求] 收到流结束信号。'); break; }
if (dataMessage.data) res.write(dataMessage.data);
}
} catch (error) {
if (error.message !== '队列超时') throw error;
this.logger.warn('[请求] 真流式响应超时,可能流已正常结束。');
} finally {
if (!res.writableEnded) res.end();
this.logger.info('[请求] 真流式响应连接已关闭。');
}
}
_setResponseHeaders(res, headerMessage) {
res.status(headerMessage.status || 200);
const headers = headerMessage.headers || {};
Object.entries(headers).forEach(([name, value]) => {
if (name.toLowerCase() !== 'content-length') res.set(name, value);
});
}
_handleRequestError(error, res) {
if (res.headersSent) {
this.logger.error(`[请求] 请求处理错误 (头已发送): ${error.message}`);
if (this.serverSystem.streamingMode === 'fake') this._sendErrorChunkToClient(res, `处理失败: ${error.message}`);
if (!res.writableEnded) res.end();
} else {
this.logger.error(`[请求] 请求处理错误: ${error.message}`);
const status = error.message.includes('超时') ? 504 : 500;
this._sendErrorResponse(res, status, `代理错误: ${error.message}`);
}
}
_sendErrorResponse(res, status, message) {
if (!res.headersSent) res.status(status || 500).type('text/plain').send(message);
}
}
class ProxyServerSystem extends EventEmitter {
constructor() {
super();
this.logger = new LoggingService('ProxySystem');
this._loadConfiguration();
this.streamingMode = this.config.streamingMode;
// 升级后的统计结构
this.stats = {
totalCalls: 0,
accountCalls: {} // e.g., { "1": { total: 10, models: { "gemini-pro": 5, "gpt-4": 5 } } }
};
this.authSource = new AuthSource(this.logger);
this.browserManager = new BrowserManager(this.logger, this.config, this.authSource);
this.connectionRegistry = new ConnectionRegistry(this.logger);
this.requestHandler = new RequestHandler(this, this.connectionRegistry, this.logger, this.browserManager, this.config, this.authSource);
this.httpServer = null;
this.wsServer = null;
}
_loadConfiguration() {
let config = {
httpPort: 8889, host: '0.0.0.0', wsPort: 9998, streamingMode: 'real',
failureThreshold: 0,
maxRetries: 3, retryDelay: 2000, browserExecutablePath: null,
apiKeys: [],
immediateSwitchStatusCodes: [],
initialAuthIndex: null,
debugMode: false,
};
const configPath = path.join(__dirname, 'config.json');
try {
if (fs.existsSync(configPath)) {
const fileConfig = JSON.parse(fs.readFileSync(configPath, 'utf-8'));
config = { ...config, ...fileConfig };
this.logger.info('[系统] 已从 config.json 加载配置。');
}
} catch (error) {
this.logger.warn(`[系统] 无法读取或解析 config.json: ${error.message}`);
}
if (process.env.PORT) config.httpPort = parseInt(process.env.PORT, 10) || config.httpPort;
if (process.env.HOST) config.host = process.env.HOST;
if (process.env.STREAMING_MODE) config.streamingMode = process.env.STREAMING_MODE;
if (process.env.FAILURE_THRESHOLD) config.failureThreshold = parseInt(process.env.FAILURE_THRESHOLD, 10) || config.failureThreshold;
if (process.env.MAX_RETRIES) config.maxRetries = parseInt(process.env.MAX_RETRIES, 10) || config.maxRetries;
if (process.env.RETRY_DELAY) config.retryDelay = parseInt(process.env.RETRY_DELAY, 10) || config.retryDelay;
if (process.env.CAMOUFOX_EXECUTABLE_PATH) config.browserExecutablePath = process.env.CAMOUFOX_EXECUTABLE_PATH;
if (process.env.API_KEYS) {
config.apiKeys = process.env.API_KEYS.split(',');
}
if (process.env.DEBUG_MODE) {
config.debugMode = process.env.DEBUG_MODE === 'true';
}
if (process.env.INITIAL_AUTH_INDEX) {
const envIndex = parseInt(process.env.INITIAL_AUTH_INDEX, 10);
if (!isNaN(envIndex) && envIndex > 0) {
config.initialAuthIndex = envIndex;
}
}
let rawCodes = process.env.IMMEDIATE_SWITCH_STATUS_CODES;
let codesSource = '环境变量';
if (!rawCodes && config.immediateSwitchStatusCodes && Array.isArray(config.immediateSwitchStatusCodes)) {
rawCodes = config.immediateSwitchStatusCodes.join(',');
codesSource = 'config.json 文件';
}
if (rawCodes && typeof rawCodes === 'string') {
config.immediateSwitchStatusCodes = rawCodes
.split(',')
.map(code => parseInt(String(code).trim(), 10))
.filter(code => !isNaN(code) && code >= 400 && code <= 599);
if (config.immediateSwitchStatusCodes.length > 0) {
this.logger.info(`[系统] 已从 ${codesSource} 加载“立即切换状态码”。`);
}
} else {
config.immediateSwitchStatusCodes = [];
}
if (Array.isArray(config.apiKeys)) {
config.apiKeys = config.apiKeys.map(k => String(k).trim()).filter(k => k);
} else {
config.apiKeys = [];
}
this.config = config;
this.logger.info('================ [ 生效配置 ] ================');
this.logger.info(` HTTP 服务端口: ${this.config.httpPort}`);
this.logger.info(` 监听地址: ${this.config.host}`);
this.logger.info(` 流式模式: ${this.config.streamingMode}`);
this.logger.info(` 调试模式: ${this.config.debugMode ? '已开启' : '已关闭'}`);
if (this.config.initialAuthIndex) {
this.logger.info(` 指定初始认证索引: ${this.config.initialAuthIndex}`);
}
this.logger.info(` 失败计数切换: ${this.config.failureThreshold > 0 ? `连续 ${this.config.failureThreshold} 次失败后切换` : '已禁用'}`);
this.logger.info(` 立即切换状态码: ${this.config.immediateSwitchStatusCodes.length > 0 ? this.config.immediateSwitchStatusCodes.join(', ') : '已禁用'}`);
this.logger.info(` 单次请求最大重试: ${this.config.maxRetries}次`);
this.logger.info(` 重试间隔: ${this.config.retryDelay}ms`);
if (this.config.apiKeys && this.config.apiKeys.length > 0) {
this.logger.info(` API 密钥认证: 已启用 (${this.config.apiKeys.length} 个密钥)`);
} else {
this.logger.info(` API 密钥认证: 已禁用`);
}
this.logger.info('=============================================================');
}
async start() {
try {
// 初始化统计对象
this.authSource.getAvailableIndices().forEach(index => {
this.stats.accountCalls[index] = { total: 0, models: {} };
});
let startupIndex = this.authSource.getFirstAvailableIndex();
const suggestedIndex = this.config.initialAuthIndex;
if (suggestedIndex) {
if (this.authSource.getAvailableIndices().includes(suggestedIndex)) {
this.logger.info(`[系统] 使用配置中指定的有效启动索引: ${suggestedIndex}`);
startupIndex = suggestedIndex;
} else {
this.logger.warn(`[系统] 配置中指定的启动索引 ${suggestedIndex} 无效或不存在,将使用第一个可用索引: ${startupIndex}`);
}
} else {
this.logger.info(`[系统] 未指定启动索引,将自动使用第一个可用索引: ${startupIndex}`);
}
await this.browserManager.launchBrowser(startupIndex);
await this._startHttpServer();
await this._startWebSocketServer();
this.logger.info(`[系统] 代理服务器系统启动完成。`);
this.emit('started');
} catch (error) {
this.logger.error(`[系统] 启动失败: ${error.message}`);
this.emit('error', error);
process.exit(1); // 启动失败时退出
}
}
_createDebugLogMiddleware() {
return (req, res, next) => {
if (!this.config.debugMode) {
return next();
}
const requestId = this.requestHandler._generateRequestId();
const log = this.logger.info.bind(this.logger);
log(`\n\n--- [调试] 开始处理入站请求 (${requestId}) ---`);
log(`[调试][${requestId}] 客户端 IP: ${req.ip}`);
log(`[调试][${requestId}] 方法: ${req.method}`);
log(`[调试][${requestId}] URL: ${req.originalUrl}`);
log(`[调试][${requestId}] 请求头: ${JSON.stringify(req.headers, null, 2)}`);
let bodyContent = '无或空';
if (req.body) {
if (Buffer.isBuffer(req.body) && req.body.length > 0) {
try {
bodyContent = JSON.stringify(JSON.parse(req.body.toString('utf-8')), null, 2);
} catch (e) {
bodyContent = `[无法解析为JSON的Buffer, 大小: ${req.body.length} 字节]`;
}
} else if (typeof req.body === 'object' && Object.keys(req.body).length > 0) {
bodyContent = JSON.stringify(req.body, null, 2);
}
}
log(`[调试][${requestId}] 请求体:\n${bodyContent}`);
log(`--- [调试] 结束处理入站请求 (${requestId}) ---\n\n`);
next();
};
}
_createAuthMiddleware() {
return (req, res, next) => {
const serverApiKeys = this.config.apiKeys;
if (!serverApiKeys || serverApiKeys.length === 0) {
return next();
}
let clientKey = null;
let keySource = null;
const headers = req.headers;
const xGoogApiKey = headers['x-goog-api-key'] || headers['x_goog_api_key'];
const xApiKey = headers['x-api-key'] || headers['x_api_key'];
const authHeader = headers.authorization;
if (xGoogApiKey) {
clientKey = xGoogApiKey;
keySource = 'x-goog-api-key 请求头';
} else if (authHeader && authHeader.startsWith('Bearer ')) {
clientKey = authHeader.substring(7);
keySource = 'Authorization 请求头';
} else if (xApiKey) {
clientKey = xApiKey;
keySource = 'X-API-Key 请求头';
} else if (req.query.key) {
clientKey = req.query.key;
keySource = '查询参数';
}
if (clientKey) {
if (serverApiKeys.includes(clientKey)) {
if (this.config.debugMode) {
this.logger.debug(`[认证][调试] 在 '${keySource}' 中找到API密钥,验证通过。`);
}
if (keySource === '查询参数') {
delete req.query.key;
}
return next();
} else {
if (this.config.debugMode) {
this.logger.warn(`[认证][调试] 拒绝请求: 无效的API密钥。IP: ${req.ip}, 路径: ${req.path}`);
this.logger.debug(`[认证][调试] 来源: ${keySource}`);
this.logger.debug(`[认证][调试] 提供的错误密钥: '${clientKey}'`);
this.logger.debug(`[认证][调试] 已加载的有效密钥: [${serverApiKeys.join(', ')}]`);
} else {
this.logger.warn(`[认证] 拒绝请求: 无效的API密钥。IP: ${req.ip}, 路径: ${req.path}`);
}
return res.status(401).json({ error: { message: "提供了无效的API密钥。" } });
}
}
this.logger.warn(`[认证] 拒绝受保护的请求: 缺少API密钥。IP: ${req.ip}, 路径: ${req.path}`);
if (this.config.debugMode) {
this.logger.debug(`[认证][调试] 未在任何标准位置找到API密钥。`);
this.logger.debug(`[认证][调试] 搜索的请求头: ${JSON.stringify(headers, null, 2)}`);
this.logger.debug(`[认证][调试] 搜索的查询参数: ${JSON.stringify(req.query)}`);
this.logger.debug(`[认证][调试] 已加载的有效密钥: [${serverApiKeys.join(', ')}]`);
}
return res.status(401).json({ error: { message: "访问被拒绝。未在请求头或查询参数中找到有效的API密钥。" } });
};
}
async _startHttpServer() {
const app = this._createExpressApp();
this.httpServer = http.createServer(app);
return new Promise((resolve) => {
this.httpServer.listen(this.config.httpPort, this.config.host, () => {
this.logger.info(`[系统] HTTP服务器已在 http://${this.config.host}:${this.config.httpPort} 上监听`);
this.logger.info(`[系统] 仪表盘可在 http://${this.config.host}:${this.config.httpPort}/dashboard 访问`);
resolve();
});
});
}
_createExpressApp() {
const app = express();
app.use(express.json({ limit: '100mb' }));
app.use(express.raw({ type: '*/*', limit: '100mb' }));
app.use((req, res, next) => {
if (req.is('application/json') && typeof req.body === 'object' && !Buffer.isBuffer(req.body)) {
// Already parsed correctly by express.json()
} else if (Buffer.isBuffer(req.body)) {
const bodyStr = req.body.toString('utf-8');
if (bodyStr) {
try {
req.body = JSON.parse(bodyStr);
} catch (e) {
// Not JSON, leave as buffer.
}
}
}
next();
});
app.use(this._createDebugLogMiddleware());
// --- 仪表盘和API端点 ---
// 新增: 将根目录重定向到仪表盘
app.get('/', (req, res) => {
res.redirect('/dashboard');
});
// 公开端点:提供仪表盘HTML
app.get('/dashboard', (req, res) => {
res.send(this._getDashboardHtml());
});
// 公开端点:用于仪表盘验证API密钥
app.post('/dashboard/verify-key', (req, res) => {
const { key } = req.body;
const serverApiKeys = this.config.apiKeys;
if (!serverApiKeys || serverApiKeys.length === 0) {
this.logger.info('[管理] 服务器未配置API密钥,自动授予仪表盘访问权限。');
return res.json({ success: true });
}
if (key && serverApiKeys.includes(key)) {
this.logger.info('[管理] 仪表盘API密钥验证成功。');
return res.json({ success: true });
}
this.logger.warn(`[管理] 仪表盘API密钥验证失败。`);
res.status(401).json({ success: false, message: '无效的API密钥。' });
});
// 中间件:保护仪表盘API路由
const dashboardApiAuth = (req, res, next) => {
const serverApiKeys = this.config.apiKeys;
if (!serverApiKeys || serverApiKeys.length === 0) {
return next(); // 未配置密钥,跳过认证
}
const clientKey = req.headers['x-dashboard-auth'];
if (clientKey && serverApiKeys.includes(clientKey)) {
return next();
}
this.logger.warn(`[管理] 拒绝未经授权的仪表盘API请求。IP: ${req.ip}, 路径: ${req.path}`);
res.status(401).json({ error: { message: 'Unauthorized dashboard access' } });
};
const dashboardApiRouter = express.Router();
dashboardApiRouter.use(dashboardApiAuth);
dashboardApiRouter.get('/data', (req, res) => {
res.json({
status: {
uptime: process.uptime(),
streamingMode: this.streamingMode,
debugMode: this.config.debugMode,
authMode: this.authSource.authMode,
apiKeyAuth: (this.config.apiKeys && this.config.apiKeys.length > 0) ? '已启用' : '已禁用',
isAuthSwitching: this.requestHandler.isAuthSwitching,
browserConnected: !!this.browserManager.browser,
internalWsClients: this.connectionRegistry.connections.size
},
auth: {
currentAuthIndex: this.requestHandler.currentAuthIndex,
accounts: this.authSource.getAccountDetails(),
failureCount: this.requestHandler.failureCount,
},
stats: this.stats,
config: this.config
});
});
dashboardApiRouter.post('/config', (req, res) => {
const newConfig = req.body;
try {
if (newConfig.hasOwnProperty('streamingMode') && ['real', 'fake'].includes(newConfig.streamingMode)) {
this.config.streamingMode = newConfig.streamingMode;
this.streamingMode = newConfig.streamingMode;
this.requestHandler.serverSystem.streamingMode = newConfig.streamingMode;
}
if (newConfig.hasOwnProperty('debugMode') && typeof newConfig.debugMode === 'boolean') {
this.config.debugMode = newConfig.debugMode;
}
if (newConfig.hasOwnProperty('failureThreshold')) {
this.config.failureThreshold = parseInt(newConfig.failureThreshold, 10) || 0;
}
if (newConfig.hasOwnProperty('maxRetries')) {
const retries = parseInt(newConfig.maxRetries, 10);
this.config.maxRetries = retries >= 0 ? retries : 3;
this.requestHandler.maxRetries = this.config.maxRetries;
}
if (newConfig.hasOwnProperty('retryDelay')) {
this.config.retryDelay = parseInt(newConfig.retryDelay, 10) || 2000;
this.requestHandler.retryDelay = this.config.retryDelay;
}
if (newConfig.hasOwnProperty('immediateSwitchStatusCodes')) {
if (Array.isArray(newConfig.immediateSwitchStatusCodes)) {
this.config.immediateSwitchStatusCodes = newConfig.immediateSwitchStatusCodes
.map(c => parseInt(c, 10))
.filter(c => !isNaN(c));
}
}
this.logger.info('[管理] 配置已通过仪表盘动态更新。');
res.status(200).json({ success: true, message: '配置已临时更新。' });
} catch (error) {
this.logger.error(`[管理] 更新配置失败: ${error.message}`);
res.status(500).json({ success: false, message: error.message });
}
});
dashboardApiRouter.post('/accounts', (req, res) => {
const { index, authData } = req.body;
if (!index || !authData) {
return res.status(400).json({ success: false, message: "必须提供索引和认证数据。" });
}
let parsedData;
try {
parsedData = (typeof authData === 'string') ? JSON.parse(authData) : authData;
} catch (e) {
return res.status(400).json({ success: false, message: "认证数据的JSON格式无效。" });
}
const result = this.authSource.addAccount(parseInt(index, 10), parsedData);
if (result.success) {
if (!this.stats.accountCalls.hasOwnProperty(index)) {
this.stats.accountCalls[index] = { total: 0, models: {} };
}
}
res.status(result.success ? 200 : 400).json(result);
});
dashboardApiRouter.delete('/accounts/:index', (req, res) => {
const index = parseInt(req.params.index, 10);
const result = this.authSource.removeAccount(index);
res.status(result.success ? 200 : 400).json(result);
});
// 挂载受保护的仪表盘API路由
app.use('/dashboard', dashboardApiRouter);
// 保护 /switch 路由
app.post('/switch', dashboardApiAuth, async (req, res) => {
this.logger.info('[管理] 接到 /switch 请求,手动触发账号切换。');
if (this.requestHandler.isAuthSwitching) {
const msg = '账号切换已在进行中,请稍后。';
this.logger.warn(`[管理] /switch 请求被拒绝: ${msg}`);
return res.status(429).send(msg);
}
const oldIndex = this.requestHandler.currentAuthIndex;
try {
await this.requestHandler._switchToNextAuth();
const newIndex = this.requestHandler.currentAuthIndex;
const message = `成功将账号从索引 ${oldIndex} 切换到 ${newIndex}。`;
this.logger.info(`[管理] 手动切换成功。 ${message}`);
res.status(200).send(message);
} catch (error) {
const errorMessage = `切换账号失败: ${error.message}`;
this.logger.error(`[管理] 手动切换失败。错误: ${errorMessage}`);
res.status(500).send(errorMessage);
}
});
app.get('/health', (req, res) => {
res.status(200).json({
status: 'healthy',
uptime: process.uptime(),
config: {
streamingMode: this.streamingMode,
debugMode: this.config.debugMode,
failureThreshold: this.config.failureThreshold,
immediateSwitchStatusCodes: this.config.immediateSwitchStatusCodes,
maxRetries: this.config.maxRetries,
authMode: this.authSource.authMode,
apiKeyAuth: (this.config.apiKeys && this.config.apiKeys.length > 0) ? '已启用' : '已禁用',
},
auth: {
currentAuthIndex: this.requestHandler.currentAuthIndex,
availableIndices: this.authSource.getAvailableIndices(),
totalAuthSources: this.authSource.getAvailableIndices().length,
failureCount: this.requestHandler.failureCount,
isAuthSwitching: this.requestHandler.isAuthSwitching,
},
stats: this.stats,
browser: {
connected: !!this.browserManager.browser,
},
websocket: {
internalClients: this.connectionRegistry.connections.size
}
});
});
// 主API代理
app.use(this._createAuthMiddleware());
app.all(/(.*)/, (req, res) => {
// 修改: 增加对根路径的判断,防止其被代理
if (req.path === '/' || req.path === '/favicon.ico' || req.path.startsWith('/dashboard')) {
return res.status(204).send();
}
this.requestHandler.processRequest(req, res);
});
return app;
}
_getDashboardHtml() {
return `
<!DOCTYPE html>
<html lang="zh-CN">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>服务器仪表盘</title>
<style>
:root {
--pico-font-size: 16px;
--pico-background-color: #11191f;
--pico-color: #dce3e9;
--pico-card-background-color: #1a242c;
--pico-card-border-color: #2b3a47;
--pico-primary: #3d8bfd;
--pico-primary-hover: #529bff;
--pico-primary-focus: rgba(61, 139, 253, 0.25);
--pico-primary-inverse: #fff;
--pico-form-element-background-color: #1a242c;
--pico-form-element-border-color: #2b3a47;
--pico-form-element-focus-color: var(--pico-primary);
--pico-h1-color: #fff;
--pico-h2-color: #f1f1f1;
--pico-muted-color: #7a8c99;
--pico-border-radius: 0.5rem;
--info-color: #17a2b8; /* 天蓝色,用于状态文本 */
}
body { font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, Helvetica, Arial, sans-serif, "Apple Color Emoji", "Segoe UI Emoji", "Segoe UI Symbol"; margin: 0; padding: 2rem; background-color: var(--pico-background-color); color: var(--pico-color); }
main.container { max-width: 1200px; margin: 0 auto; padding-top: 30px; display: none; /* Initially hidden */ }
.grid { display: grid; grid-template-columns: repeat(auto-fit, minmax(350px, 1fr)); gap: 1.5rem; }
article { border: 1px solid var(--pico-card-border-color); border-radius: var(--pico-border-radius); padding: 1.5rem; background: var(--pico-card-background-color); }
h1, h2 { margin-top: 0; color: var(--pico-h1-color); }
h2 { border-bottom: 1px solid var(--pico-card-border-color); padding-bottom: 0.5rem; margin-bottom: 1rem; color: var(--pico-h2-color); }
.status-grid { display: grid; grid-template-columns: auto 1fr; gap: 0.5rem 1rem; align-items: center;}
.status-grid strong { color: var(--pico-color); white-space: nowrap;}
.status-grid span { color: var(--pico-muted-color); text-align: right; }
.status-text-info { color: var(--info-color); font-weight: bold; }
.status-text-red { color: #dc3545; font-weight: bold; }
.status-text-yellow { color: #ffc107; font-weight: bold; }
.status-text-gray { color: var(--pico-muted-color); font-weight: bold; }
.tag { display: inline-block; padding: 0.25em 0.6em; font-size: 0.75em; font-weight: 700; line-height: 1; text-align: center; white-space: nowrap; vertical-align: baseline; border-radius: 0.35rem; color: #fff; }
.tag-info { background-color: #17a2b8; }
.tag-blue { background-color: #007bff; }
.tag-yellow { color: #212529; background-color: #ffc107; }
ul { list-style: none; padding: 0; margin: 0; }
.scrollable-list { max-height: 220px; overflow-y: auto; padding-right: 5px; border: 1px solid var(--pico-form-element-border-color); border-radius: 0.25rem; padding: 0.5rem;}
.account-list li { display: flex; justify-content: space-between; align-items: center; padding: 0.5rem; border-radius: 0.25rem; }
.account-list li:nth-child(odd) { background-color: rgba(255,255,255,0.03); }
.account-list .current { font-weight: bold; color: var(--pico-primary); }
details { width: 100%; border-bottom: 1px solid var(--pico-form-element-border-color); }
details:last-child { border-bottom: none; }
details summary { cursor: pointer; display: flex; justify-content: space-between; align-items: center; padding: 0.5rem 0.2rem; list-style: none; }
details summary::-webkit-details-marker { display: none; }
details summary:hover { background-color: rgba(255,255,255,0.05); }
.model-stats-list { padding: 0.5rem 0 0.5rem 1.5rem; font-size: 0.9em; background-color: rgba(0,0,0,0.1); }
.model-stats-list li { display: flex; justify-content: space-between; padding: 0.2rem; }
button, input[type="text"], input[type="number"] { background-color: var(--pico-form-element-background-color); border: 1px solid var(--pico-form-element-border-color); color: var(--pico-color); padding: 0.5rem 1rem; border-radius: var(--pico-border-radius); }
button { cursor: pointer; background-color: var(--pico-primary); border-color: var(--pico-primary); color: var(--pico-primary-inverse); }
button:hover { background-color: var(--pico-primary-hover); }
.btn-danger { background-color: #dc3545; border-color: #dc3545; }
.btn-sm { font-size: 0.8em; padding: 0.2rem 0.5rem; }
.top-banner { position: fixed; top: 0; right: 0; background-color: #ffc107; color: #212529; padding: 5px 15px; font-size: 0.9em; z-index: 1001; border-bottom-left-radius: 0.5rem; }
.toast { position: fixed; bottom: 20px; right: 20px; background-color: var(--pico-primary); color: white; padding: 15px; border-radius: 5px; z-index: 1000; opacity: 0; transition: opacity 0.5s; }
.toast.show { opacity: 1; }
.toast.error { background-color: #dc3545; }
form label { display: block; margin-bottom: 0.5rem; }
form input { width: 100%; box-sizing: border-box; }
.form-group { margin-bottom: 1rem; }
.switch-field { display: flex; overflow: hidden; }
.switch-field input { position: absolute !important; clip: rect(0, 0, 0, 0); height: 1px; width: 1px; border: 0; overflow: hidden; }
.switch-field label { background-color: var(--pico-form-element-background-color); color: var(--pico-muted-color); font-size: 14px; line-height: 1; text-align: center; padding: 8px 16px; margin-right: -1px; border: 1px solid var(--pico-form-element-border-color); transition: all 0.1s ease-in-out; width: 50%; }
.switch-field label:hover { cursor: pointer; }
.switch-field input:checked + label { background-color: var(--pico-primary); color: var(--pico-primary-inverse); box-shadow: none; }
.switch-field label:first-of-type { border-radius: 4px 0 0 4px; }
.switch-field label:last-of-type { border-radius: 0 4px 4px 0; }
</style>
</head>
<body data-theme="dark">
<div class="top-banner">注意: 此面板中添加的账号和修改的变量均是临时的,重启后会丢失</div>
<main class="container">
<h1>🐢 服务器仪表盘</h1>
<div class="grid">
<article>
<h2>服务器状态</h2>
<div class="status-grid">
<strong>运行时间:</strong> <span id="uptime">--</span>
<strong>浏览器:</strong> <span id="browserConnected">--</span>
<strong>认证模式:</strong> <span id="authMode">--</span>
<strong>API密钥认证:</strong> <span id="apiKeyAuth">--</span>
<strong>调试模式:</strong> <span id="debugMode">--</span>
<strong>API总调用次数:</strong> <span id="totalCalls">0</span>
</div>
</article>
<article>
<h2>调用统计</h2>
<div id="accountCalls" class="scrollable-list"></div>
</article>
<article>
<h2>账号管理</h2>
<div style="display: flex; gap: 1rem; margin-bottom: 1rem;">
<button id="switchAccountBtn">切换到下一个账号</button>
<button id="addAccountBtn">添加临时账号</button>
</div>
<h3>账号池</h3>
<div id="accountPool" class="scrollable-list"></div>
</article>
<article>
<h2>实时配置</h2>
<form id="configForm">
<div class="form-group">
<label>流式模式</label>
<div class="switch-field">
<input type="radio" id="streamingMode_fake" name="streamingMode" value="fake" />
<label for="streamingMode_fake">Fake</label>
<input type="radio" id="streamingMode_real" name="streamingMode" value="real" checked/>
<label for="streamingMode_real">Real</label>
</div>
</div>
<div class="form-group">
<label for="configFailureThreshold">几次失败后切换账号 (0为禁用)</label>
<input type="number" id="configFailureThreshold" name="failureThreshold">
</div>
<div class="form-group">
<label for="configMaxRetries">单次请求内部重试次数</label>
<input type="number" id="configMaxRetries" name="maxRetries">
</div>
<div class="form-group">
<label for="configRetryDelay">重试间隔 (毫秒)</label>
<input type="number" id="configRetryDelay" name="retryDelay">
</div>
<div class="form-group">
<label for="configImmediateSwitchStatusCodes">立即切换的状态码 (逗号分隔)</label>
<input type="text" id="configImmediateSwitchStatusCodes" name="immediateSwitchStatusCodes">
</div>
<button type="submit">应用临时更改</button>
</form>
</article>
</div>
</main>
<div id="toast" class="toast"></div>
<script>
document.addEventListener('DOMContentLoaded', () => {
const API_KEY_SESSION_STORAGE = 'dashboard_api_key';
const API_BASE = '/dashboard';
// DOM Elements
const mainContainer = document.querySelector('main.container');
const uptimeEl = document.getElementById('uptime');
const debugModeEl = document.getElementById('debugMode');
const browserConnectedEl = document.getElementById('browserConnected');
const authModeEl = document.getElementById('authMode');
const apiKeyAuthEl = document.getElementById('apiKeyAuth');
const totalCallsEl = document.getElementById('totalCalls');
const accountCallsEl = document.getElementById('accountCalls');
const accountPoolEl = document.getElementById('accountPool');
const switchAccountBtn = document.getElementById('switchAccountBtn');
const addAccountBtn = document.getElementById('addAccountBtn');
const configForm = document.getElementById('configForm');
const toastEl = document.getElementById('toast');
function getAuthHeaders(hasBody = false) {
const headers = {
'X-Dashboard-Auth': sessionStorage.getItem(API_KEY_SESSION_STORAGE) || ''
};
if (hasBody) {
headers['Content-Type'] = 'application/json';
}
return headers;
}
function showToast(message, isError = false) {
toastEl.textContent = message;
toastEl.className = isError ? 'toast show error' : 'toast show';
setTimeout(() => { toastEl.className = 'toast'; }, 3000);
}
function formatUptime(seconds) {
const d = Math.floor(seconds / (3600*24));
const h = Math.floor(seconds % (3600*24) / 3600);
const m = Math.floor(seconds % 3600 / 60);
const s = Math.floor(seconds % 60);
return \`\${d}天 \${h}小时 \${m}分钟 \${s}秒\`;
}
function handleAuthFailure() {
sessionStorage.removeItem(API_KEY_SESSION_STORAGE);
mainContainer.style.display = 'none';
document.body.insertAdjacentHTML('afterbegin', '<h1>认证已过期或无效,请刷新页面重试。</h1>');
showToast('认证失败', true);
}
async function fetchData() {
try {
const response = await fetch(\`\${API_BASE}/data\`, { headers: getAuthHeaders() });
if (response.status === 401) return handleAuthFailure();
if (!response.ok) throw new Error('获取数据失败');
const data = await response.json();
uptimeEl.textContent = formatUptime(data.status.uptime);
browserConnectedEl.innerHTML = data.status.browserConnected ? '<span class="status-text-info">已连接</span>' : '<span class="status-text-red">已断开</span>';
authModeEl.innerHTML = data.status.authMode === 'env' ? '<span class="status-text-info">环境变量</span>' : '<span class="status-text-info">Cookie文件</span>';
apiKeyAuthEl.innerHTML = data.status.apiKeyAuth === '已启用' ? '<span class="status-text-info">已启用</span>' : '<span class="status-text-gray">已禁用</span>';
debugModeEl.innerHTML = data.status.debugMode ? '<span class="status-text-yellow">已启用</span>' : '<span class="status-text-gray">已禁用</span>';
totalCallsEl.textContent = data.stats.totalCalls;
accountCallsEl.innerHTML = '';
const sortedAccounts = Object.entries(data.stats.accountCalls).sort((a,b) => parseInt(a[0]) - parseInt(b[0]));
const callsUl = document.createElement('ul');
callsUl.className = 'account-list';
for (const [index, stats] of sortedAccounts) {
const li = document.createElement('li');
const isCurrent = parseInt(index, 10) === data.auth.currentAuthIndex;
let modelStatsHtml = '<ul class="model-stats-list">';
const sortedModels = Object.entries(stats.models).sort((a,b) => b[1] - a[1]);
sortedModels.length > 0 ? sortedModels.forEach(([model, count]) => { modelStatsHtml += \`<li><span>\${model}:</span> <strong>\${count}</strong></li>\`; }) : modelStatsHtml += '<li>无模型调用记录</li>';
modelStatsHtml += '</ul>';
li.innerHTML = \`<details><summary><span class="\${isCurrent ? 'current' : ''}">账号 \${index}</span><strong>总计: \${stats.total}</strong></summary>\${modelStatsHtml}</details>\`;
if(isCurrent) { li.querySelector('summary').style.color = 'var(--pico-primary)'; }
callsUl.appendChild(li);
}
accountCallsEl.appendChild(callsUl);
accountPoolEl.innerHTML = '';
const poolUl = document.createElement('ul');
poolUl.className = 'account-list';
data.auth.accounts.forEach(acc => {
const li = document.createElement('li');
const isCurrent = acc.index === data.auth.currentAuthIndex;
const sourceTag = acc.source === 'temporary' ? '<span class="tag tag-yellow">临时</span>' : (acc.source === 'env' ? '<span class="tag tag-info">变量</span>' : '<span class="tag tag-blue">文件</span>');
let html = \`<span class="\${isCurrent ? 'current' : ''}">账号 \${acc.index} \${sourceTag}</span>\`;
if (acc.source === 'temporary') { html += \`<button class="btn-danger btn-sm" data-index="\${acc.index}">删除</button>\`; } else { html += '<span></span>'; }
li.innerHTML = html;
poolUl.appendChild(li);
});
accountPoolEl.appendChild(poolUl);
const streamingModeInput = document.querySelector(\`input[name="streamingMode"][value="\${data.config.streamingMode}"]\`);
if(streamingModeInput) streamingModeInput.checked = true;
configForm.failureThreshold.value = data.config.failureThreshold;
configForm.maxRetries.value = data.config.maxRetries;
configForm.retryDelay.value = data.config.retryDelay;
configForm.immediateSwitchStatusCodes.value = data.config.immediateSwitchStatusCodes.join(', ');
} catch (error) {
console.error('获取数据时出错:', error);
showToast(error.message, true);
}
}
function initializeDashboardListeners() {
switchAccountBtn.addEventListener('click', async () => {
switchAccountBtn.disabled = true;
switchAccountBtn.textContent = '切换中...';
try {
const response = await fetch('/switch', { method: 'POST', headers: getAuthHeaders() });
const text = await response.text();
if (!response.ok) throw new Error(text);
showToast(text);
await fetchData();
} catch (error) {
showToast(error.message, true);
} finally {
switchAccountBtn.disabled = false;
switchAccountBtn.textContent = '切换到下一个账号';
}
});
addAccountBtn.addEventListener('click', () => {
const index = prompt("为新的临时账号输入一个唯一的数字索引:");
if (!index || isNaN(parseInt(index))) { if(index !== null) alert("索引无效。"); return; }
const authDataStr = prompt("请输入单行压缩后的Cookie内容:");
if (!authDataStr) return;
let authData;
try { authData = JSON.parse(authDataStr); } catch(e) { alert("Cookie JSON格式无效。"); return; }
fetch(\`\${API_BASE}/accounts\`, { method: 'POST', headers: getAuthHeaders(true), body: JSON.stringify({ index: parseInt(index), authData }) })
.then(res => res.json().then(data => ({ ok: res.ok, data }))).then(({ok, data}) => {
if (!ok) throw new Error(data.message);
showToast(data.message); fetchData(); }).catch(err => showToast(err.message, true));
});
accountPoolEl.addEventListener('click', e => {
if (e.target.matches('button.btn-danger')) {
const index = e.target.dataset.index;
if (confirm(\`您确定要删除临时账号 \${index} 吗?\`)) {
fetch(\`\${API_BASE}/accounts/\${index}\`, { method: 'DELETE', headers: getAuthHeaders() })
.then(res => res.json().then(data => ({ ok: res.ok, data }))).then(({ok, data}) => {
if (!ok) throw new Error(data.message);
showToast(data.message); fetchData(); }).catch(err => showToast(err.message, true));
}
}
});
configForm.addEventListener('submit', e => {
e.preventDefault();
const formData = new FormData(configForm);
const data = Object.fromEntries(formData.entries());
data.immediateSwitchStatusCodes = data.immediateSwitchStatusCodes.split(',').map(s => s.trim()).filter(Boolean);
fetch(\`\${API_BASE}/config\`, { method: 'POST', headers: getAuthHeaders(true), body: JSON.stringify(data) })
.then(res => res.json().then(data => ({ ok: res.ok, data }))).then(({ok, data}) => {
if (!ok) throw new Error(data.message);
showToast('配置已应用。'); fetchData(); }).catch(err => showToast(err.message, true));
});
configForm.addEventListener('change', e => {
if (e.target.name === 'streamingMode') {
fetch(\`\${API_BASE}/config\`, { method: 'POST', headers: getAuthHeaders(true), body: JSON.stringify({ streamingMode: e.target.value }) })
.then(res => res.json().then(d => ({ ok: res.ok, data: d }))).then(({ok, data}) => {
if (!ok) throw new Error(data.message);
showToast(\`流式模式已更新为: \${e.target.value.charAt(0).toUpperCase() + e.target.value.slice(1)}\`);
}).catch(err => showToast(err.message, true));
}
});
}
async function verifyAndLoad(keyToVerify) {
try {
const response = await fetch(\`\${API_BASE}/verify-key\`, {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify({ key: keyToVerify || '' })
});
const result = await response.json();
if (response.ok && result.success) {
if (keyToVerify) {
sessionStorage.setItem(API_KEY_SESSION_STORAGE, keyToVerify);
}
mainContainer.style.display = 'block';
initializeDashboardListeners();
fetchData();
setInterval(fetchData, 5000);
return true;
} else {
sessionStorage.removeItem(API_KEY_SESSION_STORAGE);
return false;
}
} catch (err) {
document.body.innerHTML = \`<h1>认证时发生错误: \${err.message}</h1>\`;
return false;
}
}
async function checkAndInitiate() {
const storedApiKey = sessionStorage.getItem(API_KEY_SESSION_STORAGE);
// 尝试使用已存储的密钥或空密钥进行验证
const initialCheckSuccess = await verifyAndLoad(storedApiKey);
// 如果初次验证失败,说明服务器需要密钥,而我们没有提供或提供了错误的密钥
if (!initialCheckSuccess) {
const newApiKey = prompt("请输入API密钥以访问仪表盘 (服务器需要认证):");
if (newApiKey) {
// 使用用户新输入的密钥再次尝试
const secondCheckSuccess = await verifyAndLoad(newApiKey);
if (!secondCheckSuccess) {
document.body.innerHTML = \`<h1>认证失败: 无效的API密钥</h1>\`;
}
} else {
// 用户取消了输入
document.body.innerHTML = '<h1>访问被拒绝</h1>';
}
}
}
checkAndInitiate();
});
</script>
</body>
</html>
`;
}
async _startWebSocketServer() {
this.wsServer = new WebSocket.Server({ port: this.config.wsPort, host: this.config.host });
this.wsServer.on('connection', (ws, req) => {
this.connectionRegistry.addConnection(ws, { address: req.socket.remoteAddress });
});
}
}
// ===================================================================================
// 主初始化
// ===================================================================================
async function initializeServer() {
try {
const serverSystem = new ProxyServerSystem();
await serverSystem.start();
} catch (error) {
console.error('❌ 服务器启动失败:', error.message);
process.exit(1);
}
}
if (require.main === module) {
initializeServer();
}
module.exports = { ProxyServerSystem, BrowserManager, initializeServer }; |