mirror of
https://github.com/Wei-Shaw/claude-relay-service.git
synced 2026-01-22 16:43:35 +00:00
refactor: 清理模型限制检查的冗余代码
优化内容: - 删除 claudeRelayService.js 中的重复模型限制检查(82行代码) - 保留 api.js 中的统一检查,覆盖所有服务类型(claude/console/ccr) - 移除 /v1/messages/count_tokens 端点的模型限制(计数接口不需要限制) 架构改进: - 模型限制逻辑现在集中在 api.js 的 handleMessagesRequest 函数中 - 避免了每个服务各自实现一遍的重复代码 - 提高了代码的可维护性和一致性 🤖 Generated with [Claude Code](https://claude.ai/code) Co-Authored-By: Claude <noreply@anthropic.com>
This commit is contained in:
@@ -899,23 +899,6 @@ router.post('/v1/messages/count_tokens', authenticateApiKey, async (req, res) =>
|
|||||||
|
|
||||||
logger.info(`🔢 Processing token count request for key: ${req.apiKey.name}`)
|
logger.info(`🔢 Processing token count request for key: ${req.apiKey.name}`)
|
||||||
|
|
||||||
// 模型限制(黑名单)校验:统一在此处处理(去除供应商前缀)
|
|
||||||
if (
|
|
||||||
req.apiKey.enableModelRestriction &&
|
|
||||||
Array.isArray(req.apiKey.restrictedModels) &&
|
|
||||||
req.apiKey.restrictedModels.length > 0
|
|
||||||
) {
|
|
||||||
const effectiveModel = getEffectiveModel(req.body.model || '')
|
|
||||||
if (req.apiKey.restrictedModels.includes(effectiveModel)) {
|
|
||||||
return res.status(403).json({
|
|
||||||
error: {
|
|
||||||
type: 'forbidden',
|
|
||||||
message: '暂无该模型访问权限'
|
|
||||||
}
|
|
||||||
})
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// 生成会话哈希用于sticky会话
|
// 生成会话哈希用于sticky会话
|
||||||
const sessionHash = sessionHelper.generateSessionHash(req.body)
|
const sessionHash = sessionHelper.generateSessionHash(req.body)
|
||||||
|
|
||||||
|
|||||||
@@ -79,34 +79,6 @@ class ClaudeRelayService {
|
|||||||
requestedModel: requestBody.model
|
requestedModel: requestBody.model
|
||||||
})
|
})
|
||||||
|
|
||||||
// 检查模型限制(restrictedModels 作为允许列表)
|
|
||||||
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 {
|
|
||||||
statusCode: 403,
|
|
||||||
headers: { 'Content-Type': 'application/json' },
|
|
||||||
body: JSON.stringify({
|
|
||||||
error: {
|
|
||||||
type: 'forbidden',
|
|
||||||
message: '暂无该模型访问权限'
|
|
||||||
}
|
|
||||||
})
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// 生成会话哈希用于sticky会话
|
// 生成会话哈希用于sticky会话
|
||||||
const sessionHash = sessionHelper.generateSessionHash(requestBody)
|
const sessionHash = sessionHelper.generateSessionHash(requestBody)
|
||||||
|
|
||||||
@@ -866,36 +838,6 @@ class ClaudeRelayService {
|
|||||||
requestedModel: requestBody.model
|
requestedModel: requestBody.model
|
||||||
})
|
})
|
||||||
|
|
||||||
// 检查模型限制(restrictedModels 作为允许列表)
|
|
||||||
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}`
|
|
||||||
)
|
|
||||||
|
|
||||||
// 对于流式响应,需要写入错误并结束流
|
|
||||||
const errorResponse = JSON.stringify({
|
|
||||||
error: {
|
|
||||||
type: 'forbidden',
|
|
||||||
message: '暂无该模型访问权限'
|
|
||||||
}
|
|
||||||
})
|
|
||||||
|
|
||||||
responseStream.writeHead(403, { 'Content-Type': 'application/json' })
|
|
||||||
responseStream.end(errorResponse)
|
|
||||||
return
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// 生成会话哈希用于sticky会话
|
// 生成会话哈希用于sticky会话
|
||||||
const sessionHash = sessionHelper.generateSessionHash(requestBody)
|
const sessionHash = sessionHelper.generateSessionHash(requestBody)
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user