mirror of
https://github.com/Wei-Shaw/claude-relay-service.git
synced 2026-01-22 16:43:35 +00:00
Merge pull request #243 from tanaer/fixed
Fixed: 新增 Claude Console 账户限流机制开关
This commit is contained in:
@@ -269,8 +269,8 @@ class Application {
|
||||
}
|
||||
if (!version) {
|
||||
try {
|
||||
const packageJson = require('../package.json')
|
||||
version = packageJson.version
|
||||
const { version: pkgVersion } = require('../package.json')
|
||||
version = pkgVersion
|
||||
} catch (error) {
|
||||
version = '1.0.0'
|
||||
}
|
||||
|
||||
@@ -18,7 +18,6 @@ const crypto = require('crypto')
|
||||
const fs = require('fs')
|
||||
const path = require('path')
|
||||
const config = require('../../config/config')
|
||||
const { v4: uuidv4 } = require('uuid')
|
||||
|
||||
const router = express.Router()
|
||||
|
||||
@@ -1548,7 +1547,8 @@ router.post('/claude-console-accounts', authenticateAdmin, async (req, res) => {
|
||||
priority: priority || 50,
|
||||
supportedModels: supportedModels || [],
|
||||
userAgent,
|
||||
rateLimitDuration: rateLimitDuration || 60,
|
||||
rateLimitDuration:
|
||||
rateLimitDuration !== undefined && rateLimitDuration !== null ? rateLimitDuration : 60,
|
||||
proxy,
|
||||
accountType: accountType || 'shared'
|
||||
})
|
||||
@@ -4658,7 +4658,6 @@ router.post('/openai-accounts', authenticateAdmin, async (req, res) => {
|
||||
proxy,
|
||||
accountType,
|
||||
groupId,
|
||||
dedicatedApiKeys,
|
||||
rateLimitDuration,
|
||||
priority
|
||||
} = req.body
|
||||
@@ -4675,7 +4674,8 @@ router.post('/openai-accounts', authenticateAdmin, async (req, res) => {
|
||||
description: description || '',
|
||||
accountType: accountType || 'shared',
|
||||
priority: priority || 50,
|
||||
rateLimitDuration: rateLimitDuration || 60,
|
||||
rateLimitDuration:
|
||||
rateLimitDuration !== undefined && rateLimitDuration !== null ? rateLimitDuration : 60,
|
||||
openaiOauth: openaiOauth || {},
|
||||
accountInfo: accountInfo || {},
|
||||
proxy: proxy?.enabled
|
||||
|
||||
@@ -123,7 +123,9 @@ class ClaudeConsoleAccountService {
|
||||
priority: parseInt(accountData.priority) || 50,
|
||||
supportedModels: JSON.parse(accountData.supportedModels || '[]'),
|
||||
userAgent: accountData.userAgent,
|
||||
rateLimitDuration: parseInt(accountData.rateLimitDuration) || 60,
|
||||
rateLimitDuration: Number.isNaN(parseInt(accountData.rateLimitDuration))
|
||||
? 60
|
||||
: parseInt(accountData.rateLimitDuration),
|
||||
isActive: accountData.isActive === 'true',
|
||||
proxy: accountData.proxy ? JSON.parse(accountData.proxy) : null,
|
||||
accountType: accountData.accountType || 'shared',
|
||||
@@ -172,7 +174,10 @@ class ClaudeConsoleAccountService {
|
||||
|
||||
accountData.supportedModels = parsedModels
|
||||
accountData.priority = parseInt(accountData.priority) || 50
|
||||
accountData.rateLimitDuration = parseInt(accountData.rateLimitDuration) || 60
|
||||
{
|
||||
const _parsedDuration = parseInt(accountData.rateLimitDuration)
|
||||
accountData.rateLimitDuration = Number.isNaN(_parsedDuration) ? 60 : _parsedDuration
|
||||
}
|
||||
accountData.isActive = accountData.isActive === 'true'
|
||||
accountData.schedulable = accountData.schedulable !== 'false' // 默认为true
|
||||
|
||||
@@ -370,7 +375,10 @@ class ClaudeConsoleAccountService {
|
||||
const minutesSinceRateLimit = (now - rateLimitedAt) / (1000 * 60)
|
||||
|
||||
// 使用账户配置的限流时间
|
||||
const rateLimitDuration = account.rateLimitDuration || 60
|
||||
const rateLimitDuration =
|
||||
typeof account.rateLimitDuration === 'number' && !Number.isNaN(account.rateLimitDuration)
|
||||
? account.rateLimitDuration
|
||||
: 60
|
||||
|
||||
if (minutesSinceRateLimit >= rateLimitDuration) {
|
||||
await this.removeAccountRateLimit(accountId)
|
||||
@@ -510,7 +518,8 @@ class ClaudeConsoleAccountService {
|
||||
const rateLimitedAt = new Date(accountData.rateLimitedAt)
|
||||
const now = new Date()
|
||||
const minutesSinceRateLimit = Math.floor((now - rateLimitedAt) / (1000 * 60))
|
||||
const rateLimitDuration = parseInt(accountData.rateLimitDuration) || 60
|
||||
const __parsedDuration = parseInt(accountData.rateLimitDuration)
|
||||
const rateLimitDuration = Number.isNaN(__parsedDuration) ? 60 : __parsedDuration
|
||||
const minutesRemaining = Math.max(0, rateLimitDuration - minutesSinceRateLimit)
|
||||
|
||||
return {
|
||||
|
||||
@@ -3,7 +3,7 @@ const { v4: uuidv4 } = require('uuid')
|
||||
const crypto = require('crypto')
|
||||
const config = require('../../config/config')
|
||||
const logger = require('../utils/logger')
|
||||
const { maskToken } = require('../utils/tokenMask')
|
||||
// const { maskToken } = require('../utils/tokenMask')
|
||||
const {
|
||||
logRefreshStart,
|
||||
logRefreshSuccess,
|
||||
@@ -11,7 +11,7 @@ const {
|
||||
logTokenUsage,
|
||||
logRefreshSkipped
|
||||
} = require('../utils/tokenRefreshLogger')
|
||||
const tokenRefreshService = require('./tokenRefreshService')
|
||||
// const tokenRefreshService = require('./tokenRefreshService')
|
||||
|
||||
// 加密相关常量
|
||||
const ALGORITHM = 'aes-256-cbc'
|
||||
@@ -65,7 +65,7 @@ function decrypt(text) {
|
||||
}
|
||||
|
||||
// 刷新访问令牌
|
||||
async function refreshAccessToken(refreshToken) {
|
||||
async function refreshAccessToken(_refreshToken) {
|
||||
try {
|
||||
// OpenAI OAuth token 刷新实现
|
||||
// TODO: 实现具体的 OpenAI OAuth token 刷新逻辑
|
||||
@@ -146,7 +146,10 @@ async function createAccount(accountData) {
|
||||
accountType: accountData.accountType || 'shared',
|
||||
groupId: accountData.groupId || null,
|
||||
priority: accountData.priority || 50,
|
||||
rateLimitDuration: accountData.rateLimitDuration || 60,
|
||||
rateLimitDuration:
|
||||
accountData.rateLimitDuration !== undefined && accountData.rateLimitDuration !== null
|
||||
? accountData.rateLimitDuration
|
||||
: 60,
|
||||
// OAuth相关字段(加密存储)
|
||||
idToken: encrypt(oauthData.idToken || ''),
|
||||
accessToken: encrypt(oauthData.accessToken || ''),
|
||||
|
||||
Reference in New Issue
Block a user