mirror of
https://github.com/Wei-Shaw/sub2api.git
synced 2026-10-21 04:55:45 +00:00
出站伪装用的 Claude Code 版本号此前硬编码在 claude.CLICurrentVersion, 只能靠环境变量 SUB2API_CLAUDE_CLI_VERSION 覆盖且需重启;Anthropic 抬高 版本下限时(如 Opus 5.5 要求 >= 2.1.280)必须改代码发版才能跟上。 改为每小时从官方 GitHub Releases 同步最新稳定版,无需重启也无需发版。 - 新增 ClaudeCodeVersionSyncService:1 小时间隔,数据源 anthropics/claude-code 的 releases/latest,失败回退最近 30 条列表;过滤 draft/prerelease; 只向前推进,拉取失败保留上次值不降级;面板可关闭 - 新增三个设置项:claude_code_client_version(管理员手动固定)、 claude_code_client_version_synced(同步服务独占写)、 claude_code_version_auto_sync_enabled(开关,默认开) - 取值优先级:面板手动值 -> 同步值 -> SUB2API_CLAUDE_CLI_VERSION -> 内置基线, 每层经 IsSupportedCLIVersion 校验,含 60s 缓存与 singleflight - 版本号运行期化:claude.DefaultHeaders 由包级 var 改为函数,新增 EffectiveCLIVersion/DefaultUserAgent 与 SetCLIVersionResolver(未注入时 与 CLIVersion() 等价);identity 的 defaultFingerprint 不再在包 init 固化, 指纹下限与主版本超前校验改用运行期值 - 守住一致性铁律:buildUpstreamRequest 与 count_tokens 各取一次 UA 字符串, 同时喂给出站 User-Agent 头与请求体 billing 的 cc_version,避免缓存翻转 瞬间头体版本不一致被判非正版客户端 - 管理后台设置页新增配置区块,含中英文案与「当前同步到」只读展示
67 lines
2.2 KiB
Go
67 lines
2.2 KiB
Go
package service
|
|
|
|
import (
|
|
"fmt"
|
|
"regexp"
|
|
"strings"
|
|
|
|
"github.com/tidwall/gjson"
|
|
"github.com/tidwall/sjson"
|
|
)
|
|
|
|
// ccVersionInBillingRe matches the semver part of cc_version (X.Y.Z).
|
|
var ccVersionInBillingRe = regexp.MustCompile(`cc_version=\d+\.\d+\.\d+`)
|
|
|
|
var ccVersionWithFingerprintInBillingRe = regexp.MustCompile(`cc_version=\d+\.\d+\.\d+\.[0-9a-fA-F]{3}\b`)
|
|
|
|
// effectiveBillingUserAgent 选择写进 x-anthropic-billing-header 的 User-Agent。
|
|
// OAuth mimicry 强制使用调用方传入的 mimicUserAgent(与出站 User-Agent 头同源、
|
|
// 同一次请求内取一次复用,保证 cc_version 与出站头版本严格一致),
|
|
// 其余情况使用账号指纹 UA。
|
|
func effectiveBillingUserAgent(mimicUserAgent, tokenType string, mimicClaudeCode bool, fingerprint *Fingerprint) string {
|
|
if tokenType == "oauth" && mimicClaudeCode {
|
|
return mimicUserAgent
|
|
}
|
|
if fingerprint == nil {
|
|
return ""
|
|
}
|
|
return fingerprint.UserAgent
|
|
}
|
|
|
|
// syncBillingHeaderVersion rewrites cc_version in x-anthropic-billing-header
|
|
// system text blocks to match the version extracted from userAgent.
|
|
// Recompute any recognized fingerprint suffix because its input includes the version.
|
|
// Only touches system array blocks whose text starts with "x-anthropic-billing-header".
|
|
func syncBillingHeaderVersion(body []byte, userAgent string) []byte {
|
|
version := ExtractCLIVersion(userAgent)
|
|
if version == "" {
|
|
return body
|
|
}
|
|
|
|
systemResult := gjson.GetBytes(body, "system")
|
|
if !systemResult.Exists() || !systemResult.IsArray() {
|
|
return body
|
|
}
|
|
|
|
replacement := "cc_version=" + version
|
|
idx := 0
|
|
systemResult.ForEach(func(_, item gjson.Result) bool {
|
|
text := item.Get("text")
|
|
if text.Exists() && text.Type == gjson.String &&
|
|
strings.HasPrefix(text.String(), "x-anthropic-billing-header") {
|
|
fingerprintedReplacement := replacement + "." + computeClaudeCodeFingerprint(body, version)
|
|
newText := ccVersionWithFingerprintInBillingRe.ReplaceAllString(text.String(), fingerprintedReplacement)
|
|
newText = ccVersionInBillingRe.ReplaceAllString(newText, replacement)
|
|
if newText != text.String() {
|
|
if updated, err := sjson.SetBytes(body, fmt.Sprintf("system.%d.text", idx), newText); err == nil {
|
|
body = updated
|
|
}
|
|
}
|
|
}
|
|
idx++
|
|
return true
|
|
})
|
|
|
|
return body
|
|
}
|