mirror of
https://github.com/Wei-Shaw/sub2api.git
synced 2026-10-07 16:37:52 +08:00
审计修复,逐条如下。 H1 利润策略泄露给所有普通用户 profit_control_enabled / profit_min_margin / profit_safety_buffer 从 dto.Group 移到 dto.AdminGroup(后者内嵌前者),赋值相应从 groupFromServiceBase 移到 GroupFromServiceAdmin;前端 TS 同步从 Group 移到 AdminGroup。dto.Group 是 GET /api/v1/groups/available 的响应体,该响应本就带 rate_multiplier,相乘即可反推运营方上游采购成本上限。 api_contract_test.go 的 /groups/available golden JSON 回滚这三个字段,并把 fixture 改成非零值(require.JSONEq 是精确比对,缺字段即失败)。 新增 dto 层边界测试:普通用户 DTO 不含三字段、管理员 DTO 仍含。 M1 利润终检 continue 与 failover 503 退避互动产生活锁 FailoverState 新增 profitVetoedAccountIDs / profitVetoCount 与 RecordProfitVeto():加入排除集 + 计数,达 maxProfitVetoAttempts(10) 返回 FailoverExhausted。HandleSelectionExhausted 的 503 清空分支改为清空后把利润 否决的账号放回排除集;若排除集已全部由利润否决贡献,清空不会带来任何新候选, 直接判定耗尽(否则 SwitchCount 永不前进、退避条件永远成立,每 2s 空转一轮)。 五个 handler 否决点(gateway_handler ×2 / responses / chat_completions / gemini_v1beta)改为经 RecordProfitVeto 决策,耗尽时按无可用账号终止。 回归测试钉死:503 之后持续利润否决必须有限步终止且不 spin;未启用利润控制的 请求退避语义完全不变。 M2 排队等槽后才终检,延迟可放大到 N × WaitPlan.Timeout OpenAI 侧选号循环(自有 failedAccountIDs map,非 FailoverState)新增 recordOpenAIProfitVeto + handleOpenAIProfitVetoExhausted,共用同一上限语义。 覆盖 responses / messages-dispatch / chat_completions / alpha_search / embeddings / images / grok_media 七处,以及 WS 两处否决分支。 M4 ws_v2 透传 ingress 绕过 per-turn 重定价(选方案 B:最小止血) 透传 relay 只回调 AfterTurn、没有任何 turn 起始回调,hooks.BeforeTurn 永远 不触发,而 handler 把 turnPricingAt 初始化成建连时刻 ⇒ 透传连接全部 turn 按 建连时刻的高峰因子结算,客户端峰前建连保活即可全程谷价——正是本 PR 想堵的 漏洞。改为 openAIWSTurnPricing 零值起步、只由 BeforeTurn 冻结;透传路径保持 零值,RecordUsage 回退记录时刻,与引入利润控制前的基线一致。 未选方案 A(给透传补 turn 起始回调):passthrough_relay.go 是 #5167 刚修过的 取消传播/close frame 时序敏感区;且 BeforeTurn 还承担 turn>1 的并发槽位抢占, 接进去等于给透传连接引入 per-turn 抢槽,风险远超本次修复范围。透传仍有建连时 的准入门,只是没有 turn 级复核,已在两处注释写明。 测试:service 层钉死透传 ingress 不触发 BeforeTurn(含失败时的复核指引), handler 层钉死零值语义与逐 turn 覆盖。 M5 装门读分组走了带账号计数聚合的 GetByID SchedulerSnapshotService 新增 GetGroupByIDLite,openai/gateway 两处装门改用 之。门只需要平台/倍率/利润/高峰字段,且该查询发生在「是否启用利润控制」判定 之前,未启用的分组同样付代价。两个测试 stub 的 GetByID 改成 panic 守卫。 M6 认证快照注释与真实读取路径相反 门解析优先取 ctxkey.Group,而它就是本快照物化出来的对象,直连流量走的正是这 条路。改正注释,与 api_key_repo.go 投影处的说明对齐,避免后人照旧注释删列。 M3 rate_multiplier 为 nil 时利润门 fail-closed(不改行为,加护栏) 保留 fail-closed。补 repository 层测试钉死账号调度快照的 full/metadata 两份 payload 都必须保留 RateMultiplier(含 0 值),漏列在 CI 就红。 L1 迁移号注释 191 / 191-192 改为实际的 192/193。 L2 admin group Create 的利润配置预校验改用与 CreateGroup 一致的归一化平台 (新增 service.NormalizeGroupPlatform,两边共用)。保留预校验而非删除: service 层返回的是无类型 error,经 ErrorFrom 会变成 500,删掉会把合法的 400 降级成 500。 L3 前端利润校验的上界改为判定换算后的小数(后端按小数校验 [0,1)), 99.999% 会四舍五入进位成 1.0 而被后端 400;i18n en/zh 同步改为 0-99.99。 L4 clampProfitControlThreshold / profitControlOverThreshold 抽为共用函数, 线上装门/否决点与 profit-preview 不再各自实现,附边界语义测试。 L5 profit-preview 补「默认 D 有账号但最低有效 D 归零」的告警(两档都为 0 由 既有告警覆盖,不重复)。
386 lines
14 KiB
Go
386 lines
14 KiB
Go
package handler
|
|
|
|
import (
|
|
"context"
|
|
"errors"
|
|
"net/http"
|
|
"strconv"
|
|
"time"
|
|
|
|
"github.com/Wei-Shaw/sub2api/internal/pkg/ip"
|
|
middleware2 "github.com/Wei-Shaw/sub2api/internal/server/middleware"
|
|
"github.com/Wei-Shaw/sub2api/internal/service"
|
|
"github.com/gin-gonic/gin"
|
|
"github.com/tidwall/gjson"
|
|
"go.uber.org/zap"
|
|
)
|
|
|
|
// Responses handles OpenAI Responses API endpoint for Anthropic platform groups.
|
|
// POST /v1/responses
|
|
// This converts Responses API requests to Anthropic format, forwards to Anthropic
|
|
// upstream, and converts responses back to Responses format.
|
|
func (h *GatewayHandler) Responses(c *gin.Context) {
|
|
streamStarted := false
|
|
|
|
requestStart := time.Now()
|
|
|
|
apiKey, ok := middleware2.GetAPIKeyFromContext(c)
|
|
if !ok {
|
|
h.responsesErrorResponse(c, http.StatusUnauthorized, "authentication_error", "Invalid API key")
|
|
return
|
|
}
|
|
|
|
subject, ok := middleware2.GetAuthSubjectFromContext(c)
|
|
if !ok {
|
|
h.responsesErrorResponse(c, http.StatusInternalServerError, "api_error", "User context not found")
|
|
return
|
|
}
|
|
reqLog := requestLogger(
|
|
c,
|
|
"handler.gateway.responses",
|
|
zap.Int64("user_id", subject.UserID),
|
|
zap.Int64("api_key_id", apiKey.ID),
|
|
zap.Any("group_id", apiKey.GroupID),
|
|
)
|
|
|
|
// Read request body
|
|
body, err := readLenientJSONRequestBodyWithPrealloc(c.Request, h.cfg)
|
|
if err != nil {
|
|
if maxErr, ok := extractMaxBytesError(err); ok {
|
|
h.responsesErrorResponse(c, http.StatusRequestEntityTooLarge, "invalid_request_error", buildBodyTooLargeMessage(maxErr.Limit))
|
|
return
|
|
}
|
|
h.responsesErrorResponse(c, http.StatusBadRequest, "invalid_request_error", "Failed to read request body")
|
|
return
|
|
}
|
|
|
|
if len(body) == 0 {
|
|
h.responsesErrorResponse(c, http.StatusBadRequest, "invalid_request_error", "Request body is empty")
|
|
return
|
|
}
|
|
|
|
setOpsRequestContext(c, "", false)
|
|
|
|
// Validate JSON
|
|
if !gjson.ValidBytes(body) {
|
|
logRequestBodyParseFailure(reqLog, body, nil)
|
|
h.responsesErrorResponse(c, http.StatusBadRequest, "invalid_request_error", "Failed to parse request body")
|
|
return
|
|
}
|
|
|
|
// Extract model and stream using gjson (like OpenAI handler)
|
|
modelResult := gjson.GetBytes(body, "model")
|
|
if !modelResult.Exists() || modelResult.Type != gjson.String || modelResult.String() == "" {
|
|
h.responsesErrorResponse(c, http.StatusBadRequest, "invalid_request_error", "model is required")
|
|
return
|
|
}
|
|
reqModel := modelResult.String()
|
|
ensureCompositeTargetPlatform(c, apiKey, reqModel)
|
|
if !compositeTargetPlatformResolved(c, apiKey, reqModel) {
|
|
h.responsesErrorResponse(c, http.StatusBadRequest, "invalid_request_error", "Model is not supported by composite groups")
|
|
return
|
|
}
|
|
reqStream, ok := parseOpenAICompatibleStream(body)
|
|
if !ok {
|
|
h.responsesErrorResponse(c, http.StatusBadRequest, "invalid_request_error", invalidStreamFieldTypeMessage)
|
|
return
|
|
}
|
|
reqLog = reqLog.With(zap.String("model", reqModel), zap.Bool("stream", reqStream))
|
|
|
|
setOpsRequestContext(c, reqModel, reqStream)
|
|
setOpsEndpointContext(c, "", int16(service.RequestTypeFromLegacy(reqStream, false)))
|
|
requestCtx := c.Request.Context()
|
|
// 定价上下文无条件装配:/v1/responses 是 token 计费端点,声明生图工具的
|
|
// 混合请求同样按 token 计费(外加图片部分),其 token 利润保护不因请求体
|
|
// 里的任何工具声明(含 Codex 被动 image_gen namespace)而关闭。生图意图
|
|
// 仅用于能力路由与图片计费;独立图片/视频端点才在利润门范围之外。
|
|
requestCtx, pricingAt := service.WithGatewayTokenRequestPricing(requestCtx)
|
|
if service.IsImageGenerationIntentForPlatform("/v1/responses", reqModel, body, openAICompatibleRequestPlatform(c.Request.Context(), apiKey)) {
|
|
requestCtx = service.WithOpenAIImageGenerationIntent(requestCtx)
|
|
}
|
|
c.Request = c.Request.WithContext(requestCtx)
|
|
|
|
// 解析渠道级模型映射
|
|
channelMapping, _ := h.gatewayService.ResolveChannelMappingAndRestrict(requestCtx, apiKey.GroupID, reqModel)
|
|
|
|
// Claude Code only restriction:
|
|
// /v1/responses is never a Claude Code endpoint.
|
|
// When claude_code_only is enabled, this endpoint is rejected.
|
|
// The existing service-layer checkClaudeCodeRestriction handles degradation
|
|
// to fallback groups when the Forward path calls SelectAccountForModelWithExclusions.
|
|
// Here we just reject at handler level since /v1/responses clients can't be Claude Code.
|
|
if apiKey.Group != nil && apiKey.Group.ClaudeCodeOnly {
|
|
h.responsesErrorResponse(c, http.StatusForbidden, "permission_error",
|
|
"This group is restricted to Claude Code clients (/v1/messages only)")
|
|
return
|
|
}
|
|
|
|
if decision := h.checkSecurityAudit(c, reqLog, apiKey, subject, service.ContentModerationProtocolOpenAIResponses, reqModel, body); decision != nil && !decision.AllowNextStage {
|
|
h.responsesSecurityAuditError(c, decision)
|
|
return
|
|
}
|
|
|
|
// Error passthrough binding
|
|
if h.errorPassthroughService != nil {
|
|
service.BindErrorPassthroughService(c, h.errorPassthroughService)
|
|
}
|
|
|
|
subscription, _ := middleware2.GetSubscriptionFromContext(c)
|
|
|
|
service.SetOpsLatencyMs(c, service.OpsAuthLatencyMsKey, time.Since(requestStart).Milliseconds())
|
|
|
|
userReleaseFunc, err := h.concurrencyHelper.AcquireUserSlotWithWait(c, subject.UserID, subject.Concurrency, reqStream, &streamStarted)
|
|
if err != nil {
|
|
reqLog.Warn("gateway.responses.user_slot_acquire_failed", zap.Error(err))
|
|
h.handleConcurrencyError(c, err, "user", streamStarted)
|
|
return
|
|
}
|
|
userReleaseFunc = wrapReleaseOnDone(c.Request.Context(), userReleaseFunc)
|
|
if userReleaseFunc != nil {
|
|
defer userReleaseFunc()
|
|
}
|
|
|
|
// 2. Re-check billing
|
|
if err := h.billingCacheService.CheckBillingEligibility(requestCtx, apiKey.User, apiKey, apiKey.Group, subscription, service.QuotaPlatform(requestCtx, apiKey)); err != nil {
|
|
reqLog.Info("gateway.responses.billing_check_failed", zap.Error(err))
|
|
status, code, message, retryAfter := billingErrorDetails(err)
|
|
if retryAfter > 0 {
|
|
c.Header("Retry-After", strconv.Itoa(retryAfter))
|
|
}
|
|
h.responsesErrorResponse(c, status, code, message)
|
|
return
|
|
}
|
|
|
|
// Parse request for session hash
|
|
bodyRef := service.NewRequestBodyRef(body)
|
|
parsedReq, _ := service.ParseGatewayRequest(bodyRef, "responses")
|
|
if parsedReq == nil {
|
|
parsedReq = &service.ParsedRequest{Model: reqModel, Stream: reqStream, Body: bodyRef}
|
|
}
|
|
parsedReq.SessionContext = &service.SessionContext{
|
|
ClientIP: ip.GetClientIP(c),
|
|
UserAgent: c.GetHeader("User-Agent"),
|
|
APIKeyID: apiKey.ID,
|
|
}
|
|
sessionHash := h.gatewayService.GenerateSessionHash(parsedReq)
|
|
|
|
// 3. Account selection + failover loop
|
|
fs := NewFailoverState(h.maxAccountSwitches, false)
|
|
|
|
for {
|
|
if requestCtx.Err() != nil {
|
|
return
|
|
}
|
|
selection, err := h.gatewayService.SelectAccountWithLoadAwareness(requestCtx, apiKey.GroupID, sessionHash, reqModel, fs.FailedAccountIDs, "", int64(0))
|
|
if err != nil {
|
|
if len(fs.FailedAccountIDs) == 0 {
|
|
cls := classifyNoAccountErrorFromGin(c, h.gatewayService, apiKey, reqModel, reqModel, effectiveAPIKeyPlatform(c, apiKey))
|
|
if !cls.ModelNotFound {
|
|
markOpsRoutingCapacityLimitedIfNoAvailable(c, err)
|
|
}
|
|
message := cls.Message
|
|
if !cls.ModelNotFound {
|
|
message = "No available accounts: " + err.Error()
|
|
}
|
|
h.responsesErrorResponse(c, cls.Status, cls.ErrType, message)
|
|
return
|
|
}
|
|
action := fs.HandleSelectionExhausted(requestCtx)
|
|
switch action {
|
|
case FailoverContinue:
|
|
continue
|
|
case FailoverCanceled:
|
|
failoverClientGone(c)
|
|
return
|
|
default:
|
|
if fs.LastFailoverErr != nil {
|
|
h.handleResponsesFailoverExhausted(c, fs.LastFailoverErr, streamStarted)
|
|
} else {
|
|
h.responsesErrorResponse(c, http.StatusBadGateway, "server_error", "All available accounts exhausted")
|
|
}
|
|
return
|
|
}
|
|
}
|
|
account := selection.Account
|
|
setOpsSelectedAccount(c, account.ID, account.Platform)
|
|
|
|
// 4. Acquire account concurrency slot
|
|
accountReleaseFunc := selection.ReleaseFunc
|
|
if !selection.Acquired {
|
|
if selection.WaitPlan == nil {
|
|
markOpsRoutingCapacityLimited(c)
|
|
h.responsesErrorResponse(c, http.StatusServiceUnavailable, "api_error", "No available accounts")
|
|
return
|
|
}
|
|
accountReleaseFunc, err = h.concurrencyHelper.AcquireAccountSlotWithWaitTimeout(
|
|
c,
|
|
account.ID,
|
|
selection.WaitPlan.MaxConcurrency,
|
|
selection.WaitPlan.Timeout,
|
|
reqStream,
|
|
&streamStarted,
|
|
)
|
|
if err != nil {
|
|
reqLog.Warn("gateway.responses.account_slot_acquire_failed", zap.Int64("account_id", account.ID), zap.Error(err))
|
|
h.handleConcurrencyError(c, err, "account", streamStarted)
|
|
return
|
|
}
|
|
}
|
|
// 终检与准入后绑定必须使用选号结果携带的门:门安装在调度栈的局部
|
|
// ctx 上(composite/fallback 还可能解析出与入口分组不同的门),直接用
|
|
// requestCtx 会退化为空操作。
|
|
admissionCtx := service.ContextWithSelectionProfitGate(requestCtx, selection)
|
|
latest, vetoed, reason := h.gatewayService.GatewayProfitControlVetoLatest(admissionCtx, account)
|
|
if vetoed {
|
|
if accountReleaseFunc != nil {
|
|
accountReleaseFunc()
|
|
}
|
|
reqLog.Debug("gateway.responses.account_slot_profit_vetoed", zap.Int64("account_id", account.ID), zap.String("reason", reason))
|
|
if fs.RecordProfitVeto(account.ID) == FailoverExhausted {
|
|
reqLog.Warn("gateway.responses.profit_veto_attempts_exhausted", zap.Int("profit_veto_count", fs.ProfitVetoCount()))
|
|
h.responsesErrorResponse(c, http.StatusServiceUnavailable, "api_error", profitVetoExhaustedMessage)
|
|
return
|
|
}
|
|
continue
|
|
}
|
|
account = latest
|
|
selection.Account = latest
|
|
if selection.ProfitGateActive() {
|
|
if err := h.gatewayService.BindStickySessionAfterProfitAdmission(admissionCtx, apiKey.GroupID, sessionHash, account.ID); err != nil {
|
|
reqLog.Warn("gateway.responses.bind_sticky_session_after_profit_admission_failed", zap.Int64("account_id", account.ID), zap.Error(err))
|
|
}
|
|
}
|
|
accountReleaseFunc = wrapReleaseOnDone(c.Request.Context(), accountReleaseFunc)
|
|
|
|
// 5. Forward request
|
|
writerSizeBeforeForward := c.Writer.Size()
|
|
forwardBody := body
|
|
if channelMapping.Mapped {
|
|
forwardBody = h.gatewayService.ReplaceModelInBody(body, channelMapping.MappedModel)
|
|
}
|
|
var result *service.ForwardResult
|
|
setActualUpstreamEndpoint(c, "")
|
|
if shouldUseAntigravityCompat(account) {
|
|
if h.antigravityGatewayService == nil {
|
|
h.responsesErrorResponse(c, http.StatusBadGateway, "upstream_error", "Antigravity compatibility service is not configured")
|
|
if accountReleaseFunc != nil {
|
|
accountReleaseFunc()
|
|
}
|
|
return
|
|
}
|
|
setActualUpstreamEndpoint(c, EndpointAntigravityGenerateContent)
|
|
result, err = h.antigravityGatewayService.ForwardAsResponses(requestCtx, c, account, forwardBody, parsedReq)
|
|
} else {
|
|
result, err = h.gatewayService.ForwardAsResponses(requestCtx, c, account, forwardBody, parsedReq)
|
|
}
|
|
|
|
if accountReleaseFunc != nil {
|
|
accountReleaseFunc()
|
|
}
|
|
|
|
if err != nil {
|
|
var failoverErr *service.UpstreamFailoverError
|
|
if errors.As(err, &failoverErr) {
|
|
// Can't failover if streaming content already sent
|
|
if c.Writer.Size() != writerSizeBeforeForward {
|
|
h.handleResponsesFailoverExhausted(c, failoverErr, true)
|
|
return
|
|
}
|
|
action := fs.HandleFailoverError(requestCtx, h.gatewayService, account.ID, account.Platform, account.GetPoolModeRetryCount(), failoverErr)
|
|
switch action {
|
|
case FailoverContinue:
|
|
continue
|
|
case FailoverExhausted:
|
|
h.handleResponsesFailoverExhausted(c, fs.LastFailoverErr, streamStarted)
|
|
return
|
|
case FailoverCanceled:
|
|
failoverClientGone(c)
|
|
return
|
|
}
|
|
}
|
|
upstreamErrorAlreadyCommunicated := gatewayForwardErrorAlreadyCommunicated(c, writerSizeBeforeForward, err)
|
|
wroteFallback := false
|
|
if !upstreamErrorAlreadyCommunicated {
|
|
wroteFallback = h.ensureForwardErrorResponse(c, streamStarted)
|
|
}
|
|
reqLog.Error("gateway.responses.forward_failed",
|
|
zap.Int64("account_id", account.ID),
|
|
zap.Bool("fallback_error_response_written", wroteFallback),
|
|
zap.Bool("upstream_error_response_already_written", upstreamErrorAlreadyCommunicated),
|
|
zap.Error(err),
|
|
)
|
|
return
|
|
}
|
|
|
|
// 6. Record usage
|
|
userAgent := c.GetHeader("User-Agent")
|
|
clientIP := ip.GetClientIP(c)
|
|
requestPayloadHash := service.HashUsageRequestPayload(body)
|
|
inboundEndpoint := GetInboundEndpoint(c)
|
|
upstreamEndpoint := GetUpstreamEndpoint(c, account.Platform)
|
|
|
|
quotaPlatform := service.QuotaPlatform(c.Request.Context(), apiKey)
|
|
sessionID := service.ExtractClientSessionID(c)
|
|
h.submitUsageRecordTask(c.Request.Context(), func(ctx context.Context) {
|
|
if err := h.gatewayService.RecordUsage(ctx, &service.RecordUsageInput{
|
|
Result: result,
|
|
QuotaPlatform: quotaPlatform,
|
|
APIKey: apiKey,
|
|
User: apiKey.User,
|
|
Account: account,
|
|
Subscription: subscription,
|
|
PricingAt: pricingAt,
|
|
InboundEndpoint: inboundEndpoint,
|
|
UpstreamEndpoint: upstreamEndpoint,
|
|
UserAgent: userAgent,
|
|
IPAddress: clientIP,
|
|
RequestPayloadHash: requestPayloadHash,
|
|
APIKeyService: h.apiKeyService,
|
|
SessionID: sessionID,
|
|
ChannelUsageFields: clientRequestedUsageFields(c, channelMapping, reqModel, result.UpstreamModel),
|
|
}); err != nil {
|
|
reqLog.Error("gateway.responses.record_usage_failed",
|
|
zap.Int64("account_id", account.ID),
|
|
zap.Error(err),
|
|
)
|
|
}
|
|
})
|
|
return
|
|
}
|
|
}
|
|
|
|
// responsesErrorResponse writes an error in OpenAI Responses API format.
|
|
func (h *GatewayHandler) responsesErrorResponse(c *gin.Context, status int, code, message string) {
|
|
c.JSON(status, gin.H{
|
|
"error": gin.H{
|
|
"code": code,
|
|
"message": message,
|
|
},
|
|
})
|
|
}
|
|
|
|
// handleResponsesFailoverExhausted writes a failover-exhausted error in Responses format.
|
|
func (h *GatewayHandler) handleResponsesFailoverExhausted(c *gin.Context, lastErr *service.UpstreamFailoverError, streamStarted bool) {
|
|
if streamStarted {
|
|
return // Can't write error after stream started
|
|
}
|
|
if lastErr != nil {
|
|
copyFailoverRetryAfter(c, lastErr.ResponseHeaders)
|
|
}
|
|
if lastErr != nil && lastErr.IsCredentialFailure() {
|
|
status, message := credentialFailoverClientResponse(lastErr)
|
|
h.responsesErrorResponse(c, status, "server_error", message)
|
|
return
|
|
}
|
|
statusCode := http.StatusBadGateway
|
|
if lastErr != nil && lastErr.StatusCode > 0 {
|
|
statusCode = lastErr.StatusCode
|
|
}
|
|
if lastErr != nil && service.IsOpenAISilentRefusalErrorBody(lastErr.ResponseBody) {
|
|
service.SetOpsUpstreamError(c, statusCode, service.OpenAISilentRefusalClientMessage(), "")
|
|
h.responsesErrorResponse(c, http.StatusBadGateway, "upstream_error", service.OpenAISilentRefusalClientMessage())
|
|
return
|
|
}
|
|
h.responsesErrorResponse(c, statusCode, "server_error", "All available accounts exhausted")
|
|
}
|