feat(proxy): 支持品赞分组代理与自动白名单

This commit is contained in:
yml
2026-11-19 23:42:56 +08:00
parent 61ea79c7c9
commit 44a5b6c883
24 changed files with 2253 additions and 315 deletions
@@ -22,7 +22,12 @@ const formData = ref({
proxyEnabled: false,
proxyUrl: '',
proxyApi: '',
autoFetchProxyOnSchedule: false
autoFetchProxyOnSchedule: false,
accountsPerProxy: 5,
proxyAutoWhitelist: false,
proxyWhitelistReplace: true,
proxyWhitelistPassword: '',
proxyWhitelistSignKey: ''
})
const testingProxy = ref(false)
@@ -73,7 +78,14 @@ const handleFetchProxy = async () => {
if (res.success && res.proxyUrl) {
formData.value.proxyUrl = res.proxyUrl
formData.value.proxyEnabled = true
showToast('提取成功', `已获取代理: ${res.proxyUrl} (${res.region || ''})`, 'success')
const expires = res.expiresAt
? new Date(res.expiresAt).toLocaleString()
: '未知'
showToast(
'提取成功',
`已获取代理: ${res.proxyUrl}${res.provider ? ' [' + res.provider + ']' : ''} (${res.region || ''},过期 ${expires})`,
'success'
)
// 自动触发一次连通性测速
handleTestProxy()
} else {
@@ -164,6 +176,21 @@ defineExpose({ open, close })
</div>
<div v-if="formData.proxyEnabled" class="proxy-config-area">
<div class="form-group">
<label class="form-label">每个代理账号数</label>
<input
v-model.number="formData.accountsPerProxy"
class="form-input"
type="number"
min="1"
max="100"
step="1"
>
<div class="form-hint">
例如填 5:30 个账号会稳定分成 6 组,每组使用独立代理连接池;定时抢购会按组分别提取并校验代理。
</div>
</div>
<!-- 静态/当前代理 -->
<div class="form-group">
<label class="form-label">当前代理地址 (IP:端口 或 隧道)</label>
@@ -194,13 +221,13 @@ defineExpose({ open, close })
<!-- 代理提取 API -->
<div class="form-group">
<label class="form-label">代理 API 提取链接 (星空/快代理/品赞等动态 IP)</label>
<label class="form-label">代理 API 提取链接 (品赞 / 通用动态 IP)</label>
<div class="input-with-button">
<input
v-model="formData.proxyApi"
class="form-input"
type="text"
placeholder="粘贴代理平台的提取 URL,例如 http://api2.xkdaili.com/tools/XApi.ashx?..."
placeholder="粘贴品赞 core-extract 或其它代理平台提取 URL"
>
<button
type="button"
@@ -212,17 +239,61 @@ defineExpose({ open, close })
<span v-else>🔄 提取并填入</span>
</button>
</div>
<div class="form-hint">支持返回 JSON 或纯文本格式的代理 API,点击后自动拉取并填入上方地址</div>
<div class="form-hint">
品赞会自动识别并请求当前分组所需数量;账号密码模式请生成带 mode=auth 的 API,账号密码会写入代理 URL。注意:账号密码授权仍需先将执行提取的本机 IP 加入品赞白名单,白名单只用于提取 API。
</div>
</div>
<!-- 定时抢购前自动拉取最新代理 -->
<div class="form-group">
<label class="checkbox-row">
<input v-model="formData.autoFetchProxyOnSchedule" type="checkbox">
<span class="checkbox-text">定时任务触发前 15 秒自动提取最新 IP</span>
<span class="checkbox-text">定时任务触发前 30 秒自动提取并校验分组 IP</span>
</label>
<div class="form-hint">适用于 3~5 分钟短效代理:定时抢购触发前自动刷新 IP,确保开抢时代理不过期且连接最新</div>
<div class="form-hint">
按“每组账号数”提取对应数量的代理,先测斗鱼时间接口再启用;建议使用 10 分钟以上时长,覆盖抢购与最长 180 秒支付。
</div>
</div>
<div class="form-group">
<label class="checkbox-row">
<input v-model="formData.proxyAutoWhitelist" type="checkbox">
<span class="checkbox-text">提取前自动添加本机公网 IP 白名单</span>
</label>
<div class="form-hint">
先探测一次提取,从品赞返回中读取它实际看到的来源 IP,再自动校验/添加白名单;需要填写品赞登录密码和账号安全页的签名密钥。
</div>
</div>
<template v-if="formData.proxyAutoWhitelist">
<div class="form-group">
<label class="form-label">品赞登录密码</label>
<input
v-model="formData.proxyWhitelistPassword"
class="form-input"
type="password"
autocomplete="off"
placeholder="仅用于本地生成白名单签名"
>
</div>
<div class="form-group">
<label class="form-label">白名单 AES 签名密钥</label>
<input
v-model="formData.proxyWhitelistSignKey"
class="form-input"
type="password"
autocomplete="off"
placeholder="品赞账号安全页显示的签名密钥"
>
<div class="form-hint">密钥保存在本实例配置中,不会写入日志。</div>
</div>
<div class="form-group">
<label class="checkbox-row">
<input v-model="formData.proxyWhitelistReplace" type="checkbox">
<span class="checkbox-text">白名单已满时替换最早添加的 IP</span>
</label>
</div>
</template>
</div>
</div>
+18 -3
View File
@@ -23,6 +23,11 @@ export interface Settings {
proxyUrl: string
proxyApi: string
autoFetchProxyOnSchedule: boolean
accountsPerProxy: number
proxyAutoWhitelist: boolean
proxyWhitelistReplace: boolean
proxyWhitelistPassword: string
proxyWhitelistSignKey: string
}
const defaultRetryStages: RetryStage[] = [
@@ -46,7 +51,12 @@ const settings = ref<Settings>({
proxyEnabled: false,
proxyUrl: '',
proxyApi: '',
autoFetchProxyOnSchedule: false
autoFetchProxyOnSchedule: false,
accountsPerProxy: 5,
proxyAutoWhitelist: false,
proxyWhitelistReplace: true,
proxyWhitelistPassword: '',
proxyWhitelistSignKey: ''
})
export function useSettings() {
@@ -71,7 +81,12 @@ export function useSettings() {
proxyEnabled: !!cfg.proxyEnabled,
proxyUrl: cfg.proxyUrl || '',
proxyApi: cfg.proxyApi || '',
autoFetchProxyOnSchedule: !!cfg.autoFetchProxyOnSchedule
autoFetchProxyOnSchedule: !!cfg.autoFetchProxyOnSchedule,
accountsPerProxy: cfg.accountsPerProxy || 5,
proxyAutoWhitelist: !!cfg.proxyAutoWhitelist,
proxyWhitelistReplace: !!cfg.proxyWhitelistReplace,
proxyWhitelistPassword: cfg.proxyWhitelistPassword || '',
proxyWhitelistSignKey: cfg.proxyWhitelistSignKey || ''
}
} catch (e: any) {
showToast('读取设置失败', e.message, 'error')
@@ -86,7 +101,7 @@ export function useSettings() {
showToast('设置已保存', '配置已生效', 'success')
return true
} else {
showToast('保存失败', '后端未返回成功状态', 'error')
showToast('保存失败', d.error || '后端未返回成功状态', 'error')
return false
}
} catch (e: any) {
+7 -3
View File
@@ -180,13 +180,17 @@ func (a *App) Startup(ctx context.Context) {
logger.LogEvent("活动", "warn", "配置中的活动 %q 未识别,已回退到默认活动 %s", a.cfg.Activity, act.DisplayName)
}
proxyURL := a.effectiveProxyURL(a.cfg)
a.client = client.New(act, a.cfg.TimeAPIURL, a.getTrace(), proxyURL)
a.client, _ = client.NewWithProxy(act, a.cfg.TimeAPIURL, a.getTrace(), proxyURL)
if a.client == nil {
logger.LogEvent("代理", "error", "启动时代理配置无效,已回退直连: %s", client.MaskProxyURL(proxyURL))
a.client, _ = client.NewWithProxy(act, a.cfg.TimeAPIURL, a.getTrace(), "")
}
if proxyURL != "" {
logger.LogEvent("代理", "info", "已启用代理: %s", proxyURL)
logger.LogEvent("代理", "info", "已启用代理: %s", client.MaskProxyURL(proxyURL))
}
a.font = imgpkg.LoadFont(a.paths.FontFiles)
a.scheduler = seckill.New(a.client, a.paths, a.font, a.cfg.Concurrency, a.cfg.Attempts, a.cfg.RetryStages, a.cfg.SeckillMode)
a.scheduler.UpdateProxySettings(a.cfg.ProxyEnabled, a.cfg.ProxyURL, a.cfg.ProxyAPI, a.cfg.AutoFetchProxyOnSchedule)
a.scheduler.UpdateProxySettings(a.cfg)
a.scheduler.SetStateChangeCallback(func(state string) {
runtime.EventsEmit(a.ctx, seckillStateEvent, state)
})
+49 -7
View File
@@ -1,6 +1,8 @@
package app
import (
"strings"
"douyu_go/internal/activity"
"douyu_go/internal/client"
"douyu_go/internal/config"
@@ -38,12 +40,22 @@ func (a *App) SwitchActivity(id string) types.SwitchActivityResult {
if cfg.TimeAPIURL == "" {
cfg.TimeAPIURL = constants.DefaultTimeAPI
}
proxyURL := a.effectiveProxyURL(cfg)
if proxyURL != "" {
if _, err := client.ParseProxyURL(proxyURL); err != nil {
return types.SwitchActivityResult{Error: "代理地址无效: " + err.Error()}
}
}
if err := config.SaveConfig(a.paths.ConfigFile, cfg); err != nil {
logger.LogEvent("活动", "error", "切换活动配置保存失败: %v", err)
return types.SwitchActivityResult{Error: "配置保存失败: " + err.Error()}
}
a.setCfg(cfg)
newClient := client.New(act, cfg.TimeAPIURL, a.getTrace(), a.effectiveProxyURL(cfg))
newClient, clientErr := client.NewWithProxy(act, cfg.TimeAPIURL, a.getTrace(), proxyURL)
if clientErr != nil {
logger.LogEvent("代理", "error", "切换活动时代理地址无效: %v", clientErr)
return types.SwitchActivityResult{Error: clientErr.Error()}
}
a.setClient(newClient)
if !a.scheduler.UpdateClient(newClient) {
logger.LogEvent("活动", "warn", "抢购任务正在运行,调度器客户端将在下次抢购时生效")
@@ -61,7 +73,7 @@ func (a *App) GetSettings() types.Config {
}
// SaveSettings 保存配置并运行时生效。
func (a *App) SaveSettings(cfg types.Config) types.SimpleResult {
func (a *App) SaveSettings(cfg types.Config) types.ErrorResult {
oldCfg := a.getCfg()
if cfg.Activity == "" {
cfg.Activity = oldCfg.Activity
@@ -87,17 +99,43 @@ func (a *App) SaveSettings(cfg types.Config) types.SimpleResult {
if cfg.Concurrency <= 0 {
cfg.Concurrency = constants.DefaultConcurrency
}
if cfg.AccountsPerProxy <= 0 {
cfg.AccountsPerProxy = constants.DefaultAccountsPerProxy
}
if cfg.AccountsPerProxy > constants.MaxAccountsPerProxy {
cfg.AccountsPerProxy = constants.MaxAccountsPerProxy
}
if cfg.Attempts <= 0 {
cfg.Attempts = constants.DefaultAttempts
}
if cfg.LogLevel == "" {
cfg.LogLevel = "info"
}
if proxyURL := a.effectiveProxyURL(cfg); proxyURL != "" {
if _, err := client.ParseProxyURL(proxyURL); err != nil {
logger.LogEvent("代理", "warn", "设置保存失败,代理地址无效: %v", err)
return types.ErrorResult{Error: "代理地址无效: " + err.Error()}
}
}
if cfg.ProxyAutoWhitelist {
if strings.TrimSpace(cfg.ProxyAPI) == "" {
return types.ErrorResult{Error: "自动白名单需要填写代理提取 API"}
}
if strings.TrimSpace(cfg.ProxyWhitelistPassword) == "" ||
strings.TrimSpace(cfg.ProxyWhitelistSignKey) == "" {
return types.ErrorResult{Error: "自动白名单需要填写登录密码和 AES 签名密钥"}
}
switch len([]byte(strings.TrimSpace(cfg.ProxyWhitelistSignKey))) {
case 16, 24, 32:
default:
return types.ErrorResult{Error: "AES 签名密钥长度应为 16、24 或 32 字节"}
}
}
// 写入文件
if err := config.SaveConfig(a.paths.ConfigFile, cfg); err != nil {
logger.LogEvent("配置", "error", "配置保存失败: %v", err)
return types.SimpleResult{}
return types.ErrorResult{Error: "配置保存失败: " + err.Error()}
}
traceChanged := cfg.TraceEnabled != oldCfg.TraceEnabled || cfg.TraceMaxBody != oldCfg.TraceMaxBody
@@ -111,17 +149,21 @@ func (a *App) SaveSettings(cfg types.Config) types.SimpleResult {
a.setCfg(cfg)
logger.SetMinLevel(cfg.LogLevel)
a.scheduler.UpdateSettings(cfg.Concurrency, cfg.Attempts, cfg.RetryStages, cfg.SeckillMode)
a.scheduler.UpdateProxySettings(cfg.ProxyEnabled, cfg.ProxyURL, cfg.ProxyAPI, cfg.AutoFetchProxyOnSchedule)
a.scheduler.UpdateProxySettings(cfg)
if clientChanged {
proxyURL := a.effectiveProxyURL(cfg)
newClient := client.New(act, cfg.TimeAPIURL, a.getTrace(), proxyURL)
newClient, clientErr := client.NewWithProxy(act, cfg.TimeAPIURL, a.getTrace(), proxyURL)
if clientErr != nil {
logger.LogEvent("代理", "error", "代理配置无效,设置未应用: %v", clientErr)
return types.ErrorResult{Error: "代理配置无效: " + clientErr.Error()}
}
a.setClient(newClient)
if !a.scheduler.UpdateClient(newClient) {
logger.LogEvent("配置", "warn", "抢购任务正在运行,客户端配置将在下次抢购时完整生效")
}
if proxyChanged {
if proxyURL != "" {
logger.LogEvent("代理", "success", "代理已生效: %s", proxyURL)
logger.LogEvent("代理", "success", "代理已生效: %s", client.MaskProxyURL(proxyURL))
} else {
logger.LogEvent("代理", "info", "代理已停用,恢复直连")
}
@@ -135,5 +177,5 @@ func (a *App) SaveSettings(cfg types.Config) types.SimpleResult {
logger.LogEvent("配置", "success", "配置已保存并生效: 日志等级=%s 并发=%d 重试=%d 重试阶段=%d 模式=%d trace=%v 代理=%v",
cfg.LogLevel, cfg.Concurrency, cfg.Attempts, len(cfg.RetryStages), cfg.SeckillMode, cfg.TraceEnabled, cfg.ProxyEnabled)
return types.SimpleResult{Success: true}
return types.ErrorResult{Success: true}
}
+41 -4
View File
@@ -1,8 +1,13 @@
package app
import (
"context"
"time"
"douyu_go/internal/client"
"douyu_go/internal/constants"
"douyu_go/internal/logger"
proxyprovider "douyu_go/internal/proxy"
"douyu_go/internal/types"
)
@@ -19,14 +24,46 @@ func (a *App) TestProxy(proxyURL string) types.ProxyTestResult {
// FetchProxy 从指定 API 提取一个代理 IP。
func (a *App) FetchProxy(apiURL string) types.ProxyFetchResult {
res, err := client.FetchProxyFromAPI(apiURL)
ctx, cancel := context.WithTimeout(context.Background(), 30*time.Second)
defer cancel()
service := proxyprovider.NewService()
cfg := a.getCfg()
endpoints, err := service.ExtractWithWhitelist(ctx, apiURL, 1, proxyprovider.WhitelistConfig{
APIURL: apiURL,
LoginPassword: cfg.ProxyWhitelistPassword,
SignKey: cfg.ProxyWhitelistSignKey,
ReplaceOldest: cfg.ProxyWhitelistReplace,
AutoWhitelist: cfg.ProxyAutoWhitelist,
})
if err != nil {
logger.LogEvent("代理", "warn", "代理提取失败: %v", err)
return types.ProxyFetchResult{Error: err.Error()}
}
if len(endpoints) == 0 {
return types.ProxyFetchResult{
Success: false,
Error: err.Error(),
Error: "代理 API 未返回有效地址",
}
}
logger.LogEvent("代理", "success", "代理提取成功: %s (IP=%s 端口=%s 地区=%s)", res.ProxyURL, res.IP, res.Port, res.Region)
return *res
endpoint := endpoints[0]
expiresAt := int64(0)
if !endpoint.ExpiresAt.IsZero() {
expiresAt = endpoint.ExpiresAt.UnixMilli()
}
expiresLabel := "未知"
if !endpoint.ExpiresAt.IsZero() {
expiresLabel = endpoint.ExpiresAt.In(constants.CST).Format("2006-01-02 15:04:05")
}
logger.LogEvent("代理", "success", "代理提取成功: provider=%s %s (IP=%s 端口=%s 地区=%s 过期=%s)",
endpoint.Provider, client.MaskProxyURL(endpoint.URL), endpoint.IP, endpoint.Port, endpoint.Region,
expiresLabel)
return types.ProxyFetchResult{
Success: true,
ProxyURL: endpoint.URL,
IP: endpoint.IP,
Port: endpoint.Port,
Region: endpoint.Region,
Provider: endpoint.Provider,
ExpiresAt: expiresAt,
}
}
+12
View File
@@ -22,6 +22,12 @@ func (a *App) StartSeckill(accountProducts map[string]string) types.MessageResul
if len(accountProducts) == 0 {
return types.MessageResult{Error: "请选择账号和商品"}
}
if a.scheduler.IsRunning() {
return types.MessageResult{Error: "抢购任务正在运行,请勿重复提交"}
}
if err := a.scheduler.PrepareProxyGroups(accountProducts); err != nil {
return types.MessageResult{Error: "代理准备失败: " + err.Error()}
}
if !a.scheduler.TryStartRun() {
return types.MessageResult{Error: "抢购任务正在运行,请勿重复提交"}
}
@@ -65,6 +71,12 @@ func (a *App) startXiaodianSeckill(req types.FixedSeckillRequest, message string
if req.DelayMs < 0 || req.DelayMs > 60000 {
return types.MessageResult{Error: "抢购间隔需在 0-60000 毫秒之间"}
}
if a.scheduler.IsRunning() {
return types.MessageResult{Error: "抢购任务正在运行,请勿重复提交"}
}
if err := a.scheduler.PrepareProxyGroups(req.AccountProducts); err != nil {
return types.MessageResult{Error: "代理准备失败: " + err.Error()}
}
if !a.scheduler.TryStartRun() {
return types.MessageResult{Error: "抢购任务正在运行,请勿重复提交"}
}
+38 -17
View File
@@ -56,10 +56,23 @@ type xiaodianEmbedCacheItem struct {
expiresAt time.Time
}
// New 创建斗鱼客户端。act 决定使用哪个活动的接口参数;
// timeAPIURL 为空时回退到 constants.DefaultTimeAPI(全局共享的服务端时间接口);
// proxyURL 为可选参数,为空时直连,非空时配置 http/https/socks5 代理。
// New 创建斗鱼客户端。proxyURL 为可选参数,为空时直连。
// 代理地址无效时会记录错误并回退直连,调用方需要严格校验时应使用 NewWithProxy。
func New(act *activity.Activity, timeAPIURL string, tr *trace.Recorder, proxyURL ...string) *DouyuClient {
var effectiveProxy string
if len(proxyURL) > 0 {
effectiveProxy = proxyURL[0]
}
cli, err := NewWithProxy(act, timeAPIURL, tr, effectiveProxy)
if err != nil {
logger.LogEvent("代理", "error", "代理地址无效,客户端已回退直连: %v", err)
cli, _ = NewWithProxy(act, timeAPIURL, tr, "")
}
return cli
}
// NewWithProxy 创建斗鱼客户端并严格校验代理地址。
func NewWithProxy(act *activity.Activity, timeAPIURL string, tr *trace.Recorder, proxyURL string) (*DouyuClient, error) {
if act == nil {
act = activity.Default()
}
@@ -69,20 +82,10 @@ func New(act *activity.Activity, timeAPIURL string, tr *trace.Recorder, proxyURL
if tr == nil {
tr = &trace.Recorder{}
}
var effectiveProxy string
if len(proxyURL) > 0 {
effectiveProxy = proxyURL[0]
}
trsp := &http.Transport{
MaxConnsPerHost: 120,
MaxIdleConns: 120,
MaxIdleConnsPerHost: 80,
IdleConnTimeout: 30 * time.Second,
}
if effectiveProxy != "" {
if u, err := ParseProxyURL(effectiveProxy); err == nil && u != nil {
trsp.Proxy = http.ProxyURL(u)
}
effectiveProxy := strings.TrimSpace(proxyURL)
trsp, err := newHTTPTransport(effectiveProxy)
if err != nil {
return nil, err
}
return &DouyuClient{
Activity: act,
@@ -94,7 +97,25 @@ func New(act *activity.Activity, timeAPIURL string, tr *trace.Recorder, proxyURL
Timeout: 8 * time.Second,
Transport: trsp,
},
}, nil
}
func newHTTPTransport(proxyURL string) (*http.Transport, error) {
trsp := &http.Transport{
MaxConnsPerHost: 120,
MaxIdleConns: 120,
MaxIdleConnsPerHost: 80,
IdleConnTimeout: 30 * time.Second,
}
if strings.TrimSpace(proxyURL) == "" {
return trsp, nil
}
u, err := ParseProxyURL(proxyURL)
if err != nil {
return nil, err
}
trsp.Proxy = http.ProxyURL(u)
return trsp, nil
}
// SetProxy 运行时更新客户端使用的代理。
+9 -125
View File
@@ -2,7 +2,6 @@ package client
import (
"context"
"encoding/json"
"errors"
"fmt"
"io"
@@ -15,8 +14,6 @@ import (
"douyu_go/internal/types"
)
var ipPortRegex = regexp.MustCompile(`(\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}):(\d{2,5})`)
// ParseProxyURL 解析并规范化代理字符串为 *url.URL。
// 支持 http://, https://, socks5://;若未带协议头,默认补齐 http://。
func ParseProxyURL(proxyStr string) (*url.URL, error) {
@@ -41,133 +38,20 @@ func ParseProxyURL(proxyStr string) (*url.URL, error) {
return u, nil
}
// FetchProxyFromAPI 从指定的代理提供商 API 拉取一个代理 IP。
// 兼容星空代理等常见 JSON 格式,以及纯文本 IP:Port 格式。
func FetchProxyFromAPI(apiURL string) (*types.ProxyFetchResult, error) {
trimmed := strings.TrimSpace(apiURL)
// MaskProxyURL 隐藏代理地址中的用户名和密码,供日志与界面提示使用。
func MaskProxyURL(proxyStr string) string {
trimmed := strings.TrimSpace(proxyStr)
if trimmed == "" {
return nil, errors.New("代理提取 API 地址为空")
return ""
}
if !strings.HasPrefix(trimmed, "http://") && !strings.HasPrefix(trimmed, "https://") {
return nil, errors.New("代理提取 API 地址必须以 http:// 或 https:// 开头")
}
req, err := http.NewRequestWithContext(context.Background(), http.MethodGet, trimmed, nil)
u, err := ParseProxyURL(trimmed)
if err != nil {
return nil, fmt.Errorf("创建提取请求失败: %w", err)
return "<invalid-proxy>"
}
req.Header.Set("User-Agent", "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36")
client := &http.Client{
Timeout: 8 * time.Second,
if u.User != nil {
u.User = nil
}
resp, err := client.Do(req)
if err != nil {
return nil, fmt.Errorf("请求代理 API 失败: %w", err)
}
defer resp.Body.Close()
bodyBytes, err := io.ReadAll(io.LimitReader(resp.Body, 128*1024))
if err != nil {
return nil, fmt.Errorf("读取代理 API 响应失败: %w", err)
}
bodyStr := strings.TrimSpace(string(bodyBytes))
if bodyStr == "" {
return nil, errors.New("代理 API 返回内容为空")
}
// 尝试优先按照 JSON 结构体解析
var jsonObj struct {
Status int `json:"status"`
Code *int `json:"code"`
Info string `json:"info"`
Msg string `json:"msg"`
Data json.RawMessage `json:"data"`
}
if jsonErr := json.Unmarshal([]byte(bodyStr), &jsonObj); jsonErr == nil {
// 判断是否有错误提示
errInfo := jsonObj.Info
if errInfo == "" {
errInfo = jsonObj.Msg
}
// 星空代理成功状态为 100,其它平台通用成功状态为 0
hasSuccessStatus := jsonObj.Status == 100 || (jsonObj.Code != nil && *jsonObj.Code == 0)
hasErrorStatus := (jsonObj.Status != 0 && jsonObj.Status != 100) || (jsonObj.Code != nil && *jsonObj.Code != 0)
if hasErrorStatus && errInfo != "" {
return nil, fmt.Errorf("代理平台返回: %s (状态码=%d)", errInfo, jsonObj.Status)
}
if len(jsonObj.Data) > 0 {
// 1. 尝试解析为对象数组: [{"ip":"...", "port":"...", "prov":"...", "city":"..."}]
type nodeItem struct {
IP string `json:"ip"`
Port any `json:"port"`
Prov string `json:"prov"`
City string `json:"city"`
}
var items []nodeItem
if err := json.Unmarshal(jsonObj.Data, &items); err == nil && len(items) > 0 {
ip := strings.TrimSpace(items[0].IP)
portStr := fmt.Sprintf("%v", items[0].Port)
region := strings.TrimSpace(items[0].Prov + " " + items[0].City)
if ip != "" && portStr != "" {
return &types.ProxyFetchResult{
Success: true,
ProxyURL: fmt.Sprintf("http://%s:%s", ip, portStr),
IP: ip,
Port: portStr,
Region: region,
}, nil
}
}
// 2. 尝试解析为字符串数组: ["ip:port"]
var strArr []string
if err := json.Unmarshal(jsonObj.Data, &strArr); err == nil && len(strArr) > 0 {
if m := ipPortRegex.FindStringSubmatch(strArr[0]); len(m) == 3 {
return &types.ProxyFetchResult{
Success: true,
ProxyURL: fmt.Sprintf("http://%s:%s", m[1], m[2]),
IP: m[1],
Port: m[2],
}, nil
}
}
// 3. 尝试解析为单字符串
var singleStr string
if err := json.Unmarshal(jsonObj.Data, &singleStr); err == nil {
if m := ipPortRegex.FindStringSubmatch(singleStr); len(m) == 3 {
return &types.ProxyFetchResult{
Success: true,
ProxyURL: fmt.Sprintf("http://%s:%s", m[1], m[2]),
IP: m[1],
Port: m[2],
}, nil
}
}
}
if !hasSuccessStatus && errInfo != "" {
return nil, fmt.Errorf("代理平台提示: %s", errInfo)
}
}
// 兜底正则提取 IP:Port 文本格式
matches := ipPortRegex.FindStringSubmatch(bodyStr)
if len(matches) == 3 {
return &types.ProxyFetchResult{
Success: true,
ProxyURL: fmt.Sprintf("http://%s:%s", matches[1], matches[2]),
IP: matches[1],
Port: matches[2],
}, nil
}
return nil, fmt.Errorf("未能从 API 返回中识别出有效 IP:端口,原始返回: %s", truncateString(bodyStr, 120))
return u.String()
}
// TestProxyConnection 测试指定代理的连通性、出口公网 IP 与往返延迟。
+4 -56
View File
@@ -1,9 +1,6 @@
package client
import (
"fmt"
"net/http"
"net/http/httptest"
"testing"
)
@@ -44,58 +41,9 @@ func TestParseProxyURL(t *testing.T) {
}
}
func TestFetchProxyFromAPI_XKDaili(t *testing.T) {
// 模拟星空代理正常返回
ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Type", "application/json; charset=utf-8")
fmt.Fprint(w, `{ "status":100 ,"info":"成功" , "data": [{"ip": "115.230.134.174", "port": "40014", "prov": "浙江", "city": "嘉兴"}]}`)
}))
defer ts.Close()
res, err := FetchProxyFromAPI(ts.URL)
if err != nil {
t.Fatalf("FetchProxyFromAPI failed: %v", err)
}
if res.IP != "115.230.134.174" || res.Port != "40014" {
t.Fatalf("unexpected res: %+v", res)
}
if res.ProxyURL != "http://115.230.134.174:40014" {
t.Fatalf("unexpected proxy url: %s", res.ProxyURL)
}
if res.Region != "浙江 嘉兴" {
t.Fatalf("unexpected region: %s", res.Region)
}
}
func TestFetchProxyFromAPI_Error(t *testing.T) {
// 模拟星空代理未加白名单报错
ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Type", "application/json; charset=utf-8")
fmt.Fprint(w, `{ "status":211 , "info":"当前IP不在白名单内" }`)
}))
defer ts.Close()
_, err := FetchProxyFromAPI(ts.URL)
if err == nil {
t.Fatal("expected error, got nil")
}
if err.Error() != "代理平台返回: 当前IP不在白名单内 (状态码=211)" {
t.Fatalf("unexpected error string: %v", err)
}
}
func TestFetchProxyFromAPI_Text(t *testing.T) {
// 模拟纯文本返回
ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
fmt.Fprint(w, "221.122.91.45:3128\r\n")
}))
defer ts.Close()
res, err := FetchProxyFromAPI(ts.URL)
if err != nil {
t.Fatalf("FetchProxyFromAPI failed: %v", err)
}
if res.ProxyURL != "http://221.122.91.45:3128" {
t.Fatalf("unexpected proxy url: %s", res.ProxyURL)
func TestMaskProxyURL(t *testing.T) {
got := MaskProxyURL("http://user:pass@127.0.0.1:8888")
if got != "http://127.0.0.1:8888" {
t.Fatalf("MaskProxyURL() = %q", got)
}
}
+11
View File
@@ -174,6 +174,7 @@ func defaultConfig() types.Config {
RetryStages: defaultRetryStages(),
SeckillMode: types.SeckillModeAggressive,
XiaodianPageSize: 200,
AccountsPerProxy: constants.DefaultAccountsPerProxy,
}
}
@@ -228,11 +229,21 @@ func LoadConfig(path string) types.Config {
if len(cfg.RetryStages) == 0 {
cfg.RetryStages = defaultRetryStages()
}
normalizeProxyConfig(&cfg)
normalizeXiaodianConfig(&cfg)
logger.LogEvent("配置", "success", "配置加载完成: %s", path)
return cfg
}
func normalizeProxyConfig(cfg *types.Config) {
if cfg.AccountsPerProxy <= 0 {
cfg.AccountsPerProxy = constants.DefaultAccountsPerProxy
}
if cfg.AccountsPerProxy > constants.MaxAccountsPerProxy {
cfg.AccountsPerProxy = constants.MaxAccountsPerProxy
}
}
// normalizeXiaodianConfig 约束和平小店分页参数,接口单页最大为 200。
func normalizeXiaodianConfig(cfg *types.Config) {
if cfg.XiaodianPageSize <= 0 || cfg.XiaodianPageSize > 200 {
+18
View File
@@ -4,6 +4,8 @@ import (
"os"
"path/filepath"
"testing"
"douyu_go/internal/constants"
)
func TestMigrateLegacyFile(t *testing.T) {
@@ -110,3 +112,19 @@ func TestExplicitLogDirOverridesWailsDev(t *testing.T) {
t.Fatalf("显式日志目录 = %q, want %q", paths.LogDir, customLogDir)
}
}
func TestLoadConfigNormalizesAccountsPerProxy(t *testing.T) {
path := filepath.Join(t.TempDir(), "config.json")
if err := os.WriteFile(path, []byte(`{"accountsPerProxy":0}`), 0600); err != nil {
t.Fatal(err)
}
if got := LoadConfig(path).AccountsPerProxy; got != constants.DefaultAccountsPerProxy {
t.Fatalf("AccountsPerProxy=%d, want %d", got, constants.DefaultAccountsPerProxy)
}
if err := os.WriteFile(path, []byte(`{"accountsPerProxy":999}`), 0600); err != nil {
t.Fatal(err)
}
if got := LoadConfig(path).AccountsPerProxy; got != constants.MaxAccountsPerProxy {
t.Fatalf("AccountsPerProxy=%d, want %d", got, constants.MaxAccountsPerProxy)
}
}
+7 -5
View File
@@ -9,11 +9,13 @@ const Version = "0.4.0"
var BuildTime = "unknown"
const (
BaseURL = "https://www.douyu.com"
UserAgent = "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/149.0.0.0 Safari/537.36"
DefaultAttempts = 2
DefaultConcurrency = 150
ProductCacheTTL = 30 * time.Second
BaseURL = "https://www.douyu.com"
UserAgent = "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/149.0.0.0 Safari/537.36"
DefaultAttempts = 2
DefaultConcurrency = 150
DefaultAccountsPerProxy = 5
MaxAccountsPerProxy = 100
ProductCacheTTL = 30 * time.Second
DefaultAppDataDirName = "douyu-go"
DefaultConfigFile = "config.json"
+250
View File
@@ -0,0 +1,250 @@
package proxy
import (
"encoding/json"
"errors"
"fmt"
"net/url"
"regexp"
"strconv"
"strings"
"time"
)
var (
proxyTextRegex = regexp.MustCompile(`(?i)(?:(https?|socks5)://)?(?:[^@\s:/]+:[^@\s/]+@)?((?:\d{1,3}\.){3}\d{1,3}):(\d{2,5})`)
)
// GenericProvider 兼容常见 JSON 与纯文本 IP:端口 返回。
type GenericProvider struct{}
func NewGenericProvider() *GenericProvider {
return &GenericProvider{}
}
func (p *GenericProvider) ID() string {
return "generic"
}
func (p *GenericProvider) Match(u *url.URL) bool {
return u != nil
}
func (p *GenericProvider) RequestURL(apiURL string, count int) (string, error) {
u, err := url.Parse(strings.TrimSpace(apiURL))
if err != nil {
return "", fmt.Errorf("代理 API 地址无效: %w", err)
}
query := u.Query()
changed := false
for _, key := range []string{"num", "count", "quantity"} {
if query.Has(key) {
query.Set(key, strconv.Itoa(count))
changed = true
}
}
if changed {
u.RawQuery = query.Encode()
}
return u.String(), nil
}
func (p *GenericProvider) Parse(apiURL string, body []byte) ([]Endpoint, error) {
text := strings.TrimSpace(string(body))
if text == "" {
return nil, errors.New("代理 API 返回内容为空")
}
protocol := protocolFromAPI(apiURL)
var payload struct {
Code *int `json:"code"`
Status int `json:"status"`
Info string `json:"info"`
Msg string `json:"msg"`
Message string `json:"message"`
Data json.RawMessage `json:"data"`
}
if json.Unmarshal(body, &payload) == nil {
message := firstNonEmpty(payload.Info, payload.Msg, payload.Message)
hasError := (payload.Code != nil && *payload.Code != 0) ||
(payload.Code == nil && payload.Status != 0 && payload.Status != 100 && payload.Status != 200)
if hasError {
if message == "" {
message = "代理平台返回错误"
}
return nil, &ProviderError{
Provider: "通用代理",
Message: message,
Fatal: isGenericFatalMessage(message),
}
}
if len(payload.Data) > 0 {
endpoints := parseEndpointJSON(payload.Data, protocol)
if len(endpoints) > 0 {
return endpoints, nil
}
}
}
endpoints := parseEndpointJSON(body, protocol)
if len(endpoints) > 0 {
return endpoints, nil
}
endpoints = parseEndpointText(text, protocol)
if len(endpoints) > 0 {
return endpoints, nil
}
return nil, fmt.Errorf("未能从代理 API 返回中识别出有效 IP:端口: %s", truncateText(text, 120))
}
func isGenericFatalMessage(message string) bool {
if strings.Contains(message, "提取频率过快") || strings.Contains(message, "请求过于频繁") {
return false
}
for _, hint := range []string{
"白名单", "认证", "鉴权", "密钥", "密匙", "套餐", "过期", "禁用", "余额", "未授权",
} {
if strings.Contains(strings.ToLower(message), strings.ToLower(hint)) {
return true
}
}
return false
}
func parseEndpointJSON(raw []byte, protocol string) []Endpoint {
var value any
if json.Unmarshal(raw, &value) != nil {
return nil
}
return collectJSONEndpoints(value, protocol)
}
func collectJSONEndpoints(value any, protocol string) []Endpoint {
switch typed := value.(type) {
case string:
return parseEndpointText(typed, protocol)
case []any:
var out []Endpoint
for _, item := range typed {
out = append(out, collectJSONEndpoints(item, protocol)...)
}
return out
case map[string]any:
if endpoints := endpointsFromMap(typed, protocol); len(endpoints) > 0 {
return endpoints
}
var out []Endpoint
for _, key := range []string{"data", "list", "items", "result", "results", "proxies"} {
if child, ok := typed[key]; ok {
out = append(out, collectJSONEndpoints(child, protocol)...)
}
}
return out
default:
return nil
}
}
func endpointsFromMap(item map[string]any, protocol string) []Endpoint {
ip := stringValue(item["ip"], item["host"], item["server"])
port := stringValue(item["port"], item["proxy_port"])
if ip == "" || port == "" {
return nil
}
itemProtocol := stringValue(item["protocol"], item["scheme"])
if itemProtocol == "" {
itemProtocol = protocol
}
username := stringValue(item["username"], item["user"], item["account"])
password := stringValue(item["password"], item["pass"])
expiresAt := timeValue(item["expired"], item["expire"], item["expiresAt"], item["expireAt"])
region := strings.TrimSpace(strings.Join([]string{
stringValue(item["prov"], item["province"]),
stringValue(item["city"]),
stringValue(item["region"]),
}, " "))
return []Endpoint{{
URL: buildProxyURL(itemProtocol, ip, port, username, password),
IP: ip,
Port: port,
Region: region,
Protocol: itemProtocol,
Username: username,
Password: password,
ExpiresAt: expiresAt,
}}
}
func parseEndpointText(text, protocol string) []Endpoint {
matches := proxyTextRegex.FindAllStringSubmatch(text, -1)
out := make([]Endpoint, 0, len(matches))
for _, match := range matches {
if len(match) != 4 {
continue
}
matchProtocol := match[1]
if matchProtocol == "" {
matchProtocol = protocol
}
ip, port := match[2], match[3]
out = append(out, Endpoint{
URL: buildProxyURL(matchProtocol, ip, port, "", ""),
IP: ip,
Port: port,
Protocol: matchProtocol,
})
}
return out
}
func stringValue(values ...any) string {
for _, value := range values {
switch typed := value.(type) {
case string:
if strings.TrimSpace(typed) != "" {
return strings.TrimSpace(typed)
}
case float64:
return fmt.Sprintf("%.0f", typed)
case json.Number:
return typed.String()
}
}
return ""
}
func timeValue(values ...any) time.Time {
for _, value := range values {
switch typed := value.(type) {
case float64:
return unixTimestamp(typed)
case json.Number:
number, err := typed.Float64()
if err == nil {
return unixTimestamp(number)
}
case string:
typed = strings.TrimSpace(typed)
if typed == "" {
continue
}
if number, err := strconv.ParseFloat(typed, 64); err == nil {
return unixTimestamp(number)
}
if parsed, err := time.Parse(time.RFC3339, typed); err == nil {
return parsed
}
}
}
return time.Time{}
}
func unixTimestamp(value float64) time.Time {
if value <= 0 {
return time.Time{}
}
if value >= 1e12 {
return time.UnixMilli(int64(value))
}
return time.Unix(int64(value), 0)
}
+293
View File
@@ -0,0 +1,293 @@
package proxy
import (
"context"
"crypto/aes"
"encoding/hex"
"encoding/json"
"fmt"
"io"
"net/http"
"net/url"
"strconv"
"strings"
"time"
)
// IPZanProvider 接入品赞 https://service.ipzan.com/core-extract。
type IPZanProvider struct {
baseURL string
now func() time.Time
}
func NewIPZanProvider() *IPZanProvider {
return &IPZanProvider{
baseURL: "https://service.ipzan.com",
now: time.Now,
}
}
func (p *IPZanProvider) ID() string {
return "ipzan"
}
func (p *IPZanProvider) Match(u *url.URL) bool {
return u != nil &&
strings.EqualFold(u.Hostname(), "service.ipzan.com") &&
strings.TrimRight(u.Path, "/") == "/core-extract"
}
func (p *IPZanProvider) RequestURL(apiURL string, count int) (string, error) {
u, err := url.Parse(strings.TrimSpace(apiURL))
if err != nil {
return "", fmt.Errorf("品赞 API 地址无效: %w", err)
}
query := u.Query()
query.Set("num", strconv.Itoa(count))
u.RawQuery = query.Encode()
return u.String(), nil
}
func (p *IPZanProvider) Parse(apiURL string, body []byte) ([]Endpoint, error) {
text := strings.TrimSpace(string(body))
if text == "" {
return nil, &ProviderError{Provider: "品赞", Message: "API 返回内容为空", Fatal: false}
}
var payload struct {
Code *int `json:"code"`
Status int `json:"status"`
Message string `json:"message"`
Info string `json:"info"`
Msg string `json:"msg"`
Data json.RawMessage `json:"data"`
}
if json.Unmarshal(body, &payload) == nil {
if payload.Code != nil && *payload.Code != 0 {
message := firstNonEmpty(payload.Message, payload.Info, payload.Msg, "品赞返回未知错误")
return nil, &ProviderError{
Provider: "品赞",
Message: fmt.Sprintf("%s (code=%d)", message, *payload.Code),
Fatal: isIPZanFatalMessage(message),
}
}
if len(payload.Data) > 0 {
endpoints := parseEndpointJSON(payload.Data, protocolFromAPI(apiURL))
if len(endpoints) > 0 {
return endpoints, nil
}
}
}
endpoints := parseEndpointText(text, protocolFromAPI(apiURL))
if len(endpoints) == 0 {
return nil, &ProviderError{
Provider: "品赞",
Message: "未能识别有效 IP:端口,原始返回: " + truncateText(text, 120),
Fatal: false,
}
}
return endpoints, nil
}
func (p *IPZanProvider) EnsureWhitelist(ctx context.Context, httpClient *http.Client, cfg WhitelistConfig) error {
if strings.TrimSpace(cfg.IP) == "" {
return &ProviderError{Provider: "品赞", Message: "自动白名单缺少公网 IP", Fatal: true}
}
apiURL, err := url.Parse(strings.TrimSpace(cfg.APIURL))
if err != nil {
return fmt.Errorf("品赞 API 地址无效: %w", err)
}
no := strings.TrimSpace(apiURL.Query().Get("no"))
secret := strings.TrimSpace(apiURL.Query().Get("secret"))
if no == "" || secret == "" {
return &ProviderError{Provider: "品赞", Message: "提取 API 缺少 no 或 secret,无法自动加白名单", Fatal: true}
}
if strings.TrimSpace(cfg.LoginPassword) == "" || strings.TrimSpace(cfg.SignKey) == "" {
return &ProviderError{Provider: "品赞", Message: "自动白名单需要品赞登录密码和签名密钥", Fatal: true}
}
exists, err := p.verifyWhitelist(ctx, httpClient, no, cfg.IP)
if err != nil {
return err
}
if exists {
return nil
}
return p.AddWhitelist(ctx, httpClient, cfg)
}
func (p *IPZanProvider) AddWhitelist(ctx context.Context, httpClient *http.Client, cfg WhitelistConfig) error {
if strings.TrimSpace(cfg.IP) == "" {
return &ProviderError{Provider: "品赞", Message: "自动白名单缺少公网 IP", Fatal: true}
}
apiURL, err := url.Parse(strings.TrimSpace(cfg.APIURL))
if err != nil {
return fmt.Errorf("品赞 API 地址无效: %w", err)
}
no := strings.TrimSpace(apiURL.Query().Get("no"))
secret := strings.TrimSpace(apiURL.Query().Get("secret"))
if no == "" || secret == "" {
return &ProviderError{Provider: "品赞", Message: "提取 API 缺少 no 或 secret,无法自动加白名单", Fatal: true}
}
if strings.TrimSpace(cfg.LoginPassword) == "" || strings.TrimSpace(cfg.SignKey) == "" {
return &ProviderError{Provider: "品赞", Message: "自动白名单需要品赞登录密码和签名密钥", Fatal: true}
}
sign, err := p.signWhitelist(cfg.LoginPassword, secret, cfg.SignKey)
if err != nil {
return err
}
form := url.Values{}
form.Set("no", no)
form.Set("sign", sign)
form.Set("ip", cfg.IP)
if cfg.ReplaceOldest {
form.Set("replace", "1")
} else {
form.Set("replace", "0")
}
req, err := http.NewRequestWithContext(
ctx,
http.MethodPost,
strings.TrimRight(p.baseURL, "/")+"/whiteList-add",
strings.NewReader(form.Encode()),
)
if err != nil {
return fmt.Errorf("创建品赞白名单请求失败: %w", err)
}
req.Header.Set("Content-Type", "application/x-www-form-urlencoded")
if err := p.doWhitelistRequest(req, httpClient, "添加白名单"); err != nil {
return err
}
return nil
}
func (p *IPZanProvider) verifyWhitelist(ctx context.Context, httpClient *http.Client, no, ip string) (bool, error) {
endpoint, err := url.Parse(strings.TrimRight(p.baseURL, "/") + "/whiteList-verify")
if err != nil {
return false, err
}
query := endpoint.Query()
query.Set("no", no)
query.Set("ip", ip)
endpoint.RawQuery = query.Encode()
req, err := http.NewRequestWithContext(ctx, http.MethodGet, endpoint.String(), nil)
if err != nil {
return false, fmt.Errorf("创建品赞白名单校验请求失败: %w", err)
}
body, err := p.doWhitelistRequestData(req, httpClient, "校验白名单")
if err != nil {
return false, err
}
var payload struct {
Code *int `json:"code"`
Data json.RawMessage `json:"data"`
}
if err := json.Unmarshal(body, &payload); err != nil {
return false, fmt.Errorf("解析品赞白名单校验响应失败: %w", err)
}
if payload.Code == nil || *payload.Code != 0 {
return false, &ProviderError{Provider: "品赞", Message: "白名单校验失败", Fatal: true}
}
var exists bool
if json.Unmarshal(payload.Data, &exists) == nil {
return exists, nil
}
var existsText string
if json.Unmarshal(payload.Data, &existsText) == nil {
return strings.EqualFold(strings.TrimSpace(existsText), "true"), nil
}
return false, fmt.Errorf("品赞白名单校验响应格式无法识别: %s", truncateText(string(body), 120))
}
func (p *IPZanProvider) signWhitelist(loginPassword, secret, signKey string) (string, error) {
key := []byte(signKey)
switch len(key) {
case 16, 24, 32:
default:
return "", &ProviderError{
Provider: "品赞",
Message: fmt.Sprintf("签名密钥长度无效: %d,应为 16/24/32 字节", len(key)),
Fatal: true,
}
}
plain := fmt.Sprintf("%s:%s:%d", loginPassword, secret, p.now().Unix())
block, err := aes.NewCipher(key)
if err != nil {
return "", fmt.Errorf("初始化 AES 签名失败: %w", err)
}
padding := block.BlockSize() - len(plain)%block.BlockSize()
data := make([]byte, 0, len(plain)+padding)
data = append(data, []byte(plain)...)
for i := 0; i < padding; i++ {
data = append(data, byte(padding))
}
encrypted := make([]byte, len(data))
for offset := 0; offset < len(data); offset += block.BlockSize() {
block.Encrypt(encrypted[offset:offset+block.BlockSize()], data[offset:offset+block.BlockSize()])
}
return hex.EncodeToString(encrypted), nil
}
func (p *IPZanProvider) doWhitelistRequest(req *http.Request, httpClient *http.Client, action string) error {
body, err := p.doWhitelistRequestData(req, httpClient, action)
if err != nil {
return err
}
var payload struct {
Code *int `json:"code"`
Message string `json:"message"`
}
if err := json.Unmarshal(body, &payload); err != nil {
return fmt.Errorf("解析品赞%s响应失败: %w", action, err)
}
if payload.Code == nil || *payload.Code != 0 {
message := firstNonEmpty(payload.Message, "品赞返回未知错误")
return &ProviderError{
Provider: "品赞",
Message: action + "失败: " + message,
Fatal: isIPZanFatalMessage(message),
}
}
return nil
}
func (p *IPZanProvider) doWhitelistRequestData(req *http.Request, httpClient *http.Client, action string) ([]byte, error) {
req.Header.Set("User-Agent", "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36")
resp, err := httpClient.Do(req)
if err != nil {
return nil, fmt.Errorf("请求品赞%s接口失败: %w", action, err)
}
defer resp.Body.Close()
body, err := io.ReadAll(io.LimitReader(resp.Body, 64*1024))
if err != nil {
return nil, fmt.Errorf("读取品赞%s响应失败: %w", action, err)
}
if resp.StatusCode < http.StatusOK || resp.StatusCode >= http.StatusMultipleChoices {
return nil, fmt.Errorf("品赞%s接口 HTTP %d: %s", action, resp.StatusCode, truncateText(string(body), 120))
}
return body, nil
}
func isIPZanFatalMessage(message string) bool {
if strings.Contains(message, "提取频率过快") {
return false
}
for _, hint := range []string{
"白名单", "secret", "密匙", "密钥", "套餐", "过期", "禁用", "余额", "身份未认证", "用户被禁用",
} {
if strings.Contains(strings.ToLower(message), strings.ToLower(hint)) {
return true
}
}
return false
}
func firstNonEmpty(values ...string) string {
for _, value := range values {
if strings.TrimSpace(value) != "" {
return strings.TrimSpace(value)
}
}
return ""
}
+289
View File
@@ -0,0 +1,289 @@
package proxy
import (
"context"
"errors"
"fmt"
"io"
"net/http"
"net/url"
"regexp"
"strings"
"time"
)
var whitelistIPRegex = regexp.MustCompile(`\b(?:\d{1,3}\.){3}\d{1,3}\b`)
// Endpoint 是代理提供商返回的一条可连接代理。
type Endpoint struct {
URL string
IP string
Port string
Region string
Protocol string
Username string
Password string
Provider string
ExpiresAt time.Time
}
// Provider 把不同代理平台的参数规则与响应格式隔离在各自实现中。
type Provider interface {
ID() string
Match(*url.URL) bool
RequestURL(apiURL string, count int) (string, error)
Parse(apiURL string, body []byte) ([]Endpoint, error)
}
// WhitelistConfig 是代理平台自动白名单所需参数。
type WhitelistConfig struct {
APIURL string
IP string
LoginPassword string
SignKey string
ReplaceOldest bool
AutoWhitelist bool
}
// WhitelistProvider 由支持自动白名单的代理平台实现。
type WhitelistProvider interface {
Provider
EnsureWhitelist(context.Context, *http.Client, WhitelistConfig) error
AddWhitelist(context.Context, *http.Client, WhitelistConfig) error
}
// ProviderError 是代理平台返回的业务错误。
type ProviderError struct {
Provider string
Message string
Fatal bool
}
func (e *ProviderError) Error() string {
if e == nil {
return ""
}
if e.Provider == "" {
return e.Message
}
return e.Provider + ": " + e.Message
}
// IsFatal 判断错误是否重试也不会恢复,例如密钥错误、白名单缺失或套餐过期。
func IsFatal(err error) bool {
var providerErr *ProviderError
return errors.As(err, &providerErr) && providerErr.Fatal
}
// Service 负责通用 HTTP 请求、provider 选择和结果去重。
type Service struct {
client *http.Client
providers []Provider
}
func NewService() *Service {
return NewServiceWithProviders(
&http.Client{Timeout: 10 * time.Second},
NewIPZanProvider(),
NewGenericProvider(),
)
}
func NewServiceWithProviders(client *http.Client, providers ...Provider) *Service {
if client == nil {
client = &http.Client{Timeout: 10 * time.Second}
}
return &Service{client: client, providers: providers}
}
// Extract 自动识别代理平台并请求所需数量;调用方可用剩余数量再次提取补足。
func (s *Service) Extract(ctx context.Context, apiURL string, count int) ([]Endpoint, error) {
if count <= 0 {
count = 1
}
u, err := url.Parse(strings.TrimSpace(apiURL))
if err != nil {
return nil, fmt.Errorf("代理 API 地址无效: %w", err)
}
if u.Scheme != "http" && u.Scheme != "https" {
return nil, errors.New("代理 API 地址必须以 http:// 或 https:// 开头")
}
provider := s.providerFor(u)
if provider == nil {
return nil, errors.New("没有匹配的代理提供商")
}
requestURL, err := provider.RequestURL(apiURL, count)
if err != nil {
return nil, err
}
body, err := s.fetch(ctx, requestURL)
if err != nil {
return nil, err
}
endpoints, err := provider.Parse(apiURL, body)
if err != nil {
return nil, err
}
for i := range endpoints {
if endpoints[i].Provider == "" {
endpoints[i].Provider = provider.ID()
}
}
endpoints = dedupeEndpoints(endpoints, count)
if len(endpoints) == 0 {
return nil, errors.New("代理 API 返回的代理均已过期或无效")
}
return endpoints, nil
}
func (s *Service) ExtractOne(ctx context.Context, apiURL string) (Endpoint, error) {
endpoints, err := s.Extract(ctx, apiURL, 1)
if err != nil {
return Endpoint{}, err
}
if len(endpoints) == 0 {
return Endpoint{}, errors.New("代理 API 未返回有效地址")
}
return endpoints[0], nil
}
// EnsureWhitelist 自动把指定公网 IP 加入当前 API 套餐白名单;已存在时直接跳过。
func (s *Service) EnsureWhitelist(ctx context.Context, cfg WhitelistConfig) error {
u, err := url.Parse(strings.TrimSpace(cfg.APIURL))
if err != nil {
return fmt.Errorf("代理 API 地址无效: %w", err)
}
provider := s.providerFor(u)
manager, ok := provider.(WhitelistProvider)
if !ok {
return errors.New("当前代理提供商不支持自动白名单")
}
return manager.EnsureWhitelist(ctx, s.client, cfg)
}
// AddWhitelist 直接添加白名单,不再调用独立的 verify 接口。
func (s *Service) AddWhitelist(ctx context.Context, cfg WhitelistConfig) error {
u, err := url.Parse(strings.TrimSpace(cfg.APIURL))
if err != nil {
return fmt.Errorf("代理 API 地址无效: %w", err)
}
provider := s.providerFor(u)
manager, ok := provider.(WhitelistProvider)
if !ok {
return errors.New("当前代理提供商不支持自动白名单")
}
return manager.AddWhitelist(ctx, s.client, cfg)
}
// ExtractWithWhitelist 先探测提取;若平台提示当前 IP 不在白名单,则直接使用错误中
// 平台实际看到的来源 IP,添加后重试,避免第三方公网 IP 探测与实际出口不一致。
func (s *Service) ExtractWithWhitelist(ctx context.Context, apiURL string, count int, cfg WhitelistConfig) ([]Endpoint, error) {
endpoints, err := s.Extract(ctx, apiURL, count)
if err == nil || !cfg.AutoWhitelist {
return endpoints, err
}
ip, ok := whitelistIPFromError(err)
if !ok {
return nil, err
}
cfg.APIURL = apiURL
cfg.IP = ip
if addErr := s.AddWhitelist(ctx, cfg); addErr != nil {
return nil, fmt.Errorf("品赞要求添加白名单 IP %s,自动添加失败: %w", ip, addErr)
}
return s.Extract(ctx, apiURL, count)
}
func whitelistIPFromError(err error) (string, bool) {
if err == nil || !strings.Contains(err.Error(), "白名单") {
return "", false
}
ip := whitelistIPRegex.FindString(err.Error())
return ip, ip != ""
}
func (s *Service) providerFor(u *url.URL) Provider {
for _, provider := range s.providers {
if provider != nil && provider.Match(u) {
return provider
}
}
return nil
}
func (s *Service) fetch(ctx context.Context, rawURL string) ([]byte, error) {
req, err := http.NewRequestWithContext(ctx, http.MethodGet, rawURL, nil)
if err != nil {
return nil, fmt.Errorf("创建代理 API 请求失败: %w", err)
}
req.Header.Set("User-Agent", "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36")
resp, err := s.client.Do(req)
if err != nil {
return nil, fmt.Errorf("请求代理 API 失败: %w", err)
}
defer resp.Body.Close()
body, err := io.ReadAll(io.LimitReader(resp.Body, 128*1024))
if err != nil {
return nil, fmt.Errorf("读取代理 API 响应失败: %w", err)
}
if resp.StatusCode < http.StatusOK || resp.StatusCode >= http.StatusMultipleChoices {
return nil, fmt.Errorf("代理 API HTTP %d: %s", resp.StatusCode, truncateText(string(body), 160))
}
return body, nil
}
func dedupeEndpoints(endpoints []Endpoint, limit int) []Endpoint {
out := make([]Endpoint, 0, min(len(endpoints), limit))
seen := make(map[string]struct{}, len(endpoints))
for _, endpoint := range endpoints {
endpoint.URL = strings.TrimSpace(endpoint.URL)
if endpoint.URL == "" {
continue
}
if !endpoint.ExpiresAt.IsZero() && !time.Now().Before(endpoint.ExpiresAt) {
continue
}
if _, ok := seen[endpoint.URL]; ok {
continue
}
seen[endpoint.URL] = struct{}{}
out = append(out, endpoint)
if len(out) >= limit {
break
}
}
return out
}
func buildProxyURL(protocol, ip, port, username, password string) string {
scheme := "http"
if protocol == "3" || strings.EqualFold(protocol, "socks5") || strings.EqualFold(protocol, "socket5") {
scheme = "socks5"
}
auth := ""
if username != "" || password != "" {
auth = url.UserPassword(username, password).String() + "@"
}
return fmt.Sprintf("%s://%s%s:%s", scheme, auth, strings.TrimSpace(ip), strings.TrimSpace(port))
}
func protocolFromAPI(apiURL string) string {
u, err := url.Parse(apiURL)
if err != nil {
return "1"
}
if value := strings.TrimSpace(u.Query().Get("protocol")); value != "" {
return value
}
return "1"
}
func truncateText(value string, maxLen int) string {
runes := []rune(strings.TrimSpace(value))
if len(runes) <= maxLen {
return string(runes)
}
return string(runes[:maxLen]) + "..."
}
+283
View File
@@ -0,0 +1,283 @@
package proxy
import (
"context"
"crypto/aes"
"encoding/hex"
"io"
"net/http"
"net/http/httptest"
"net/url"
"strings"
"testing"
"time"
)
func TestIPZanRequestURLSetsRequestedCount(t *testing.T) {
raw := "https://service.ipzan.com/core-extract?num=1&minute=10&protocol=1&secret=x"
got, err := NewIPZanProvider().RequestURL(raw, 6)
if err != nil {
t.Fatal(err)
}
if !strings.Contains(got, "num=6") || strings.Contains(got, "num=1") {
t.Fatalf("RequestURL() = %q", got)
}
}
func TestIPZanParseTextMultiple(t *testing.T) {
body := []byte("1.2.3.4:8001\r\n5.6.7.8:8002\r\n")
got, err := NewIPZanProvider().Parse(
"https://service.ipzan.com/core-extract?protocol=3",
body,
)
if err != nil {
t.Fatal(err)
}
if len(got) != 2 {
t.Fatalf("len=%d, want 2", len(got))
}
if got[0].URL != "socks5://1.2.3.4:8001" {
t.Fatalf("URL=%q", got[0].URL)
}
}
func TestIPZanParseJSONError(t *testing.T) {
body := []byte(`{"code":-1,"data":null,"status":200,"message":"请先将1.2.3.4加入到白名单再进行提取"}`)
_, err := NewIPZanProvider().Parse("https://service.ipzan.com/core-extract", body)
if err == nil || !IsFatal(err) {
t.Fatalf("expected fatal provider error, got %v", err)
}
}
func TestIPZanParseJSONList(t *testing.T) {
body := []byte(`{"code":0,"status":200,"data":{"list":[{"ip":"1.2.3.4","port":8001,"account":"user-a","password":"pass-a","expired":1893456000000},{"ip":"5.6.7.8","port":"8002"}]}}`)
got, err := NewIPZanProvider().Parse(
"https://service.ipzan.com/core-extract?protocol=1",
body,
)
if err != nil {
t.Fatal(err)
}
if len(got) != 2 || got[0].URL != "http://user-a:pass-a@1.2.3.4:8001" || got[1].URL != "http://5.6.7.8:8002" {
t.Fatalf("unexpected endpoints: %#v", got)
}
if got[0].ExpiresAt != time.UnixMilli(1893456000000) {
t.Fatalf("ExpiresAt=%v", got[0].ExpiresAt)
}
}
func TestGenericParseJSONListWithAuth(t *testing.T) {
body := []byte(`{"code":0,"data":{"list":[{"ip":"1.2.3.4","port":9000,"username":"u","password":"p"}]}}`)
got, err := NewGenericProvider().Parse("https://example.test/api", body)
if err != nil {
t.Fatal(err)
}
if len(got) != 1 || got[0].URL != "http://u:p@1.2.3.4:9000" {
t.Fatalf("unexpected endpoints: %#v", got)
}
}
func TestGenericParseIPZanAccountPasswordSample(t *testing.T) {
body := []byte(`{"data":{"list":[{"ip":"203.0.113.10","port":"15471","expired":1893456000000,"net":"电信","account":"test-account","password":"test-password"}]},"code":0,"message":"","status":200}`)
got, err := NewGenericProvider().Parse("https://example.test/api?protocol=1", body)
if err != nil {
t.Fatal(err)
}
if len(got) != 1 {
t.Fatalf("len=%d, want 1", len(got))
}
if got[0].URL != "http://test-account:test-password@203.0.113.10:15471" {
t.Fatalf("URL=%q", got[0].URL)
}
if got[0].Username != "test-account" || got[0].Password != "test-password" {
t.Fatalf("credentials not parsed: %#v", got[0])
}
if got[0].ExpiresAt != time.UnixMilli(1893456000000) {
t.Fatalf("ExpiresAt=%v", got[0].ExpiresAt)
}
}
func TestGenericProviderDoesNotTreatErrorIPAsProxy(t *testing.T) {
body := []byte(`{"code":-1,"status":200,"message":"请先将1.2.3.4加入白名单"}`)
_, err := NewGenericProvider().Parse("https://example.test/api", body)
if err == nil || !IsFatal(err) {
t.Fatalf("expected fatal provider error, got %v", err)
}
}
func TestGenericProviderParsesLegacyJSONData(t *testing.T) {
body := []byte(`{"status":100,"info":"成功","data":[{"ip":"1.2.3.4","port":"8001","prov":"浙江","city":"嘉兴"}]}`)
got, err := NewGenericProvider().Parse("https://example.test/api", body)
if err != nil {
t.Fatal(err)
}
if len(got) != 1 || got[0].URL != "http://1.2.3.4:8001" || got[0].Region != "浙江 嘉兴" {
t.Fatalf("unexpected endpoints: %#v", got)
}
}
type roundTripFunc func(*http.Request) (*http.Response, error)
func (fn roundTripFunc) RoundTrip(req *http.Request) (*http.Response, error) {
return fn(req)
}
type autoWhitelistTestProvider struct {
parseCalls int
addedIP string
}
func (p *autoWhitelistTestProvider) ID() string { return "test" }
func (p *autoWhitelistTestProvider) Match(*url.URL) bool { return true }
func (p *autoWhitelistTestProvider) RequestURL(apiURL string, count int) (string, error) {
return apiURL, nil
}
func (p *autoWhitelistTestProvider) Parse(string, []byte) ([]Endpoint, error) {
p.parseCalls++
if p.parseCalls == 1 {
return nil, &ProviderError{
Provider: "test",
Message: "请先将1.2.3.4加入白名单再提取",
Fatal: true,
}
}
return []Endpoint{{URL: "http://1.2.3.4:9000", IP: "1.2.3.4"}}, nil
}
func (p *autoWhitelistTestProvider) EnsureWhitelist(context.Context, *http.Client, WhitelistConfig) error {
return nil
}
func (p *autoWhitelistTestProvider) AddWhitelist(_ context.Context, _ *http.Client, cfg WhitelistConfig) error {
p.addedIP = cfg.IP
return nil
}
func TestExtractWithWhitelistUsesProviderReportedIP(t *testing.T) {
provider := &autoWhitelistTestProvider{}
client := &http.Client{Transport: roundTripFunc(func(*http.Request) (*http.Response, error) {
return &http.Response{
StatusCode: http.StatusOK,
Body: io.NopCloser(strings.NewReader("probe")),
Header: make(http.Header),
}, nil
})}
service := NewServiceWithProviders(client, provider)
got, err := service.ExtractWithWhitelist(
context.Background(),
"https://example.test/extract",
1,
WhitelistConfig{AutoWhitelist: true},
)
if err != nil {
t.Fatal(err)
}
if provider.addedIP != "1.2.3.4" {
t.Fatalf("addedIP=%q", provider.addedIP)
}
if len(got) != 1 {
t.Fatalf("len=%d, want 1", len(got))
}
}
func TestServiceSelectsIPZanProvider(t *testing.T) {
client := &http.Client{Transport: roundTripFunc(func(req *http.Request) (*http.Response, error) {
if got := req.URL.Query().Get("num"); got != "3" {
t.Fatalf("num=%q, want 3", got)
}
return &http.Response{
StatusCode: http.StatusOK,
Body: io.NopCloser(strings.NewReader("1.2.3.4:8001\r\n5.6.7.8:8002\r\n9.9.9.9:8003")),
Header: make(http.Header),
}, nil
})}
service := NewServiceWithProviders(client, NewIPZanProvider(), NewGenericProvider())
got, err := service.Extract(
context.Background(),
"https://service.ipzan.com/core-extract?num=1&protocol=1&secret=x",
3,
)
if err != nil {
t.Fatal(err)
}
if len(got) != 3 {
t.Fatalf("len=%d, want 3", len(got))
}
if got[0].Provider != "ipzan" {
t.Fatalf("provider=%q, want ipzan", got[0].Provider)
}
}
func TestIPZanEnsureWhitelistSignsAndAdds(t *testing.T) {
const (
loginPassword = "login-pass"
secret = "extract-secret"
signKey = "0123456789abcdef"
publicIP = "1.2.3.4"
)
fixedNow := time.Unix(1700000000, 0)
var addCalled bool
server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
switch r.URL.Path {
case "/whiteList-verify":
if r.URL.Query().Get("no") != "plan-no" || r.URL.Query().Get("ip") != publicIP {
t.Fatalf("unexpected verify query: %s", r.URL.RawQuery)
}
io.WriteString(w, `{"code":0,"data":false,"status":200,"message":""}`)
case "/whiteList-add":
addCalled = true
if err := r.ParseForm(); err != nil {
t.Fatal(err)
}
if r.Form.Get("replace") != "1" {
t.Fatalf("replace=%q", r.Form.Get("replace"))
}
encrypted, err := hex.DecodeString(r.Form.Get("sign"))
if err != nil {
t.Fatal(err)
}
block, err := aes.NewCipher([]byte(signKey))
if err != nil {
t.Fatal(err)
}
decrypted := make([]byte, len(encrypted))
for offset := 0; offset < len(encrypted); offset += block.BlockSize() {
block.Decrypt(decrypted[offset:offset+block.BlockSize()], encrypted[offset:offset+block.BlockSize()])
}
padding := int(decrypted[len(decrypted)-1])
plain := string(decrypted[:len(decrypted)-padding])
if want := loginPassword + ":" + secret + ":1700000000"; plain != want {
t.Fatalf("sign plain=%q, want %q", plain, want)
}
io.WriteString(w, `{"code":0,"data":true,"status":200,"message":""}`)
default:
http.NotFound(w, r)
}
}))
defer server.Close()
provider := &IPZanProvider{
baseURL: server.URL,
now: func() time.Time { return fixedNow },
}
err := provider.EnsureWhitelist(
context.Background(),
server.Client(),
WhitelistConfig{
APIURL: "https://service.ipzan.com/core-extract?no=plan-no&secret=" + secret,
IP: publicIP,
LoginPassword: loginPassword,
SignKey: signKey,
ReplaceOldest: true,
},
)
if err != nil {
t.Fatal(err)
}
if !addCalled {
t.Fatal("whiteList-add was not called")
}
}
+32 -4
View File
@@ -116,7 +116,15 @@ type attemptOutcome struct {
// runAccountAttempt 按活动执行一次完整尝试。
func (s *Scheduler) runAccountAttempt(attempt types.ExchangeAttempt, mode types.SeckillMode, ctl *runControl) attemptOutcome {
cli := s.clientSnapshot()
cli := s.clientForAccount(attempt.Account.Name)
if cli == nil {
return attemptOutcome{result: types.ExchangeResult{
Account: attempt.Account.Name,
Product: attempt.Product.CommodityName,
Attempt: attempt.Index,
Message: "失败|没有可用代理客户端",
}}
}
switch {
case cli.Activity.IsXiaodian:
// 和平小店正常走 RunXiaodian;这里保留兜底分类,避免万一被路由进来时
@@ -136,7 +144,15 @@ func (s *Scheduler) runAccountAttempt(attempt types.ExchangeAttempt, mode types.
// 锁单失败立即返回,等待与重试交给外层每账号独立循环,因此单个账号的长退避
// 不会阻塞其它账号;锁单成功后的支付阶梯不受 75 秒抢购窗口约束。
func (s *Scheduler) runPeaceLockPay(attempt types.ExchangeAttempt, ctl *runControl) attemptOutcome {
cli := s.clientSnapshot()
cli := s.clientForAccount(attempt.Account.Name)
if cli == nil {
return attemptOutcome{result: types.ExchangeResult{
Account: attempt.Account.Name,
Product: attempt.Product.CommodityName,
Attempt: attempt.Index,
Message: "失败|没有可用代理客户端",
}}
}
out := attemptOutcome{result: types.ExchangeResult{
Account: attempt.Account.Name,
Product: attempt.Product.CommodityName,
@@ -324,7 +340,15 @@ func (s *Scheduler) payOnce(cli *client.DouyuClient, attempt types.ExchangeAttem
// runDirectExchange 电竞手册直接兑换。
func (s *Scheduler) runDirectExchange(attempt types.ExchangeAttempt, reuseCache bool, ctl *runControl) attemptOutcome {
cli := s.clientSnapshot()
cli := s.clientForAccount(attempt.Account.Name)
if cli == nil {
return attemptOutcome{result: types.ExchangeResult{
Account: attempt.Account.Name,
Product: attempt.Product.CommodityName,
Attempt: attempt.Index,
Message: "失败|没有可用代理客户端",
}}
}
out := attemptOutcome{result: types.ExchangeResult{
Account: attempt.Account.Name,
Product: attempt.Product.CommodityName,
@@ -450,7 +474,7 @@ func (s *Scheduler) rememberAuth(accountName, csrf, roleName string) {
// runXiaodianAttempt 和平小店专用兑换:跳过手册鉴权、批量限制和预兑换,直接调用 livebuy。
func (s *Scheduler) runXiaodianAttempt(attempt types.ExchangeAttempt) types.ExchangeResult {
cli := s.clientSnapshot()
cli := s.clientForAccount(attempt.Account.Name)
result := types.ExchangeResult{
Account: attempt.Account.Name,
Product: attempt.Product.CommodityName,
@@ -461,6 +485,10 @@ func (s *Scheduler) runXiaodianAttempt(attempt types.ExchangeAttempt) types.Exch
result.Message = "失败|已收到停止指令"
return result
}
if cli == nil {
result.Message = "失败|没有可用代理客户端"
return result
}
logger.LogAccountEvent("抢购", "info", attempt.Account.Name,
"和平小店直接兑换: 账号=%s 商品=%s 第%d次",
+522
View File
@@ -0,0 +1,522 @@
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
}
+122
View File
@@ -0,0 +1,122 @@
package seckill
import (
"fmt"
"net/http"
"net/http/httptest"
"strconv"
"sync/atomic"
"testing"
"time"
"douyu_go/internal/activity"
"douyu_go/internal/client"
"douyu_go/internal/trace"
"douyu_go/internal/types"
)
func TestChunkAccountNames(t *testing.T) {
names := []string{"k", "a", "i", "c", "b", "h", "e", "d", "g", "f", "j"}
groups := chunkAccountNames(names, 5)
if len(groups) != 3 {
t.Fatalf("groups=%d, want 3", len(groups))
}
if len(groups[0]) != 5 || len(groups[1]) != 5 || len(groups[2]) != 1 {
t.Fatalf("unexpected group sizes: %d,%d,%d", len(groups[0]), len(groups[1]), len(groups[2]))
}
}
func TestSplitProxyURLs(t *testing.T) {
got := splitProxyURLs("http://a:1\nhttp://b:2; http://c:3,http://d:4")
if len(got) != 4 {
t.Fatalf("splitProxyURLs()=%v", got)
}
}
func TestProxyPoolExpiresAtUsesMinuteParameter(t *testing.T) {
now := time.Date(2026, 11, 19, 12, 0, 0, 0, time.Local)
got := proxyPoolExpiresAt("https://example.test/extract?minute=10", now)
if want := now.Add(10 * time.Minute); !got.Equal(want) {
t.Fatalf("expiresAt=%s, want %s", got, want)
}
}
func TestAccountClientsRespectExpiryAndSettingsRevision(t *testing.T) {
scheduler := &Scheduler{
client: &client.DouyuClient{},
accountClients: make(map[string]*client.DouyuClient),
}
groupClient := &client.DouyuClient{}
scheduler.replaceAccountClients(
map[string]*client.DouyuClient{"a": groupClient},
[]*client.DouyuClient{groupClient},
time.Now().Add(time.Minute),
)
if !scheduler.accountClientsCover([]string{"a"}) {
t.Fatal("未过期代理组应可复用")
}
scheduler.UpdateProxySettings(types.Config{
ProxyEnabled: true,
AutoFetchProxyOnSchedule: true,
AccountsPerProxy: 5,
})
if scheduler.accountClientsCover([]string{"a"}) {
t.Fatal("配置变更后代理组应标记为需要刷新")
}
}
func TestPrepareProxyGroupsRoutesAccountsToDistinctClients(t *testing.T) {
timeAPIURL := "http://douyu.test/time"
proxyServers := make([]*httptest.Server, 0, 3)
for i := 0; i < 3; i++ {
proxyServers = append(proxyServers, newTimeProxyServer(t))
}
var next int32
apiServer := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
index := int(atomic.AddInt32(&next, 1)-1) % len(proxyServers)
fmt.Fprint(w, proxyServers[index].URL)
}))
defer apiServer.Close()
act := *activity.Get("peace")
base := client.New(&act, timeAPIURL, &trace.Recorder{}, "")
scheduler := New(base, types.AppPaths{}, nil, 8, 1, nil, types.SeckillModeAggressive)
scheduler.UpdateProxySettings(types.Config{
ProxyEnabled: true,
ProxyAPI: apiServer.URL,
AutoFetchProxyOnSchedule: true,
AccountsPerProxy: 2,
})
accountProducts := make(map[string]string)
for _, name := range []string{"a", "b", "c", "d", "e"} {
accountProducts[name] = "goods-1"
}
if err := scheduler.prepareProxyGroups(accountProducts, true); err != nil {
t.Fatalf("prepareProxyGroups() error = %v", err)
}
if scheduler.clientForAccount("a") != scheduler.clientForAccount("b") {
t.Fatal("a 和 b 应使用同一个代理客户端")
}
if scheduler.clientForAccount("b") == scheduler.clientForAccount("c") {
t.Fatal("b 和 c 不应使用同一个代理客户端")
}
if scheduler.clientForAccount("c") != scheduler.clientForAccount("d") {
t.Fatal("c 和 d 应使用同一个代理客户端")
}
if scheduler.clientForAccount("e") == scheduler.clientForAccount("a") {
t.Fatal("e 应使用独立代理客户端")
}
}
func newTimeProxyServer(t *testing.T) *httptest.Server {
t.Helper()
server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
now := strconv.FormatInt(time.Now().Unix(), 10)
w.Header().Set("Content-Type", "application/json")
fmt.Fprintf(w, `{"error":0,"data":{"preExchange":{"currentTime":%q}}}`, now)
}))
t.Cleanup(server.Close)
return server
}
+9 -19
View File
@@ -7,7 +7,6 @@ import (
"sync/atomic"
"time"
"douyu_go/internal/client"
"douyu_go/internal/constants"
"douyu_go/internal/logger"
"douyu_go/internal/types"
@@ -47,12 +46,9 @@ func (s *Scheduler) ScheduleRun(req types.SeckillRequest) (types.ScheduleResult,
s.scheduleTimers(ap, targetTime, triggerTime, calibration, req.Attempts, req.DelayMs, false)
s.proxyMu.RLock()
autoFetch := s.autoFetchProxyOnSchedule
apiURL := s.proxyAPI
s.proxyMu.RUnlock()
proxyCfg := s.proxySettings()
if autoFetch && apiURL != "" {
if proxyCfg.enabled && proxyCfg.autoFetchOnTimer && proxyCfg.proxyAPI != "" {
proxyFetchRemain := triggerTime.Sub(serverTime) - proxyFetchBeforeTrigger
if proxyFetchRemain < 0 {
proxyFetchRemain = 0
@@ -62,20 +58,11 @@ func (s *Scheduler) ScheduleRun(req types.SeckillRequest) (types.ScheduleResult,
if s.IsRunning() {
return
}
logger.LogEvent("代理", "info", "定时任务触发前,自动从 API 提取最新独立代理...")
res, err := client.FetchProxyFromAPI(apiURL)
if err != nil {
logger.LogEvent("代理", "warn", "定时自动提取代理失败: %v,继续使用现有网络通道", err)
logger.LogEvent("代理", "info", "定时任务触发前,自动提取并校验分组代理...")
if err := s.prepareProxyGroups(ap, true); err != nil {
logger.LogEvent("代理", "error", "定时分组代理准备失败: %v", err)
return
}
cli := s.clientSnapshot()
if cli != nil {
if err := cli.SetProxy(res.ProxyURL); err != nil {
logger.LogEvent("代理", "warn", "应用提取代理失败: %v", err)
} else {
logger.LogEvent("代理", "success", "定时自动应用最新代理: %s (IP=%s 归属=%s)", res.ProxyURL, res.IP, res.Region)
}
}
})
s.scheduleMu.Unlock()
}
@@ -202,7 +189,10 @@ func (s *Scheduler) CancelSchedule() bool {
}
func (s *Scheduler) calibrateDouyuTime(label string) types.TimeCalibration {
cli := s.clientSnapshot()
cli := s.calibrationClient()
if cli == nil {
return types.TimeCalibration{Fallback: true}
}
calibration, err := cli.BestServerTimeCalibration(timeCalibrateRounds, timeCalibrateWait)
if err != nil {
logger.LogEvent("时间", "warn", "%s校准降级: %v", label, err)
+96 -41
View File
@@ -16,6 +16,7 @@ import (
"douyu_go/internal/constants"
"douyu_go/internal/image"
"douyu_go/internal/logger"
proxyprovider "douyu_go/internal/proxy"
"douyu_go/internal/types"
"douyu_go/internal/utils"
)
@@ -27,7 +28,7 @@ const (
timeCalibrateRounds = 3
timeCalibrateWait = 1200 * time.Millisecond
resyncBeforeTrigger = 8 * time.Second
proxyFetchBeforeTrigger = 15 * time.Second
proxyFetchBeforeTrigger = 30 * time.Second
calibrationHistorySize = 3
maxCooldownCount = 5
xiaodianDefaultDelay = 500 * time.Millisecond
@@ -59,8 +60,13 @@ type runSettings struct {
type Scheduler struct {
clientMu sync.RWMutex
client *client.DouyuClient
paths types.AppPaths
font *truetype.Font
// accountClients 按账号名路由到独立的代理客户端;未命中的账号使用 client。
accountClients map[string]*client.DouyuClient
accountClientOrder []*client.DouyuClient
accountClientsDirty bool
accountClientsExpireAt time.Time
paths types.AppPaths
font *truetype.Font
successImages *image.SuccessImageSaver
@@ -90,18 +96,24 @@ type Scheduler struct {
// pendingClient 保存运行中收到的客户端更新,当前任务结束后自动应用。
pendingClient *client.DouyuClient
scheduleMu sync.Mutex
schedule *time.Timer
warmupSchedule *time.Timer
resyncSchedule *time.Timer
proxyFetchSchedule *time.Timer
warmupGen int64
scheduleMu sync.Mutex
schedule *time.Timer
warmupSchedule *time.Timer
resyncSchedule *time.Timer
proxyFetchSchedule *time.Timer
warmupGen int64
proxyMu sync.RWMutex
proxyEnabled bool
proxyURL string
proxyAPI string
autoFetchProxyOnSchedule bool
accountsPerProxy int
autoWhitelist bool
whitelistReplace bool
whitelistPassword string
whitelistSignKey string
proxyService *proxyprovider.Service
warmupsMu sync.RWMutex
warmups map[string]types.WarmupData
@@ -143,6 +155,7 @@ func New(cli *client.DouyuClient, paths types.AppPaths, font *truetype.Font, con
}
scheduler := &Scheduler{
client: cli,
accountClients: make(map[string]*client.DouyuClient),
paths: paths,
font: font,
accounts: NewAccountStore(paths.AccountFile, paths.LegacyAccountFile),
@@ -161,6 +174,8 @@ func New(cli *client.DouyuClient, paths types.AppPaths, font *truetype.Font, con
attemptCache: make(map[string]*types.AttemptCache),
products: make(map[string]types.Product),
calibrationOffsets: make([]time.Duration, 0, calibrationHistorySize),
accountsPerProxy: constants.DefaultAccountsPerProxy,
proxyService: proxyprovider.NewService(),
}
scheduler.scoreBaselines.Load()
scheduler.loadLatestXiaodianReport()
@@ -169,6 +184,7 @@ func New(cli *client.DouyuClient, paths types.AppPaths, font *truetype.Font, con
// Close 关闭调度器的后台资源。
func (s *Scheduler) Close() {
s.closeAccountClients()
if s.successImages != nil {
s.successImages.Close()
}
@@ -185,6 +201,16 @@ func (s *Scheduler) clientSnapshot() *client.DouyuClient {
return s.client
}
// clientForAccount 返回账号所属代理组的客户端,未分组时回退到基础客户端。
func (s *Scheduler) clientForAccount(accountName string) *client.DouyuClient {
s.clientMu.RLock()
defer s.clientMu.RUnlock()
if cli := s.accountClients[accountName]; cli != nil {
return cli
}
return s.client
}
// UpdateClient 热替换底层 HTTP 客户端(用于活动切换)。
// 若有抢购任务正在运行则暂存新客户端,当前任务结束后自动应用,并返回 false。
func (s *Scheduler) UpdateClient(newClient *client.DouyuClient) bool {
@@ -205,12 +231,18 @@ func (s *Scheduler) UpdateClient(newClient *client.DouyuClient) bool {
func (s *Scheduler) replaceClient(newClient *client.DouyuClient) {
s.clientMu.Lock()
defer s.clientMu.Unlock()
old := s.client
oldGroups := s.accountClients
s.client = newClient
s.accountClients = make(map[string]*client.DouyuClient)
s.accountClientOrder = nil
s.accountClientsDirty = true
s.accountClientsExpireAt = time.Time{}
s.clientMu.Unlock()
if old != nil && old.HTTPClient != nil {
old.HTTPClient.CloseIdleConnections()
}
closeDouyuClients(oldGroups)
}
// UpdateProducts 更新商品缓存,抢购成功图片会优先使用中文商品名。
@@ -246,16 +278,6 @@ func (s *Scheduler) UpdateSettings(concurrency, attempts int, retryStages []type
s.seckillMode = normalizeSeckillMode(seckillMode)
}
// UpdateProxySettings 运行时更新代理配置。
func (s *Scheduler) UpdateProxySettings(enabled bool, proxyURL, proxyAPI string, autoFetch bool) {
s.proxyMu.Lock()
defer s.proxyMu.Unlock()
s.proxyEnabled = enabled
s.proxyURL = proxyURL
s.proxyAPI = proxyAPI
s.autoFetchProxyOnSchedule = autoFetch
}
func (s *Scheduler) settingsSnapshot() runSettings {
s.settingsMu.RLock()
settings := runSettings{
@@ -449,6 +471,10 @@ func (s *Scheduler) RunNow(accountProducts map[string]string) {
logger.LogEvent("抢购", "warn", "没有可执行的抢购任务")
return
}
if err := s.prepareProxyGroups(accountProductsForTasks(tasks), false); err != nil {
logger.LogEvent("代理", "error", "分组代理准备失败,本次抢购不执行: %v", err)
return
}
start := time.Now()
deadline := start.Add(seckillMaxDuration)
@@ -467,7 +493,10 @@ func (s *Scheduler) RunNow(accountProducts map[string]string) {
// 逐条网络日志已降为 debug 级,这里开始统计每类接口的调用情况,
// 任务结束时输出一行聚合结果。
if cli := s.clientSnapshot(); cli != nil {
for _, cli := range s.allClients() {
if cli == nil {
continue
}
cli.ResetRequestStats()
}
@@ -484,8 +513,8 @@ func (s *Scheduler) RunNow(accountProducts map[string]string) {
s.logRunSummary(outcomes, ctl, time.Since(start))
// 先核对积分(会产生"积分余额"请求,一并计入后面的接口统计)。
s.verifyRunResults(outcomes, ctl, s.clientSnapshot())
s.logNetworkSummary(s.clientSnapshot())
s.verifyRunResults(outcomes, ctl)
s.logNetworkSummary()
}
// verifyRunResults 抢购结束后对成功账号核对积分余额。
@@ -494,8 +523,9 @@ func (s *Scheduler) RunNow(accountProducts map[string]string) {
// 2026-09-30 就出现过订单实际已支付、程序却报成失败的情况,只看接口返回不够。
// 核对在任务结束之后进行,不影响抢购本身;单个账号查询失败只记警告,
// 不改变"抢到"这个结论。
func (s *Scheduler) verifyRunResults(outcomes []accountOutcome, ctl *runControl, cli *client.DouyuClient) {
if cli == nil || cli.Activity.ScoreURL == "" {
func (s *Scheduler) verifyRunResults(outcomes []accountOutcome, ctl *runControl) {
base := s.clientSnapshot()
if base == nil || base.Activity.ScoreURL == "" {
return
}
successes := make([]accountOutcome, 0, len(outcomes))
@@ -521,6 +551,11 @@ func (s *Scheduler) verifyRunResults(outcomes []accountOutcome, ctl *runControl,
logger.LogAccountEvent("核对", "warn", out.account, "积分核对跳过: 账号=%s 没有可用 Cookie", out.account)
return
}
cli := s.clientForAccount(out.account)
if cli == nil {
logger.LogAccountEvent("核对", "warn", out.account, "积分核对跳过: 账号=%s 没有可用客户端", out.account)
return
}
if ctl != nil {
if !ctl.enter() {
return
@@ -604,8 +639,8 @@ func (s *Scheduler) ForgetScoreBaselines(accountNames []string) {
// 只在"添加账号"这类抢购之外的操作里调用:账号多的时候,
// 抢购链路(预热或触发时刻)绝不能承担这批额外请求。
func (s *Scheduler) FetchScoreBaselines(accounts []types.Account) {
cli := s.clientSnapshot()
if cli == nil || cli.Activity.ScoreURL == "" || len(accounts) == 0 {
base := s.clientSnapshot()
if base == nil || base.Activity.ScoreURL == "" || len(accounts) == 0 {
return
}
if s.IsRunning() {
@@ -628,6 +663,11 @@ func (s *Scheduler) FetchScoreBaselines(accounts []types.Account) {
defer wg.Done()
limit <- struct{}{}
defer func() { <-limit }()
cli := s.clientForAccount(account.Name)
if cli == nil {
logger.LogAccountEvent("账号", "warn", account.Name, "积分基线查询失败: 账号=%s 没有可用客户端", account.Name)
return
}
score, err := cli.Score(account.Cookie)
if err != nil {
logger.LogAccountEvent("账号", "warn", account.Name, "积分基线查询失败: 账号=%s err=%v", account.Name, err)
@@ -649,22 +689,29 @@ func (s *Scheduler) FetchScoreBaselines(accounts []types.Account) {
}
// logNetworkSummary 输出本次任务期间各类接口的聚合统计,替代逐条刷屏的网络日志。
func (s *Scheduler) logNetworkSummary(cli *client.DouyuClient) {
if cli == nil {
return
}
for _, stat := range cli.RequestStats() {
avg := time.Duration(0)
if stat.Count > 0 {
avg = stat.Total / time.Duration(stat.Count)
}
if stat.LastErr != "" {
logger.LogEvent("网络", "info", "接口统计: %s ×%d 失败%d 平均=%s 最慢=%s 最后错误=%s",
stat.Label, stat.Count, stat.Failed, utils.RoundDuration(avg), utils.RoundDuration(stat.Slowest), stat.LastErr)
func (s *Scheduler) logNetworkSummary() {
clients := s.allClients()
for index, cli := range clients {
if cli == nil {
continue
}
logger.LogEvent("网络", "info", "接口统计: %s ×%d 失败%d 平均=%s 最慢=%s",
stat.Label, stat.Count, stat.Failed, utils.RoundDuration(avg), utils.RoundDuration(stat.Slowest))
label := "基础客户端"
if index > 0 {
label = fmt.Sprintf("代理组%d", index)
}
for _, stat := range cli.RequestStats() {
avg := time.Duration(0)
if stat.Count > 0 {
avg = stat.Total / time.Duration(stat.Count)
}
if stat.LastErr != "" {
logger.LogEvent("网络", "info", "%s接口统计: %s ×%d 失败%d 平均=%s 最慢=%s 最后错误=%s",
label, stat.Label, stat.Count, stat.Failed, utils.RoundDuration(avg), utils.RoundDuration(stat.Slowest), stat.LastErr)
continue
}
logger.LogEvent("网络", "info", "%s接口统计: %s ×%d 失败%d 平均=%s 最慢=%s",
label, stat.Label, stat.Count, stat.Failed, utils.RoundDuration(avg), utils.RoundDuration(stat.Slowest))
}
}
}
@@ -1095,6 +1142,14 @@ func (s *Scheduler) RunXiaodian(accountProducts map[string]string, attempts, del
logger.LogEvent("抢购", "warn", "和平小店抢购没有可执行账号")
return
}
accountProductsForProxy := make(map[string]string, len(accounts))
for _, account := range accounts {
accountProductsForProxy[account.Name] = products[account.Name].CommodityID
}
if err := s.prepareProxyGroups(accountProductsForProxy, false); err != nil {
logger.LogEvent("代理", "error", "分组代理准备失败,本次和平小店抢购不执行: %v", err)
return
}
s.startXiaodianReport(accounts, products, reportSessionID)
stoppedAccounts := make(map[string]string)
+36 -6
View File
@@ -20,6 +20,11 @@ type scoreBaselineEntry struct {
At int64 `json:"at"` // Unix 秒,便于判断基线有多旧
}
type scoreBaselineJob struct {
snapshot map[string]scoreBaselineEntry
ack chan struct{}
}
// ScoreBaselineStore 记录每个账号"上次已知的积分余额",作为抢购结束后核对的基线。
//
// 刻意不在抢购链路里取基线:账号多的时候,预热或触发时刻的任何额外请求都会挤占抢购。
@@ -35,7 +40,7 @@ type ScoreBaselineStore struct {
entries map[string]scoreBaselineEntry
// flushCh 承接待落盘的快照;后台协程串行写盘,避免并发写互相覆盖。
flushCh chan map[string]scoreBaselineEntry
flushCh chan scoreBaselineJob
// done 在关闭时等待后台写盘结束,保证退出前不丢数据。
done chan struct{}
// closeMu 保护 flushCh 的发送与关闭,避免"关闭后发送"导致 panic。
@@ -51,7 +56,7 @@ func NewScoreBaselineStore(path string) *ScoreBaselineStore {
entries: make(map[string]scoreBaselineEntry),
}
if path != "" {
store.flushCh = make(chan map[string]scoreBaselineEntry, 8)
store.flushCh = make(chan scoreBaselineJob, 8)
store.done = make(chan struct{})
go store.flushLoop()
}
@@ -64,8 +69,13 @@ func NewScoreBaselineStore(path string) *ScoreBaselineStore {
// 队列满了就丢弃旧快照(写入最新状态即可),绝不让抢购侧阻塞等待。
func (s *ScoreBaselineStore) flushLoop() {
defer close(s.done)
for snapshot := range s.flushCh {
s.writeFile(snapshot)
for job := range s.flushCh {
if job.snapshot != nil {
s.writeFile(job.snapshot)
}
if job.ack != nil {
close(job.ack)
}
}
}
@@ -98,14 +108,21 @@ func (s *ScoreBaselineStore) enqueue(snapshot map[string]scoreBaselineEntry) {
if s.closed {
return
}
s.enqueueLocked(scoreBaselineJob{snapshot: snapshot})
}
func (s *ScoreBaselineStore) enqueueLocked(job scoreBaselineJob) {
for {
select {
case s.flushCh <- snapshot:
case s.flushCh <- job:
return
default:
// 队列满:丢掉最旧的一份,腾出位置放最新的。
select {
case <-s.flushCh:
case dropped := <-s.flushCh:
if dropped.ack != nil {
close(dropped.ack)
}
default:
}
}
@@ -222,6 +239,19 @@ func (s *ScoreBaselineStore) Flush() {
if s == nil || s.path == "" {
return
}
if s.flushCh != nil {
ack := make(chan struct{})
s.closeMu.Lock()
if !s.closed {
s.enqueueLocked(scoreBaselineJob{ack: ack})
} else {
ack = nil
}
s.closeMu.Unlock()
if ack != nil {
<-ack
}
}
s.mu.RLock()
snapshot := make(map[string]scoreBaselineEntry, len(s.entries))
for name, entry := range s.entries {
+5 -1
View File
@@ -51,7 +51,6 @@ func (s *Scheduler) warmupAttempts(attempts []types.ExchangeAttempt) {
logger.LogEvent("预热", "info", "开始预热: %d 个账号", len(attempts))
settings := s.settingsSnapshot()
cli := s.clientSnapshot()
limit := make(chan struct{}, settings.concurrency)
var wg sync.WaitGroup
for _, attempt := range attempts {
@@ -60,6 +59,11 @@ func (s *Scheduler) warmupAttempts(attempts []types.ExchangeAttempt) {
defer wg.Done()
limit <- struct{}{}
defer func() { <-limit }()
cli := s.clientForAccount(attempt.Account.Name)
if cli == nil {
logger.LogAccountEvent("预热", "warn", attempt.Account.Name, "预热跳过: 账号=%s 没有可用客户端", attempt.Account.Name)
return
}
start := time.Now()
csrf, err := cli.GenerateCSRF(attempt.Account.Cookie)
+24 -17
View File
@@ -7,20 +7,25 @@ import (
// Config 应用配置。
type Config struct {
TimeAPIURL string `json:"timeApiUrl"`
Activity string `json:"activity"` // 当前活动 ID,见 internal/activity
TraceEnabled bool `json:"traceEnabled"`
TraceMaxBody int `json:"traceMaxBody"`
LogLevel string `json:"logLevel"` // debug / info / warn / error
Concurrency int `json:"concurrency"` // 同时在途的请求数上限
Attempts int `json:"attempts"` // 仅和平小店:抢购次数缺省值(手册链路按窗口重试,不受此项影响)
RetryStages []RetryStage `json:"retryStages"` // 精英/电竞手册的锁单重试节奏(按经过秒数分档)
SeckillMode SeckillMode `json:"seckillMode"` // 抢购模式:0=标准, 1=激进
XiaodianPageSize int `json:"xiaodianPageSize"`
TimeAPIURL string `json:"timeApiUrl"`
Activity string `json:"activity"` // 当前活动 ID,见 internal/activity
TraceEnabled bool `json:"traceEnabled"`
TraceMaxBody int `json:"traceMaxBody"`
LogLevel string `json:"logLevel"` // debug / info / warn / error
Concurrency int `json:"concurrency"` // 同时在途的请求数上限
Attempts int `json:"attempts"` // 仅和平小店:抢购次数缺省值(手册链路按窗口重试,不受此项影响)
RetryStages []RetryStage `json:"retryStages"` // 精英/电竞手册的锁单重试节奏(按经过秒数分档)
SeckillMode SeckillMode `json:"seckillMode"` // 抢购模式:0=标准, 1=激进
XiaodianPageSize int `json:"xiaodianPageSize"`
ProxyEnabled bool `json:"proxyEnabled"` // 是否启用代理
ProxyURL string `json:"proxyUrl"` // 代理地址,支持 http://, https://, socks5://
ProxyAPI string `json:"proxyApi"` // 代理提取 API 地址(如星空代理)
ProxyAPI string `json:"proxyApi"` // 代理提取 API 地址,支持品赞与通用格式
AutoFetchProxyOnSchedule bool `json:"autoFetchProxyOnSchedule"` // 定时抢购触发前自动从 API 提取最新代理
AccountsPerProxy int `json:"accountsPerProxy"` // 每个独立代理承载的账号数
ProxyAutoWhitelist bool `json:"proxyAutoWhitelist"` // 提取前自动添加本机公网 IP 白名单
ProxyWhitelistReplace bool `json:"proxyWhitelistReplace"` // 白名单已满时替换最早 IP
ProxyWhitelistPassword string `json:"proxyWhitelistPassword"` // 白名单签名使用的登录密码
ProxyWhitelistSignKey string `json:"proxyWhitelistSignKey"` // 白名单签名使用的 AES 密钥
}
// ProxyTestResult 代理测试结果。
@@ -34,12 +39,14 @@ type ProxyTestResult struct {
// ProxyFetchResult 代理 API 提取结果。
type ProxyFetchResult struct {
Success bool `json:"success"`
ProxyURL string `json:"proxyUrl,omitempty"`
IP string `json:"ip,omitempty"`
Port string `json:"port,omitempty"`
Region string `json:"region,omitempty"`
Error string `json:"error,omitempty"`
Success bool `json:"success"`
ProxyURL string `json:"proxyUrl,omitempty"`
IP string `json:"ip,omitempty"`
Port string `json:"port,omitempty"`
Region string `json:"region,omitempty"`
Provider string `json:"provider,omitempty"`
ExpiresAt int64 `json:"expiresAt,omitempty"`
Error string `json:"error,omitempty"`
}
// XiaodianProductListOptions 是和平小店商品列表的角色与分页参数。