523 lines
14 KiB
Go
523 lines
14 KiB
Go
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
|
|
proxyFetchRetryWait = 300 * time.Millisecond
|
|
proxyRequestTimeout = 15 * time.Second
|
|
proxyValidationTimeout = 3 * time.Second
|
|
proxyValidationWorkers = 3
|
|
proxyValidateAttempts = 1
|
|
)
|
|
|
|
type proxySettingsSnapshot struct {
|
|
enabled bool
|
|
proxyURL string
|
|
proxyAPI string
|
|
autoFetchOnTimer bool
|
|
accountsPerProxy int
|
|
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
|
|
}
|
|
|
|
// UpdateProxySettings 运行时更新代理配置。
|
|
func (s *Scheduler) UpdateProxySettings(cfg types.Config) {
|
|
enabled := cfg.ProxyEnabled
|
|
proxyURL := cfg.ProxyURL
|
|
proxyAPI := cfg.ProxyAPI
|
|
autoFetch := cfg.AutoFetchProxyOnSchedule
|
|
accountsPerProxy := cfg.AccountsPerProxy
|
|
if accountsPerProxy <= 0 {
|
|
accountsPerProxy = constants.DefaultAccountsPerProxy
|
|
}
|
|
if accountsPerProxy > constants.MaxAccountsPerProxy {
|
|
accountsPerProxy = constants.MaxAccountsPerProxy
|
|
}
|
|
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.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.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.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
|
|
}
|
|
return proxySettingsSnapshot{
|
|
enabled: s.proxyEnabled,
|
|
proxyURL: s.proxyURL,
|
|
proxyAPI: s.proxyAPI,
|
|
autoFetchOnTimer: s.autoFetchProxyOnSchedule,
|
|
accountsPerProxy: size,
|
|
autoWhitelist: s.autoWhitelist,
|
|
whitelistReplace: s.whitelistReplace,
|
|
whitelistPassword: s.whitelistPassword,
|
|
whitelistSignKey: s.whitelistSignKey,
|
|
}
|
|
}
|
|
|
|
func (s *Scheduler) closeAccountClients() {
|
|
s.clientMu.Lock()
|
|
old := s.accountClients
|
|
s.accountClients = make(map[string]*client.DouyuClient)
|
|
s.accountClientOrder = nil
|
|
s.accountClientsDirty = true
|
|
s.accountClientsExpireAt = time.Time{}
|
|
s.clientMu.Unlock()
|
|
closeDouyuClients(old)
|
|
}
|
|
|
|
func (s *Scheduler) replaceAccountClients(accountClients map[string]*client.DouyuClient, order []*client.DouyuClient, expiresAt time.Time) {
|
|
s.clientMu.Lock()
|
|
old := s.accountClients
|
|
s.accountClients = accountClients
|
|
s.accountClientOrder = order
|
|
s.accountClientsDirty = false
|
|
s.accountClientsExpireAt = expiresAt
|
|
s.clientMu.Unlock()
|
|
closeDouyuClients(old)
|
|
}
|
|
|
|
func closeDouyuClients(clients map[string]*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()
|
|
}
|
|
}
|
|
}
|
|
|
|
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 {
|
|
if s.accountClients[name] == nil {
|
|
return false
|
|
}
|
|
}
|
|
return true
|
|
}
|
|
|
|
func (s *Scheduler) calibrationClient() *client.DouyuClient {
|
|
s.clientMu.RLock()
|
|
defer s.clientMu.RUnlock()
|
|
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)
|
|
}
|
|
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 {
|
|
cfg := s.proxySettings()
|
|
names := selectedAccountNames(accountProducts)
|
|
if len(names) == 0 {
|
|
return nil
|
|
}
|
|
if !cfg.enabled {
|
|
s.closeAccountClients()
|
|
return nil
|
|
}
|
|
if !forceRefresh && s.accountClientsCover(names) {
|
|
return nil
|
|
}
|
|
|
|
groups := chunkAccountNames(names, cfg.accountsPerProxy)
|
|
base := s.clientSnapshot()
|
|
if base == nil {
|
|
return errors.New("基础客户端未初始化")
|
|
}
|
|
|
|
poolStartedAt := time.Now()
|
|
proxies, err := s.buildProxyPool(base, cfg, len(groups))
|
|
if err != nil {
|
|
return err
|
|
}
|
|
if len(proxies) < len(groups) {
|
|
return fmt.Errorf("代理数量不足: 需要%d组,实际%d组", len(groups), len(proxies))
|
|
}
|
|
|
|
accountClients := make(map[string]*client.DouyuClient, len(names))
|
|
clientOrder := make([]*client.DouyuClient, 0, len(groups))
|
|
for i, group := range groups {
|
|
proxy := proxies[i]
|
|
clientOrder = append(clientOrder, proxy.client)
|
|
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)
|
|
}
|
|
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(accountClients, clientOrder, expiresAt)
|
|
logger.LogEvent("代理", "success", "代理分组完成: 账号=%d,每组=%d,代理组=%d",
|
|
len(names), cfg.accountsPerProxy, len(groups))
|
|
return nil
|
|
}
|
|
|
|
// PrepareProxyGroups 供立即抢购入口在启动前同步准备代理,便于把错误直接返回给界面。
|
|
func (s *Scheduler) PrepareProxyGroups(accountProducts map[string]string) error {
|
|
return s.prepareProxyGroups(accountProducts, false)
|
|
}
|
|
|
|
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 (s *Scheduler) buildProxyPool(base *client.DouyuClient, cfg proxySettingsSnapshot, count int) ([]validatedProxy, error) {
|
|
if count <= 0 {
|
|
return nil, nil
|
|
}
|
|
if cfg.proxyAPI != "" {
|
|
proxies, err := s.fetchAndValidateProxyPool(base, cfg, count)
|
|
if err == nil && len(proxies) >= count {
|
|
return proxies, nil
|
|
}
|
|
if err != nil {
|
|
logger.LogEvent("代理", "warn", "动态代理提取未完全成功: %v", err)
|
|
}
|
|
}
|
|
|
|
staticURLs := splitProxyURLs(cfg.proxyURL)
|
|
if len(staticURLs) == 0 {
|
|
return nil, errors.New("代理已启用,但没有可用的提取 API 或静态代理")
|
|
}
|
|
if len(staticURLs) == 1 {
|
|
logger.LogEvent("代理", "warn", "当前只有一个静态代理,%d 个账号组将共享该出口", count)
|
|
}
|
|
proxies := make([]validatedProxy, 0, count)
|
|
for i := 0; i < count; i++ {
|
|
rawURL := staticURLs[i%len(staticURLs)]
|
|
proxy, err := s.validateProxy(base, rawURL, "静态")
|
|
if err != nil {
|
|
return nil, fmt.Errorf("静态代理组%d不可用: %w", i+1, err)
|
|
}
|
|
proxies = append(proxies, proxy)
|
|
}
|
|
return proxies, nil
|
|
}
|
|
|
|
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) ([]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)
|
|
var lastErr error
|
|
|
|
for round := 0; round < proxyFetchRounds; round++ {
|
|
need := count - len(proxies)
|
|
if need <= 0 {
|
|
break
|
|
}
|
|
|
|
ctx, cancel := context.WithTimeout(context.Background(), proxyRequestTimeout)
|
|
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)
|
|
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)
|
|
continue
|
|
}
|
|
if _, exists := seen[item.endpoint.URL]; exists {
|
|
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 < proxyFetchRounds-1 {
|
|
time.Sleep(proxyFetchRetryWait)
|
|
}
|
|
}
|
|
|
|
if len(proxies) < count {
|
|
if lastErr != nil {
|
|
return proxies, lastErr
|
|
}
|
|
return proxies, fmt.Errorf("代理 API 仅返回 %d/%d 个有效代理", len(proxies), count)
|
|
}
|
|
return proxies, nil
|
|
}
|
|
|
|
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) (validatedProxy, error) {
|
|
cli, err := client.NewWithProxy(base.Activity, base.TimeAPIURL, base.Trace, proxyURL)
|
|
if err != nil {
|
|
return validatedProxy{}, err
|
|
}
|
|
if cli.HTTPClient != nil && cli.HTTPClient.Timeout > proxyValidationTimeout {
|
|
cli.HTTPClient.Timeout = proxyValidationTimeout
|
|
}
|
|
var lastErr error
|
|
for attempt := 0; attempt < proxyValidateAttempts; attempt++ {
|
|
sample, sampleErr := cli.ServerTimeSample()
|
|
if sampleErr == nil && !sample.Fallback {
|
|
return validatedProxy{
|
|
url: proxyURL,
|
|
client: cli,
|
|
rtt: sample.RTT,
|
|
source: source,
|
|
}, nil
|
|
}
|
|
if sampleErr != nil {
|
|
lastErr = sampleErr
|
|
} else {
|
|
lastErr = errors.New("斗鱼时间接口测试失败")
|
|
}
|
|
}
|
|
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) []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 {
|
|
proxy, err := s.validateProxy(base, endpoint.URL, "API/"+endpoint.Provider)
|
|
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
|
|
}
|