-
Notifications
You must be signed in to change notification settings - Fork 252
fix(网关): 完善 Claude OAuth/Claude Code 兼容 #316
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Conversation
fix(gateway): avoid invalid SSE error on canceled stream
|
改写metadata是为了尽可能减少特特征避免被识别 |
@Wei-Shaw
Claude Code 官方请求里,metadata.user_id 往往长得像:user_{用户id}account{accountUuid}session{sessionId},而且这几个部分与用户真实环境(.claude.json、最近 session、账号 uuid 等)高度相关,属于稳定但“合理”的指纹。 对非 Claude Code 客户端的改动是参照了 https://github.com/anomalyco/opencode-anthropic-auth 改的 |
|
对 Claude Code 客户端:尽量透传原始。这个有问题啊,你一个账号分发给20个人用,20个客户端的 metadata都不一样 这不是暴露特征吗?目前的格式也是跟官方的格式一样的,另外如果是apikey方式使用claude code,user_id里面的account_id 是空的,sub2api会补全这一段,因为如果你使用oauth/setup token账号使用 claude code,那个account_id是完整的。 |
|
复盘了你提到的“改写 metadata 是为了尽可能减少特征避免被识别”的点,确实之前把 Claude Code 客户端走 OAuth 上游时的 metadata.user_id / 指纹处理改得过于“透传”。 在“一个 OAuth/SetupToken 上游账号分发给多个人/多台机器使用”的情况下,完全透传每个 Claude Code 客户端自己的 metadata.user_id 和 x-stainless-*(OS/arch/runtime 等)会导致同一个 OAuth token 对应大量不同指纹,反而更容易被聚类识别为账号共享/代理来源。 因此这次调整为:只要上游账号是 OAuth/SetupToken,就统一做账号级指纹收敛(RewriteUserID + ApplyFingerprint),包括 Claude Code 客户端请求也会补齐 account_uuid(把 account__ 变成 account_{account_uuid}),并让 header 指纹保持稳定;同时仍保持“只有非 Claude Code 客户端才做 tools/schema/description 等内容层面的伪装/兼容”。 这样可以在不改变 Claude Code 请求语义的前提下,尽量减少多客户端暴露出来的差异特征,更贴合你原本的设计目标。 @Wei-Shaw 可以看下是否符合,有问题我再改 |
86fd7d1 to
1245f07
Compare
背景
Sub2API 支持使用 Anthropic OAuth/SetupToken 账号作为上游,但实际接入方既可能是官方 Claude Code CLI,也可能是其他 Claude API 兼容客户端。
对非 Claude Code 客户端,为了让 OAuth 账号稳定可用,需要对齐 Claude Code 的一些“请求形态/约束”,避免出现 tools/schema/metadata 不符合上游校验或导致签名/解析问题。
修改内容
anthropic-beta: oauth-2025-04-20(messages 与 count_tokens 都覆盖),避免上游拒绝 OAuth。opencode-anthropic-auth的兼容策略补齐请求/响应适配:description清理(去掉本机路径/去除 OpenCode 字样等)。input_schema.properties/required参数名 camelCase -> snake_case,并在响应中反向还原。metadata.user_id自动注入:优先从账号配置中读取claude_user_id/anthropic_user_id,缺省回退到指纹生成。count_tokens补齐token-counting-2024-11-01beta。影响范围
测试
go test ./...