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
+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: "抢购任务正在运行,请勿重复提交"}
}