将扫码登录、人脸验证、Cookie 续期、MTOP Token 与 WebSocket 凭证更新按官网 JS 流程改为 Go 协议实现,Chromium 仅保留本机指纹读取和滑块验证用途。 补齐权威 Cookie Jar、重定向 Set-Cookie、滑块后快照、静默续期异步响应和并发写回处理,并完善相关回归测试。
496 lines
17 KiB
Go
496 lines
17 KiB
Go
package renewal
|
|
|
|
import (
|
|
"context"
|
|
crand "crypto/rand"
|
|
"encoding/hex"
|
|
"errors"
|
|
"fmt"
|
|
"log/slog"
|
|
"strconv"
|
|
"strings"
|
|
"time"
|
|
|
|
"xianyu-go/internal/db"
|
|
"xianyu-go/internal/xianyu/cookierefresh"
|
|
"xianyu-go/internal/xianyu/mtop"
|
|
apirenew "xianyu-go/internal/xianyu/renew"
|
|
)
|
|
|
|
const (
|
|
loginRenewInterval = 600 * time.Second
|
|
cookiesRefreshInterval = 600 * time.Second
|
|
apiCookieRenewInterval = 3600 * time.Second
|
|
accountRequestInterval = time.Second
|
|
sessionExpiredCooldown = 300 * time.Second
|
|
passwordLoginCooldown = 300 * time.Second
|
|
passwordErrorCooldown = 5 * time.Hour
|
|
)
|
|
|
|
const (
|
|
loginRenewEnabledSetting = "renewal.login_renew.enabled"
|
|
loginRenewIntervalSetting = "renewal.login_renew.interval_seconds"
|
|
apiCookieRenewEnabledSetting = "renewal.api_cookie_renew.enabled"
|
|
apiCookieRenewIntervalSetting = "renewal.api_cookie_renew.interval_seconds"
|
|
cookiesRefreshEnabledSetting = "renewal.cookies_refresh.enabled"
|
|
cookiesRefreshIntervalSetting = "renewal.cookies_refresh.interval_seconds"
|
|
)
|
|
|
|
type AccountStarter interface {
|
|
Start(ctx context.Context, cookieID, cookieValue string) error
|
|
}
|
|
|
|
type accountRestarter interface {
|
|
Restart(ctx context.Context, cookieID string) error
|
|
}
|
|
|
|
type PasswordRefresher interface {
|
|
OnPasswordLoginRefresh(ctx context.Context, cookieID string) bool
|
|
}
|
|
|
|
type Scheduler struct {
|
|
store *db.Store
|
|
starter AccountStarter
|
|
refresher PasswordRefresher
|
|
logger *slog.Logger
|
|
mtop *mtop.ClientImpl
|
|
api apirenew.Service
|
|
cooldown *CooldownManager
|
|
}
|
|
|
|
func NewScheduler(store *db.Store, starter AccountStarter, refresher PasswordRefresher, logger *slog.Logger) *Scheduler {
|
|
if logger == nil {
|
|
logger = slog.Default()
|
|
}
|
|
return &Scheduler{
|
|
store: store,
|
|
starter: starter,
|
|
refresher: refresher,
|
|
logger: logger,
|
|
mtop: mtop.NewClient(),
|
|
api: apirenew.Service{},
|
|
cooldown: GlobalCooldown,
|
|
}
|
|
}
|
|
|
|
func (s *Scheduler) Run(ctx context.Context) {
|
|
if s == nil || s.store == nil {
|
|
return
|
|
}
|
|
go s.runFixed(ctx, "login_renew", loginRenewEnabledSetting, loginRenewIntervalSetting, false, loginRenewInterval, s.executeLoginRenew)
|
|
// 官网静默插件只在页面/账号运行时启动时执行,不做每小时轮询。保留
|
|
// 调度器入口供显式兼容配置使用,但默认关闭。
|
|
go s.runFixed(ctx, "api_cookie_renew", apiCookieRenewEnabledSetting, apiCookieRenewIntervalSetting, false, apiCookieRenewInterval, s.executeAPICookieRenew)
|
|
// 兼容原 cookies_refresh 配置名,但执行同一套 Go HTTP auto-login
|
|
// 协议;Chromium 不参与普通 Cookie 续期。
|
|
go s.runFixed(ctx, "cookies_refresh", cookiesRefreshEnabledSetting, cookiesRefreshIntervalSetting, false, cookiesRefreshInterval, s.executeAPICookieRenew)
|
|
}
|
|
|
|
func (s *Scheduler) runFixed(ctx context.Context, name, settingKey, intervalKey string, defaultEnabled bool, defaultInterval time.Duration, fn func(context.Context)) {
|
|
if s.settingEnabled(ctx, settingKey, defaultEnabled) {
|
|
fn(ctx)
|
|
}
|
|
for {
|
|
interval := s.settingInterval(ctx, intervalKey, defaultInterval)
|
|
timer := time.NewTimer(interval)
|
|
select {
|
|
case <-ctx.Done():
|
|
timer.Stop()
|
|
return
|
|
case <-timer.C:
|
|
}
|
|
if !s.settingEnabled(ctx, settingKey, defaultEnabled) {
|
|
continue
|
|
}
|
|
s.logger.Info("执行续期任务", "task", name)
|
|
fn(ctx)
|
|
}
|
|
}
|
|
|
|
func (s *Scheduler) executeLoginRenew(ctx context.Context) {
|
|
s.cleanupExpiredLogs(ctx)
|
|
batchID := newBatchID()
|
|
accounts, err := s.store.Cookies.ActiveRenewalAccounts(ctx)
|
|
if err != nil {
|
|
s.logger.Warn("login_renew 加载账号失败", "err", err)
|
|
return
|
|
}
|
|
for i, account := range accounts {
|
|
if s.isSessionCooled(account.ID) {
|
|
s.logger.Info("login_renew session 冷却中,跳过", "account", account.ID)
|
|
continue
|
|
}
|
|
s.loginRenewOne(ctx, batchID, account)
|
|
if i < len(accounts)-1 {
|
|
_ = sleepCtx(ctx, accountRequestInterval)
|
|
}
|
|
}
|
|
}
|
|
|
|
func (s *Scheduler) loginRenewOne(ctx context.Context, batchID string, account db.RenewalAccount) {
|
|
credentialUnlock := s.store.LockAccountCredentials(account.ID)
|
|
defer credentialUnlock()
|
|
started := time.Now()
|
|
latest, err := s.reloadRenewalAccount(ctx, account)
|
|
if err != nil {
|
|
s.addLoginLog(ctx, batchID, account.ID, "failed", "重读账号凭证失败: "+err.Error(), nil, time.Since(started))
|
|
return
|
|
}
|
|
account = latest
|
|
if !account.Enabled {
|
|
return
|
|
}
|
|
runCtx, cancel := context.WithTimeout(ctx, 30*time.Second)
|
|
defer cancel()
|
|
var mtopCtx context.Context
|
|
var cookieSession *mtop.CookieSession
|
|
if snapshot, ok := cookierefresh.SnapshotFromMetadataOK(account.MetadataJSON); ok {
|
|
mtopCtx, cookieSession = mtop.WithCookieSnapshot(runCtx, snapshot)
|
|
} else {
|
|
mtopCtx, cookieSession = mtop.WithFlatCookieSession(runCtx, account.Value)
|
|
}
|
|
res, callErr := s.mtop.CheckLoginStatusContext(mtopCtx, account.Value)
|
|
|
|
// 对齐浏览器在响应头到达时立即应用 Set-Cookie 的时序。权威 session
|
|
// 因此必须在处理请求或解析错误之前持久化,否则下次请求会
|
|
// 从数据库回滚到旧 Jar。
|
|
updated := []string(nil)
|
|
value, snapshot, changed := cookieSession.State()
|
|
// 完整 Jar 即使本轮没有变化也已经权威接管请求;不能因扁平 Cookie
|
|
// 的顺序或尾分号不同回退写入并清掉快照。
|
|
sessionHandled := snapshot != nil
|
|
if changed {
|
|
updated = cookierefresh.ChangedCookieNames(account.Value, value)
|
|
metadata := cookierefresh.MetadataWithoutSnapshot(account.MetadataJSON)
|
|
if snapshot != nil {
|
|
metadata = cookierefresh.MetadataWithSnapshot(account.MetadataJSON, snapshot)
|
|
}
|
|
if persistErr := s.store.Cookies.UpdateRenewalCookie(ctx, account.ID, value, metadata, time.Now().Unix()); persistErr != nil {
|
|
if callErr != nil {
|
|
persistErr = errors.Join(callErr, fmt.Errorf("保存 loginuser.get 响应 Cookie Jar: %w", persistErr))
|
|
}
|
|
s.addLoginLog(ctx, batchID, account.ID, "failed", persistErr.Error(), updated, time.Since(started))
|
|
s.logger.Warn("login_renew 保存响应 Cookie Jar 失败", "account", account.ID, "err", persistErr)
|
|
return
|
|
}
|
|
}
|
|
if callErr != nil {
|
|
s.addLoginLog(ctx, batchID, account.ID, "failed", callErr.Error(), updated, time.Since(started))
|
|
s.logger.Warn("login_renew 失败", "account", account.ID, "err", callErr)
|
|
return
|
|
}
|
|
if res == nil {
|
|
s.addLoginLog(ctx, batchID, account.ID, "failed", "loginuser.get 未返回结果", nil, time.Since(started))
|
|
return
|
|
}
|
|
if !sessionHandled {
|
|
updated = cookierefresh.ChangedCookieNames(account.Value, res.UpdatedCookies)
|
|
if res.UpdatedCookies != "" && res.UpdatedCookies != account.Value {
|
|
// 注入 mock 或没有权威快照的历史账号仍走扁平
|
|
// Cookie 兼容路径。扁平值无法证明旧 snapshot 的
|
|
// Domain/Path/expiry 仍有效,因此必须清除旧快照。
|
|
metadata := cookierefresh.MetadataWithoutSnapshot(account.MetadataJSON)
|
|
if err := s.store.Cookies.UpdateRenewalCookie(ctx, account.ID, res.UpdatedCookies, metadata, time.Now().Unix()); err != nil {
|
|
s.addLoginLog(ctx, batchID, account.ID, "failed", "保存 Cookie 失败: "+err.Error(), updated, time.Since(started))
|
|
return
|
|
}
|
|
}
|
|
}
|
|
s.addLoginLog(ctx, batchID, account.ID, res.Status, res.Message, updated, time.Since(started))
|
|
if res.Status == mtop.LoginStatusSessionExpired || res.Status == mtop.LoginStatusTokenEmpty {
|
|
s.markSessionExpired(account.ID)
|
|
}
|
|
}
|
|
|
|
func (s *Scheduler) executeAPICookieRenew(ctx context.Context) {
|
|
s.cleanupExpiredLogs(ctx)
|
|
batchID := newBatchID()
|
|
accounts, err := s.store.Cookies.ActiveRenewalAccounts(ctx)
|
|
if err != nil {
|
|
s.logger.Warn("api_cookie_renew 加载账号失败", "err", err)
|
|
return
|
|
}
|
|
for i, account := range accounts {
|
|
s.apiCookieRenewOne(ctx, batchID, account)
|
|
if i < len(accounts)-1 {
|
|
_ = sleepCtx(ctx, accountRequestInterval)
|
|
}
|
|
}
|
|
}
|
|
|
|
func (s *Scheduler) apiCookieRenewOne(ctx context.Context, batchID string, account db.RenewalAccount) {
|
|
credentialUnlock := s.store.LockAccountCredentials(account.ID)
|
|
credentialLocked := true
|
|
defer func() {
|
|
if credentialLocked {
|
|
credentialUnlock()
|
|
}
|
|
}()
|
|
started := time.Now()
|
|
latest, err := s.reloadRenewalAccount(ctx, account)
|
|
if err != nil {
|
|
s.addAPILog(ctx, db.RenewalLog{BatchID: batchID, CookieID: account.ID, Status: "failed", ErrorMessage: "重读账号凭证失败: " + err.Error(), RenewMethod: "auto_login_plugin"})
|
|
return
|
|
}
|
|
account = latest
|
|
if !account.Enabled {
|
|
return
|
|
}
|
|
res, callErr := s.renewAPI(ctx, account.Value, cookierefresh.SnapshotFromMetadata(account.MetadataJSON))
|
|
if res == nil {
|
|
message := "接口续期未返回结果"
|
|
if callErr != nil {
|
|
message = callErr.Error()
|
|
}
|
|
s.addAPILog(ctx, db.RenewalLog{BatchID: batchID, CookieID: account.ID, Status: "failed", ErrorMessage: message, RenewMethod: "auto_login_plugin", DurationMS: time.Since(started).Milliseconds()})
|
|
return
|
|
}
|
|
if res.HasPending() {
|
|
s.watchPendingAPIRenew(account.ID, res)
|
|
}
|
|
stepDetails := make([]string, 0, len(res.StepDetails)+1)
|
|
for _, step := range res.StepDetails {
|
|
stepDetails = append(stepDetails, fmt.Sprintf("%s: http=%d business_ok=%v set_cookie=%d", step.Name, step.HTTPStatus, step.BusinessOK, step.SetCookieCount))
|
|
}
|
|
stepDetails = append(stepDetails, fmt.Sprintf("result: success=%v skipped=%v reason=%s", res.Success, res.Skipped, res.SkipReason))
|
|
updated := cookierefresh.ChangedCookieNames(account.Value, res.NewCookies)
|
|
if res.CookieSnapshotComplete || len(res.SetCookies) > 0 || res.NewCookies != account.Value {
|
|
metadata := cookierefresh.MetadataWithoutSnapshot(account.MetadataJSON)
|
|
if res.CookieSnapshotComplete {
|
|
metadata = cookierefresh.MetadataWithSnapshot(account.MetadataJSON, res.CookieSnapshot)
|
|
}
|
|
if !s.saveRenewedCookies(ctx, account.ID, res.NewCookies, metadata) {
|
|
s.addAPILog(ctx, db.RenewalLog{BatchID: batchID, CookieID: account.ID, Status: "failed", ErrorMessage: "保存 Cookie 失败", UpdatedCookieNames: updated, StepDetails: strings.Join(stepDetails, " | "), RenewMethod: res.RenewMethod, DurationMS: time.Since(started).Milliseconds(), RequestCount: res.RequestCount})
|
|
return
|
|
}
|
|
}
|
|
if callErr != nil {
|
|
s.addAPILog(ctx, db.RenewalLog{
|
|
BatchID: batchID, CookieID: account.ID, Status: "failed", ErrorMessage: callErr.Error(),
|
|
UpdatedCookieNames: updated, StepDetails: strings.Join(stepDetails, " | "), RenewMethod: res.RenewMethod,
|
|
DurationMS: time.Since(started).Milliseconds(), RequestCount: res.RequestCount,
|
|
})
|
|
s.logger.Warn("api_cookie_renew 失败,已保存响应头 Cookie", "account", account.ID, "err", callErr)
|
|
return
|
|
}
|
|
if res.Success && account.Enabled {
|
|
credentialUnlock()
|
|
credentialLocked = false
|
|
if restarter, ok := s.starter.(accountRestarter); ok {
|
|
if err := restarter.Restart(ctx, account.ID); err != nil {
|
|
s.addAPILog(ctx, db.RenewalLog{BatchID: batchID, CookieID: account.ID, Status: "failed", ErrorMessage: "重建消息连接失败: " + err.Error(), UpdatedCookieNames: updated, StepDetails: strings.Join(stepDetails, " | "), RenewMethod: res.RenewMethod, DurationMS: time.Since(started).Milliseconds(), RequestCount: res.RequestCount})
|
|
return
|
|
}
|
|
}
|
|
}
|
|
status := "failed"
|
|
if res.Skipped {
|
|
status = "skipped"
|
|
} else if res.Success && len(updated) > 0 {
|
|
status = "cookie_updated"
|
|
} else if res.Success {
|
|
status = "success"
|
|
}
|
|
s.addAPILog(ctx, db.RenewalLog{
|
|
BatchID: batchID,
|
|
CookieID: account.ID,
|
|
Status: status,
|
|
Message: res.Message,
|
|
UpdatedCookieNames: updated,
|
|
ResponseContent: res.ResponseText,
|
|
StepDetails: strings.Join(stepDetails, " | "),
|
|
RenewMethod: res.RenewMethod,
|
|
DurationMS: time.Since(started).Milliseconds(),
|
|
RequestCount: res.RequestCount,
|
|
})
|
|
}
|
|
|
|
func (s *Scheduler) renewAPI(ctx context.Context, cookieStr string, snapshot []cookierefresh.BrowserCookie) (*apirenew.Result, error) {
|
|
runCtx, cancel := context.WithTimeout(ctx, 90*time.Second)
|
|
defer cancel()
|
|
return s.api.RenewAPIFirst(runCtx, cookieStr, snapshot)
|
|
}
|
|
|
|
func (s *Scheduler) watchPendingAPIRenew(cookieID string, result *apirenew.Result) {
|
|
if result == nil || !result.HasPending() || s.store == nil || s.store.Cookies == nil {
|
|
return
|
|
}
|
|
go func() {
|
|
ctx, cancel := context.WithTimeout(context.Background(), 35*time.Second)
|
|
defer cancel()
|
|
late, waitErr := result.AwaitPending(ctx)
|
|
if late == nil {
|
|
if waitErr != nil {
|
|
s.logger.Warn("等待定时静默续期底层响应失败", "account", cookieID, "err", waitErr)
|
|
}
|
|
return
|
|
}
|
|
unlock := s.store.LockAccountCredentials(cookieID)
|
|
defer unlock()
|
|
detail, getErr := s.store.Cookies.GetDetails(ctx, cookieID)
|
|
if getErr != nil || detail == nil {
|
|
if getErr == nil {
|
|
getErr = db.ErrNotFound
|
|
}
|
|
s.logger.Warn("保存定时静默续期迟到 Cookie 前读取账号失败", "account", cookieID, "err", getErr)
|
|
return
|
|
}
|
|
newCookies, metadata, changed := apirenew.RebaseResponseCookies(detail.Value, detail.MetadataJSON, late)
|
|
if changed {
|
|
if saveErr := s.store.Cookies.UpdateRenewalCookie(ctx, cookieID, newCookies, metadata, time.Now().Unix()); saveErr != nil {
|
|
s.logger.Warn("保存定时静默续期迟到 Cookie 失败", "account", cookieID, "err", saveErr)
|
|
return
|
|
}
|
|
if s.store.Tokens != nil {
|
|
_ = s.store.Tokens.Clear(ctx, cookieID)
|
|
}
|
|
s.logger.Info("已异步接收定时静默续期迟到 Cookie", "account", cookieID)
|
|
}
|
|
if waitErr != nil {
|
|
s.logger.Warn("定时静默续期底层响应失败,已保存响应 Cookie", "account", cookieID, "err", waitErr)
|
|
}
|
|
}()
|
|
}
|
|
|
|
func (s *Scheduler) reloadRenewalAccount(ctx context.Context, account db.RenewalAccount) (db.RenewalAccount, error) {
|
|
detail, err := s.store.Cookies.GetDetails(ctx, account.ID)
|
|
if err != nil || detail == nil {
|
|
if err == nil {
|
|
err = db.ErrNotFound
|
|
}
|
|
return db.RenewalAccount{}, err
|
|
}
|
|
enabled, reason, err := s.store.Cookies.StatusWithReason(ctx, account.ID)
|
|
if err != nil {
|
|
return db.RenewalAccount{}, err
|
|
}
|
|
account.Value = detail.Value
|
|
account.UserID = detail.UserID
|
|
account.Enabled = enabled
|
|
account.DisableReason = reason
|
|
account.Username = detail.Username
|
|
account.Password = detail.Password
|
|
account.ShowBrowser = detail.ShowBrowser
|
|
account.MetadataJSON = detail.MetadataJSON
|
|
account.LastRefreshAt = detail.LastRefreshAt
|
|
return account, nil
|
|
}
|
|
|
|
func (s *Scheduler) saveRenewedCookies(ctx context.Context, cookieID, cookieStr, metadata string) bool {
|
|
if err := s.store.Cookies.UpdateRenewalCookie(ctx, cookieID, cookieStr, metadata, time.Now().Unix()); err != nil {
|
|
s.logger.Warn("保存续期 Cookie 失败", "account", cookieID, "err", err)
|
|
return false
|
|
}
|
|
return true
|
|
}
|
|
|
|
func (s *Scheduler) addLoginLog(ctx context.Context, batchID, cookieID, status, message string, updated []string, duration time.Duration) {
|
|
_ = s.store.Renewal.AddLoginRenewLog(ctx, db.RenewalLog{
|
|
BatchID: batchID,
|
|
CookieID: cookieID,
|
|
Status: status,
|
|
Message: message,
|
|
UpdatedCookieNames: updated,
|
|
RenewMethod: "loginuser.get",
|
|
StepDetails: fmt.Sprintf("loginuser.get status=%s message=%s updated=%d", status, message, len(updated)),
|
|
DurationMS: duration.Milliseconds(),
|
|
RequestCount: 1,
|
|
})
|
|
}
|
|
|
|
func (s *Scheduler) addAPILog(ctx context.Context, log db.RenewalLog) {
|
|
_ = s.store.Renewal.AddAPICookieRenewLog(ctx, log)
|
|
}
|
|
|
|
func (s *Scheduler) cleanupExpiredLogs(ctx context.Context) {
|
|
if s.store == nil || s.store.Renewal == nil {
|
|
return
|
|
}
|
|
days := s.settingInt(ctx, "renewal_log_retention_days", 10)
|
|
if err := s.store.Renewal.CleanupLogs(ctx, days); err != nil {
|
|
s.logger.Warn("清理续期日志失败", "err", err)
|
|
}
|
|
}
|
|
|
|
func (s *Scheduler) markSessionExpired(cookieID string) {
|
|
s.cooldown.MarkSessionExpired(cookieID)
|
|
}
|
|
|
|
func (s *Scheduler) isSessionCooled(cookieID string) bool {
|
|
ok, _ := s.cooldown.IsSessionCooled(cookieID)
|
|
return ok
|
|
}
|
|
|
|
func (s *Scheduler) settingEnabled(ctx context.Context, key string, defaultEnabled bool) bool {
|
|
if s.store == nil || s.store.Settings == nil {
|
|
return defaultEnabled
|
|
}
|
|
value, err := s.store.Settings.Get(ctx, key)
|
|
if err != nil || strings.TrimSpace(value) == "" {
|
|
return defaultEnabled
|
|
}
|
|
switch strings.ToLower(strings.TrimSpace(value)) {
|
|
case "1", "true", "yes", "on", "enabled":
|
|
return true
|
|
case "0", "false", "no", "off", "disabled":
|
|
return false
|
|
default:
|
|
return defaultEnabled
|
|
}
|
|
}
|
|
|
|
func (s *Scheduler) settingInterval(ctx context.Context, key string, defaultInterval time.Duration) time.Duration {
|
|
if s.store == nil || s.store.Settings == nil {
|
|
return defaultInterval
|
|
}
|
|
value, err := s.store.Settings.Get(ctx, key)
|
|
if err != nil || strings.TrimSpace(value) == "" {
|
|
return defaultInterval
|
|
}
|
|
value = strings.TrimSpace(value)
|
|
if seconds, err := strconv.ParseFloat(value, 64); err == nil && seconds > 0 {
|
|
return time.Duration(seconds * float64(time.Second))
|
|
}
|
|
if d, err := time.ParseDuration(value); err == nil && d > 0 {
|
|
return d
|
|
}
|
|
return defaultInterval
|
|
}
|
|
|
|
func (s *Scheduler) settingInt(ctx context.Context, key string, defaultValue int) int {
|
|
if s.store == nil || s.store.Settings == nil {
|
|
return defaultValue
|
|
}
|
|
value, err := s.store.Settings.Get(ctx, key)
|
|
if err != nil || strings.TrimSpace(value) == "" {
|
|
return defaultValue
|
|
}
|
|
n, err := strconv.Atoi(strings.TrimSpace(value))
|
|
if err != nil || n < 0 {
|
|
return defaultValue
|
|
}
|
|
return n
|
|
}
|
|
|
|
func newBatchID() string {
|
|
var b [16]byte
|
|
if _, err := crand.Read(b[:]); err != nil {
|
|
return fmt.Sprintf("%d", time.Now().UnixNano())
|
|
}
|
|
return hex.EncodeToString(b[:])
|
|
}
|
|
|
|
func sleepCtx(ctx context.Context, d time.Duration) error {
|
|
if d <= 0 {
|
|
return nil
|
|
}
|
|
timer := time.NewTimer(d)
|
|
defer timer.Stop()
|
|
select {
|
|
case <-ctx.Done():
|
|
return ctx.Err()
|
|
case <-timer.C:
|
|
return nil
|
|
}
|
|
}
|