feat: 添加Chrome插件兜底支持,解决第三方插件401错误问题

• 新增browserFallback中间件,自动识别并处理Chrome插件请求
• 增强CORS支持,明确允许chrome-extension://来源
• 优化请求头过滤,移除可能触发Claude CORS检查的浏览器头信息
• 完善401错误处理逻辑,避免因临时token问题导致账号被错误停用

🤖 Generated with [Claude Code](https://claude.ai/code)

Co-Authored-By: Claude <noreply@anthropic.com>
This commit is contained in:
root
2025-09-10 07:48:41 +00:00
parent ca79e08c81
commit bdae9d6ceb
4 changed files with 92 additions and 33 deletions

View File

@@ -757,7 +757,7 @@ const requireAdmin = (req, res, next) => {
// 注意:使用统计现在直接在/api/v1/messages路由中处理
// 以便从Claude API响应中提取真实的usage数据
// 🚦 CORS中间件优化版
// 🚦 CORS中间件优化版支持Chrome插件
const corsMiddleware = (req, res, next) => {
const { origin } = req.headers
@@ -769,8 +769,11 @@ const corsMiddleware = (req, res, next) => {
'https://127.0.0.1:3000'
]
// 🆕 检查是否为Chrome插件请求
const isChromeExtension = origin && origin.startsWith('chrome-extension://')
// 设置CORS头
if (allowedOrigins.includes(origin) || !origin) {
if (allowedOrigins.includes(origin) || !origin || isChromeExtension) {
res.header('Access-Control-Allow-Origin', origin || '*')
}
@@ -785,7 +788,9 @@ const corsMiddleware = (req, res, next) => {
'Authorization',
'x-api-key',
'api-key',
'x-admin-token'
'x-admin-token',
'anthropic-version',
'anthropic-dangerous-direct-browser-access'
].join(', ')
)