mirror of
https://github.com/chenyme/grok2api.git
synced 2026-12-05 08:01:07 +00:00
refactor: introduce shouldInvalidateConsoleClearance function
This commit is contained in:
1 parent
d33f6b179e
commit
f52e1eb45d
5 files changed
+36
-21
No files matched your search
@@ -146,7 +146,7 @@ func (a *Adapter) ForwardResponse(ctx context.Context, request provider.Response
|
||||
cancel()
|
||||
return nil, readErr
|
||||
}
|
||||
if !provider.IsDefinitiveAccountBlockBody(data) && !provider.IsDPoPProofRequiredBody(data) {
|
||||
if shouldInvalidateConsoleClearance(data) {
|
||||
lease.InvalidateClearance()
|
||||
a.egress.FeedbackForScope(context.WithoutCancel(ctx), egressdomain.ScopeConsole, lease.NodeID, response.StatusCode, nil)
|
||||
}
|
||||
@@ -220,6 +220,12 @@ func (a *Adapter) ForwardResponse(ctx context.Context, request provider.Response
|
||||
return result, nil
|
||||
}
|
||||
|
||||
// shouldInvalidateConsoleClearance keeps account-level and protocol-level
|
||||
// rejections from being misclassified as a broken browser/egress binding.
|
||||
func shouldInvalidateConsoleClearance(body []byte) bool {
|
||||
return !provider.IsDefinitiveAccountBlockBody(body) && !provider.IsDPoPProofRequiredBody(body)
|
||||
}
|
||||
|
||||
func normalizeConversationError(data []byte, operation string, status int) []byte {
|
||||
var envelope struct {
|
||||
Error json.RawMessage `json:"error"`
|
||||
|
||||
@@ -614,6 +614,28 @@ func TestAdapterDoesNotPenalizeEgressForBlockedAccount(t *testing.T) {
|
||||
}
|
||||
}
|
||||
|
||||
func TestShouldInvalidateConsoleClearance(t *testing.T) {
|
||||
t.Parallel()
|
||||
tests := []struct {
|
||||
name string
|
||||
body string
|
||||
want bool
|
||||
}{
|
||||
{name: "dpop protocol rejection", body: `{"code":"unauthorized:dpop-required","error":"DPoP proof required"}`, want: false},
|
||||
{name: "blocked account", body: `{"code":"unauthorized:blocked-user","error":"User is blocked"}`, want: false},
|
||||
{name: "anti-bot rejection", body: `{"error":{"code":7,"message":"Request rejected by anti-bot rules."}}`, want: true},
|
||||
{name: "generic forbidden", body: `forbidden`, want: true},
|
||||
}
|
||||
for _, test := range tests {
|
||||
t.Run(test.name, func(t *testing.T) {
|
||||
t.Parallel()
|
||||
if got := shouldInvalidateConsoleClearance([]byte(test.body)); got != test.want {
|
||||
t.Fatalf("shouldInvalidateConsoleClearance() = %v, want %v", got, test.want)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestAdapterForwardsConsoleHeadersAndNormalizedBody(t *testing.T) {
|
||||
var received map[string]any
|
||||
server := httptest.NewServer(http.HandlerFunc(func(writer http.ResponseWriter, request *http.Request) {
|
||||
|
||||
@@ -231,7 +231,7 @@ func (a *Adapter) fetchDPoPSession(ctx context.Context, ssoToken string, lease *
|
||||
clockSkew := dpopClockSkewFromDateHeader(response.Header.Get("Date"), localBefore, localAfter)
|
||||
if response.StatusCode < 200 || response.StatusCode >= 300 {
|
||||
if response.StatusCode == http.StatusForbidden {
|
||||
if !provider.IsDefinitiveAccountBlockBody(data) {
|
||||
if shouldInvalidateConsoleClearance(data) {
|
||||
lease.InvalidateClearance()
|
||||
}
|
||||
}
|
||||
@@ -410,12 +410,6 @@ func dpopProofIAT(session dpopSession, localNow time.Time) int64 {
|
||||
return localNow.Add(session.clockSkew).UTC().Unix()
|
||||
}
|
||||
|
||||
// dpopClockSkewFromDateHeader mirrors console.x.ai frontend behavior:
|
||||
// skewSeconds ≈ round((serverDate − localNow) / 1s), applied later as
|
||||
// iat = floor((localNow + skew) / 1s). localBefore/localAfter bound the RTT
|
||||
// around the mint response so we estimate local time at header receipt without
|
||||
// an extra time API call. Returns 0 when Date is missing or unparseable so
|
||||
// callers always receive a usable skew value.
|
||||
// dpopClockSkewFromDateHeader mirrors console.x.ai frontend behavior:
|
||||
// skewSeconds ≈ round((serverDate − localNow) / 1s), applied later as
|
||||
// iat = floor((localNow + skew) / 1s). localBefore/localAfter bound the RTT
|
||||
@@ -440,16 +434,9 @@ func dpopClockSkewFromDateHeader(dateHeader string, localBefore, localAfter time
|
||||
}
|
||||
// Mid-point of the local observation window approximates "now" when Date was set.
|
||||
localMid := localBefore.Add(localAfter.Sub(localBefore) / 2)
|
||||
skewSeconds := int64(serverTime.UTC().Sub(localMid.UTC()).Seconds())
|
||||
// Round half away from zero to match Math.round in the official frontend.
|
||||
if rem := serverTime.UTC().Sub(localMid.UTC()) % time.Second; rem != 0 {
|
||||
if rem > 0 && rem >= time.Second/2 {
|
||||
skewSeconds++
|
||||
} else if rem < 0 && -rem >= time.Second/2 {
|
||||
skewSeconds--
|
||||
}
|
||||
}
|
||||
return time.Duration(skewSeconds) * time.Second
|
||||
// Store whole seconds because DPoP iat has second precision. Duration.Round
|
||||
// makes the half-away-from-zero behavior explicit for both skew directions.
|
||||
return serverTime.UTC().Sub(localMid.UTC()).Round(time.Second)
|
||||
}
|
||||
|
||||
func dpopHTU(request *http.Request) string {
|
||||
|
||||
@@ -175,7 +175,7 @@ func (a *Adapter) forwardConsoleMedia(ctx context.Context, credential account.Cr
|
||||
data, truncated, readErr := provider.ReadDiagnosticBody(response.Body)
|
||||
_ = response.Body.Close()
|
||||
dpopRequired := response.StatusCode == http.StatusForbidden && provider.IsDPoPProofRequiredBody(data)
|
||||
if response.StatusCode == http.StatusForbidden && !provider.IsDefinitiveAccountBlockBody(data) && !dpopRequired {
|
||||
if response.StatusCode == http.StatusForbidden && shouldInvalidateConsoleClearance(data) {
|
||||
lease.InvalidateClearance()
|
||||
}
|
||||
if !dpopRequired {
|
||||
@@ -503,7 +503,7 @@ func (a *Adapter) doConsoleVideoJSON(ctx context.Context, credential account.Cre
|
||||
}
|
||||
if response.StatusCode < 200 || response.StatusCode >= 300 {
|
||||
dpopRequired := response.StatusCode == http.StatusForbidden && provider.IsDPoPProofRequiredBody(data)
|
||||
if response.StatusCode == http.StatusForbidden && !provider.IsDefinitiveAccountBlockBody(data) && !dpopRequired {
|
||||
if response.StatusCode == http.StatusForbidden && shouldInvalidateConsoleClearance(data) {
|
||||
lease.InvalidateClearance()
|
||||
}
|
||||
if !dpopRequired {
|
||||
|
||||
@@ -71,7 +71,7 @@ func (a *Adapter) syncConsoleQuotas(ctx context.Context, credential account.Cred
|
||||
return nil, time.Time{}, fmt.Errorf("%w: Console usage rejected", provider.ErrUnauthorized)
|
||||
}
|
||||
dpopRequired := response.StatusCode == http.StatusForbidden && provider.IsDPoPProofRequiredBody(data)
|
||||
if response.StatusCode == http.StatusForbidden && !dpopRequired {
|
||||
if response.StatusCode == http.StatusForbidden && shouldInvalidateConsoleClearance(data) {
|
||||
lease.InvalidateClearance()
|
||||
}
|
||||
if !dpopRequired {
|
||||
|
||||
Reference in new issue
Block a user