mirror of
https://github.com/Wei-Shaw/claude-relay-service.git
synced 2026-01-22 16:43:35 +00:00
fix: 修复API Key模型限制功能不生效的问题
- 在apiKeyService.validateApiKey()中添加缺失的enableModelRestriction和restrictedModels字段返回 - 添加详细的调试日志来追踪模型限制检查的执行流程 - 解析存储在Redis中的restrictedModels JSON数据 - 确保模型限制数据正确传递到claudeRelayService 🤖 Generated with [Claude Code](https://claude.ai/code) Co-Authored-By: Claude <noreply@anthropic.com>
This commit is contained in:
@@ -108,6 +108,14 @@ class ApiKeyService {
|
||||
|
||||
logger.api(`🔓 API key validated successfully: ${keyData.id}`);
|
||||
|
||||
// 解析限制模型数据
|
||||
let restrictedModels = [];
|
||||
try {
|
||||
restrictedModels = keyData.restrictedModels ? JSON.parse(keyData.restrictedModels) : [];
|
||||
} catch (e) {
|
||||
restrictedModels = [];
|
||||
}
|
||||
|
||||
return {
|
||||
valid: true,
|
||||
keyData: {
|
||||
@@ -115,7 +123,9 @@ class ApiKeyService {
|
||||
name: keyData.name,
|
||||
claudeAccountId: keyData.claudeAccountId,
|
||||
tokenLimit: parseInt(keyData.tokenLimit),
|
||||
concurrencyLimit: parseInt(keyData.concurrencyLimit || 0),
|
||||
concurrencyLimit: parseInt(keyData.concurrencyLimit || 0),
|
||||
enableModelRestriction: keyData.enableModelRestriction === 'true',
|
||||
restrictedModels: restrictedModels,
|
||||
usage
|
||||
}
|
||||
};
|
||||
|
||||
@@ -22,9 +22,19 @@ class ClaudeRelayService {
|
||||
let upstreamRequest = null;
|
||||
|
||||
try {
|
||||
// 调试日志:查看API Key数据
|
||||
logger.info(`🔍 API Key data received:`, {
|
||||
apiKeyName: apiKeyData.name,
|
||||
enableModelRestriction: apiKeyData.enableModelRestriction,
|
||||
restrictedModels: apiKeyData.restrictedModels,
|
||||
requestedModel: requestBody.model
|
||||
});
|
||||
|
||||
// 检查模型限制
|
||||
if (apiKeyData.enableModelRestriction && apiKeyData.restrictedModels && apiKeyData.restrictedModels.length > 0) {
|
||||
const requestedModel = requestBody.model;
|
||||
logger.info(`🔒 Model restriction check - Requested model: ${requestedModel}, Restricted models: ${JSON.stringify(apiKeyData.restrictedModels)}`);
|
||||
|
||||
if (requestedModel && apiKeyData.restrictedModels.includes(requestedModel)) {
|
||||
logger.warn(`🚫 Model restriction violation for key ${apiKeyData.name}: Attempted to use restricted model ${requestedModel}`);
|
||||
return {
|
||||
@@ -437,9 +447,19 @@ class ClaudeRelayService {
|
||||
// 🌊 处理流式响应(带usage数据捕获)
|
||||
async relayStreamRequestWithUsageCapture(requestBody, apiKeyData, responseStream, clientHeaders, usageCallback) {
|
||||
try {
|
||||
// 调试日志:查看API Key数据(流式请求)
|
||||
logger.info(`🔍 [Stream] API Key data received:`, {
|
||||
apiKeyName: apiKeyData.name,
|
||||
enableModelRestriction: apiKeyData.enableModelRestriction,
|
||||
restrictedModels: apiKeyData.restrictedModels,
|
||||
requestedModel: requestBody.model
|
||||
});
|
||||
|
||||
// 检查模型限制
|
||||
if (apiKeyData.enableModelRestriction && apiKeyData.restrictedModels && apiKeyData.restrictedModels.length > 0) {
|
||||
const requestedModel = requestBody.model;
|
||||
logger.info(`🔒 [Stream] Model restriction check - Requested model: ${requestedModel}, Restricted models: ${JSON.stringify(apiKeyData.restrictedModels)}`);
|
||||
|
||||
if (requestedModel && apiKeyData.restrictedModels.includes(requestedModel)) {
|
||||
logger.warn(`🚫 Model restriction violation for key ${apiKeyData.name}: Attempted to use restricted model ${requestedModel}`);
|
||||
|
||||
|
||||
Reference in New Issue
Block a user