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
+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}
}