Spaces:
Paused
Paused
Upload unified-server.js
Browse files- unified-server.js +43 -19
unified-server.js
CHANGED
|
@@ -369,9 +369,10 @@ class LoggingService {
|
|
| 369 |
}
|
| 370 |
|
| 371 |
debug(message) {
|
| 372 |
-
|
| 373 |
-
|
| 374 |
-
|
|
|
|
| 375 |
}
|
| 376 |
}
|
| 377 |
|
|
@@ -589,7 +590,12 @@ class RequestHandler {
|
|
| 589 |
return correctedDetails;
|
| 590 |
}
|
| 591 |
|
| 592 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 593 |
const correctedDetails = { ...errorDetails };
|
| 594 |
if (correctedDetails.message && typeof correctedDetails.message === 'string') {
|
| 595 |
const regex = /(?:HTTP|status code)\s*(\d{3})|"code"\s*:\s*(\d{3})/;
|
|
@@ -665,6 +671,15 @@ class RequestHandler {
|
|
| 665 |
}
|
| 666 |
|
| 667 |
async processRequest(req, res) {
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 668 |
// 提前获取模型名称和当前账号
|
| 669 |
const modelName = this._getModelFromRequest(req);
|
| 670 |
const currentAccount = this.currentAuthIndex;
|
|
@@ -703,23 +718,32 @@ class RequestHandler {
|
|
| 703 |
}
|
| 704 |
}
|
| 705 |
_generateRequestId() { return `${Date.now()}_${Math.random().toString(36).substring(2, 11)}`; }
|
| 706 |
-
|
| 707 |
-
|
| 708 |
-
|
| 709 |
-
|
| 710 |
-
|
| 711 |
-
|
| 712 |
-
|
| 713 |
-
|
| 714 |
-
}
|
| 715 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 716 |
}
|
| 717 |
|
| 718 |
-
return
|
| 719 |
-
path: req.path, method: req.method, headers: req.headers, query_params: req.query,
|
| 720 |
-
body: requestBodyString,
|
| 721 |
-
request_id: requestId, streaming_mode: this.serverSystem.streamingMode
|
| 722 |
-
};
|
| 723 |
}
|
| 724 |
_forwardRequest(proxyRequest) {
|
| 725 |
const connection = this.connectionRegistry.getFirstConnection();
|
|
|
|
| 369 |
}
|
| 370 |
|
| 371 |
debug(message) {
|
| 372 |
+
// 修正:移除内部对环境变量的检查。
|
| 373 |
+
// 现在,只要调用此方法,就会打印日志。
|
| 374 |
+
// 是否调用取决于程序其他部分的 this.config.debugMode 判断。
|
| 375 |
+
console.debug(this._formatMessage('DEBUG', message));
|
| 376 |
}
|
| 377 |
}
|
| 378 |
|
|
|
|
| 590 |
return correctedDetails;
|
| 591 |
}
|
| 592 |
|
| 593 |
+
async _handleRequestFailureAndSwitch(errorDetails, res) {
|
| 594 |
+
// 新增:在调试模式下打印完整的原始错误信息
|
| 595 |
+
if (this.config.debugMode) {
|
| 596 |
+
this.logger.debug(`[认证][调试] 收到来自浏览器的完整错误详情:\n${JSON.stringify(errorDetails, null, 2)}`);
|
| 597 |
+
}
|
| 598 |
+
|
| 599 |
const correctedDetails = { ...errorDetails };
|
| 600 |
if (correctedDetails.message && typeof correctedDetails.message === 'string') {
|
| 601 |
const regex = /(?:HTTP|status code)\s*(\d{3})|"code"\s*:\s*(\d{3})/;
|
|
|
|
| 671 |
}
|
| 672 |
|
| 673 |
async processRequest(req, res) {
|
| 674 |
+
// 关键修复 (V2): 使用 hasOwnProperty 来准确判断 'key' 参数是否存在,
|
| 675 |
+
// 无论其值是空字符串还是有内容。
|
| 676 |
+
if ((!this.config.apiKeys || this.config.apiKeys.length === 0) && req.query && req.query.hasOwnProperty('key')) {
|
| 677 |
+
if (this.config.debugMode) {
|
| 678 |
+
this.logger.debug(`[请求预处理] 服务器API密钥认证已禁用。检测到并移除了来自客户端的 'key' 查询参数 (值为: '${req.query.key}')。`);
|
| 679 |
+
}
|
| 680 |
+
delete req.query.key;
|
| 681 |
+
}
|
| 682 |
+
|
| 683 |
// 提前获取模型名称和当前账号
|
| 684 |
const modelName = this._getModelFromRequest(req);
|
| 685 |
const currentAccount = this.currentAuthIndex;
|
|
|
|
| 718 |
}
|
| 719 |
}
|
| 720 |
_generateRequestId() { return `${Date.now()}_${Math.random().toString(36).substring(2, 11)}`; }
|
| 721 |
+
_buildProxyRequest(req, requestId) {
|
| 722 |
+
const proxyRequest = {
|
| 723 |
+
path: req.path,
|
| 724 |
+
method: req.method,
|
| 725 |
+
headers: req.headers,
|
| 726 |
+
query_params: req.query,
|
| 727 |
+
request_id: requestId,
|
| 728 |
+
streaming_mode: this.serverSystem.streamingMode
|
| 729 |
+
};
|
| 730 |
+
|
| 731 |
+
// 关键修正:只在允许有请求体的HTTP方法中添加body字段
|
| 732 |
+
if (req.method !== 'GET' && req.method !== 'HEAD') {
|
| 733 |
+
let requestBodyString;
|
| 734 |
+
if (typeof req.body === 'object' && req.body !== null) {
|
| 735 |
+
requestBodyString = JSON.stringify(req.body);
|
| 736 |
+
} else if (typeof req.body === 'string') {
|
| 737 |
+
requestBodyString = req.body;
|
| 738 |
+
} else if (Buffer.isBuffer(req.body)) {
|
| 739 |
+
requestBodyString = req.body.toString('utf-8');
|
| 740 |
+
} else {
|
| 741 |
+
requestBodyString = '';
|
| 742 |
+
}
|
| 743 |
+
proxyRequest.body = requestBodyString;
|
| 744 |
}
|
| 745 |
|
| 746 |
+
return proxyRequest;
|
|
|
|
|
|
|
|
|
|
|
|
|
| 747 |
}
|
| 748 |
_forwardRequest(proxyRequest) {
|
| 749 |
const connection = this.connectionRegistry.getFirstConnection();
|