mirror of
https://github.com/Wei-Shaw/sub2api.git
synced 2026-12-06 00:05:30 +00:00
平台清单之外,后端还有几处用"国产厂商"或"国产厂商 ∪ OpenCode"表示 多协议供应商,新登记的供应商会被漏掉(例如上游模型同步直接报不支持)。 - 适用于全部多协议供应商的判断改用 IsMultiProtocolAPIKey:上游模型同步与 模型目录基址、Anthropic count_tokens 本地估算、HTML 403 豁免与累计计数、Claude 型号 计费候选过滤; - 新增 RoutesProtocolByInbound(profile 分流方式为按入站协议),替换原来 只针对国产厂商、实际描述"按入站协议分流"的判断:Responses 直转回退、 Responses 能力标记(含创建 / 更新后的异步探测调度)、上游计费探测的 adaptive 基址、Anthropic API Key 鉴权方式、OpenAI 输入 token 本地估算。 按模型分流的供应商(OpenCode)仍走 routesByModel 分支。 依赖各家额度 / 余额接口的逻辑(Coding Plan 窗口、余额不足冷却等)仍按平台 显式判断。现有平台上两组判断与改动前逐一相同(测试覆盖全部平台与非法值); 另测试临时登记的新供应商按分流方式分别获得国产厂商与 OpenCode 的行为。 Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
62 lines
2.7 KiB
Go
62 lines
2.7 KiB
Go
package service
|
||
|
||
import (
|
||
"net/http"
|
||
"strings"
|
||
)
|
||
|
||
const (
|
||
anthropicAPIKeyAuthSchemeExtraKey = "anthropic_apikey_auth_scheme"
|
||
|
||
AnthropicAPIKeyAuthSchemeXAPIKey = "x_api_key"
|
||
AnthropicAPIKeyAuthSchemeAuthorizationBearer = "authorization_bearer"
|
||
)
|
||
|
||
// GetAnthropicAPIKeyAuthScheme returns the upstream authentication scheme for
|
||
// Anthropic API-key accounts. Missing or invalid values keep the historical
|
||
// x-api-key behavior. CN providers using their native Anthropic endpoints
|
||
// (api_protocol=anthropic) share the same override knob — Kimi/DeepSeek default
|
||
// to x-api-key, Zhipu can opt into Authorization: Bearer.
|
||
func (a *Account) GetAnthropicAPIKeyAuthScheme() string {
|
||
if a == nil || a.Type != AccountTypeAPIKey {
|
||
return AnthropicAPIKeyAuthSchemeXAPIKey
|
||
}
|
||
if a.Platform != PlatformAnthropic && !a.RoutesProtocolByInbound() {
|
||
return AnthropicAPIKeyAuthSchemeXAPIKey
|
||
}
|
||
|
||
switch strings.TrimSpace(a.GetExtraString(anthropicAPIKeyAuthSchemeExtraKey)) {
|
||
case AnthropicAPIKeyAuthSchemeAuthorizationBearer:
|
||
return AnthropicAPIKeyAuthSchemeAuthorizationBearer
|
||
default:
|
||
return AnthropicAPIKeyAuthSchemeXAPIKey
|
||
}
|
||
}
|
||
|
||
// isOllamaCloudAnthropicAuthBaseURL 判定本次实际选用的 Anthropic 上游 base_url
|
||
// 是否指向 Ollama Cloud。与真实出站 URL 组装同源:先 TrimSpace + TrimRight "/"
|
||
// (urlvalidator 与 nativeAnthropicTargetURL 同款归一化,'https://ollama.com/'
|
||
// 与 '.../v1' 均合理),再复用既有严格 host helper isOllamaCloudBaseURL(精确
|
||
// host、https、无 query/fragment、路径仅空或 /v1),不按 substring 匹配。
|
||
func isOllamaCloudAnthropicAuthBaseURL(baseURL string) bool {
|
||
return isOllamaCloudBaseURL(strings.TrimRight(strings.TrimSpace(baseURL), "/"))
|
||
}
|
||
|
||
// setAnthropicAPIKeyAuthHeader 写入上游认证头。除 extra 覆写外,Ollama Cloud
|
||
// 官方 Anthropic 兼容端点只认 Authorization: Bearer:判定依据是本次实际选用的
|
||
// 上游 base_url(与出站 URL 组装同源,而非 account.Platform——Ollama Cloud key
|
||
// 可挂在多个平台分组下),即使 extra 缺失或显式 x_api_key 也强制 Bearer;
|
||
// 其它上游保持历史 extra/default 行为。baseURL 为该请求实际选用的 Anthropic
|
||
// 上游 base(GetBaseURL / GetAnthropicProtocolBaseURL 等),默认官方端点时传空。
|
||
func setAnthropicAPIKeyAuthHeader(header http.Header, account *Account, token, baseURL string) {
|
||
if account.Type == AccountTypeAPIKey && isOllamaCloudAnthropicAuthBaseURL(baseURL) {
|
||
header.Set("Authorization", "Bearer "+token)
|
||
return
|
||
}
|
||
if account.GetAnthropicAPIKeyAuthScheme() == AnthropicAPIKeyAuthSchemeAuthorizationBearer {
|
||
header.Set("Authorization", "Bearer "+token)
|
||
return
|
||
}
|
||
header.Set("x-api-key", token)
|
||
}
|