945 lines
42 KiB
Go
945 lines
42 KiB
Go
// Package adapter 是账号运行时与外部能力(风控验证、通知、自动化中心)的装配层。
|
||
//
|
||
// 它实现 engine.Handler 与 automation.OrderDetailFetcher,把系统事件转发到自动化中心、
|
||
// 把订单详情抓取/凭证续期接到 Go 协议客户端、把账号告警推到通知器。业务逻辑集中在此,
|
||
// cmd/server 只负责构造与接线。
|
||
package adapter
|
||
|
||
import (
|
||
"context"
|
||
"errors"
|
||
"fmt"
|
||
"log/slog"
|
||
"strings"
|
||
"sync"
|
||
"time"
|
||
|
||
accountapp "xianyu-go/internal/application/account"
|
||
automationapp "xianyu-go/internal/application/automation"
|
||
"xianyu-go/internal/automation"
|
||
"xianyu-go/internal/browser"
|
||
"xianyu-go/internal/chat"
|
||
"xianyu-go/internal/db"
|
||
"xianyu-go/internal/engine"
|
||
"xianyu-go/internal/logsafe"
|
||
"xianyu-go/internal/renewal"
|
||
"xianyu-go/internal/xianyu/cookierefresh"
|
||
"xianyu-go/internal/xianyu/mtop"
|
||
"xianyu-go/internal/xianyu/protocol"
|
||
xrenew "xianyu-go/internal/xianyu/renew"
|
||
)
|
||
|
||
// browserManager 只暴露风控验证能力。普通 Token、Cookie 续期、订单和
|
||
// WebSocket 流程不得通过 Chromium 实现。
|
||
// browserManager 保存浏览器Manager,供当前处理流程使用
|
||
type browserManager interface {
|
||
TokenCaptchaRecover(ctx context.Context, cookieID, cookieStr, verificationURL string, headless bool, provider browser.TokenCaptchaURLProvider) (string, error)
|
||
}
|
||
|
||
// browserTokenCaptchaRecoverer 保存浏览器令牌CaptchaRecoverer,供当前处理流程使用
|
||
type browserTokenCaptchaRecoverer interface {
|
||
TokenCaptchaRecover(ctx context.Context, cookieID, cookieStr, verificationURL string, headless bool, provider browser.TokenCaptchaURLProvider) (string, error)
|
||
}
|
||
|
||
// browserTokenCaptchaEngineRecoverer 保存浏览器令牌CaptchaEngineRecoverer,供当前处理流程使用
|
||
type browserTokenCaptchaEngineRecoverer interface {
|
||
TokenCaptchaRecoverWithEngine(ctx context.Context, cookieID, cookieStr, verificationURL string, headless bool, provider browser.TokenCaptchaURLProvider) (cookies, engine string, err error)
|
||
}
|
||
|
||
// browserTokenCaptchaSnapshotReader 保存浏览器令牌CaptchaSnapshotReader,供当前处理流程使用
|
||
type browserTokenCaptchaSnapshotReader interface {
|
||
TokenCaptchaCookieSnapshot(ctx context.Context, cookieID string, headless bool) (cookies string, snapshot []cookierefresh.BrowserCookie, err error)
|
||
}
|
||
|
||
// tokenCaptchaRequester 保存令牌CaptchaRequester,供当前处理流程使用
|
||
type tokenCaptchaRequester interface {
|
||
RequestFreshCaptchaURLContext(ctx context.Context, cookiesStr, deviceID string) (*mtop.FreshCaptchaResult, error)
|
||
}
|
||
|
||
// orderDetailClient 保存订单DetailClient,供当前处理流程使用
|
||
type orderDetailClient interface {
|
||
FetchOrderDetail(ctx context.Context, cookiesStr, orderID string) (*mtop.OrderDetailResult, error)
|
||
}
|
||
|
||
// Adapter 实现 engine.Handler 与 automation.OrderDetailFetcher,
|
||
// 把系统消息、订单详情抓取和协议级凭证续期接到 Go 客户端与自动化中心。
|
||
//
|
||
// 自动发货只走 automation.Center;用户聊天消息由 Account 内部 ReplyService 处理,
|
||
// 故 HandleChatMessage 为空实现。
|
||
// Adapter 保存Adapter,供当前处理流程使用
|
||
type Adapter struct {
|
||
store *db.Store
|
||
browser browserManager
|
||
logger *slog.Logger
|
||
automation *automation.Center
|
||
notifier notifyNotifier
|
||
// credentialWake 负责凭证写回后的自动化任务唤醒;Adapter 只依赖应用端口,不直接决定任务状态。
|
||
credentialWake accountapp.CredentialWakePort
|
||
renewSvc xrenew.Service
|
||
cooldown *renewal.CooldownManager
|
||
captchaReq tokenCaptchaRequester
|
||
orderMTop orderDetailClient
|
||
chat *chat.Service
|
||
|
||
orderFetchMu sync.Mutex
|
||
lastOrderFetch time.Time
|
||
// passwordCoordinator 按账号协调协议凭证恢复,避免重复外部续期并允许不同账号并行执行。
|
||
passwordCoordinator *accountapp.CredentialRefreshCoordinator
|
||
// passwordResultMu 仅保护 passwordResults;持锁期间不执行凭证读取或平台 I/O。
|
||
passwordResultMu sync.Mutex
|
||
// passwordResults 保存正在执行的协议续期结果,后到调用方可等待同一账号的结果。
|
||
passwordResults map[string]*passwordRenewalResult
|
||
}
|
||
|
||
// passwordRenewalResult 是同账号并发续期调用共享的完成信号与最终成功状态。
|
||
type passwordRenewalResult struct {
|
||
// done 由首次续期调用关闭,所有等待者只接收不关闭。
|
||
done chan struct{}
|
||
// renewed 在关闭 done 前写入,关闭 channel 建立等待者读取该字段的 happens-before 关系。
|
||
renewed bool
|
||
}
|
||
|
||
// notifyNotifier 是 *notify.Notifier 的最小接口,避免 adapter 直接依赖 notify 包
|
||
// (notify 包未来若反向引用 adapter 也不会形成循环)。
|
||
// notifyNotifier 保存notifyNotifier,供当前处理流程使用
|
||
type notifyNotifier interface {
|
||
NotifyAccountAlert(cookieID, level, title, body string)
|
||
}
|
||
|
||
// notifyEventNotifier 保存notifyEventNotifier,供当前处理流程使用
|
||
type notifyEventNotifier interface {
|
||
NotifyAccountEvent(cookieID, eventType, level, title, body string)
|
||
}
|
||
|
||
// New 构造 Adapter。automation 与 notifier 通过 Set* 后期注入(因创建顺序存在循环:
|
||
// mgr 依赖 adapter,automation 依赖 mgr,adapter 又依赖 automation)。
|
||
// New 负责New相关处理。
|
||
func New(store *db.Store, bm *browser.Manager, logger *slog.Logger) *Adapter {
|
||
if logger == nil {
|
||
logger = slog.Default()
|
||
}
|
||
return &Adapter{
|
||
store: store,
|
||
browser: browserManagerOrNil(bm),
|
||
logger: logger,
|
||
credentialWake: newCredentialWakeService(store),
|
||
cooldown: renewal.GlobalCooldown,
|
||
captchaReq: mtop.NewClient(),
|
||
orderMTop: mtop.NewClient(),
|
||
passwordCoordinator: accountapp.NewCredentialRefreshCoordinator(),
|
||
passwordResults: make(map[string]*passwordRenewalResult),
|
||
}
|
||
}
|
||
|
||
// newCredentialWakeService 构造凭证唤醒应用服务;数据库仓储仅在适配器边界内创建并隐藏。
|
||
func newCredentialWakeService(store *db.Store) accountapp.CredentialWakePort {
|
||
if store == nil {
|
||
return nil
|
||
}
|
||
// repository 保存凭证唤醒所需的窄数据库适配器。
|
||
repository := NewAutomationCredentialWakeRepository(store)
|
||
// service、err 保存凭证唤醒应用服务及其装配错误。
|
||
service, err := automationapp.NewCredentialWakeService(repository)
|
||
if err != nil {
|
||
return nil
|
||
}
|
||
return service
|
||
}
|
||
|
||
// browserManagerOrNil 把 *browser.Manager 转为接口;nil 时返回 nil 接口。
|
||
func browserManagerOrNil(bm *browser.Manager) browserManager {
|
||
if bm == nil {
|
||
return nil
|
||
}
|
||
return bm
|
||
}
|
||
|
||
// SetAutomation 注入系统事件的自动化中心;该 setter 仅保留给构造环过渡和隔离测试,待生产装配改为构造注入且旧测试清零后删除。
|
||
func (a *Adapter) SetAutomation(c *automation.Center) { a.automation = c }
|
||
|
||
// SetNotifier 注入账号告警通知器;该 setter 仅保留给构造环过渡和隔离测试,待生产装配改为构造注入且旧测试清零后删除。
|
||
func (a *Adapter) SetNotifier(n notifyNotifier) { a.notifier = n }
|
||
|
||
// SetCredentialWakeService 注入凭证恢复后的自动化唤醒端口;仅供构造环过渡和隔离测试,待统一应用装配后删除。
|
||
func (a *Adapter) SetCredentialWakeService(service accountapp.CredentialWakePort) {
|
||
a.credentialWake = service
|
||
}
|
||
|
||
// SetBrowser 覆盖浏览器实现;生产启动后不得替换必需依赖,待测试改用构造选项后删除此兼容入口。
|
||
func (a *Adapter) SetBrowser(b browserManager) { a.browser = b }
|
||
|
||
// SetRenewService 覆盖轻量续期服务;该 setter 是测试替身覆盖点,待续期端口构造注入并迁移现有测试后删除。
|
||
func (a *Adapter) SetRenewService(s xrenew.Service) { a.renewSvc = s }
|
||
|
||
// SetTokenCaptchaRequester 覆盖 token 风控验证链接刷新器;仅供测试隔离网络,冻结验证码生产路径不得运行时替换。
|
||
func (a *Adapter) SetTokenCaptchaRequester(r tokenCaptchaRequester) { a.captchaReq = r }
|
||
|
||
// SetOrderDetailClient 覆盖纯 Go 订单详情客户端;该 setter 是测试替身覆盖点,待订单端口构造注入并迁移测试后删除。
|
||
func (a *Adapter) SetOrderDetailClient(c orderDetailClient) { a.orderMTop = c }
|
||
|
||
// SetChatService 注入用户聊天旁路服务;它只负责持久化和广播,不改变自动回复路径,待构造环拆除后删除兼容 setter。
|
||
func (a *Adapter) SetChatService(service *chat.Service) { a.chat = service }
|
||
|
||
// HandleChatMessage 用户聊天消息由 Account 内部 ReplyService 处理,此处空实现满足接口。
|
||
func (a *Adapter) HandleChatMessage(ctx context.Context, message engine.ChatMessage) error {
|
||
if a.chat == nil {
|
||
return nil
|
||
}
|
||
// Xianyu echoes messages sent by this account back over the same WS. Those
|
||
// sends are already captured by HandleOutgoingChatMessage; recording the
|
||
// echo as incoming would put our own bubble on the buyer side and duplicate it.
|
||
// selfID 保存当前账号从 Cookie 解析出的平台用户标识,用于过滤同账号 WebSocket 回显。
|
||
if selfID := protocol.TransCookies(message.CookieStr)["unb"]; selfID != "" &&
|
||
strings.TrimSuffix(strings.TrimSpace(message.SenderUserID), "@goofish") == strings.TrimSuffix(strings.TrimSpace(selfID), "@goofish") {
|
||
a.logger.Info("忽略账号自身发送的聊天回显", "account", message.AccountID, "chat_id", message.ChatID, "sender_id", message.SenderUserID)
|
||
return nil
|
||
}
|
||
// stored、inserted、err 保存落库消息、是否首次插入及持久化错误。
|
||
stored, inserted, err := a.chat.RecordIncoming(ctx, chat.Incoming{
|
||
AccountID: message.AccountID, ChatID: message.ChatID, BuyerID: message.SenderUserID,
|
||
BuyerName: message.SenderName, Text: message.Text, MessageID: message.MessageID, ItemID: message.ItemID, Raw: message.Raw,
|
||
})
|
||
if stored != nil {
|
||
a.logger.Info("实时聊天消息已入库", "account", message.AccountID, "chat_id", message.ChatID,
|
||
"message_key", stored.MessageKey, "message_type", stored.MessageType, "inserted", inserted)
|
||
}
|
||
return err
|
||
}
|
||
|
||
// HandleOutgoingChatMessage records successful manual/automatic text sends as
|
||
// a side channel; it never participates in platform delivery.
|
||
// HandleOutgoingChatMessage 处理Outgoing聊天消息。
|
||
func (a *Adapter) HandleOutgoingChatMessage(ctx context.Context, message engine.OutgoingChatMessage) error {
|
||
if a.chat == nil {
|
||
return nil
|
||
}
|
||
// err 保存err,供当前处理流程使用
|
||
_, err := a.chat.RecordOutgoingSent(ctx, db.ChatSession{CookieID: message.AccountID, ChatID: message.ChatID,
|
||
BuyerID: message.BuyerID}, message.MessageKey, message.Text)
|
||
return err
|
||
}
|
||
|
||
// HandleMessageRead 接收平台出站消息已读回执,并把非敏感已读状态更新委托给聊天服务。
|
||
func (a *Adapter) HandleMessageRead(ctx context.Context, event engine.MessageReadEvent) error {
|
||
if a.chat == nil {
|
||
return nil
|
||
}
|
||
// message、err 保存按平台消息键更新的出站消息及持久化错误,以便缺键时回退会话最近消息。
|
||
message, err := a.chat.MarkOutgoingRead(ctx, event.AccountID, event.MessageID, event.ReadAt)
|
||
if errors.Is(err, db.ErrNotFound) && event.ChatID != "" {
|
||
message, err = a.chat.MarkLatestOutgoingRead(ctx, event.AccountID, event.ChatID, event.ReadAt)
|
||
}
|
||
if err == nil && message != nil {
|
||
a.logger.Info("聊天出站消息已标记已读", "account", event.AccountID, "chat_id", event.ChatID,
|
||
"message_id", event.MessageID, "message_key", message.MessageKey, "read_status", message.ReadStatus)
|
||
}
|
||
return err
|
||
}
|
||
|
||
// OnAccountAlert 把账号告警(token 失效/自动恢复失败/风控验证等)转发给通知器,
|
||
// 推送到该账号已绑定的通知渠道。
|
||
// OnAccountAlert 负责On账号Alert相关处理。
|
||
func (a *Adapter) OnAccountAlert(ctx context.Context, cookieID, level, title, body string) {
|
||
a.OnAccountEvent(ctx, cookieID, classifyAccountAlertEvent(title, body), level, title, body)
|
||
}
|
||
|
||
// OnAccountEvent 把带类型的账号事件转发给通知器。
|
||
func (a *Adapter) OnAccountEvent(_ context.Context, cookieID, eventType, level, title, body string) {
|
||
if a.notifier == nil {
|
||
a.logger.Warn("账号事件通知未发送:通知器未注入", "account", cookieID, "event_type", eventType, "level", level, "title", title)
|
||
return
|
||
}
|
||
if // n、ok 保存n、ok,供当前处理流程使用
|
||
n, ok := a.notifier.(notifyEventNotifier); ok {
|
||
n.NotifyAccountEvent(cookieID, eventType, level, title, body)
|
||
return
|
||
}
|
||
a.notifier.NotifyAccountAlert(cookieID, level, title, body)
|
||
}
|
||
|
||
// classifyAccountAlertEvent 负责classify账号AlertEvent相关处理。
|
||
func classifyAccountAlertEvent(title, body string) string {
|
||
// msg 保存msg,供当前处理流程使用
|
||
msg := strings.ToLower(title + " " + body)
|
||
switch {
|
||
case strings.Contains(msg, "风控"), strings.Contains(msg, "验证"),
|
||
strings.Contains(msg, "滑块"), strings.Contains(msg, "captcha"),
|
||
strings.Contains(msg, "risk"), strings.Contains(msg, "x5sec"):
|
||
return engine.EventSecurityVerification
|
||
case strings.Contains(msg, "禁用"), strings.Contains(msg, "disabled"):
|
||
return engine.EventAccountDisabled
|
||
case strings.Contains(msg, "掉线"), strings.Contains(msg, "离线"),
|
||
strings.Contains(msg, "offline"), strings.Contains(msg, "session"),
|
||
strings.Contains(msg, "登录凭证已失效"):
|
||
return engine.EventAccountOffline
|
||
case strings.Contains(msg, "token"), strings.Contains(msg, "续期"), strings.Contains(msg, "renew"):
|
||
return engine.EventTokenRenewal
|
||
default:
|
||
return engine.EventSystemError
|
||
}
|
||
}
|
||
|
||
// OnTokenCaptchaVerification 处理 token 刷新触发的闲鱼滑块风控。
|
||
func (a *Adapter) OnTokenCaptchaVerification(ctx context.Context, cookieID, cookieStr, verificationURL, deviceID string) (*mtop.RefreshResult, bool) {
|
||
// start 保存开始,供当前处理流程使用
|
||
start := time.Now()
|
||
// logID 保存logID,供当前处理流程使用
|
||
var logID int64
|
||
if a.store != nil && a.store.RiskLogs != nil {
|
||
if // id、err 保存id、err,供当前处理流程使用
|
||
id, err := a.store.RiskLogs.Add(ctx, db.RiskControlLog{
|
||
CookieID: cookieID,
|
||
EventType: "slider_captcha",
|
||
EventDescription: "触发场景: Token刷新, URL: " + verificationURL,
|
||
ProcessingStatus: "processing",
|
||
}); err == nil {
|
||
logID = id
|
||
} else {
|
||
a.logger.Warn("记录风控日志失败", "account", cookieID, "err", err)
|
||
}
|
||
}
|
||
|
||
// showBrowser 保存show浏览器,供当前处理流程使用
|
||
showBrowser := false
|
||
// metadataJSON 保存metadataJSON,供当前处理流程使用
|
||
metadataJSON := ""
|
||
if a.store == nil || a.store.Cookies == nil {
|
||
a.OnAccountEvent(ctx, cookieID, engine.EventSecurityVerification, engine.AlertLevelWarn,
|
||
"token 风控验证无法保存", "账号存储未初始化,无法保存验证后的 Cookie。")
|
||
return nil, false
|
||
}
|
||
|
||
if // d、err 保存d、err,供当前处理流程使用
|
||
d, err := a.store.Cookies.GetCookiePlatformRuntimeData(ctx, cookieID); err == nil {
|
||
showBrowser = d.ShowBrowser
|
||
metadataJSON = d.MetadataJSON
|
||
}
|
||
|
||
// provider 保存provider,供当前处理流程使用
|
||
provider := func(runCtx context.Context, currentCookies string) (string, bool, string, error) {
|
||
if a.captchaReq == nil {
|
||
return "", false, "", nil
|
||
}
|
||
// res、err 保存res、err,供当前处理流程使用
|
||
res, err := a.captchaReq.RequestFreshCaptchaURLContext(runCtx, currentCookies, deviceID)
|
||
if err != nil || res == nil {
|
||
return "", false, "", err
|
||
}
|
||
return res.VerificationURL, res.TokenOK, res.UpdatedCookies, nil
|
||
}
|
||
|
||
// newCookies 保存newCookies,供当前处理流程使用
|
||
newCookies := ""
|
||
// captchaEngine 保存captchaEngine,供当前处理流程使用
|
||
captchaEngine := "playwright"
|
||
// remoteHandled 保存remoteHandled,供当前处理流程使用
|
||
remoteHandled := false
|
||
// captchaHeadless 保存captchaHeadless,供当前处理流程使用
|
||
captchaHeadless := browser.ResolveHeadless(showBrowser)
|
||
// err 保存err,供当前处理流程使用
|
||
var err error
|
||
if // remoteConfig 保存remote配置,供当前处理流程使用
|
||
remoteConfig := a.loadRemoteCaptchaConfig(ctx, cookieID); remoteConfig != nil {
|
||
newCookies, remoteHandled, err = solveRemoteCaptcha(
|
||
ctx, newRemoteCaptchaHTTPClient(), *remoteConfig,
|
||
cookieID, verificationURL, cookieStr, deviceID, provider,
|
||
)
|
||
if remoteHandled {
|
||
captchaEngine = "remote"
|
||
} else if err != nil {
|
||
a.logger.Warn("远程过滑块不可用,回退本机逻辑", "account", cookieID, "err", err)
|
||
err = nil
|
||
}
|
||
}
|
||
if !remoteHandled {
|
||
// br、ok 保存br、ok,供当前处理流程使用
|
||
br, ok := a.browser.(browserTokenCaptchaRecoverer)
|
||
if a.browser == nil || !ok {
|
||
a.OnAccountEvent(ctx, cookieID, engine.EventSecurityVerification, engine.AlertLevelWarn,
|
||
"token 风控验证无法自动处理", "远程服务不可用且浏览器自动化未启用,无法自动完成 token 滑块验证。")
|
||
return nil, false
|
||
}
|
||
if // withEngine、ok 保存withEngine、ok,供当前处理流程使用
|
||
withEngine, ok := a.browser.(browserTokenCaptchaEngineRecoverer); ok {
|
||
newCookies, captchaEngine, err = withEngine.TokenCaptchaRecoverWithEngine(
|
||
ctx, cookieID, cookieStr, verificationURL, captchaHeadless, provider,
|
||
)
|
||
} else {
|
||
newCookies, err = br.TokenCaptchaRecover(
|
||
ctx, cookieID, cookieStr, verificationURL, captchaHeadless, provider,
|
||
)
|
||
}
|
||
}
|
||
if err != nil {
|
||
// manualURL 保存manualURL,供当前处理流程使用
|
||
manualURL := browser.TokenCaptchaManualVerificationURL(err)
|
||
if strings.TrimSpace(manualURL) == "" {
|
||
manualURL = verificationURL
|
||
}
|
||
a.logger.Warn("token 风控滑块处理失败", "account", cookieID, "err", logsafe.Error(err), "verification_url", logsafe.URL(manualURL))
|
||
if a.store != nil && a.store.RiskLogs != nil {
|
||
_ = a.store.RiskLogs.Update(ctx, logID, db.RiskControlLog{
|
||
ProcessingStatus: "failed",
|
||
ProcessingResult: fmt.Sprintf("token 风控滑块处理失败,耗时: %.2f秒", time.Since(start).Seconds()),
|
||
CaptchaEngine: captchaEngine,
|
||
ErrorMessage: err.Error(),
|
||
DurationMS: time.Since(start).Milliseconds(),
|
||
})
|
||
}
|
||
a.OnAccountEvent(ctx, cookieID, engine.EventSecurityVerification, engine.AlertLevelWarn,
|
||
"token 风控验证失败", err.Error())
|
||
return nil, false
|
||
}
|
||
if strings.TrimSpace(newCookies) == "" {
|
||
return nil, false
|
||
}
|
||
// cookieSnapshot 保存登录凭证Snapshot,供当前处理流程使用
|
||
var cookieSnapshot []cookierefresh.BrowserCookie
|
||
// snapshotComplete 保存snapshotComplete,供当前处理流程使用
|
||
snapshotComplete := false
|
||
if !remoteHandled {
|
||
if // reader、ok 保存reader、ok,供当前处理流程使用
|
||
reader, ok := a.browser.(browserTokenCaptchaSnapshotReader); ok {
|
||
// profileCookies、profileSnapshot、readErr 保存profileCookies、profileSnapshot、readErr,供当前处理流程使用
|
||
profileCookies, profileSnapshot, readErr := reader.TokenCaptchaCookieSnapshot(ctx, cookieID, captchaHeadless)
|
||
if readErr != nil {
|
||
a.logger.Warn("读取滑块验证后完整 Cookie Jar 失败,回退 Go 快照合并", "account", cookieID, "err", readErr)
|
||
} else {
|
||
cookieSnapshot = cookierefresh.NormalizeSnapshot(profileSnapshot)
|
||
if cookieSnapshot == nil {
|
||
cookieSnapshot = []cookierefresh.BrowserCookie{}
|
||
}
|
||
snapshotComplete = true
|
||
newCookies = profileCookies
|
||
}
|
||
}
|
||
}
|
||
if !snapshotComplete {
|
||
if // existing、complete 保存existing、complete,供当前处理流程使用
|
||
existing, complete := cookierefresh.SnapshotFromMetadataOK(metadataJSON); complete {
|
||
cookieSnapshot = cookierefresh.ReconcileSnapshotWithCookieString(existing, newCookies)
|
||
snapshotComplete = true
|
||
}
|
||
}
|
||
// updatedMetadata 保存updatedMetadata,供当前处理流程使用
|
||
updatedMetadata := cookierefresh.MetadataWithoutSnapshot(metadataJSON)
|
||
if snapshotComplete {
|
||
updatedMetadata = cookierefresh.MetadataWithSnapshot(metadataJSON, cookieSnapshot)
|
||
}
|
||
if // err 保存err,供当前处理流程使用
|
||
err := a.store.Cookies.UpdateRenewalCookie(ctx, cookieID, newCookies, updatedMetadata, time.Now().Unix()); err != nil {
|
||
a.logger.Warn("保存 token 风控恢复 Cookie 失败", "account", cookieID, "err", err)
|
||
if a.store != nil && a.store.RiskLogs != nil {
|
||
_ = a.store.RiskLogs.Update(ctx, logID, db.RiskControlLog{
|
||
ProcessingStatus: "error",
|
||
ProcessingResult: "滑块完成但保存 Cookie 失败",
|
||
CaptchaEngine: captchaEngine,
|
||
ErrorMessage: err.Error(),
|
||
DurationMS: time.Since(start).Milliseconds(),
|
||
})
|
||
}
|
||
return nil, false
|
||
}
|
||
if a.store.Tokens != nil {
|
||
_ = a.store.Tokens.Clear(ctx, cookieID)
|
||
}
|
||
if a.store != nil && a.store.RiskLogs != nil {
|
||
_ = a.store.RiskLogs.Update(ctx, logID, db.RiskControlLog{
|
||
ProcessingStatus: "success",
|
||
ProcessingResult: fmt.Sprintf("token 风控滑块验证成功(%s),已更新登录凭证,耗时: %.2f秒", captchaEngine, time.Since(start).Seconds()),
|
||
CaptchaEngine: captchaEngine,
|
||
DurationMS: time.Since(start).Milliseconds(),
|
||
})
|
||
}
|
||
a.OnAccountEvent(ctx, cookieID, engine.EventSecurityVerification, engine.AlertLevelInfo,
|
||
"token 风控验证已自动恢复", "系统已完成验证并更新登录凭证。")
|
||
return &mtop.RefreshResult{
|
||
UpdatedCookies: newCookies,
|
||
CookieSnapshot: cookieSnapshot,
|
||
CookieSnapshotComplete: snapshotComplete,
|
||
CookieStateChanged: newCookies != cookieStr || snapshotComplete,
|
||
}, true
|
||
}
|
||
|
||
// HandleSystemEvent 把系统卡片事件转发到自动化中心,由自动化规则决定是否执行。
|
||
func (a *Adapter) HandleSystemEvent(ctx context.Context, task automation.Task) error {
|
||
if a.automation == nil {
|
||
return nil
|
||
}
|
||
a.logger.Info("系统自动化事件", "account", task.AccountID, "trigger", task.TriggerType, "order_id", task.OrderID)
|
||
return a.automation.HandleTask(ctx, task)
|
||
}
|
||
|
||
// FetchOrderDetail 实现 automation.OrderDetailFetcher。只在本地订单缺少关键字段时
|
||
// 调用纯 Go MTOP 客户端,并将详情请求串行化、至少间隔 3 秒,避免短时间高频访问闲鱼。
|
||
// FetchOrderDetail 负责Fetch订单Detail相关处理。
|
||
func (a *Adapter) FetchOrderDetail(ctx context.Context, cookieID, orderID, itemID, buyerID, _ string) (*automation.OrderDetail, error) {
|
||
if // detail、ok 保存detail、ok,供当前处理流程使用
|
||
detail, ok := a.localOrderDetail(ctx, orderID); ok {
|
||
return detail, nil
|
||
}
|
||
if a.orderMTop == nil {
|
||
return nil, fmt.Errorf("订单详情 MTOP 客户端未配置")
|
||
}
|
||
// detail、err 保存detail、err,供当前处理流程使用
|
||
detail, err := a.fetchOrderDetailAttempt(ctx, cookieID, orderID)
|
||
if err == nil || !mtop.IsSessionExpiredErr(err) {
|
||
return detail, err
|
||
}
|
||
a.logger.Warn("订单详情检测到 Session 过期,开始即时续期", "account", cookieID, "order_id", orderID)
|
||
if !a.OnPasswordLoginRefresh(ctx, cookieID) {
|
||
return nil, fmt.Errorf("订单详情 Session 过期且即时续期失败: %w", err)
|
||
}
|
||
a.logger.Info("Cookie 即时续期成功,重新请求订单详情", "account", cookieID, "order_id", orderID)
|
||
return a.fetchOrderDetailAttempt(ctx, cookieID, orderID)
|
||
}
|
||
|
||
// fetchOrderDetailAttempt 负责fetch订单Detail尝试次数相关处理。
|
||
func (a *Adapter) fetchOrderDetailAttempt(ctx context.Context, cookieID, orderID string) (*automation.OrderDetail, error) {
|
||
|
||
a.orderFetchMu.Lock()
|
||
defer a.orderFetchMu.Unlock()
|
||
// 等锁期间其他流程可能已经补齐订单,再检查一次。
|
||
if detail, ok := a.localOrderDetail(ctx, orderID); ok {
|
||
return detail, nil
|
||
}
|
||
if // remain 保存remain,供当前处理流程使用
|
||
remain := 3*time.Second - time.Since(a.lastOrderFetch); !a.lastOrderFetch.IsZero() && remain > 0 {
|
||
// timer 保存定时器,供当前处理流程使用
|
||
timer := time.NewTimer(remain)
|
||
defer timer.Stop()
|
||
select {
|
||
case <-ctx.Done():
|
||
return nil, ctx.Err()
|
||
case <-timer.C:
|
||
}
|
||
}
|
||
a.lastOrderFetch = time.Now()
|
||
// credentialUnlock 保存credentialUnlock,供当前处理流程使用
|
||
credentialUnlock := a.store.LockAccountCredentials(cookieID)
|
||
// credentialLocked 标识当前调用是否持有账号凭证锁。
|
||
credentialLocked := true
|
||
defer func() {
|
||
if credentialLocked {
|
||
credentialUnlock()
|
||
}
|
||
}()
|
||
platformData, err := a.store.Cookies.GetCookiePlatformRuntimeData(ctx, cookieID) // platformData 只包含订单 MTOP 请求所需的 Cookie 与 metadata。
|
||
if err != nil {
|
||
return nil, fmt.Errorf("读取订单账号最新 Cookie: %w", err)
|
||
}
|
||
if strings.TrimSpace(platformData.Value) == "" && !hasCompleteCookieSnapshot(platformData.MetadataJSON) {
|
||
return nil, fmt.Errorf("订单账号 %s Cookie 为空", cookieID)
|
||
}
|
||
// cookieStr 保存登录凭证Str,供当前处理流程使用
|
||
cookieStr := platformData.Value
|
||
// requestCtx 保存请求Ctx,供当前处理流程使用
|
||
var requestCtx context.Context
|
||
// cookieSession 保存登录凭证会话,供当前处理流程使用
|
||
var cookieSession *mtop.CookieSession
|
||
if // snapshot、complete 保存snapshot、complete,供当前处理流程使用
|
||
snapshot, complete := cookierefresh.SnapshotFromMetadataOK(platformData.MetadataJSON); complete {
|
||
requestCtx, cookieSession = mtop.WithCookieSnapshot(ctx, snapshot)
|
||
} else {
|
||
requestCtx, cookieSession = mtop.WithFlatCookieSession(ctx, cookieStr)
|
||
}
|
||
// 账号凭证快照已读取完成;慢速 MTOP 请求不得继续持有共享凭证锁。
|
||
credentialUnlock()
|
||
credentialLocked = false
|
||
// detail、fetchErr 保存detail、fetchErr,供当前处理流程使用
|
||
detail, fetchErr := a.orderMTop.FetchOrderDetail(requestCtx, cookieStr, orderID)
|
||
// authoritativeCookies、authoritativeSnapshot、sessionChanged 保存authoritativeCookies、authoritativeSnapshot、sessionChanged,供当前处理流程使用
|
||
authoritativeCookies, authoritativeSnapshot, sessionChanged := cookieSession.State()
|
||
// credentialUnlock 保存重新进入凭证提交临界区的释放函数。
|
||
credentialUnlock = a.store.LockAccountCredentials(cookieID)
|
||
credentialLocked = true
|
||
// latestPlatformData 和 reloadErr 保存外部调用完成后的最新凭证快照及重读错误。
|
||
latestPlatformData, reloadErr := a.store.Cookies.GetCookiePlatformRuntimeData(ctx, cookieID)
|
||
if reloadErr != nil {
|
||
fetchErr = errors.Join(fetchErr, fmt.Errorf("重读订单账号最新 Cookie: %w", reloadErr))
|
||
} else if latestPlatformData.Value != platformData.Value || latestPlatformData.MetadataJSON != platformData.MetadataJSON {
|
||
// 凭证在外部调用期间已被其他流程更新,丢弃旧快照响应,避免覆盖更新后的状态。
|
||
sessionChanged = false
|
||
authoritativeSnapshot = nil
|
||
} else if sessionChanged {
|
||
// metadata 保存metadata,供当前处理流程使用
|
||
metadata := cookierefresh.MetadataWithoutSnapshot(latestPlatformData.MetadataJSON)
|
||
if authoritativeSnapshot != nil {
|
||
metadata = cookierefresh.MetadataWithSnapshot(latestPlatformData.MetadataJSON, authoritativeSnapshot)
|
||
}
|
||
if // persistErr 保存persistErr,供当前处理流程使用
|
||
persistErr := a.store.Cookies.UpdateRenewalCookie(ctx, cookieID, authoritativeCookies, metadata, time.Now().Unix()); persistErr != nil {
|
||
fetchErr = errors.Join(fetchErr, fmt.Errorf("保存订单详情响应 Cookie: %w", persistErr))
|
||
} else {
|
||
cookieStr = authoritativeCookies
|
||
a.wakeCredentialBlockedAutomation(ctx, cookieID)
|
||
}
|
||
}
|
||
if fetchErr != nil {
|
||
return nil, fetchErr
|
||
}
|
||
if detail == nil {
|
||
return nil, errors.New("订单详情 MTOP 接口返回空结果")
|
||
}
|
||
if reloadErr == nil && latestPlatformData.Value == platformData.Value && latestPlatformData.MetadataJSON == platformData.MetadataJSON && !sessionChanged && authoritativeSnapshot == nil && detail.UpdatedCookies != "" && detail.UpdatedCookies != cookieStr {
|
||
// metadata 保存metadata,供当前处理流程使用
|
||
metadata := cookierefresh.MetadataWithoutSnapshot(latestPlatformData.MetadataJSON)
|
||
if // err 保存err,供当前处理流程使用
|
||
err := a.store.Cookies.UpdateRenewalCookie(ctx, cookieID, detail.UpdatedCookies, metadata, time.Now().Unix()); err != nil {
|
||
return nil, fmt.Errorf("保存订单详情响应 Cookie: %w", err)
|
||
}
|
||
a.wakeCredentialBlockedAutomation(ctx, cookieID)
|
||
}
|
||
// 外部调用产生的凭证结果已处理完成,释放短暂提交临界区。
|
||
credentialUnlock()
|
||
credentialLocked = false
|
||
return &automation.OrderDetail{
|
||
Quantity: detail.Quantity, SpecName: detail.SpecName, SpecValue: detail.SpecValue,
|
||
Amount: detail.Amount, OrderStatus: detail.OrderStatus,
|
||
}, nil
|
||
}
|
||
|
||
// wakeCredentialBlockedAutomation 负责wakeCredentialBlocked自动化相关处理。
|
||
func (a *Adapter) wakeCredentialBlockedAutomation(ctx context.Context, cookieID string) {
|
||
if a.credentialWake == nil {
|
||
return
|
||
}
|
||
if // err 保存err,供当前处理流程使用
|
||
err := a.credentialWake.WakeCredentialBlocked(ctx, cookieID); err != nil {
|
||
a.logger.Warn("Cookie 更新后唤醒自动化任务失败", "account", cookieID, "err", err)
|
||
}
|
||
}
|
||
|
||
// hasCompleteCookieSnapshot 负责hasComplete登录凭证Snapshot相关处理。
|
||
func hasCompleteCookieSnapshot(metadata string) bool {
|
||
// ok 保存ok,供当前处理流程使用
|
||
_, ok := cookierefresh.SnapshotFromMetadataOK(metadata)
|
||
return ok
|
||
}
|
||
|
||
// localOrderDetail 命中本地完整订单时直接返回,避免不必要的 MTOP 请求。
|
||
func (a *Adapter) localOrderDetail(ctx context.Context, orderID string) (*automation.OrderDetail, bool) {
|
||
// order、err 保存order、err,供当前处理流程使用
|
||
order, err := a.store.Orders.Get(ctx, orderID)
|
||
if err != nil || order == nil {
|
||
return nil, false
|
||
}
|
||
if order.Amount == "" || order.Quantity == "" || order.SpecName == "" || order.SpecValue == "" {
|
||
return nil, false
|
||
}
|
||
return &automation.OrderDetail{
|
||
Quantity: order.Quantity, SpecName: order.SpecName, SpecValue: order.SpecValue,
|
||
Amount: order.Amount, OrderStatus: order.OrderStatus,
|
||
}, true
|
||
}
|
||
|
||
// OnPasswordLoginRefresh 是 engine 的历史回调名。Go 客户端只执行协议级
|
||
// auto-login 续期;失败后要求重新扫码,不得启动 Chromium 密码登录或页面校验。
|
||
// OnPasswordLoginRefresh 负责On密码登录Refresh相关处理。
|
||
func (a *Adapter) OnPasswordLoginRefresh(ctx context.Context, cookieID string) bool {
|
||
// cooldown 保存cooldown,供当前处理流程使用
|
||
cooldown := a.cooldown
|
||
if cooldown == nil {
|
||
cooldown = renewal.GlobalCooldown
|
||
}
|
||
if // ok、remain、reason 保存ok、remain、reason,供当前处理流程使用
|
||
ok, remain, reason := cooldown.PasswordLoginAllowed(cookieID, engine.PasswordLoginMinGap); !ok {
|
||
a.logger.Warn("协议续期冷却中", "account", cookieID, "remain", remain.Round(time.Second))
|
||
a.recordPasswordLogin(ctx, cookieID, 0, "skipped_cooldown", reason, fmt.Sprintf("协议续期冷却中,还需等待 %s", remain.Round(time.Second)))
|
||
return false
|
||
}
|
||
// primary 标识当前调用是否负责执行协议续期;后到调用方只等待其结果。
|
||
if primary := a.beginPasswordRenewalResult(cookieID); !primary {
|
||
a.logger.Warn("协议续期已在处理中,等待当前结果", "account", cookieID)
|
||
return a.waitPasswordRenewalResult(ctx, cookieID)
|
||
}
|
||
// sharedRenewed 保存首次调用的最终恢复结果,并在所有返回路径唤醒等待者。
|
||
sharedRenewed := false
|
||
defer func() { a.finishPasswordRenewalResult(cookieID, sharedRenewed) }()
|
||
// platformData 保存成功或失败时用于审计的账号平台运行数据;明文凭证只在续期工作闭包内短暂使用。
|
||
var platformData db.CookiePlatformRuntimeData
|
||
// lookupFailed 标识是否在调用协议续期前读取账号数据失败,以保持历史审计原因。
|
||
lookupFailed := false
|
||
// accepted、renewed、renewErr 保存协调器登记结果、续期结果和底层错误。
|
||
accepted, renewed, renewErr := a.passwordCoordinator.Run(ctx, cookieID, func(runCtx context.Context) (bool, error) {
|
||
// loaded、loadErr 保存账号平台运行数据及其读取错误。
|
||
loaded, loadErr := a.store.Cookies.GetCookiePlatformRuntimeData(runCtx, cookieID)
|
||
if loadErr != nil {
|
||
lookupFailed = true
|
||
return false, loadErr
|
||
}
|
||
platformData = loaded
|
||
return a.tryProtocolCredentialRenew(runCtx, &platformData)
|
||
})
|
||
sharedRenewed = renewed
|
||
if !accepted {
|
||
if errors.Is(renewErr, accountapp.ErrCredentialRefreshInFlight) {
|
||
a.logger.Warn("协议续期已在处理中", "account", cookieID)
|
||
return false
|
||
}
|
||
a.logger.Warn("协议续期协调器不可用", "account", cookieID, "err", renewErr)
|
||
return false
|
||
}
|
||
if lookupFailed {
|
||
a.logger.Warn("协议续期失败:读取账号详情失败", "account", cookieID, "err", renewErr)
|
||
// message 保存账号详情读取失败时写入登录审计的脱敏说明。
|
||
message := "读取账号详情失败"
|
||
if renewErr != nil {
|
||
message = renewErr.Error()
|
||
}
|
||
a.recordPasswordLogin(ctx, cookieID, 0, "failed", "account_lookup_failed", message)
|
||
return false
|
||
}
|
||
if renewed {
|
||
a.wakeCredentialBlockedAutomation(ctx, cookieID)
|
||
a.recordPasswordLogin(ctx, cookieID, platformData.UserID, "success", "", "Go 协议续期成功")
|
||
return true
|
||
}
|
||
// message 保存消息,供当前处理流程使用
|
||
message := "Go 协议续期未恢复登录凭证,请重新扫码登录"
|
||
if renewErr != nil {
|
||
message += ":" + renewErr.Error()
|
||
}
|
||
a.logger.Warn("协议续期未恢复账号", "account", cookieID, "err", renewErr)
|
||
a.OnAccountEvent(ctx, cookieID, engine.EventAccountOffline, engine.AlertLevelWarn, "账号需要重新扫码", message)
|
||
a.recordPasswordLogin(ctx, cookieID, platformData.UserID, "failed", "qr_login_required", message)
|
||
return false
|
||
}
|
||
|
||
// RecoverExpiredCredential 供自动化外部动作在平台明确拒绝旧 Session 后恢复凭证。
|
||
func (a *Adapter) RecoverExpiredCredential(ctx context.Context, cookieID string) bool {
|
||
return a.OnPasswordLoginRefresh(ctx, cookieID)
|
||
}
|
||
|
||
// OnCredentialUpdated 接收账号运行时保存的新凭证并唤醒失败任务。
|
||
func (a *Adapter) OnCredentialUpdated(ctx context.Context, cookieID string) {
|
||
a.wakeCredentialBlockedAutomation(ctx, cookieID)
|
||
}
|
||
|
||
// OnTransportReady 在 WS 注册完成后立即唤醒发送前明确未执行的任务。
|
||
func (a *Adapter) OnTransportReady(ctx context.Context, cookieID string) {
|
||
a.wakeCredentialBlockedAutomation(ctx, cookieID)
|
||
}
|
||
|
||
// beginPasswordLogin 兼容旧测试对账号恢复登记的访问;生产路径统一使用 account.CredentialRefreshCoordinator.Run。
|
||
func (a *Adapter) beginPasswordLogin(cookieID string) bool {
|
||
if a.passwordCoordinator == nil {
|
||
return false
|
||
}
|
||
return a.passwordCoordinator.TryBegin(cookieID)
|
||
}
|
||
|
||
// finishPasswordLogin 兼容旧测试对账号恢复收尾的访问;正式调用必须由协调器 Run 自动释放状态。
|
||
func (a *Adapter) finishPasswordLogin(cookieID string) {
|
||
if a.passwordCoordinator != nil {
|
||
a.passwordCoordinator.Finish(cookieID)
|
||
}
|
||
}
|
||
|
||
// beginPasswordRenewalResult 为账号首次协议续期创建共享结果状态;返回 false 表示已有调用负责续期。
|
||
func (a *Adapter) beginPasswordRenewalResult(cookieID string) bool {
|
||
// passwordResultMu 只保护账号到共享结果状态的映射,绝不跨越慢速外部 I/O。
|
||
a.passwordResultMu.Lock()
|
||
defer a.passwordResultMu.Unlock()
|
||
if a.passwordResults == nil {
|
||
a.passwordResults = make(map[string]*passwordRenewalResult)
|
||
}
|
||
// exists 表示该账号是否已有首个调用负责协议续期,存在时当前调用改为等待共享结果。
|
||
if _, exists := a.passwordResults[cookieID]; exists {
|
||
return false
|
||
}
|
||
a.passwordResults[cookieID] = &passwordRenewalResult{done: make(chan struct{})}
|
||
return true
|
||
}
|
||
|
||
// finishPasswordRenewalResult 写入首次协议续期结果并唤醒同账号等待者;重复收尾保持幂等。
|
||
func (a *Adapter) finishPasswordRenewalResult(cookieID string, renewed bool) {
|
||
// passwordResultMu 只保护结果写入、映射删除和 channel 关闭的唯一性。
|
||
a.passwordResultMu.Lock()
|
||
defer a.passwordResultMu.Unlock()
|
||
// result 保存当前账号等待者共享的续期结果状态,负责承载最终结果并关闭通知通道。
|
||
result := a.passwordResults[cookieID]
|
||
if result == nil {
|
||
return
|
||
}
|
||
result.renewed = renewed
|
||
delete(a.passwordResults, cookieID)
|
||
close(result.done)
|
||
}
|
||
|
||
// waitPasswordRenewalResult 等待首次协议续期完成或调用上下文取消,返回其最终恢复结果。
|
||
func (a *Adapter) waitPasswordRenewalResult(ctx context.Context, cookieID string) bool {
|
||
// passwordResultMu 只保护共享状态快照读取,等待发生在释放锁之后。
|
||
a.passwordResultMu.Lock()
|
||
// result 保存锁内取得的共享续期状态快照;释放锁后只等待其完成信号。
|
||
result := a.passwordResults[cookieID]
|
||
a.passwordResultMu.Unlock()
|
||
if result == nil {
|
||
return false
|
||
}
|
||
select {
|
||
case <-result.done:
|
||
return result.renewed
|
||
case <-ctx.Done():
|
||
return false
|
||
}
|
||
}
|
||
|
||
// recordPasswordLogin 负责record密码登录相关处理。
|
||
func (a *Adapter) recordPasswordLogin(ctx context.Context, cookieID string, userID int64, status, failureReason, message string) {
|
||
if a.store == nil || a.store.LoginLogs == nil {
|
||
return
|
||
}
|
||
if // err 保存err,供当前处理流程使用
|
||
err := a.store.LoginLogs.Add(ctx, db.AccountLoginLog{
|
||
CookieID: cookieID,
|
||
UserID: userID,
|
||
Method: "protocol",
|
||
Status: status,
|
||
Message: truncateMessage(message, 500),
|
||
TriggerReason: "令牌/Session过期",
|
||
FailureReason: failureReason,
|
||
ErrorMessage: truncateMessage(message, 500),
|
||
AccountIdentifier: cookieID,
|
||
DurationMS: 0,
|
||
CreatedAt: time.Now().Unix(),
|
||
}); err != nil {
|
||
a.logger.Warn("记录协议续期日志失败", "account", cookieID, "err", err)
|
||
}
|
||
}
|
||
|
||
// truncateMessage 负责truncate消息相关处理。
|
||
func truncateMessage(s string, n int) string {
|
||
if n <= 0 || len(s) <= n {
|
||
return s
|
||
}
|
||
return s[:n]
|
||
}
|
||
|
||
// tryProtocolCredentialRenew 负责tryProtocolCredentialRenew相关处理。
|
||
func (a *Adapter) tryProtocolCredentialRenew(ctx context.Context, d *db.CookiePlatformRuntimeData) (bool, error) {
|
||
if d == nil {
|
||
return false, nil
|
||
}
|
||
// current 保存current,供当前处理流程使用
|
||
current := d.Value
|
||
// api 保存api,供当前处理流程使用
|
||
api := a.renewSvc
|
||
// save 保存save,供当前处理流程使用
|
||
save := func(cookieStr string, setCookies []string, completeSnapshot []cookierefresh.BrowserCookie) error {
|
||
if cookieStr == current && len(setCookies) == 0 && completeSnapshot == nil {
|
||
return nil
|
||
}
|
||
// metadata 保存metadata,供当前处理流程使用
|
||
metadata := cookierefresh.MetadataWithoutSnapshot(d.MetadataJSON)
|
||
if completeSnapshot != nil {
|
||
// API 在完整 Jar 基础上得到的快照是权威结果,包含
|
||
// 服务端删除和新的 Domain/Path/expiry 属性。
|
||
metadata = cookierefresh.MetadataWithSnapshot(d.MetadataJSON, completeSnapshot)
|
||
}
|
||
if // err 保存err,供当前处理流程使用
|
||
err := a.store.Cookies.UpdateRenewalCookie(ctx, d.ID, cookieStr, metadata, time.Now().Unix()); err != nil {
|
||
a.logger.Warn("轻量续期保存 Cookie 失败", "account", d.ID, "err", err)
|
||
return err
|
||
}
|
||
// valueChanged 保存值Changed,供当前处理流程使用
|
||
valueChanged := cookieStr != current
|
||
current = cookieStr
|
||
d.Value = cookieStr
|
||
d.MetadataJSON = metadata
|
||
if valueChanged && a.store.Tokens != nil {
|
||
if // err 保存err,供当前处理流程使用
|
||
err := a.store.Tokens.Clear(ctx, d.ID); err != nil {
|
||
// Token 仅是运行期缓存;Cookie 已原子提交后不能再把整次
|
||
// 续期报告成失败,否则调用方可能用旧凭证重试并覆盖新 Jar。
|
||
a.logger.Warn("轻量续期清理旧 Token 缓存失败", "account", d.ID, "err", err)
|
||
}
|
||
}
|
||
return nil
|
||
}
|
||
// 官网始终先由 auto-login plugin 按 havana_lgc_exp/cookie3_bak_exp
|
||
// 决定是否调用 silentHasLogin。Go 客户端复刻该 HTTP 协议,不加载页面。
|
||
// runCtx、cancel 保存运行Ctx、cancel,供当前处理流程使用
|
||
runCtx, cancel := context.WithTimeout(ctx, 90*time.Second)
|
||
defer cancel()
|
||
// res、err 保存res、err,供当前处理流程使用
|
||
res, err := api.RenewAfterSessionExpired(runCtx, current, cookierefresh.SnapshotFromMetadata(d.MetadataJSON))
|
||
if res != nil {
|
||
// completeSnapshot 保存completeSnapshot,供当前处理流程使用
|
||
var completeSnapshot []cookierefresh.BrowserCookie
|
||
if res.CookieSnapshotComplete {
|
||
completeSnapshot = res.CookieSnapshot
|
||
if completeSnapshot == nil {
|
||
completeSnapshot = []cookierefresh.BrowserCookie{}
|
||
}
|
||
}
|
||
if // saveErr 保存saveErr,供当前处理流程使用
|
||
saveErr := save(res.NewCookies, res.SetCookies, completeSnapshot); saveErr != nil {
|
||
return false, saveErr
|
||
}
|
||
if res.HasPending() {
|
||
// 恢复路径不能把“底层请求仍在进行”提前记为成功,否则上层会
|
||
// 重置失败计数并继续使用未确认的旧凭证。这里等待最终响应;定时
|
||
// 调度仍使用异步 watcher,不阻塞健康账号。
|
||
// waitCtx、waitCancel 保存waitCtx、wait取消,供当前处理流程使用
|
||
waitCtx, waitCancel := context.WithTimeout(ctx, 35*time.Second)
|
||
// late、waitErr 保存late、waitErr,供当前处理流程使用
|
||
late, waitErr := res.AwaitPending(waitCtx)
|
||
waitCancel()
|
||
if late == nil {
|
||
if waitErr != nil {
|
||
return false, waitErr
|
||
}
|
||
return false, errors.New("协议续期底层响应未返回结果")
|
||
}
|
||
// lateSnapshot 保存lateSnapshot,供当前处理流程使用
|
||
var lateSnapshot []cookierefresh.BrowserCookie
|
||
if late.CookieSnapshotComplete {
|
||
lateSnapshot = late.CookieSnapshot
|
||
if lateSnapshot == nil {
|
||
lateSnapshot = []cookierefresh.BrowserCookie{}
|
||
}
|
||
}
|
||
if // saveErr 保存saveErr,供当前处理流程使用
|
||
saveErr := save(late.NewCookies, late.SetCookies, lateSnapshot); saveErr != nil {
|
||
return false, saveErr
|
||
}
|
||
if waitErr != nil {
|
||
return false, waitErr
|
||
}
|
||
if late.Success {
|
||
a.logger.Info("Go 协议续期迟到响应成功", "account", d.ID)
|
||
return true, nil
|
||
}
|
||
// message 保存消息,供当前处理流程使用
|
||
message := strings.TrimSpace(late.Message)
|
||
if message == "" {
|
||
message = "协议续期未通过"
|
||
}
|
||
return false, errors.New(message)
|
||
}
|
||
if err == nil && res.Success {
|
||
a.logger.Info("Go 协议续期成功", "account", d.ID)
|
||
return true, nil
|
||
}
|
||
if err == nil {
|
||
// message 保存平台续期失败原因;空原因使用稳定的非敏感默认提示。
|
||
message := strings.TrimSpace(res.Message)
|
||
if message == "" {
|
||
message = "协议续期未通过"
|
||
}
|
||
return false, errors.New(message)
|
||
}
|
||
}
|
||
if err == nil {
|
||
err = errors.New("协议续期未返回结果")
|
||
}
|
||
return false, err
|
||
}
|
||
|
||
// 编译期保证 *Adapter 同时实现 engine.Handler 与 automation.OrderDetailFetcher。
|
||
var (
|
||
_ engine.Handler = (*Adapter)(nil)
|
||
_ automation.OrderDetailFetcher = (*Adapter)(nil)
|
||
)
|