mirror of
https://github.com/farion1231/cc-switch.git
synced 2026-06-16 13:34:04 +08:00
fix(stream_check): respect auth_mode for Claude health checks (#824)
Previously, check_claude_stream always added the x-api-key header, ignoring the provider's auth_mode setting. This caused health check failures for proxy services that only support Bearer authentication. Now the function respects the auth.strategy field: - AuthStrategy::Anthropic: Authorization Bearer + x-api-key - AuthStrategy::ClaudeAuth: Authorization Bearer only - AuthStrategy::Bearer: Authorization Bearer only This aligns with the behavior of ClaudeAdapter::add_auth_headers and fixes health checks for proxy providers with auth_mode="bearer_only". Changes: - Modified check_claude_stream to conditionally add x-api-key header - Added AuthStrategy import - Added test_auth_strategy_imports unit test Tests: All passing (7/7 for stream_check module) Co-authored-by: Claude Sonnet 4.5 <noreply@anthropic.com>
This commit is contained in:
committed by
GitHub
Unverified
parent
d098ecad64
commit
58153333ce
@@ -12,7 +12,7 @@ use std::time::Instant;
|
||||
use crate::app_config::AppType;
|
||||
use crate::error::AppError;
|
||||
use crate::provider::Provider;
|
||||
use crate::proxy::providers::{get_adapter, AuthInfo};
|
||||
use crate::proxy::providers::{get_adapter, AuthInfo, AuthStrategy};
|
||||
|
||||
/// 健康状态枚举
|
||||
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq)]
|
||||
@@ -303,12 +303,18 @@ impl StreamCheckService {
|
||||
let os_name = Self::get_os_name();
|
||||
let arch_name = Self::get_arch_name();
|
||||
|
||||
// 严格按照 Claude CLI 请求格式设置 headers
|
||||
let response = client
|
||||
// 根据 auth.strategy 构建认证 headers
|
||||
let mut request_builder = client
|
||||
.post(&url)
|
||||
// 认证 headers(双重认证)
|
||||
.header("authorization", format!("Bearer {}", auth.api_key))
|
||||
.header("x-api-key", &auth.api_key)
|
||||
.header("authorization", format!("Bearer {}", auth.api_key));
|
||||
|
||||
// 只有 Anthropic 官方策略才添加 x-api-key
|
||||
if auth.strategy == AuthStrategy::Anthropic {
|
||||
request_builder = request_builder.header("x-api-key", &auth.api_key);
|
||||
}
|
||||
|
||||
// 严格按照 Claude CLI 请求格式设置其他 headers
|
||||
let response = request_builder
|
||||
// Anthropic 必需 headers
|
||||
.header("anthropic-version", "2023-06-01")
|
||||
.header(
|
||||
@@ -703,4 +709,22 @@ mod tests {
|
||||
#[cfg(target_arch = "x86_64")]
|
||||
assert_eq!(arch_name, "x86_64");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_auth_strategy_imports() {
|
||||
// 验证 AuthStrategy 枚举可以正常使用
|
||||
let anthropic = AuthStrategy::Anthropic;
|
||||
let claude_auth = AuthStrategy::ClaudeAuth;
|
||||
let bearer = AuthStrategy::Bearer;
|
||||
|
||||
// 验证不同的策略是不相等的
|
||||
assert_ne!(anthropic, claude_auth);
|
||||
assert_ne!(anthropic, bearer);
|
||||
assert_ne!(claude_auth, bearer);
|
||||
|
||||
// 验证相同策略是相等的
|
||||
assert_eq!(anthropic, AuthStrategy::Anthropic);
|
||||
assert_eq!(claude_auth, AuthStrategy::ClaudeAuth);
|
||||
assert_eq!(bearer, AuthStrategy::Bearer);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user