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 proxyImmediateTimeout = 5 * time.Second proxyScheduledTimeout = 20 * time.Second proxyRefillTimeout = 20 * 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 } 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.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.proxyFallbackActive = 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.proxyFallbackActive = 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 "" } 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 len(s.proxyStandbys) < s.proxyStandbyTarget { return false } if !s.accountClientsExpireAt.IsZero() && !time.Now().Before(s.accountClientsExpireAt.Add(-30*time.Second)) { return false } for _, name := range accountNames { if s.accountClients[name] == nil { 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 } // 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) { 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("代理准备截止时间已到")) } proxies, err := s.buildProxyPool(base, cfg, activeCount, activeCount+standbyTarget, nil, timeout) 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.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.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 = "" s.updateProxyGroupHistoryLocked(group) s.clientMu.Unlock() return } if !client.IsProxyTransportFailure(err) || group.failed { s.clientMu.Unlock() return } 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() } } func (s *Scheduler) switchGroupToStandbyLocked(oldClient *client.DouyuClient, group *proxyRuntimeGroup) (proxySwitchEvent, []*client.DouyuClient, bool) { expiredClients := make([]*client.DouyuClient, 0, 1) 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 } 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 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 } 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 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() { 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 || 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 } 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.switchGroupToStandbyLocked(pendingClient, pendingGroup) 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 standbyCount < standbyTarget && len(proxies) > 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 }