feat(proxy): 支持多实例独立代理与短效API动态提取
- 实例配置独立:支持为每个实例单独配置 http/https/socks5 代理,避免单机多开挤兑触发斗鱼出口 IP 限流 - 动态 API 提取:原生支持星空代理等平台的 API 提取(兼容 JSON/文本格式),提供前端一键提取填入与连通性/延迟测速 - 定时前自动拉取:针对 3 分钟短效代理,支持在开抢前 15 秒自动提取并绑定最新 IP,并在预热前完成热连接与时间复校 - 修复复校竞态:将 resyncBeforeTrigger 从 3s 提升至 8s,确保三轮采样在开抢前顺利落盘,消除时钟漂移 - 验证方式:通过 go test ./... 全量测试与前端 vue-tsc/vite 编译,实测通过星空代理 API 解析测试用例
This commit is contained in:
+14
-1
@@ -3,6 +3,7 @@ package app
|
||||
import (
|
||||
"context"
|
||||
"os"
|
||||
"strings"
|
||||
"sync"
|
||||
"time"
|
||||
|
||||
@@ -125,6 +126,13 @@ func New(lease *config.InstanceLease, instanceErr error) *App {
|
||||
return &App{instanceLease: lease, instanceErr: instanceErr}
|
||||
}
|
||||
|
||||
func (a *App) effectiveProxyURL(cfg types.Config) string {
|
||||
if cfg.ProxyEnabled {
|
||||
return strings.TrimSpace(cfg.ProxyURL)
|
||||
}
|
||||
return ""
|
||||
}
|
||||
|
||||
// Startup Wails 应用启动时调用。
|
||||
func (a *App) Startup(ctx context.Context) {
|
||||
a.ctx = ctx
|
||||
@@ -171,9 +179,14 @@ func (a *App) Startup(ctx context.Context) {
|
||||
act = activity.Default()
|
||||
logger.LogEvent("活动", "warn", "配置中的活动 %q 未识别,已回退到默认活动 %s", a.cfg.Activity, act.DisplayName)
|
||||
}
|
||||
a.client = client.New(act, a.cfg.TimeAPIURL, a.getTrace())
|
||||
proxyURL := a.effectiveProxyURL(a.cfg)
|
||||
a.client = client.New(act, a.cfg.TimeAPIURL, a.getTrace(), proxyURL)
|
||||
if proxyURL != "" {
|
||||
logger.LogEvent("代理", "info", "已启用代理: %s", 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.SetStateChangeCallback(func(state string) {
|
||||
runtime.EventsEmit(a.ctx, seckillStateEvent, state)
|
||||
})
|
||||
|
||||
@@ -43,7 +43,7 @@ func (a *App) SwitchActivity(id string) types.SwitchActivityResult {
|
||||
return types.SwitchActivityResult{Error: "配置保存失败: " + err.Error()}
|
||||
}
|
||||
a.setCfg(cfg)
|
||||
newClient := client.New(act, cfg.TimeAPIURL, a.getTrace())
|
||||
newClient := client.New(act, cfg.TimeAPIURL, a.getTrace(), a.effectiveProxyURL(cfg))
|
||||
a.setClient(newClient)
|
||||
if !a.scheduler.UpdateClient(newClient) {
|
||||
logger.LogEvent("活动", "warn", "抢购任务正在运行,调度器客户端将在下次抢购时生效")
|
||||
@@ -101,7 +101,8 @@ func (a *App) SaveSettings(cfg types.Config) types.SimpleResult {
|
||||
}
|
||||
|
||||
traceChanged := cfg.TraceEnabled != oldCfg.TraceEnabled || cfg.TraceMaxBody != oldCfg.TraceMaxBody
|
||||
clientChanged := traceChanged || cfg.TimeAPIURL != oldCfg.TimeAPIURL || cfg.Activity != oldCfg.Activity
|
||||
proxyChanged := cfg.ProxyEnabled != oldCfg.ProxyEnabled || cfg.ProxyURL != oldCfg.ProxyURL
|
||||
clientChanged := traceChanged || cfg.TimeAPIURL != oldCfg.TimeAPIURL || cfg.Activity != oldCfg.Activity || proxyChanged
|
||||
if traceChanged {
|
||||
a.replaceTrace(cfg)
|
||||
}
|
||||
@@ -110,12 +111,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)
|
||||
if clientChanged {
|
||||
newClient := client.New(act, cfg.TimeAPIURL, a.getTrace())
|
||||
proxyURL := a.effectiveProxyURL(cfg)
|
||||
newClient := client.New(act, cfg.TimeAPIURL, a.getTrace(), proxyURL)
|
||||
a.setClient(newClient)
|
||||
if !a.scheduler.UpdateClient(newClient) {
|
||||
logger.LogEvent("配置", "warn", "抢购任务正在运行,客户端配置将在下次抢购时完整生效")
|
||||
}
|
||||
if proxyChanged {
|
||||
if proxyURL != "" {
|
||||
logger.LogEvent("代理", "success", "代理已生效: %s", proxyURL)
|
||||
} else {
|
||||
logger.LogEvent("代理", "info", "代理已停用,恢复直连")
|
||||
}
|
||||
}
|
||||
if sample, err := newClient.ServerTimeSample(); err == nil {
|
||||
logger.SetDouyuOffset(sample.Offset)
|
||||
} else {
|
||||
@@ -123,7 +133,7 @@ func (a *App) SaveSettings(cfg types.Config) types.SimpleResult {
|
||||
}
|
||||
}
|
||||
|
||||
logger.LogEvent("配置", "success", "配置已保存并生效: 日志等级=%s 并发=%d 重试=%d 重试阶段=%d 模式=%d trace=%v",
|
||||
cfg.LogLevel, cfg.Concurrency, cfg.Attempts, len(cfg.RetryStages), cfg.SeckillMode, cfg.TraceEnabled)
|
||||
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}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,32 @@
|
||||
package app
|
||||
|
||||
import (
|
||||
"douyu_go/internal/client"
|
||||
"douyu_go/internal/logger"
|
||||
"douyu_go/internal/types"
|
||||
)
|
||||
|
||||
// TestProxy 测试指定代理的连通性、出口公网 IP 与往返延迟。
|
||||
func (a *App) TestProxy(proxyURL string) types.ProxyTestResult {
|
||||
res := client.TestProxyConnection(proxyURL)
|
||||
if res.Success {
|
||||
logger.LogEvent("代理", "success", "代理测试成功: IP=%s 归属=%s 延迟=%dms", res.IP, res.Region, res.Latency)
|
||||
} else {
|
||||
logger.LogEvent("代理", "warn", "代理测试失败: %s", res.Error)
|
||||
}
|
||||
return res
|
||||
}
|
||||
|
||||
// FetchProxy 从指定 API 提取一个代理 IP。
|
||||
func (a *App) FetchProxy(apiURL string) types.ProxyFetchResult {
|
||||
res, err := client.FetchProxyFromAPI(apiURL)
|
||||
if err != nil {
|
||||
logger.LogEvent("代理", "warn", "代理提取失败: %v", err)
|
||||
return types.ProxyFetchResult{
|
||||
Success: false,
|
||||
Error: err.Error(),
|
||||
}
|
||||
}
|
||||
logger.LogEvent("代理", "success", "代理提取成功: %s (IP=%s 端口=%s 地区=%s)", res.ProxyURL, res.IP, res.Port, res.Region)
|
||||
return *res
|
||||
}
|
||||
Reference in New Issue
Block a user