fix: исправить баги, sentinel-ошибки, устаревшие доки
Баги: - udprelay: errors.Is(DeadlineExceeded) вместо сравнения строк; короткий backoff в ветке lockout; обернуть DTLS-ошибку через %w - bondserver.copyBondToBackend: лимит pending-map в 1024 (паритет с bondclient) - srtpmimicry.RelayPacketConn: bufPool для горячего UDP-пути (паритет с listen.go) - vkauth.token.go: ручной солвер под ctx.WithTimeout(ctx, ...) вместо Background — нет утечки горутины при shutdown Гигиена: - sentinel-ошибки vkauth как var = errors.New(...); вызыватели используют errors.Is - убраны устаревшие комментарии этапов рефакторинга (Pion SPDX, "Package bond", internal/bond, удалённый файл заметок) - .gitignore: *.exe, *.so Тесты + lint проходят.
This commit is contained in:
1 parent
7e3369f478
commit
ed6825976a
18 files changed
+203
-448
No files matched your search
@@ -2,6 +2,7 @@ package vkauth
|
||||
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
"fmt"
|
||||
"log"
|
||||
"math/rand"
|
||||
@@ -211,7 +212,7 @@ func (c *Client) fetchSerialized(ctx context.Context, link string, streamID int)
|
||||
// fetch loops over c.credentials, returning on first success or terminal error.
|
||||
func (c *Client) fetch(ctx context.Context, link string, streamID int) (string, string, []string, error) {
|
||||
if time.Now().Unix() < c.lockout.Load() {
|
||||
return "", "", nil, fmt.Errorf("CAPTCHA_WAIT_REQUIRED: global lockout active")
|
||||
return "", "", nil, fmt.Errorf("%w: %w", ErrCaptchaWaitRequired, ErrLockoutActive)
|
||||
}
|
||||
|
||||
var lastErr error
|
||||
@@ -227,10 +228,10 @@ func (c *Client) fetch(ctx context.Context, link string, streamID int) (string,
|
||||
lastErr = err
|
||||
log.Printf("[STREAM %d] [VK Auth] Failed with client_id=%s: %v", streamID, creds.ClientID, err)
|
||||
|
||||
es := err.Error()
|
||||
if strings.Contains(es, ErrCaptchaWaitRequired) || strings.Contains(es, "FATAL_CAPTCHA") {
|
||||
if errors.Is(err, ErrCaptchaWaitRequired) || errors.Is(err, ErrFatalCaptchaNoStreams) {
|
||||
return "", "", nil, err
|
||||
}
|
||||
es := err.Error()
|
||||
if strings.Contains(es, "error_code:29") || strings.Contains(es, "error_code: 29") || strings.Contains(es, "Rate limit") {
|
||||
log.Printf("[STREAM %d] [VK Auth] Rate limit detected, trying next credentials...", streamID)
|
||||
}
|
||||
|
||||
@@ -4,7 +4,6 @@ import (
|
||||
"context"
|
||||
"errors"
|
||||
"fmt"
|
||||
"strings"
|
||||
"sync/atomic"
|
||||
"testing"
|
||||
"time"
|
||||
@@ -124,11 +123,11 @@ func TestFetchShortCircuitsOnCaptchaWait(t *testing.T) {
|
||||
var calls atomic.Int32
|
||||
c := newTestClient(t, func(_ context.Context, _ string, _ int, _ VKCredentials, _ tlsclient.CookieJar) (string, string, []string, error) {
|
||||
calls.Add(1)
|
||||
return "", "", nil, errors.New(ErrCaptchaWaitRequired)
|
||||
return "", "", nil, ErrCaptchaWaitRequired
|
||||
})
|
||||
|
||||
_, _, _, err := c.GetCredentials(context.Background(), "L", 0)
|
||||
if err == nil || !strings.Contains(err.Error(), ErrCaptchaWaitRequired) {
|
||||
if err == nil || !errors.Is(err, ErrCaptchaWaitRequired) {
|
||||
t.Fatalf("expected CAPTCHA_WAIT_REQUIRED, got %v", err)
|
||||
}
|
||||
if calls.Load() != 1 {
|
||||
@@ -184,7 +183,7 @@ func TestLockoutBlocksFetch(t *testing.T) {
|
||||
}
|
||||
|
||||
_, _, _, err := c.GetCredentials(context.Background(), "L", 0)
|
||||
if err == nil || !strings.Contains(err.Error(), "global lockout active") {
|
||||
if err == nil || !errors.Is(err, ErrLockoutActive) {
|
||||
t.Fatalf("expected lockout error, got %v", err)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -135,9 +135,9 @@ func (c *Client) getTokenChain(ctx context.Context, link string, streamID int, c
|
||||
c.engageLockout(60 * time.Second)
|
||||
if c.streamsFn() == 0 {
|
||||
log.Printf("[STREAM %d] [FATAL] 0 connected streams and captcha solve modes exhausted.", streamID)
|
||||
return "", "", nil, errors.New(ErrFatalCaptchaNoStreams)
|
||||
return "", "", nil, ErrFatalCaptchaNoStreams
|
||||
}
|
||||
return "", "", nil, errors.New(ErrCaptchaWaitRequired)
|
||||
return "", "", nil, ErrCaptchaWaitRequired
|
||||
}
|
||||
|
||||
var successToken string
|
||||
@@ -164,8 +164,11 @@ func (c *Client) getTokenChain(ctx context.Context, link string, streamID int, c
|
||||
break
|
||||
}
|
||||
log.Printf("[STREAM %d] [Captcha] Triggering manual captcha fallback...", streamID)
|
||||
// Background ctx so a short parent deadline doesn't cut user solve time.
|
||||
manualCtx, manualCancel := context.WithTimeout(context.Background(), 3*time.Minute)
|
||||
// Manual solver gets its own 3-min budget so a tight parent
|
||||
// deadline doesn't cut user solve time. We still propagate
|
||||
// parent cancellation (app shutdown) so the in-flight solver
|
||||
// goroutine doesn't outlive the process.
|
||||
manualCtx, manualCancel := context.WithTimeout(ctx, 3*time.Minute)
|
||||
|
||||
type manualRes struct {
|
||||
token string
|
||||
@@ -193,7 +196,7 @@ func (c *Client) getTokenChain(ctx context.Context, link string, streamID int, c
|
||||
log.Printf("[STREAM %d] [Captcha] manual solver returned error: %v", streamID, solveErr)
|
||||
}
|
||||
case <-manualCtx.Done():
|
||||
if manualCtx.Err() == context.DeadlineExceeded {
|
||||
if errors.Is(manualCtx.Err(), context.DeadlineExceeded) {
|
||||
solveErr = fmt.Errorf("manual captcha timed out after 3m")
|
||||
} else {
|
||||
solveErr = fmt.Errorf("manual captcha interrupted: %w", manualCtx.Err())
|
||||
@@ -212,9 +215,9 @@ func (c *Client) getTokenChain(ctx context.Context, link string, streamID int, c
|
||||
c.engageLockout(60 * time.Second)
|
||||
if c.streamsFn() == 0 {
|
||||
log.Printf("[STREAM %d] [FATAL] 0 connected streams and manual captcha failed/timed out.", streamID)
|
||||
return "", "", nil, errors.New(ErrFatalCaptchaNoStreams)
|
||||
return "", "", nil, ErrFatalCaptchaNoStreams
|
||||
}
|
||||
return "", "", nil, errors.New(ErrCaptchaWaitRequired)
|
||||
return "", "", nil, ErrCaptchaWaitRequired
|
||||
}
|
||||
|
||||
if captchaErr.CaptchaAttempt == "0" || captchaErr.CaptchaAttempt == "" {
|
||||
|
||||
@@ -1,6 +1,9 @@
|
||||
package vkauth
|
||||
|
||||
import "time"
|
||||
import (
|
||||
"errors"
|
||||
"time"
|
||||
)
|
||||
|
||||
// VKCredentials is one app_id/app_secret pair used to obtain anonymous tokens.
|
||||
type VKCredentials struct {
|
||||
@@ -35,10 +38,10 @@ const (
|
||||
DefaultStreamsPerCache = 10
|
||||
)
|
||||
|
||||
// Sentinel error message fragments produced by the auth flow. Consumers match
|
||||
// on substring to preserve wire compatibility with the existing caller logic.
|
||||
const (
|
||||
ErrCaptchaWaitRequired = "CAPTCHA_WAIT_REQUIRED"
|
||||
ErrFatalCaptchaNoStreams = "FATAL_CAPTCHA_FAILED_NO_STREAMS"
|
||||
ErrLockoutActiveSuffix = "global lockout active"
|
||||
// Sentinel errors produced by the auth flow. Callers use errors.Is.
|
||||
// The string forms remain stable for log output.
|
||||
var (
|
||||
ErrCaptchaWaitRequired = errors.New("CAPTCHA_WAIT_REQUIRED")
|
||||
ErrFatalCaptchaNoStreams = errors.New("FATAL_CAPTCHA_FAILED_NO_STREAMS")
|
||||
ErrLockoutActive = errors.New("global lockout active")
|
||||
)
|
||||
Reference in new issue
Block a user