Files
dygou_go/internal/seckill/proxy_groups.go
T

2006 lines
58 KiB
Go
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
package seckill
import (
"context"
"errors"
"fmt"
"net/url"
"sort"
"strconv"
"strings"
"sync"
"time"
"douyu_go/internal/client"
"douyu_go/internal/constants"
"douyu_go/internal/logger"
proxyprovider "douyu_go/internal/proxy"
"douyu_go/internal/types"
)
const (
proxyFetchRounds = 3
proxyFetchMaxRounds = 20
proxyFetchRetryWait = 1200 * time.Millisecond
proxyRequestTimeout = 15 * time.Second
proxyValidationTimeout = 3 * time.Second
proxyValidationWorkers = 3
proxyValidateAttempts = 1
proxyStandbyProbeEvery = 20 * time.Second
proxyStandbyFailLimit = 2
proxyImmediateTimeout = 5 * time.Second
proxyScheduledTimeout = 20 * time.Second
proxyRefillTimeout = 20 * time.Second
proxyRiskProbeWorkers = 3
proxyRiskProbeMaxRounds = 20
proxyRiskPreflightReserve = 15 * time.Second
)
type proxySettingsSnapshot struct {
enabled bool
proxyURL string
proxyAPI string
autoFetchOnTimer bool
accountsPerProxy int
sparePercent int
required bool
ready bool
state string
blockedReason string
autoWhitelist bool
whitelistReplace bool
whitelistPassword string
whitelistSignKey string
}
type validatedProxy struct {
url string
client *client.DouyuClient
rtt time.Duration
ip string
region string
source string
expiresAt time.Time
failures int
}
type proxyRuntimeGroup struct {
groupIndex int
accountNames []string
activeURL string
activeIP string
activeRegion string
activeRTT time.Duration
switchCount int
lastError string
consecutiveFailures int
failed bool
}
type preparedProxyPool struct {
accountClients map[string]*client.DouyuClient
activeClients []*client.DouyuClient
groups map[*client.DouyuClient]*proxyRuntimeGroup
standbys []validatedProxy
allClients []*client.DouyuClient
excludedURLs map[string]struct{}
standbyTarget int
expiresAt time.Time
}
type proxySwitchEvent struct {
accountNames []string
fromURL string
toURL string
toIP string
toRegion string
toRTT time.Duration
toSource string
}
// UpdateProxySettings 运行时更新代理配置。
func (s *Scheduler) UpdateProxySettings(cfg types.Config) {
enabled := cfg.ProxyEnabled
proxyURL := cfg.ProxyURL
proxyAPI := cfg.ProxyAPI
autoFetch := cfg.AutoFetchProxyOnSchedule
accountsPerProxy := cfg.AccountsPerProxy
sparePercent := cfg.ProxySparePercent
required := cfg.ProxyRequired
if accountsPerProxy <= 0 {
accountsPerProxy = constants.DefaultAccountsPerProxy
}
if accountsPerProxy > constants.MaxAccountsPerProxy {
accountsPerProxy = constants.MaxAccountsPerProxy
}
if sparePercent < constants.MinProxySparePercent {
sparePercent = constants.DefaultProxySparePercent
}
if sparePercent > constants.MaxProxySparePercent {
sparePercent = constants.MaxProxySparePercent
}
proxyURL = strings.TrimSpace(proxyURL)
proxyAPI = strings.TrimSpace(proxyAPI)
whitelistPassword := strings.TrimSpace(cfg.ProxyWhitelistPassword)
whitelistSignKey := strings.TrimSpace(cfg.ProxyWhitelistSignKey)
s.proxyMu.Lock()
changed := s.proxyEnabled != enabled ||
s.proxyURL != proxyURL ||
s.proxyAPI != proxyAPI ||
s.autoFetchProxyOnSchedule != autoFetch ||
s.accountsPerProxy != accountsPerProxy ||
s.proxySparePercent != sparePercent ||
s.proxyRequired != required ||
s.autoWhitelist != cfg.ProxyAutoWhitelist ||
s.whitelistReplace != cfg.ProxyWhitelistReplace ||
s.whitelistPassword != whitelistPassword ||
s.whitelistSignKey != whitelistSignKey
s.proxyEnabled = enabled
s.proxyURL = proxyURL
s.proxyAPI = proxyAPI
s.autoFetchProxyOnSchedule = autoFetch
s.accountsPerProxy = accountsPerProxy
s.proxySparePercent = sparePercent
s.proxyRequired = required
if changed {
s.proxyReady = false
s.proxyState = "stale"
s.proxyBlockedReason = "代理配置已变更,等待重新准备"
}
s.autoWhitelist = cfg.ProxyAutoWhitelist
s.whitelistReplace = cfg.ProxyWhitelistReplace
s.whitelistPassword = whitelistPassword
s.whitelistSignKey = whitelistSignKey
s.proxyMu.Unlock()
if !changed {
return
}
s.clientMu.Lock()
s.accountClientsDirty = true
s.proxyFallbackActive = false
s.proxyBusinessRiskVerified = false
s.clientMu.Unlock()
}
func (s *Scheduler) proxySettings() proxySettingsSnapshot {
s.proxyMu.RLock()
defer s.proxyMu.RUnlock()
size := s.accountsPerProxy
if size <= 0 {
size = constants.DefaultAccountsPerProxy
}
if size > constants.MaxAccountsPerProxy {
size = constants.MaxAccountsPerProxy
}
sparePercent := s.proxySparePercent
if sparePercent < constants.MinProxySparePercent {
sparePercent = constants.DefaultProxySparePercent
}
if sparePercent > constants.MaxProxySparePercent {
sparePercent = constants.MaxProxySparePercent
}
return proxySettingsSnapshot{
enabled: s.proxyEnabled,
proxyURL: s.proxyURL,
proxyAPI: s.proxyAPI,
autoFetchOnTimer: s.autoFetchProxyOnSchedule,
accountsPerProxy: size,
sparePercent: sparePercent,
required: s.proxyRequired,
ready: s.proxyReady,
state: s.proxyState,
blockedReason: s.proxyBlockedReason,
autoWhitelist: s.autoWhitelist,
whitelistReplace: s.whitelistReplace,
whitelistPassword: s.whitelistPassword,
whitelistSignKey: s.whitelistSignKey,
}
}
func (s *Scheduler) closeAccountClients() {
s.clientMu.Lock()
oldClients := append([]*client.DouyuClient(nil), s.proxyClientPool...)
for _, cli := range s.accountClients {
oldClients = append(oldClients, cli)
}
s.accountClients = make(map[string]*client.DouyuClient)
s.accountClientOrder = nil
s.accountClientsDirty = true
s.accountClientsExpireAt = time.Time{}
s.proxyGroups = make(map[*client.DouyuClient]*proxyRuntimeGroup)
s.proxyGroupHistory = nil
s.accountProxyGroup = make(map[string]int)
s.proxyStandbys = nil
s.proxyClientPool = nil
s.proxyExcludedURLs = make(map[string]struct{})
s.proxyStandbyTarget = 0
s.proxyPoolGeneration++
s.proxyRefillInFlight = false
s.proxyRefillRetryAt = time.Time{}
s.proxyFallbackActive = false
s.proxyBusinessRiskVerified = false
s.clientMu.Unlock()
closeDouyuClientList(oldClients)
}
func (s *Scheduler) replaceAccountClients(pool preparedProxyPool) {
if pool.accountClients == nil {
pool.accountClients = make(map[string]*client.DouyuClient)
}
if pool.groups == nil {
pool.groups = make(map[*client.DouyuClient]*proxyRuntimeGroup)
}
if pool.excludedURLs == nil {
pool.excludedURLs = make(map[string]struct{})
}
s.clientMu.Lock()
oldClients := append([]*client.DouyuClient(nil), s.proxyClientPool...)
for _, cli := range s.accountClients {
oldClients = append(oldClients, cli)
}
s.accountClients = pool.accountClients
s.accountClientOrder = pool.activeClients
s.accountClientsDirty = false
s.accountClientsExpireAt = pool.expiresAt
s.proxyGroups = pool.groups
s.accountProxyGroup = make(map[string]int)
for _, group := range pool.groups {
if group == nil {
continue
}
for _, name := range group.accountNames {
s.accountProxyGroup[name] = group.groupIndex
}
}
s.proxyStandbys = pool.standbys
s.proxyClientPool = pool.allClients
s.proxyExcludedURLs = pool.excludedURLs
s.proxyStandbyTarget = pool.standbyTarget
s.proxyPoolGeneration++
s.proxyRefillInFlight = false
s.proxyRefillRetryAt = time.Time{}
s.proxyFallbackActive = false
s.proxyBusinessRiskVerified = false
s.clientMu.Unlock()
closeDouyuClientList(oldClients)
}
func (s *Scheduler) replaceProxyGroupHistory(groups [][]string, activeCount int, proxies []validatedProxy) {
history := make([]types.ProxyGroupStatus, 0, activeCount)
for index, accounts := range groups {
if index >= len(proxies) {
break
}
proxy := proxies[index]
history = append(history, types.ProxyGroupStatus{
GroupIndex: index + 1,
Role: "active",
State: "active",
AccountCount: len(accounts),
Accounts: append([]string(nil), accounts...),
Proxy: client.MaskProxyURL(proxy.url),
Region: proxy.region,
RTTMs: proxy.rtt.Milliseconds(),
})
}
s.clientMu.Lock()
s.proxyGroupHistory = history
s.clientMu.Unlock()
}
// ProxyStatus returns a structured status snapshot. It never includes proxy
// credentials and is safe to expose to the desktop and remote dashboards.
func (s *Scheduler) ProxyStatus() types.ProxyStatusView {
cfg := s.proxySettings()
s.clientMu.RLock()
history := make([]types.ProxyGroupStatus, len(s.proxyGroupHistory))
copy(history, s.proxyGroupHistory)
for i := range history {
history[i].Accounts = append([]string(nil), history[i].Accounts...)
}
accountGroups := make(map[string]int, len(s.accountProxyGroup))
for name, index := range s.accountProxyGroup {
accountGroups[name] = index
}
names := make([]string, 0, len(s.accountClients))
for name := range s.accountClients {
names = append(names, name)
}
sort.Strings(names)
groupsByIndex := make(map[int]types.ProxyGroupStatus, len(history))
for _, group := range history {
groupsByIndex[group.GroupIndex] = group
}
accounts := make([]types.AccountProxyView, 0, len(names))
directCount := 0
for _, name := range names {
groupIndex := accountGroups[name]
view := types.AccountProxyView{
AccountName: name,
GroupIndex: groupIndex,
GroupLabel: "未分配",
Transport: "unassigned",
}
if cli := s.accountClients[name]; cli == s.directClient {
view.GroupLabel = "直连"
view.Transport = "direct"
directCount++
} else if groupIndex > 0 {
view.GroupLabel = fmt.Sprintf("G%d", groupIndex)
view.Transport = "proxy"
if group, ok := groupsByIndex[groupIndex]; ok {
view.Proxy = group.Proxy
}
}
accounts = append(accounts, view)
}
standbyCount := len(s.proxyStandbys)
s.clientMu.RUnlock()
activeCount := 0
for _, group := range history {
if group.Role == "active" {
activeCount++
}
}
standbyTarget := proxyStandbyCount(activeCount, cfg.sparePercent)
state := cfg.state
if state == "" {
state = "empty"
}
lastError := cfg.blockedReason
switch {
case !cfg.enabled:
state = "disabled"
lastError = ""
case directCount > 0:
state = "direct_fallback"
if lastError == "" {
lastError = "账号已回退直连"
}
case cfg.ready:
state = "ready"
lastError = ""
if reason := proxyDegradedReason(history, standbyCount, standbyTarget); reason != "" {
state = "degraded"
lastError = reason
}
case state == "preparing":
// Keep the explicit preparing state visible while the pool is refreshed.
case lastError != "":
state = "failed"
case len(history) == 0:
state = "empty"
default:
state = "preparing"
}
return types.ProxyStatusView{
Enabled: cfg.enabled,
Required: cfg.required,
State: state,
ActiveCount: activeCount,
StandbyCount: standbyCount,
StandbyTarget: standbyTarget,
DirectCount: directCount,
LastError: lastError,
UpdatedAt: time.Now().In(constants.CST).Format("2006-01-02 15:04:05.000"),
Groups: history,
Accounts: accounts,
}
}
func proxyDegradedReason(groups []types.ProxyGroupStatus, standbyCount, standbyTarget int) string {
failed := 0
for _, group := range groups {
if group.State == "failed" {
failed++
}
}
if failed > 0 {
return fmt.Sprintf("%d 个代理组失败,等待备用代理", failed)
}
if standbyCount < standbyTarget {
return fmt.Sprintf("备用代理不足: %d/%d", standbyCount, standbyTarget)
}
return ""
}
type proxyBusinessRiskTarget struct {
client *client.DouyuClient
groupIndex int
accountNames []string
activeURL string
activeIP string
}
type proxyBusinessRiskProbeResult struct {
target proxyBusinessRiskTarget
accountName string
err error
definitive bool
}
// preflightProxyBusinessRiskUntil 对每个活动代理组执行只读批量限制请求。
// 命中活动火爆时切换备用代理,并对切换后的完整代理组重新检查;任何一组
// 在截止时间前没有拿到明确业务响应,都不允许严格代理模式启动。
func (s *Scheduler) preflightProxyBusinessRiskUntil(accountProducts map[string]string, deadline time.Time) error {
base := s.clientSnapshot()
if base == nil || base.Activity == nil {
return errors.New("代理风控预检缺少活动配置")
}
if base.Activity.IsXiaodian || base.Activity.UseCTN {
logger.LogEvent("代理", "warn",
"当前活动没有批量限制接口,跳过1644400007启动前预检")
s.setProxyBusinessRiskVerified(true)
return nil
}
for round := 1; round <= proxyRiskProbeMaxRounds; round++ {
if !time.Now().Before(deadline) {
return fmt.Errorf("代理风控预检超过截止时间")
}
targets, err := s.proxyBusinessRiskTargets(accountProducts)
if err != nil {
return err
}
if len(targets) == 0 {
return errors.New("代理风控预检没有可检查的活动代理组")
}
results := make(chan proxyBusinessRiskProbeResult, len(targets))
limit := make(chan struct{}, proxyRiskProbeWorkers)
var wg sync.WaitGroup
for _, target := range targets {
wg.Add(1)
go func(target proxyBusinessRiskTarget) {
defer wg.Done()
limit <- struct{}{}
defer func() { <-limit }()
accountName, probeErr, definitive := s.probeProxyBusinessRiskTarget(
target,
accountProducts,
deadline,
)
results <- proxyBusinessRiskProbeResult{
target: target,
accountName: accountName,
err: probeErr,
definitive: definitive,
}
}(target)
}
wg.Wait()
close(results)
var (
hotTargets []proxyBusinessRiskProbeResult
unverified []string
)
for result := range results {
switch {
case result.definitive && isProxyRiskBusinessError(result.err):
hotTargets = append(hotTargets, result)
case result.definitive:
logger.LogEvent("代理", "success",
"代理风控预检通过: 组=%d 账号=%s 出口=%s",
result.target.groupIndex,
result.accountName,
client.MaskProxyURL(result.target.activeURL),
)
default:
unverified = append(unverified, fmt.Sprintf(
"G%d(%s): %v",
result.target.groupIndex,
client.MaskProxyURL(result.target.activeURL),
result.err,
))
}
}
if len(hotTargets) > 0 {
for _, result := range hotTargets {
logger.LogEvent("代理", "warn",
"代理风控预检命中活动火爆/1644400007: 组=%d 账号=%s err=%v",
result.target.groupIndex,
result.accountName,
result.err,
)
s.handleProxyBusinessRisk(result.accountName, result.target.client, result.err)
}
if err := s.refillProxyStandbysForPreflight(base, s.proxySettings(), deadline); err != nil {
s.clientMu.RLock()
remainingFailed := countFailedProxyGroups(s.proxyGroups)
s.clientMu.RUnlock()
if remainingFailed > 0 {
return fmt.Errorf("代理风控预检无法恢复全部代理组: %w", err)
}
logger.LogEvent("代理", "warn",
"代理活动组已切换,但备用代理补提取未完成: %v", err)
}
continue
}
if len(unverified) > 0 {
return fmt.Errorf("代理风控预检未通过: %s", strings.Join(unverified, ";"))
}
s.setProxyBusinessRiskVerified(true)
s.setProxyReadiness(true, "")
if ok, reason := s.ProxyRunGateFor(accountProducts); !ok {
s.setProxyBusinessRiskVerified(false)
s.setProxyReadiness(false, reason)
return fmt.Errorf("代理风控预检后门禁未通过: %s", reason)
}
logger.LogEvent("代理", "success",
"代理风控预检完成: 活动组=%d,轮次=%d,全部未返回活动火爆/1644400007",
len(targets), round)
return nil
}
return fmt.Errorf("代理风控预检切换轮次超过上限%d", proxyRiskProbeMaxRounds)
}
func (s *Scheduler) refillProxyStandbysForPreflight(
base *client.DouyuClient,
cfg proxySettingsSnapshot,
deadline time.Time,
) error {
if strings.TrimSpace(cfg.proxyAPI) == "" {
return errors.New("没有代理提取API,无法补充新的备用代理")
}
timeout := time.Until(deadline)
if timeout <= 0 {
return errors.New("代理风控预检已到截止时间,无法继续补提取")
}
s.clientMu.RLock()
generation := s.proxyPoolGeneration
need := s.proxyStandbyTarget - len(s.proxyStandbys)
if need < 0 {
need = 0
}
need += countFailedProxyGroups(s.proxyGroups)
excludedURLs := copyStringSet(s.proxyExcludedURLs)
s.clientMu.RUnlock()
if need <= 0 {
return nil
}
logger.LogEvent("代理", "info",
"代理风控预检正在补提取备用代理: 需要=%d,截止=%s",
need, deadline.In(constants.CST).Format("15:04:05.000"))
proxies, fetchErr := s.fetchAndValidateProxyPool(base, cfg, need, excludedURLs, timeout)
var extras []*client.DouyuClient
var expiredClients []*client.DouyuClient
var switches []proxySwitchEvent
s.clientMu.Lock()
if generation != s.proxyPoolGeneration {
s.clientMu.Unlock()
closeValidatedProxies(proxies)
return errors.New("代理池已被重建,取消本次补提取")
}
currentNeed := s.proxyStandbyTarget - len(s.proxyStandbys)
if currentNeed < 0 {
currentNeed = 0
}
currentNeed += countFailedProxyGroups(s.proxyGroups)
if len(proxies) > currentNeed {
for _, proxy := range proxies[currentNeed:] {
extras = append(extras, proxy.client)
}
proxies = proxies[:currentNeed]
}
for _, proxy := range proxies {
proxyClient := proxy.client
client.MonitorProxyHealth(proxyClient, func(err error) {
s.recordProxyResult(proxyClient, err)
})
s.proxyStandbys = append(s.proxyStandbys, proxy)
s.proxyClientPool = append(s.proxyClientPool, proxy.client)
s.proxyExcludedURLs[proxy.url] = struct{}{}
}
for {
pendingClient, pendingGroup := findFailedProxyGroup(s.proxyGroups)
if pendingClient == nil {
break
}
event, expired, switched := s.switchGroupToStandbyAvoidIPLocked(
pendingClient,
pendingGroup,
pendingGroup.activeIP,
)
expiredClients = append(expiredClients, expired...)
if !switched {
break
}
switches = append(switches, event)
}
remainingFailed := countFailedProxyGroups(s.proxyGroups)
standbyCount := len(s.proxyStandbys)
standbyTarget := s.proxyStandbyTarget
s.clientMu.Unlock()
closeDouyuClientList(extras)
closeDouyuClientList(expiredClients)
for _, event := range switches {
logProxySwitch("代理风控补提取后切换", event)
}
if len(proxies) > 0 {
logger.LogEvent("代理", "success",
"代理风控预检补提取完成: 新增=%d,备用=%d/%d,剩余失败组=%d",
len(proxies), standbyCount, standbyTarget, remainingFailed)
}
if remainingFailed > 0 {
if fetchErr != nil {
return fmt.Errorf("仍有%d个代理组等待备用代理,补提取失败: %w", remainingFailed, fetchErr)
}
return fmt.Errorf("仍有%d个代理组没有可用的不同出口备用代理", remainingFailed)
}
if fetchErr != nil && len(proxies) == 0 {
logger.LogEvent("代理", "warn", "代理风控预检备用补提取未成功: %v", fetchErr)
}
return nil
}
func (s *Scheduler) proxyBusinessRiskTargets(accountProducts map[string]string) ([]proxyBusinessRiskTarget, error) {
names := selectedAccountNames(accountProducts)
s.clientMu.RLock()
targetsByGroup := make(map[int]proxyBusinessRiskTarget)
var problems []string
for _, name := range names {
proxyClient := s.accountClients[name]
if proxyClient == nil || proxyClient == s.directClient {
problems = append(problems, name+"未分配代理")
continue
}
group := s.proxyGroups[proxyClient]
if group == nil || group.failed || strings.TrimSpace(group.activeURL) == "" {
problems = append(problems, name+"所在代理组不可用")
continue
}
if _, exists := targetsByGroup[group.groupIndex]; exists {
continue
}
targetsByGroup[group.groupIndex] = proxyBusinessRiskTarget{
client: proxyClient,
groupIndex: group.groupIndex,
accountNames: append([]string(nil), group.accountNames...),
activeURL: group.activeURL,
activeIP: group.activeIP,
}
}
s.clientMu.RUnlock()
if len(problems) > 0 {
return nil, fmt.Errorf("代理风控预检分组不完整: %s", strings.Join(problems, ";"))
}
targets := make([]proxyBusinessRiskTarget, 0, len(targetsByGroup))
for _, target := range targetsByGroup {
targets = append(targets, target)
}
sort.Slice(targets, func(i, j int) bool {
return targets[i].groupIndex < targets[j].groupIndex
})
return targets, nil
}
func (s *Scheduler) probeProxyBusinessRiskTarget(
target proxyBusinessRiskTarget,
accountProducts map[string]string,
deadline time.Time,
) (string, error, bool) {
var lastErr error
for _, accountName := range target.accountNames {
if !time.Now().Before(deadline) {
return accountName, errors.New("代理风控预检超过截止时间"), false
}
productID := strings.TrimSpace(accountProducts[accountName])
if productID == "" {
continue
}
cookie, ok := s.AccountCookieByName(accountName)
if !ok || strings.TrimSpace(cookie) == "" {
lastErr = errors.New("账号Cookie为空")
continue
}
_, csrf := s.cachedAuth(types.ExchangeAttempt{
Account: types.Account{Name: accountName},
Product: types.Product{CommodityID: productID},
})
if strings.TrimSpace(csrf) == "" {
token, err := target.client.GenerateCSRF(cookie)
if err != nil {
lastErr = fmt.Errorf("获取CSRF失败: %w", err)
logger.LogEvent("代理", "debug",
"代理风控预检账号不可用: 组=%d 账号=%s err=%v",
target.groupIndex, accountName, err)
continue
}
csrf = token
}
_, err := target.client.BatchExchangeLimit(cookie, productID, &csrf)
if err == nil {
return accountName, nil, true
}
var orderErr *client.OrderError
if errors.As(err, &orderErr) {
return accountName, err, true
}
lastErr = err
logger.LogEvent("代理", "debug",
"代理风控预检账号请求失败: 组=%d 账号=%s err=%v",
target.groupIndex, accountName, err)
}
if lastErr == nil {
lastErr = errors.New("组内没有可检查的账号")
}
return "", fmt.Errorf("组内账号均未取得明确业务响应: %w", lastErr), false
}
func (s *Scheduler) setProxyBusinessRiskVerified(verified bool) {
s.clientMu.Lock()
s.proxyBusinessRiskVerified = verified
s.clientMu.Unlock()
}
func (s *Scheduler) proxyBusinessRiskVerifiedSnapshot() bool {
s.clientMu.RLock()
verified := s.proxyBusinessRiskVerified
s.clientMu.RUnlock()
return verified
}
func closeDouyuClients(clients map[string]*client.DouyuClient) {
items := make([]*client.DouyuClient, 0, len(clients))
for _, cli := range clients {
items = append(items, cli)
}
closeDouyuClientList(items)
}
func closeDouyuClientList(clients []*client.DouyuClient) {
seen := make(map[*client.DouyuClient]struct{}, len(clients))
for _, cli := range clients {
if cli == nil {
continue
}
if _, ok := seen[cli]; ok {
continue
}
seen[cli] = struct{}{}
if cli.HTTPClient != nil {
cli.HTTPClient.CloseIdleConnections()
}
if cli.TimeHTTPClient != nil {
cli.TimeHTTPClient.CloseIdleConnections()
}
}
}
func (s *Scheduler) accountClientsCover(accountNames []string) bool {
if len(accountNames) == 0 {
return true
}
s.clientMu.RLock()
defer s.clientMu.RUnlock()
if len(s.accountClients) == 0 {
return false
}
if s.accountClientsDirty {
return false
}
if !s.accountClientsExpireAt.IsZero() && !time.Now().Before(s.accountClientsExpireAt.Add(-30*time.Second)) {
return false
}
for _, name := range accountNames {
cli := s.accountClients[name]
if cli == nil || cli == s.directClient {
return false
}
group := s.proxyGroups[cli]
if group == nil || group.failed || group.activeURL == "" {
return false
}
}
return true
}
func (s *Scheduler) calibrationClient() *client.DouyuClient {
s.clientMu.RLock()
defer s.clientMu.RUnlock()
if s.proxyFallbackActive && s.directClient != nil {
return s.directClient
}
if len(s.accountClientOrder) > 0 && s.accountClientOrder[0] != nil {
return s.accountClientOrder[0]
}
return s.client
}
func (s *Scheduler) allClients() []*client.DouyuClient {
s.clientMu.RLock()
defer s.clientMu.RUnlock()
out := make([]*client.DouyuClient, 0, len(s.accountClientOrder)+1)
seen := make(map[*client.DouyuClient]struct{}, len(s.accountClientOrder)+1)
if s.client != nil {
seen[s.client] = struct{}{}
out = append(out, s.client)
}
if (s.proxyFallbackActive || s.client == nil) && s.directClient != nil {
if _, ok := seen[s.directClient]; !ok {
seen[s.directClient] = struct{}{}
out = append(out, s.directClient)
}
}
for _, cli := range s.accountClientOrder {
if cli == nil {
continue
}
if _, ok := seen[cli]; ok {
continue
}
seen[cli] = struct{}{}
out = append(out, cli)
}
return out
}
func (s *Scheduler) monitorProxyStandbys(stop <-chan struct{}, generation uint64) {
ticker := time.NewTicker(proxyStandbyProbeEvery)
defer ticker.Stop()
for {
select {
case <-stop:
return
case <-ticker.C:
if !s.probeProxyStandbys(generation) {
return
}
}
}
}
func (s *Scheduler) probeProxyStandbys(generation uint64) bool {
s.clientMu.RLock()
if generation != s.proxyPoolGeneration {
s.clientMu.RUnlock()
return false
}
standbys := append([]validatedProxy(nil), s.proxyStandbys...)
s.clientMu.RUnlock()
if len(standbys) == 0 {
return true
}
type probeResult struct {
proxy validatedProxy
rtt time.Duration
err error
}
results := make(chan probeResult, len(standbys))
limit := make(chan struct{}, proxyValidationWorkers)
var wg sync.WaitGroup
for _, standby := range standbys {
wg.Add(1)
go func(standby validatedProxy) {
defer wg.Done()
limit <- struct{}{}
defer func() { <-limit }()
if standby.client == nil {
results <- probeResult{proxy: standby, err: errors.New("备用代理客户端为空")}
return
}
probe, err := standby.client.ProbeProxyEndpoint()
results <- probeResult{proxy: standby, rtt: probe.Latency, err: err}
}(standby)
}
wg.Wait()
close(results)
var failedClients []*client.DouyuClient
needRefill := false
s.clientMu.Lock()
if generation != s.proxyPoolGeneration {
s.clientMu.Unlock()
return false
}
for result := range results {
index := findStandbyIndexLocked(s.proxyStandbys, result.proxy.client)
if index < 0 {
continue
}
if result.err == nil {
s.proxyStandbys[index].failures = 0
s.proxyStandbys[index].rtt = result.rtt
continue
}
s.proxyStandbys[index].failures++
if s.proxyStandbys[index].failures < proxyStandbyFailLimit {
continue
}
failed := s.proxyStandbys[index]
s.proxyStandbys = append(s.proxyStandbys[:index], s.proxyStandbys[index+1:]...)
s.proxyClientPool = removeClientFromPool(s.proxyClientPool, failed.client)
failedClients = append(failedClients, failed.client)
needRefill = true
logger.LogEvent("代理", "warn", "备用代理连续探测失败,已移出备用池: 出口=%s err=%v",
client.MaskProxyURL(failed.url), result.err)
}
s.clientMu.Unlock()
closeDouyuClientList(failedClients)
if needRefill && s.IsRunning() {
s.startProxyRefill()
}
return true
}
func findStandbyIndexLocked(standbys []validatedProxy, proxyClient *client.DouyuClient) int {
for index := range standbys {
if standbys[index].client == proxyClient {
return index
}
}
return -1
}
func removeClientFromPool(pool []*client.DouyuClient, target *client.DouyuClient) []*client.DouyuClient {
out := pool[:0]
for _, cli := range pool {
if cli != target {
out = append(out, cli)
}
}
return out
}
// prepareProxyGroups 将本次参与抢购的账号按固定顺序分组,并为每组准备独立客户端。
// forceRefresh=true 用于定时任务启动前主动刷新短效代理。
func (s *Scheduler) prepareProxyGroups(accountProducts map[string]string, forceRefresh bool) error {
timeout := proxyImmediateTimeout
if forceRefresh {
timeout = proxyScheduledTimeout
}
return s.prepareProxyGroupsUntil(accountProducts, forceRefresh, time.Now().Add(timeout))
}
func (s *Scheduler) prepareProxyGroupsUntil(accountProducts map[string]string, forceRefresh bool, deadline time.Time) error {
s.proxyPrepareMu.Lock()
defer s.proxyPrepareMu.Unlock()
cfg := s.proxySettings()
names := selectedAccountNames(accountProducts)
if len(names) == 0 {
return nil
}
if !cfg.enabled {
s.closeAccountClients()
s.setProxyReadiness(false, "代理未启用")
return nil
}
if !forceRefresh && !cfg.required && s.proxyFallbackSnapshot() {
logger.LogEvent("代理", "warn", "代理此前准备失败,本次抢购直接使用直连,避免再次等待")
return nil
}
if !forceRefresh && s.accountClientsCover(names) {
if s.proxyBusinessRiskVerifiedSnapshot() {
s.setProxyReadiness(true, "")
return nil
}
s.setProxyPreparing("代理已覆盖,正在补做业务风控预检")
if err := s.preflightProxyBusinessRiskUntil(accountProducts, deadline); err != nil {
return s.handleProxyPrepareFailure(cfg, names, err)
}
s.setProxyReadiness(true, "")
return nil
}
s.setProxyPreparing("代理正在准备")
groups := chunkAccountNames(names, cfg.accountsPerProxy)
activeCount := len(groups)
standbyTarget := proxyStandbyCount(activeCount, cfg.sparePercent)
base := s.clientSnapshot()
if base == nil {
return s.handleProxyPrepareFailure(cfg, names, errors.New("基础客户端未初始化"))
}
timeout := time.Until(deadline)
if timeout <= 0 {
return s.handleProxyPrepareFailure(cfg, names, errors.New("代理准备截止时间已到"))
}
buildTimeout := timeout
if buildTimeout > proxyRiskPreflightReserve*2 {
buildTimeout -= proxyRiskPreflightReserve
}
proxies, err := s.buildProxyPool(base, cfg, activeCount, activeCount+standbyTarget, nil, buildTimeout)
if err != nil {
return s.handleProxyPrepareFailure(cfg, names, err)
}
if len(proxies) < activeCount {
closeValidatedProxies(proxies)
return s.handleProxyPrepareFailure(cfg, names, fmt.Errorf("代理数量不足: 需要%d个活动代理,实际%d个", activeCount, len(proxies)))
}
if len(proxies) < activeCount+standbyTarget {
logger.LogEvent("代理", "warn", "备用代理不足: 目标%d个,实际%d个;活动代理不受影响",
standbyTarget, len(proxies)-activeCount)
}
accountClients := make(map[string]*client.DouyuClient, len(names))
clientOrder := make([]*client.DouyuClient, 0, activeCount)
runtimeGroups := make(map[*client.DouyuClient]*proxyRuntimeGroup, activeCount)
for i, group := range groups {
proxy := proxies[i]
clientOrder = append(clientOrder, proxy.client)
runtimeGroups[proxy.client] = &proxyRuntimeGroup{
groupIndex: i + 1,
accountNames: append([]string(nil), group...),
activeURL: proxy.url,
activeIP: proxy.ip,
activeRegion: proxy.region,
activeRTT: proxy.rtt,
}
for _, name := range group {
accountClients[name] = proxy.client
}
logger.LogEvent("代理", "success", "代理组%d: 账号=%d 出口=%s IP=%s 归属=%s RTT=%s 来源=%s",
i+1, len(group), client.MaskProxyURL(proxy.url), proxy.ip, proxy.region,
proxy.rtt.Round(time.Millisecond), proxy.source)
}
standbys := append([]validatedProxy(nil), proxies[activeCount:]...)
allClients := make([]*client.DouyuClient, 0, len(proxies))
excludedURLs := make(map[string]struct{}, len(proxies))
for _, proxy := range proxies {
allClients = append(allClients, proxy.client)
excludedURLs[proxy.url] = struct{}{}
proxyClient := proxy.client
client.MonitorProxyHealth(proxyClient, func(err error) {
s.recordProxyResult(proxyClient, err)
})
}
poolStartedAt := time.Now()
expiresAt := time.Time{}
if cfg.proxyAPI != "" {
expiresAt = proxyPoolExpiresAt(cfg.proxyAPI, poolStartedAt)
}
for _, proxy := range proxies {
if proxy.expiresAt.IsZero() {
continue
}
if expiresAt.IsZero() || proxy.expiresAt.Before(expiresAt) {
expiresAt = proxy.expiresAt
}
}
s.replaceAccountClients(preparedProxyPool{
accountClients: accountClients,
activeClients: clientOrder,
groups: runtimeGroups,
standbys: standbys,
allClients: allClients,
excludedURLs: excludedURLs,
standbyTarget: effectiveProxyStandbyTarget(cfg, standbyTarget, len(standbys)),
expiresAt: expiresAt,
})
s.replaceProxyGroupHistory(groups, activeCount, proxies)
logger.LogEvent("代理", "success", "代理池准备完成: 账号=%d,每组=%d,活动代理=%d,备用代理=%d/%d",
len(names), cfg.accountsPerProxy, activeCount, len(standbys), standbyTarget)
s.setProxyPreparing("代理池已建立,正在逐组执行活动火爆预检")
if err := s.preflightProxyBusinessRiskUntil(accountProducts, deadline); err != nil {
return s.handleProxyPrepareFailure(cfg, names, err)
}
s.setProxyReadiness(true, "")
return nil
}
func (s *Scheduler) handleProxyPrepareFailure(cfg proxySettingsSnapshot, names []string, cause error) error {
if cfg.required {
s.closeAccountClients()
s.setProxyReadiness(false, cause.Error())
logger.LogEvent("代理", "error", "严格代理模式准备失败,本次不启动且不回退直连: %v", cause)
return cause
}
s.activateProxyDirectFallback(cause)
s.setDirectFallbackAccounts(names)
return nil
}
func (s *Scheduler) setDirectFallbackAccounts(names []string) {
s.clientMu.Lock()
if s.directClient != nil {
s.accountClients = make(map[string]*client.DouyuClient, len(names))
for _, name := range names {
s.accountClients[name] = s.directClient
}
s.accountClientOrder = []*client.DouyuClient{s.directClient}
s.accountClientsDirty = false
}
s.proxyBusinessRiskVerified = false
s.clientMu.Unlock()
}
func (s *Scheduler) setProxyReadiness(ready bool, reason string) {
s.proxyMu.Lock()
s.proxyReady = ready
if ready {
s.proxyState = "ready"
} else if reason != "" {
s.proxyState = "failed"
} else {
s.proxyState = "empty"
}
s.proxyBlockedReason = reason
s.proxyMu.Unlock()
}
func (s *Scheduler) setProxyPreparing(reason string) {
s.proxyMu.Lock()
s.proxyReady = false
s.proxyState = "preparing"
s.proxyBlockedReason = reason
s.proxyMu.Unlock()
}
func (s *Scheduler) proxyFallbackSnapshot() bool {
s.clientMu.RLock()
defer s.clientMu.RUnlock()
return s.proxyFallbackActive
}
func (s *Scheduler) activateProxyDirectFallback(cause error) {
s.closeAccountClients()
s.clientMu.Lock()
active := s.directClient != nil
s.proxyFallbackActive = active
s.clientMu.Unlock()
if active {
s.setProxyReadiness(false, cause.Error())
logger.LogEvent("代理", "warn", "代理准备失败,已回退直连以保证抢购继续: %v", cause)
return
}
s.setProxyReadiness(false, cause.Error())
logger.LogEvent("代理", "error", "代理准备失败且直连客户端不可用: %v", cause)
}
func effectiveProxyStandbyTarget(cfg proxySettingsSnapshot, target, actual int) int {
if cfg.proxyAPI == "" {
return actual
}
return target
}
// PrepareProxyGroups 供立即抢购入口在启动前同步准备代理,便于把错误直接返回给界面。
func (s *Scheduler) PrepareProxyGroups(accountProducts map[string]string) error {
return s.prepareProxyGroups(accountProducts, false)
}
func (s *Scheduler) PrepareProxyGroupsUntil(accountProducts map[string]string, deadline time.Time) error {
return s.prepareProxyGroupsUntil(accountProducts, true, deadline)
}
func selectedAccountNames(accountProducts map[string]string) []string {
names := make([]string, 0, len(accountProducts))
for name, productID := range accountProducts {
name = strings.TrimSpace(name)
if name == "" || strings.TrimSpace(productID) == "" {
continue
}
names = append(names, name)
}
sort.Strings(names)
return names
}
func chunkAccountNames(names []string, size int) [][]string {
if size <= 0 {
size = constants.DefaultAccountsPerProxy
}
if size > constants.MaxAccountsPerProxy {
size = constants.MaxAccountsPerProxy
}
groups := make([][]string, 0, (len(names)+size-1)/size)
for start := 0; start < len(names); start += size {
end := start + size
if end > len(names) {
end = len(names)
}
groups = append(groups, append([]string(nil), names[start:end]...))
}
return groups
}
func proxyStandbyCount(activeCount, sparePercent int) int {
if activeCount <= 0 {
return 0
}
if sparePercent < constants.MinProxySparePercent {
sparePercent = constants.DefaultProxySparePercent
}
if sparePercent > constants.MaxProxySparePercent {
sparePercent = constants.MaxProxySparePercent
}
count := (activeCount*sparePercent + 99) / 100
if count < 1 {
count = 1
}
return count
}
func (s *Scheduler) buildProxyPool(base *client.DouyuClient, cfg proxySettingsSnapshot, required, desired int, excludedURLs map[string]struct{}, timeout time.Duration) ([]validatedProxy, error) {
if desired < required {
desired = required
}
if required <= 0 {
return nil, nil
}
deadline := time.Now().Add(timeout)
if cfg.proxyAPI != "" {
proxies, err := s.fetchAndValidateProxyPool(base, cfg, desired, excludedURLs, timeout)
if len(proxies) >= required {
if len(proxies) < desired {
logger.LogEvent("代理", "warn", "动态代理备用数量不足: 目标%d个,实际%d个", desired, len(proxies))
}
return proxies, nil
}
closeValidatedProxies(proxies)
if err != nil {
logger.LogEvent("代理", "warn", "动态代理提取未完全成功: %v", err)
}
}
staticURLs := uniqueStrings(splitProxyURLs(cfg.proxyURL))
if len(staticURLs) == 0 {
return nil, errors.New("代理已启用,但没有可用的提取 API 或静态代理")
}
if len(staticURLs) == 1 {
logger.LogEvent("代理", "warn", "当前只有一个静态代理,%d 个账号组将共享该出口且没有可用备用代理", required)
}
validateCount := len(staticURLs)
if validateCount > desired {
validateCount = desired
}
templates := make([]validatedProxy, 0, validateCount)
for _, rawURL := range staticURLs[:validateCount] {
if time.Now().After(deadline) {
closeValidatedProxies(templates)
return nil, errors.New("静态代理校验超时")
}
proxy, err := s.validateProxy(base, rawURL, "静态", time.Until(deadline))
if err != nil {
closeValidatedProxies(templates)
return nil, fmt.Errorf("静态代理不可用: %w", err)
}
templates = append(templates, proxy)
}
total := required
if len(templates) > required {
total += len(templates) - required
if total > desired {
total = desired
}
}
proxies := make([]validatedProxy, 0, total)
var cloneErr error
for i := 0; i < total; i++ {
template := templates[i%len(templates)]
proxy := template
if i >= len(templates) {
proxy, cloneErr = cloneValidatedProxy(base, template)
if cloneErr != nil {
closeValidatedProxies(proxies)
closeValidatedProxies(templates)
return nil, fmt.Errorf("创建共享静态代理连接失败: %w", cloneErr)
}
}
proxies = append(proxies, proxy)
}
if total < len(templates) {
closeValidatedProxies(templates[total:])
}
return proxies, nil
}
func uniqueStrings(items []string) []string {
seen := make(map[string]struct{}, len(items))
out := make([]string, 0, len(items))
for _, item := range items {
if _, ok := seen[item]; ok {
continue
}
seen[item] = struct{}{}
out = append(out, item)
}
return out
}
func cloneValidatedProxy(base *client.DouyuClient, template validatedProxy) (validatedProxy, error) {
cli, err := client.NewWithProxy(base.Activity, base.TimeAPIURL, base.Trace, template.url)
if err != nil {
return validatedProxy{}, err
}
template.client = cli
return template, nil
}
func closeValidatedProxies(proxies []validatedProxy) {
clients := make([]*client.DouyuClient, 0, len(proxies))
for _, proxy := range proxies {
clients = append(clients, proxy.client)
}
closeDouyuClientList(clients)
}
func splitProxyURLs(raw string) []string {
parts := strings.FieldsFunc(raw, func(r rune) bool {
return r == '\n' || r == '\r' || r == ';' || r == ','
})
out := make([]string, 0, len(parts))
for _, part := range parts {
part = strings.TrimSpace(part)
if part != "" {
out = append(out, part)
}
}
return out
}
func (s *Scheduler) fetchAndValidateProxyPool(base *client.DouyuClient, cfg proxySettingsSnapshot, count int, excludedURLs map[string]struct{}, timeout time.Duration) ([]validatedProxy, error) {
service := s.proxyService
if service == nil {
service = proxyprovider.NewService()
s.proxyService = service
}
proxies := make([]validatedProxy, 0, count)
seen := make(map[string]struct{}, count+len(excludedURLs))
for rawURL := range excludedURLs {
seen[rawURL] = struct{}{}
}
var lastErr error
deadline := time.Now().Add(timeout)
rounds := proxyFetchRounds
if count > rounds {
rounds = count
}
if rounds > proxyFetchMaxRounds {
rounds = proxyFetchMaxRounds
}
for round := 0; round < rounds; round++ {
remaining := time.Until(deadline)
if remaining <= 0 {
if lastErr == nil {
lastErr = errors.New("代理准备总时长超时")
}
break
}
need := count - len(proxies)
if need <= 0 {
break
}
requestTimeout := proxyRequestTimeout
if remaining < requestTimeout {
requestTimeout = remaining
}
ctx, cancel := context.WithTimeout(context.Background(), requestTimeout)
endpoints, err := service.ExtractWithWhitelist(ctx, cfg.proxyAPI, need, proxyprovider.WhitelistConfig{
APIURL: cfg.proxyAPI,
LoginPassword: cfg.whitelistPassword,
SignKey: cfg.whitelistSignKey,
ReplaceOldest: cfg.whitelistReplace,
AutoWhitelist: cfg.autoWhitelist,
})
cancel()
if err != nil {
lastErr = err
if proxyprovider.IsFatal(err) {
break
}
} else {
validated := s.validateProxyBatch(base, endpoints, deadline)
for _, item := range validated {
if item.err != nil {
lastErr = item.err
continue
}
if !item.endpoint.ExpiresAt.IsZero() && !time.Now().Before(item.endpoint.ExpiresAt) {
lastErr = fmt.Errorf("代理 %s 已过期", item.endpoint.IP)
closeDouyuClientList([]*client.DouyuClient{item.proxy.client})
continue
}
if _, exists := seen[item.endpoint.URL]; exists {
closeDouyuClientList([]*client.DouyuClient{item.proxy.client})
continue
}
seen[item.endpoint.URL] = struct{}{}
proxy := item.proxy
if proxy.ip == "" {
proxy.ip = item.endpoint.IP
}
if proxy.region == "" {
proxy.region = item.endpoint.Region
}
proxy.expiresAt = item.endpoint.ExpiresAt
proxies = append(proxies, proxy)
}
}
if round < rounds-1 {
wait := proxyFetchRetryWait
if remaining := time.Until(deadline); remaining < wait {
wait = remaining
}
if wait > 0 {
time.Sleep(wait)
}
}
}
if len(proxies) < count {
if lastErr != nil {
return proxies, lastErr
}
return proxies, fmt.Errorf("代理 API 仅返回 %d/%d 个有效代理", len(proxies), count)
}
return proxies, nil
}
func (s *Scheduler) recordProxyResult(proxyClient *client.DouyuClient, err error) {
if proxyClient == nil {
return
}
cfg := s.proxySettings()
s.clientMu.Lock()
group := s.proxyGroups[proxyClient]
if group == nil {
s.clientMu.Unlock()
return
}
if err == nil {
group.consecutiveFailures = 0
group.lastError = ""
group.failed = false
s.updateProxyGroupHistoryLocked(group)
s.clientMu.Unlock()
return
}
if !client.IsProxyTransportFailure(err) || group.failed {
s.clientMu.Unlock()
return
}
s.proxyBusinessRiskVerified = false
group.consecutiveFailures++
group.lastError = err.Error()
s.updateProxyGroupHistoryLocked(group)
if group.consecutiveFailures < 2 {
failures := group.consecutiveFailures
activeURL := group.activeURL
s.clientMu.Unlock()
logger.LogEvent("代理", "debug", "代理网络异常计数: %s 连续失败=%d", client.MaskProxyURL(activeURL), failures)
return
}
event, expiredClients, switched := s.switchGroupToStandbyLocked(proxyClient, group)
if !switched && cfg.required {
group.failed = true
} else if !switched && s.directClient != nil {
event = s.switchGroupToDirectLocked(proxyClient, group)
switched = true
}
needRefill := false
failedAccounts := 0
failedURL := ""
if switched {
needRefill = len(s.proxyStandbys) < s.proxyStandbyTarget && !s.proxyRefillInFlight
} else {
group.failed = true
failedAccounts = len(group.accountNames)
failedURL = group.activeURL
needRefill = !s.proxyRefillInFlight
s.updateProxyGroupHistoryLocked(group)
}
s.clientMu.Unlock()
closeDouyuClientList(expiredClients)
if switched {
logProxySwitch("代理故障切换", event)
} else {
if cfg.required {
logger.LogEvent("代理", "warn", "严格代理模式下代理组连续失败且暂无备用,暂不直连并等待补提取: 账号=%d 出口=%s",
failedAccounts, client.MaskProxyURL(failedURL))
} else {
logger.LogEvent("代理", "warn", "代理组连续失败且暂无备用: 账号=%d 出口=%s,正在尝试补提取",
failedAccounts, client.MaskProxyURL(failedURL))
}
}
if needRefill {
s.startProxyRefill()
}
}
// handleProxyBusinessRisk switches away from the egress IP used by an account
// when the lock API returns the explicit "activity hot" risk-control code.
func (s *Scheduler) handleProxyBusinessRisk(accountName string, proxyClient *client.DouyuClient, cause error) {
if !isProxyRiskBusinessError(cause) {
return
}
cfg := s.proxySettings()
type riskGroup struct {
client *client.DouyuClient
group *proxyRuntimeGroup
}
s.clientMu.Lock()
if proxyClient == nil || proxyClient == s.directClient || s.proxyFallbackActive {
s.clientMu.Unlock()
return
}
initial := s.proxyGroups[proxyClient]
if initial == nil || initial.failed {
s.clientMu.Unlock()
return
}
riskIP := strings.TrimSpace(initial.activeIP)
targets := make([]riskGroup, 0, 1)
seenGroups := make(map[int]struct{})
for proxyClient, group := range s.proxyGroups {
if group == nil {
continue
}
if group != initial && (riskIP == "" || strings.TrimSpace(group.activeIP) != riskIP) {
continue
}
if _, seen := seenGroups[group.groupIndex]; seen {
continue
}
seenGroups[group.groupIndex] = struct{}{}
targets = append(targets, riskGroup{client: proxyClient, group: group})
}
var (
expiredClients []*client.DouyuClient
switchEvents []proxySwitchEvent
failedGroups []*proxyRuntimeGroup
needRefill bool
)
for _, target := range targets {
target.group.lastError = cause.Error()
s.updateProxyGroupHistoryLocked(target.group)
event, expired, switched := s.switchGroupToStandbyAvoidIPLocked(target.client, target.group, riskIP)
expiredClients = append(expiredClients, expired...)
if switched {
switchEvents = append(switchEvents, event)
needRefill = needRefill || len(s.proxyStandbys) < s.proxyStandbyTarget
continue
}
if !cfg.required && s.directClient != nil {
event = s.switchGroupToDirectLocked(target.client, target.group)
switchEvents = append(switchEvents, event)
needRefill = true
continue
}
target.group.failed = true
s.proxyBusinessRiskVerified = false
failedGroups = append(failedGroups, target.group)
needRefill = true
s.updateProxyGroupHistoryLocked(target.group)
}
needRefill = needRefill && !s.proxyRefillInFlight
s.clientMu.Unlock()
closeDouyuClientList(expiredClients)
for _, event := range switchEvents {
logProxySwitch("代理风控切换", event)
}
for _, group := range failedGroups {
logger.LogEvent("代理", "warn",
"代理风控命中且无不同IP备用: 组=%d 账号=%d 出口=%s,严格模式暂停并等待补提取",
group.groupIndex, len(group.accountNames), client.MaskProxyURL(group.activeURL))
}
logger.LogEvent("代理", "warn",
"代理业务风控: 账号=%s 出口IP=%s 原因=%v 切换组数=%d",
accountName, riskIP, cause, len(switchEvents))
if needRefill {
s.startProxyRefill()
}
}
func (s *Scheduler) switchGroupToStandbyLocked(oldClient *client.DouyuClient, group *proxyRuntimeGroup) (proxySwitchEvent, []*client.DouyuClient, bool) {
return s.switchGroupToStandbyAvoidIPLocked(oldClient, group, "")
}
func (s *Scheduler) switchGroupToStandbyAvoidIPLocked(oldClient *client.DouyuClient, group *proxyRuntimeGroup, avoidIP string) (proxySwitchEvent, []*client.DouyuClient, bool) {
expiredClients := make([]*client.DouyuClient, 0, 1)
var skipped []validatedProxy
for len(s.proxyStandbys) > 0 {
standby := s.proxyStandbys[0]
s.proxyStandbys = s.proxyStandbys[1:]
if !standby.expiresAt.IsZero() && !time.Now().Before(standby.expiresAt) {
expiredClients = append(expiredClients, standby.client)
continue
}
if avoidIP != "" && standby.ip == avoidIP {
skipped = append(skipped, standby)
continue
}
if len(skipped) > 0 {
s.proxyStandbys = append(skipped, s.proxyStandbys...)
}
event := proxySwitchEvent{
accountNames: append([]string(nil), group.accountNames...),
fromURL: group.activeURL,
toURL: standby.url,
toIP: standby.ip,
toRegion: standby.region,
toRTT: standby.rtt,
toSource: standby.source,
}
group.activeURL = standby.url
group.activeIP = standby.ip
group.activeRegion = standby.region
group.activeRTT = standby.rtt
group.switchCount++
group.consecutiveFailures = 0
group.lastError = ""
group.failed = false
s.proxyBusinessRiskVerified = false
for _, accountName := range group.accountNames {
s.accountClients[accountName] = standby.client
}
delete(s.proxyGroups, oldClient)
s.proxyGroups[standby.client] = group
replaceClientInOrder(s.accountClientOrder, oldClient, standby.client)
s.updateProxyGroupHistoryLocked(group)
return event, expiredClients, true
}
if len(skipped) > 0 {
s.proxyStandbys = append(skipped, s.proxyStandbys...)
}
return proxySwitchEvent{}, expiredClients, false
}
func (s *Scheduler) switchGroupToDirectLocked(oldClient *client.DouyuClient, group *proxyRuntimeGroup) proxySwitchEvent {
event := proxySwitchEvent{
accountNames: append([]string(nil), group.accountNames...),
fromURL: group.activeURL,
}
group.activeURL = ""
group.activeIP = ""
group.activeRegion = ""
group.activeRTT = 0
group.switchCount++
group.consecutiveFailures = 0
group.lastError = ""
group.failed = false
s.proxyBusinessRiskVerified = false
for _, accountName := range group.accountNames {
s.accountClients[accountName] = s.directClient
}
delete(s.proxyGroups, oldClient)
replaceClientInOrder(s.accountClientOrder, oldClient, s.directClient)
s.updateProxyGroupHistoryLocked(group)
return event
}
func (s *Scheduler) updateProxyGroupHistoryLocked(group *proxyRuntimeGroup) {
if group == nil || group.groupIndex <= 0 {
return
}
for i := range s.proxyGroupHistory {
item := &s.proxyGroupHistory[i]
if item.GroupIndex != group.groupIndex {
continue
}
item.AccountCount = len(group.accountNames)
item.Accounts = append([]string(nil), group.accountNames...)
item.SwitchCount = group.switchCount
item.LastError = group.lastError
switch {
case group.activeURL == "":
item.State = "direct"
item.Proxy = ""
item.Region = ""
case group.failed:
item.State = "failed"
default:
item.State = "active"
item.Proxy = client.MaskProxyURL(group.activeURL)
item.Region = group.activeRegion
item.RTTMs = group.activeRTT.Milliseconds()
}
return
}
}
func replaceClientInOrder(order []*client.DouyuClient, oldClient, newClient *client.DouyuClient) {
for i, cli := range order {
if cli == oldClient {
order[i] = newClient
return
}
}
}
func (s *Scheduler) startProxyRefill() {
if !s.IsRunning() {
return
}
cfg := s.proxySettings()
if !cfg.enabled || cfg.proxyAPI == "" {
return
}
base := s.clientSnapshot()
if base == nil {
return
}
s.clientMu.Lock()
need := s.proxyStandbyTarget - len(s.proxyStandbys)
if need < 0 {
need = 0
}
need += countFailedProxyGroups(s.proxyGroups)
if need <= 0 || s.proxyRefillInFlight {
s.clientMu.Unlock()
return
}
s.proxyRefillInFlight = true
generation := s.proxyPoolGeneration
excludedURLs := copyStringSet(s.proxyExcludedURLs)
s.clientMu.Unlock()
go s.refillProxyStandbys(base, cfg, generation, need, excludedURLs)
}
func (s *Scheduler) refillProxyStandbys(base *client.DouyuClient, cfg proxySettingsSnapshot, generation uint64, need int, excludedURLs map[string]struct{}) {
proxies, err := s.fetchAndValidateProxyPool(base, cfg, need, excludedURLs, proxyRefillTimeout)
if err != nil {
logger.LogEvent("代理", "warn", "备用代理补提取未完全成功: %v", err)
}
var extras []*client.DouyuClient
var switches []proxySwitchEvent
var expiredClients []*client.DouyuClient
s.clientMu.Lock()
if generation != s.proxyPoolGeneration {
s.clientMu.Unlock()
closeValidatedProxies(proxies)
return
}
currentNeed := s.proxyStandbyTarget - len(s.proxyStandbys)
if currentNeed < 0 {
currentNeed = 0
}
currentNeed += countFailedProxyGroups(s.proxyGroups)
if len(proxies) > currentNeed {
for _, proxy := range proxies[currentNeed:] {
extras = append(extras, proxy.client)
}
proxies = proxies[:currentNeed]
}
for _, proxy := range proxies {
proxyClient := proxy.client
client.MonitorProxyHealth(proxyClient, func(err error) {
s.recordProxyResult(proxyClient, err)
})
s.proxyStandbys = append(s.proxyStandbys, proxy)
s.proxyClientPool = append(s.proxyClientPool, proxy.client)
s.proxyExcludedURLs[proxy.url] = struct{}{}
}
for {
pendingClient, pendingGroup := findFailedProxyGroup(s.proxyGroups)
if pendingClient == nil {
break
}
event, expired, switched := s.switchGroupToStandbyAvoidIPLocked(
pendingClient,
pendingGroup,
pendingGroup.activeIP,
)
expiredClients = append(expiredClients, expired...)
if !switched {
break
}
switches = append(switches, event)
}
s.proxyRefillInFlight = false
remainingFailed := countFailedProxyGroups(s.proxyGroups)
standbyCount := len(s.proxyStandbys)
standbyTarget := s.proxyStandbyTarget
s.clientMu.Unlock()
closeDouyuClientList(extras)
closeDouyuClientList(expiredClients)
for _, event := range switches {
logProxySwitch("备用代理补提取成功", event)
}
if len(proxies) > 0 {
logger.LogEvent("代理", "success", "备用代理已补充: 新增=%d,剩余=%d,目标=%d",
len(proxies), standbyCount, standbyTarget)
}
if remainingFailed > 0 {
logger.LogEvent("代理", "warn", "仍有%d个代理组等待备用代理,请检查提取 API 配额与白名单", remainingFailed)
}
if s.IsRunning() && (remainingFailed > 0 || standbyCount < standbyTarget) {
s.scheduleProxyRefillRetry(generation)
}
}
func (s *Scheduler) scheduleProxyRefillRetry(generation uint64) {
s.clientMu.Lock()
if generation != s.proxyPoolGeneration || s.proxyRefillInFlight {
s.clientMu.Unlock()
return
}
now := time.Now()
if now.Before(s.proxyRefillRetryAt) {
s.clientMu.Unlock()
return
}
s.proxyRefillRetryAt = now.Add(proxyRefillRetryDelay)
s.clientMu.Unlock()
time.AfterFunc(proxyRefillRetryDelay, func() {
if !s.IsRunning() {
return
}
s.clientMu.Lock()
if generation != s.proxyPoolGeneration {
s.clientMu.Unlock()
return
}
s.proxyRefillRetryAt = time.Time{}
needStandby := s.proxyStandbyTarget - len(s.proxyStandbys)
failedGroups := countFailedProxyGroups(s.proxyGroups)
s.clientMu.Unlock()
if needStandby > 0 || failedGroups > 0 {
s.startProxyRefill()
}
})
}
func findFailedProxyGroup(groups map[*client.DouyuClient]*proxyRuntimeGroup) (*client.DouyuClient, *proxyRuntimeGroup) {
for cli, group := range groups {
if group != nil && group.failed {
return cli, group
}
}
return nil, nil
}
func countFailedProxyGroups(groups map[*client.DouyuClient]*proxyRuntimeGroup) int {
count := 0
for _, group := range groups {
if group != nil && group.failed {
count++
}
}
return count
}
func copyStringSet(source map[string]struct{}) map[string]struct{} {
out := make(map[string]struct{}, len(source))
for value := range source {
out[value] = struct{}{}
}
return out
}
func logProxySwitch(label string, event proxySwitchEvent) {
logger.LogEvent("代理", "warn", "%s: 账号=%d %s -> %s IP=%s 归属=%s RTT=%s 来源=%s",
label, len(event.accountNames), proxyDisplay(event.fromURL), proxyDisplay(event.toURL),
event.toIP, event.toRegion, event.toRTT.Round(time.Millisecond), event.toSource)
}
func proxyDisplay(raw string) string {
if strings.TrimSpace(raw) == "" {
return "直连"
}
return client.MaskProxyURL(raw)
}
func proxyPoolExpiresAt(apiURL string, now time.Time) time.Time {
minutes := 10
if u, err := url.Parse(strings.TrimSpace(apiURL)); err == nil {
query := u.Query()
raw := query.Get("minute")
if raw == "" {
raw = query.Get("min")
}
if value, err := strconv.Atoi(raw); err == nil && value > 0 && value <= 60 {
minutes = value
}
}
return now.Add(time.Duration(minutes) * time.Minute)
}
func (s *Scheduler) validateProxy(base *client.DouyuClient, proxyURL, source string, timeout time.Duration) (validatedProxy, error) {
cli, err := client.NewWithProxy(base.Activity, base.TimeAPIURL, base.Trace, proxyURL)
if err != nil {
return validatedProxy{}, err
}
if timeout <= 0 || timeout > proxyValidationTimeout {
timeout = proxyValidationTimeout
}
if cli.HTTPClient != nil {
cli.HTTPClient.Timeout = timeout
}
var lastErr error
for attempt := 0; attempt < proxyValidateAttempts; attempt++ {
probe, probeErr := cli.ProbeProxyEndpoint()
if probeErr == nil {
return validatedProxy{
url: proxyURL,
client: cli,
rtt: probe.Latency,
source: source,
}, nil
}
lastErr = probeErr
}
if cli.HTTPClient != nil {
cli.HTTPClient.CloseIdleConnections()
}
return validatedProxy{}, lastErr
}
type proxyValidationResult struct {
endpoint proxyprovider.Endpoint
proxy validatedProxy
err error
}
func (s *Scheduler) validateProxyBatch(base *client.DouyuClient, endpoints []proxyprovider.Endpoint, deadline time.Time) []proxyValidationResult {
if len(endpoints) == 0 {
return nil
}
workers := proxyValidationWorkers
if len(endpoints) < workers {
workers = len(endpoints)
}
jobs := make(chan proxyprovider.Endpoint)
results := make(chan proxyValidationResult, len(endpoints))
var wg sync.WaitGroup
for i := 0; i < workers; i++ {
wg.Add(1)
go func() {
defer wg.Done()
for endpoint := range jobs {
remaining := time.Until(deadline)
if remaining <= 0 {
results <- proxyValidationResult{endpoint: endpoint, err: errors.New("代理校验总时长超时")}
continue
}
proxy, err := s.validateProxy(base, endpoint.URL, "API/"+endpoint.Provider, remaining)
results <- proxyValidationResult{endpoint: endpoint, proxy: proxy, err: err}
}
}()
}
for _, endpoint := range endpoints {
jobs <- endpoint
}
close(jobs)
wg.Wait()
close(results)
out := make([]proxyValidationResult, 0, len(endpoints))
for result := range results {
out = append(out, result)
}
return out
}
func accountProductsForTasks(tasks []types.ExchangeAttempt) map[string]string {
out := make(map[string]string, len(tasks))
for _, task := range tasks {
out[task.Account.Name] = task.Product.CommodityID
}
return out
}