From 635d80e63e4cfcd9a741b8a37ac9d1a807a16330 Mon Sep 17 00:00:00 2001 From: yml Date: Fri, 20 Nov 2026 12:08:13 +0800 Subject: [PATCH] =?UTF-8?q?feat(proxy):=20=E5=A2=9E=E5=8A=A0=E4=BB=A3?= =?UTF-8?q?=E7=90=86=E6=B1=A0=E6=BC=94=E7=BB=83=E5=B9=B6=E4=BF=AE=E5=A4=8D?= =?UTF-8?q?=E8=B4=A6=E5=8F=B7=E5=AF=86=E7=A0=81=E6=8F=90=E5=8F=96=EF=BC=8C?= =?UTF-8?q?=E5=8F=91=E5=B8=83=200.5.1?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../src/components/modal/SettingsModal.vue | 255 +++++++++++++- internal/app/app.go | 5 +- internal/app/app_proxy_drill.go | 312 ++++++++++++++++++ internal/app/app_proxy_drill_test.go | 49 +++ internal/client/proxy_probe.go | 86 +++++ internal/client/proxy_probe_test.go | 68 ++++ internal/constants/constants.go | 2 +- internal/proxy/ipzan.go | 27 ++ internal/proxy/service_test.go | 29 ++ internal/seckill/proxy_groups.go | 12 +- internal/seckill/proxy_groups_test.go | 5 +- internal/types/proxy_drill.go | 39 +++ wails.json | 4 +- 13 files changed, 874 insertions(+), 19 deletions(-) create mode 100644 internal/app/app_proxy_drill.go create mode 100644 internal/app/app_proxy_drill_test.go create mode 100644 internal/client/proxy_probe.go create mode 100644 internal/client/proxy_probe_test.go create mode 100644 internal/types/proxy_drill.go diff --git a/frontend/src/components/modal/SettingsModal.vue b/frontend/src/components/modal/SettingsModal.vue index 6e9fd1e..adbd460 100644 --- a/frontend/src/components/modal/SettingsModal.vue +++ b/frontend/src/components/modal/SettingsModal.vue @@ -2,7 +2,8 @@ import { computed, nextTick, ref } from 'vue' import { useSettings, type RetryStage } from '@/composables/useSettings' import { useToast } from '@/composables/useToast' -import { TestProxy, FetchProxy } from '@wails/go/app/App' +import { TestProxy, FetchProxy, RunProxyDrill } from '@wails/go/app/App' +import { types } from '@wails/go/models' const { settings, loadSettings, saveSettings } = useSettings() const { showToast } = useToast() @@ -36,6 +37,9 @@ const formData = ref({ const testingProxy = ref(false) const testProxyResult = ref<{ success?: boolean; text?: string; error?: string } | null>(null) const fetchingProxy = ref(false) +const drillAccounts = ref(30) +const drillRunning = ref(false) +const drillResult = ref(null) const proxyOverviewTitle = computed(() => ( formData.value.proxyEnabled ? '代理已启用' : '代理未启用' @@ -127,6 +131,32 @@ const handleFetchProxy = async () => { } } +const handleProxyDrill = async () => { + if (!formData.value.proxyApi.trim()) { + showToast('无法演练', '请先填写代理提取 API', 'error') + return + } + drillRunning.value = true + drillResult.value = null + try { + const saved = await saveSettings(formData.value) + if (!saved) return + const res = await RunProxyDrill(types.ProxyDrillRequest.createFrom({ + simulatedAccounts: drillAccounts.value + })) + drillResult.value = res + if (res.success) { + showToast('演练通过', `活动代理 ${res.healthyActive}/${res.activeCount},备用代理 ${res.healthyStandby}/${res.standbyCount}`, 'success', 5000) + } else { + showToast('演练未通过', res.error || '部分活动代理未通过业务请求验证', 'error', 6000) + } + } catch (e: any) { + showToast('演练失败', e.message || String(e), 'error') + } finally { + drillRunning.value = false + } +} + const open = async (section = '') => { await loadSettings() formData.value = { @@ -134,6 +164,7 @@ const open = async (section = '') => { retryStages: settings.value.retryStages.map(s => ({ ...s })) } testProxyResult.value = null + drillResult.value = null visible.value = true if (section === 'proxy') { await nextTick() @@ -301,7 +332,7 @@ defineExpose({ open, close })
- 品赞会自动识别并请求当前分组所需数量;账号密码模式请生成带 mode=auth 的 API,账号密码会写入代理 URL。注意:账号密码授权仍需先将执行提取的本机 IP 加入品赞白名单,白名单只用于提取 API。 + 品赞会自动识别并请求当前分组所需数量;账号密码模式请生成带 mode=auth 的 API,程序会自动改用 JSON 以取得每个代理的账号密码。账号密码授权仍需先将执行提取的本机 IP 加入品赞白名单,白名单只用于提取 API。
@@ -312,7 +343,79 @@ defineExpose({ open, close }) 定时抢购触发前 30 秒自动预取并校验代理
- 仅对定时抢购生效。按“每组账号数”提取对应数量,先测斗鱼时间接口再启用;立即抢购会在开始前同步准备代理。建议使用 10 分钟以上时长,覆盖抢购与最长 180 秒支付。 + 仅对定时抢购生效。按“每组账号数”提取对应数量并校验;立即抢购会在开始前同步准备代理。建议使用 10 分钟以上时长,覆盖抢购与最长 180 秒支付。 +
+ + +
+
+
+
代理池演练
+
+ 手册活动使用只读商品接口,和平小店使用轻量接口兜底;不读取 CK,不锁单、不支付、不消耗账号积分。演练会先保存当前设置。 +
+
+
+ + +
+
+ +
+
+ {{ drillResult.success ? '演练通过' : '演练未通过' }} + + {{ drillResult.simulatedAccounts }} 账号 → + {{ drillResult.activeCount }} 活动 + {{ drillResult.standbyCount }} 备用 + + 健康:{{ drillResult.healthyActive }}/{{ drillResult.activeCount }} 活动,{{ drillResult.healthyStandby }}/{{ drillResult.standbyCount }} 备用 + 耗时 {{ drillResult.durationMs }}ms +
+
{{ drillResult.error }}
+
+ 探测接口:{{ drillResult.endpoint }} +
+ +
+
+
+ + {{ item.role }} {{ item.group }} + + {{ item.success }}/{{ item.requests }} 成功 +
+
{{ item.ip || item.proxy }} · {{ item.region }}
+
+ 平均 {{ item.avgLatencyMs }}ms + 最慢 {{ item.maxLatencyMs }}ms + 流量 {{ item.bytes }}B + {{ item.provider }} +
+
{{ item.error }}
+
+
@@ -1054,6 +1157,152 @@ defineExpose({ open, close }) font-weight: 500; } +.proxy-drill { + margin: 14px 0; + padding: 12px; + border: 1px solid var(--accent-border); + border-radius: var(--radius-sm); + background: var(--accent-soft); +} + +.proxy-drill-head { + display: flex; + align-items: flex-start; + justify-content: space-between; + gap: 12px; + flex-wrap: wrap; +} + +.proxy-drill-title { + margin-bottom: 4px; + color: var(--text-strong); + font-size: 13px; + font-weight: 600; +} + +.proxy-drill-actions { + display: flex; + align-items: flex-end; + gap: 8px; +} + +.proxy-drill-account { + display: flex; + flex-direction: column; + gap: 4px; + color: var(--muted); + font-size: 11px; +} + +.proxy-drill-account .form-input { + width: 88px; +} + +.proxy-drill-result { + margin-top: 12px; + padding-top: 12px; + border-top: 1px solid var(--accent-border); +} + +.proxy-drill-summary { + display: flex; + align-items: center; + gap: 8px 12px; + flex-wrap: wrap; + font-size: 11.5px; +} + +.proxy-drill-summary strong { + font-size: 13px; +} + +.proxy-drill-summary.success strong { + color: var(--success); +} + +.proxy-drill-summary.error strong, +.proxy-drill-error, +.proxy-drill-item-error { + color: var(--danger); +} + +.proxy-drill-error { + margin-top: 8px; + font-size: 12px; +} + +.proxy-drill-endpoint { + margin-top: 8px; + overflow: hidden; + color: var(--muted); + font-size: 10.5px; + text-overflow: ellipsis; + white-space: nowrap; +} + +.proxy-drill-list { + display: flex; + flex-direction: column; + gap: 6px; + margin-top: 10px; +} + +.proxy-drill-item { + padding: 9px 10px; + border: 1px solid var(--line); + border-radius: var(--radius-sm); + background: var(--surface); +} + +.proxy-drill-item.healthy { + border-color: var(--success); +} + +.proxy-drill-item-head, +.proxy-drill-metrics { + display: flex; + align-items: center; + justify-content: space-between; + gap: 8px; + flex-wrap: wrap; +} + +.proxy-drill-item-head { + font-size: 12px; +} + +.proxy-drill-role { + color: var(--accent); + font-weight: 600; +} + +.proxy-drill-role.standby { + color: var(--warning); +} + +.proxy-drill-proxy { + margin-top: 5px; + overflow: hidden; + color: var(--text-strong); + font-size: 11.5px; + text-overflow: ellipsis; + white-space: nowrap; +} + +.proxy-drill-metrics { + justify-content: flex-start; + margin-top: 5px; + color: var(--muted); + font-size: 10.5px; +} + +.proxy-drill-item-error { + margin-top: 5px; + font-size: 10.5px; + line-height: 1.4; + word-break: break-word; +} + .proxy-config-area { margin-top: 14px; padding: 14px; diff --git a/internal/app/app.go b/internal/app/app.go index 655a934..01ae3fb 100644 --- a/internal/app/app.go +++ b/internal/app/app.go @@ -48,7 +48,8 @@ type App struct { productsAt time.Time // accountsMu 串行化内部账号库修改,避免并发导入、编辑或删除互相覆盖。 - accountsMu sync.Mutex + accountsMu sync.Mutex + proxyDrillMu sync.Mutex closeLog func() closeTrace func() @@ -256,7 +257,7 @@ func (a *App) Startup(ctx context.Context) { proxyURL = a.effectiveProxyURL(a.getCfg()) if proxyURL != "" { - if _, err := a.getClient().ProbeServerTime(); err != nil { + if _, err := a.getClient().ProbeProxyEndpoint(); err != nil { logger.LogEvent("代理", "warn", "启动代理探测失败: %v", err) currentCfg := a.getCfg() if currentCfg.ProxyEnabled && diff --git a/internal/app/app_proxy_drill.go b/internal/app/app_proxy_drill.go new file mode 100644 index 0000000..c0e0a8f --- /dev/null +++ b/internal/app/app_proxy_drill.go @@ -0,0 +1,312 @@ +package app + +import ( + "fmt" + "strings" + "sync" + "time" + + "douyu_go/internal/activity" + "douyu_go/internal/client" + "douyu_go/internal/constants" + "douyu_go/internal/logger" + proxyprovider "douyu_go/internal/proxy" + "douyu_go/internal/types" +) + +const ( + defaultProxyDrillAccounts = 30 + maxProxyDrillAccounts = 200 + proxyDrillExtractRounds = 3 + proxyDrillMaxRounds = 20 +) + +type proxyDrillPlan struct { + accounts int + groupSize int + active int + standby int +} + +type proxyDrillTarget struct { + role string + group int + requests int + endpoint proxyprovider.Endpoint +} + +type proxyDrillProbe struct { + success bool + latency time.Duration + bytes int64 + err error +} + +// RunProxyDrill 使用无需 Cookie 的商品接口验证代理池,不读取账号、不锁单、不支付。 +func (a *App) RunProxyDrill(req types.ProxyDrillRequest) types.ProxyDrillResult { + a.proxyDrillMu.Lock() + defer a.proxyDrillMu.Unlock() + + startedAt := time.Now() + cfg := a.getCfg() + if strings.TrimSpace(cfg.ProxyAPI) == "" { + return types.ProxyDrillResult{Error: "请先填写代理提取 API"} + } + if scheduler := a.getScheduler(); scheduler != nil && scheduler.IsRunning() { + return types.ProxyDrillResult{Error: "抢购任务运行中,不能同时执行代理池演练"} + } + + act := activity.Get(cfg.Activity) + if act == nil { + act = activity.Default() + } + if strings.TrimSpace(act.StoreHomeURL) == "" { + return types.ProxyDrillResult{Error: "当前活动没有可用于代理演练的只读接口"} + } + probeEndpoint := act.StoreHomeURL + if act.IsXiaodian { + probeEndpoint = cfg.TimeAPIURL + } + + accounts := req.SimulatedAccounts + if accounts <= 0 { + accounts = defaultProxyDrillAccounts + } + if accounts > maxProxyDrillAccounts { + accounts = maxProxyDrillAccounts + } + groupSize := cfg.AccountsPerProxy + if groupSize <= 0 { + groupSize = constants.DefaultAccountsPerProxy + } + if groupSize > constants.MaxAccountsPerProxy { + groupSize = constants.MaxAccountsPerProxy + } + plan := proxyDrillPlan{ + accounts: accounts, + groupSize: groupSize, + active: (accounts + groupSize - 1) / groupSize, + } + plan.standby = proxyDrillStandbyCount(plan.active, cfg.ProxySparePercent) + desired := plan.active + plan.standby + + result := types.ProxyDrillResult{ + Endpoint: probeEndpoint, + SimulatedAccounts: plan.accounts, + AccountsPerProxy: plan.groupSize, + ActiveCount: plan.active, + StandbyCount: plan.standby, + Items: []types.ProxyDrillItem{}, + } + logger.LogEvent("代理", "info", "开始代理池演练: 模拟账号=%d,活动代理=%d,备用代理=%d,接口=%s", + plan.accounts, plan.active, plan.standby, probeEndpoint) + + endpoints, err := a.extractProxyDrillPool(cfg, desired) + if len(endpoints) < plan.active { + result.Error = fmt.Sprintf("代理数量不足: 需要%d个活动代理,实际%d个", plan.active, len(endpoints)) + if err != nil { + result.Error += ";" + err.Error() + } + result.DurationMs = time.Since(startedAt).Milliseconds() + return result + } + + targets := buildProxyDrillTargets(plan, endpoints) + result.Items = make([]types.ProxyDrillItem, len(targets)) + var wg sync.WaitGroup + for i, target := range targets { + wg.Add(1) + go func(index int, target proxyDrillTarget) { + defer wg.Done() + result.Items[index] = a.runProxyDrillTarget(act, cfg.TimeAPIURL, target) + }(i, target) + } + wg.Wait() + + for _, item := range result.Items { + if item.Success != item.Requests || item.Requests == 0 { + continue + } + if item.Role == "活动代理" { + result.HealthyActive++ + } else { + result.HealthyStandby++ + } + } + result.Success = result.HealthyActive >= plan.active && result.HealthyStandby >= plan.standby + if !result.Success { + result.Error = "活动代理或备用代理未全部通过演练" + } + result.DurationMs = time.Since(startedAt).Milliseconds() + logger.LogEvent("代理", "success", "代理池演练完成: 成功=%v,活动代理=%d/%d,备用代理=%d/%d,耗时=%s", + result.Success, result.HealthyActive, plan.active, result.HealthyStandby, plan.standby, + time.Duration(result.DurationMs)*time.Millisecond) + return result +} + +func proxyDrillStandbyCount(activeCount, sparePercent int) int { + if activeCount <= 0 { + return 0 + } + if sparePercent <= 0 { + sparePercent = constants.DefaultProxySparePercent + } + if sparePercent > constants.MaxProxySparePercent { + sparePercent = constants.MaxProxySparePercent + } + count := (activeCount*sparePercent + 99) / 100 + if count < 1 { + count = 1 + } + return count +} + +func (a *App) extractProxyDrillPool(cfg types.Config, desired int) ([]proxyprovider.Endpoint, error) { + rounds := proxyDrillExtractRounds + if desired > rounds { + rounds = desired + } + if rounds > proxyDrillMaxRounds { + rounds = proxyDrillMaxRounds + } + + seen := make(map[string]struct{}, desired) + endpoints := make([]proxyprovider.Endpoint, 0, desired) + var lastErr error + for round := 0; round < rounds && len(endpoints) < desired; round++ { + batch, err := extractProxyEndpoints(cfg, cfg.ProxyAPI, desired-len(endpoints)) + if err != nil { + lastErr = err + if proxyprovider.IsFatal(err) { + break + } + } + for _, endpoint := range batch { + if !endpoint.ExpiresAt.IsZero() && !time.Now().Before(endpoint.ExpiresAt) { + continue + } + if _, exists := seen[endpoint.URL]; exists { + continue + } + seen[endpoint.URL] = struct{}{} + endpoints = append(endpoints, endpoint) + if len(endpoints) >= desired { + break + } + } + if round < rounds-1 { + time.Sleep(200 * time.Millisecond) + } + } + if len(endpoints) < desired { + if lastErr != nil { + return endpoints, lastErr + } + return endpoints, fmt.Errorf("代理 API 仅返回 %d/%d 个有效代理", len(endpoints), desired) + } + return endpoints, nil +} + +func buildProxyDrillTargets(plan proxyDrillPlan, endpoints []proxyprovider.Endpoint) []proxyDrillTarget { + targets := make([]proxyDrillTarget, 0, len(endpoints)) + remaining := plan.accounts + for i := 0; i < plan.active; i++ { + requests := min(plan.groupSize, remaining) + if requests < 1 { + requests = 1 + } + remaining -= requests + targets = append(targets, proxyDrillTarget{ + role: "活动代理", + group: i + 1, + requests: requests, + endpoint: endpoints[i], + }) + } + for i := plan.active; i < len(endpoints); i++ { + requests := min(plan.groupSize, plan.accounts) + if requests < 1 { + requests = 1 + } + targets = append(targets, proxyDrillTarget{ + role: "备用代理", + group: i - plan.active + 1, + requests: requests, + endpoint: endpoints[i], + }) + } + return targets +} + +func (a *App) runProxyDrillTarget(act *activity.Activity, timeAPIURL string, target proxyDrillTarget) types.ProxyDrillItem { + item := types.ProxyDrillItem{ + Role: target.role, + Group: target.group, + Proxy: client.MaskProxyURL(target.endpoint.URL), + IP: target.endpoint.IP, + Region: target.endpoint.Region, + Provider: target.endpoint.Provider, + Requests: target.requests, + } + if !target.endpoint.ExpiresAt.IsZero() { + item.ExpiresAt = target.endpoint.ExpiresAt.UnixMilli() + } + proxyClient, err := client.NewWithProxy(act, timeAPIURL, a.getTrace(), target.endpoint.URL) + if err != nil { + item.Failed = target.requests + item.Error = err.Error() + return item + } + defer closeProxyDrillClient(proxyClient) + + results := make(chan proxyDrillProbe, target.requests) + var wg sync.WaitGroup + for i := 0; i < target.requests; i++ { + wg.Add(1) + go func() { + defer wg.Done() + probe, err := proxyClient.ProbeProxyEndpoint() + results <- proxyDrillProbe{ + success: err == nil, + latency: probe.Latency, + bytes: probe.Bytes, + err: err, + } + }() + } + wg.Wait() + close(results) + + var totalLatency time.Duration + for probe := range results { + if probe.success { + item.Success++ + } else { + item.Failed++ + if item.Error == "" && probe.err != nil { + item.Error = probe.err.Error() + } + } + totalLatency += probe.latency + item.Bytes += probe.bytes + if probe.latency > time.Duration(item.MaxLatencyMs)*time.Millisecond { + item.MaxLatencyMs = probe.latency.Milliseconds() + } + } + if target.requests > 0 { + item.AvgLatencyMs = (totalLatency / time.Duration(target.requests)).Milliseconds() + } + return item +} + +func closeProxyDrillClient(cli *client.DouyuClient) { + if cli == nil { + return + } + if cli.HTTPClient != nil { + cli.HTTPClient.CloseIdleConnections() + } + if cli.TimeHTTPClient != nil { + cli.TimeHTTPClient.CloseIdleConnections() + } +} diff --git a/internal/app/app_proxy_drill_test.go b/internal/app/app_proxy_drill_test.go new file mode 100644 index 0000000..84985d5 --- /dev/null +++ b/internal/app/app_proxy_drill_test.go @@ -0,0 +1,49 @@ +package app + +import ( + "fmt" + "net/http" + "net/http/httptest" + "testing" + + proxyprovider "douyu_go/internal/proxy" + "douyu_go/internal/types" +) + +func TestProxyDrillPlanAndTargets(t *testing.T) { + if got := proxyDrillStandbyCount(6, 25); got != 2 { + t.Fatalf("proxyDrillStandbyCount(6, 25)=%d, want 2", got) + } + plan := proxyDrillPlan{accounts: 30, groupSize: 5, active: 6, standby: 2} + endpoints := make([]proxyprovider.Endpoint, 8) + targets := buildProxyDrillTargets(plan, endpoints) + if len(targets) != 8 { + t.Fatalf("targets=%d, want 8", len(targets)) + } + totalRequests := 0 + for _, target := range targets { + totalRequests += target.requests + } + if totalRequests != 40 { + t.Fatalf("totalRequests=%d, want 40 virtual requests", totalRequests) + } +} + +func TestExtractProxyDrillPoolDeduplicates(t *testing.T) { + apiServer := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { + for i := 0; i < 8; i++ { + fmt.Fprintf(w, "http://127.0.0.1:%d\n", 20000+i) + } + })) + defer apiServer.Close() + + app := &App{} + cfg := types.Config{ProxyAPI: apiServer.URL} + endpoints, err := app.extractProxyDrillPool(cfg, 8) + if err != nil { + t.Fatalf("extractProxyDrillPool() error = %v", err) + } + if len(endpoints) != 8 { + t.Fatalf("endpoints=%d, want 8", len(endpoints)) + } +} diff --git a/internal/client/proxy_probe.go b/internal/client/proxy_probe.go new file mode 100644 index 0000000..da3bf3b --- /dev/null +++ b/internal/client/proxy_probe.go @@ -0,0 +1,86 @@ +package client + +import ( + "encoding/json" + "errors" + "fmt" + "net/http" + "strings" + "time" + + "douyu_go/internal/utils" +) + +// BusinessProbeResult 是经过代理访问真实业务接口的探测结果。 +type BusinessProbeResult struct { + Latency time.Duration + Bytes int64 +} + +// ProbeBusinessEndpoint 使用当前代理访问无需账号的商品接口。 +// 该接口只读、不下单、不支付,但会经过与真实抢购相同的斗鱼业务域名和浏览器请求头。 +func (c *DouyuClient) ProbeBusinessEndpoint() (BusinessProbeResult, error) { + if c == nil || c.Activity == nil { + return BusinessProbeResult{}, errors.New("活动配置未初始化") + } + if strings.TrimSpace(c.Activity.StoreHomeURL) == "" { + return BusinessProbeResult{}, errors.New("当前活动没有可用的商品探测接口") + } + + target := utils.AppendCacheBuster(c.Activity.StoreHomeURL) + req, err := http.NewRequest(http.MethodGet, target, nil) + if err != nil { + return BusinessProbeResult{}, err + } + if c.Activity.IsXiaodian { + c.setXiaodianHeaders(req) + } else { + c.setBrowserHeaders(req, "") + } + + start := time.Now() + body, err := c.doText(req, "代理业务演练", true) + latency := time.Since(start) + if err != nil { + return BusinessProbeResult{Latency: latency}, err + } + if len(body) < 32 { + return BusinessProbeResult{Latency: latency, Bytes: int64(len(body))}, + fmt.Errorf("响应体过短: %d 字节", len(body)) + } + if !json.Valid(body) { + return BusinessProbeResult{Latency: latency, Bytes: int64(len(body))}, + errors.New("响应不是有效 JSON,可能被代理或网关改写") + } + + var payload struct { + Error int `json:"error"` + Message string `json:"message"` + Msg string `json:"msg"` + } + if err := json.Unmarshal(body, &payload); err == nil && payload.Error != 0 { + message := strings.TrimSpace(payload.Message) + if message == "" { + message = strings.TrimSpace(payload.Msg) + } + if message == "" { + message = "业务接口返回错误" + } + return BusinessProbeResult{Latency: latency, Bytes: int64(len(body))}, + fmt.Errorf("%s (error=%d)", message, payload.Error) + } + return BusinessProbeResult{ + Latency: latency, + Bytes: int64(len(body)), + }, nil +} + +// ProbeProxyEndpoint 选择当前活动适合的代理质量探测方式。 +// 手册活动访问真实商品列表接口;和平小店角色参数更复杂,保留时间接口兜底。 +func (c *DouyuClient) ProbeProxyEndpoint() (BusinessProbeResult, error) { + if c != nil && c.Activity != nil && c.Activity.IsXiaodian { + sample, err := c.ProbeServerTime() + return BusinessProbeResult{Latency: sample.RTT}, err + } + return c.ProbeBusinessEndpoint() +} diff --git a/internal/client/proxy_probe_test.go b/internal/client/proxy_probe_test.go new file mode 100644 index 0000000..ca7fa06 --- /dev/null +++ b/internal/client/proxy_probe_test.go @@ -0,0 +1,68 @@ +package client + +import ( + "net/http" + "net/http/httptest" + "strings" + "testing" + + "douyu_go/internal/activity" + "douyu_go/internal/trace" +) + +func TestProbeBusinessEndpointAcceptsJSON(t *testing.T) { + server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { + w.Header().Set("Content-Type", "application/json") + _, _ = w.Write([]byte(`{"error":0,"msg":"","data":{"list":[{"commodityId":"bag-1","commodityName":"测试商品","count":100}]}}`)) + })) + defer server.Close() + + act := *activity.Get("peace") + act.StoreHomeURL = server.URL + cli, err := NewWithProxy(&act, server.URL, &trace.Recorder{}, "") + if err != nil { + t.Fatal(err) + } + result, err := cli.ProbeBusinessEndpoint() + if err != nil { + t.Fatalf("ProbeBusinessEndpoint() error = %v", err) + } + if result.Bytes < 64 || result.Latency <= 0 { + t.Fatalf("ProbeBusinessEndpoint()=%+v, want positive latency and body size", result) + } +} + +func TestProbeBusinessEndpointRejectsNonJSON(t *testing.T) { + server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { + _, _ = w.Write([]byte(strings.Repeat("proxy error", 8))) + })) + defer server.Close() + + act := *activity.Get("peace") + act.StoreHomeURL = server.URL + cli, err := NewWithProxy(&act, server.URL, &trace.Recorder{}, "") + if err != nil { + t.Fatal(err) + } + if _, err := cli.ProbeBusinessEndpoint(); err == nil { + t.Fatal("ProbeBusinessEndpoint() should reject non-JSON responses") + } +} + +func TestProbeBusinessEndpointAcceptsShortPeacematchResponse(t *testing.T) { + server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { + w.Header().Set("Content-Type", "application/json") + _, _ = w.Write([]byte(`{"error":0,"msg":"","data":{"score":0,"list":[],"peacePass":0}}`)) + })) + defer server.Close() + + act := *activity.Get("peacematch") + act.StoreHomeURL = server.URL + cli, err := NewWithProxy(&act, server.URL, &trace.Recorder{}, "") + if err != nil { + t.Fatal(err) + } + if _, err := cli.ProbeBusinessEndpoint(); err != nil { + t.Fatalf("ProbeBusinessEndpoint() rejected short valid JSON: %v", err) + } +} diff --git a/internal/constants/constants.go b/internal/constants/constants.go index 3e27b18..add6149 100644 --- a/internal/constants/constants.go +++ b/internal/constants/constants.go @@ -3,7 +3,7 @@ package constants import "time" // Version 应用版本号,与 wails.json version 同步。 -const Version = "0.5.0" +const Version = "0.5.1" // BuildTime 构建时间,由编译时 ldflags 注入。 var BuildTime = "unknown" diff --git a/internal/proxy/ipzan.go b/internal/proxy/ipzan.go index c993c4b..5726030 100644 --- a/internal/proxy/ipzan.go +++ b/internal/proxy/ipzan.go @@ -44,6 +44,10 @@ func (p *IPZanProvider) RequestURL(apiURL string, count int) (string, error) { } query := u.Query() query.Set("num", strconv.Itoa(count)) + if strings.EqualFold(strings.TrimSpace(query.Get("mode")), "auth") { + // txt 只返回 IP:端口,会丢失每个代理各自的账号密码,随后必然触发 407。 + query.Set("format", "json") + } u.RawQuery = query.Encode() return u.String(), nil } @@ -74,6 +78,9 @@ func (p *IPZanProvider) Parse(apiURL string, body []byte) ([]Endpoint, error) { if len(payload.Data) > 0 { endpoints := parseEndpointJSON(payload.Data, protocolFromAPI(apiURL)) if len(endpoints) > 0 { + if err := validateIPZanAuthEndpoints(apiURL, endpoints); err != nil { + return nil, err + } return endpoints, nil } } @@ -87,9 +94,29 @@ func (p *IPZanProvider) Parse(apiURL string, body []byte) ([]Endpoint, error) { Fatal: false, } } + if err := validateIPZanAuthEndpoints(apiURL, endpoints); err != nil { + return nil, err + } return endpoints, nil } +func validateIPZanAuthEndpoints(apiURL string, endpoints []Endpoint) error { + u, err := url.Parse(strings.TrimSpace(apiURL)) + if err != nil || !strings.EqualFold(strings.TrimSpace(u.Query().Get("mode")), "auth") { + return nil + } + for _, endpoint := range endpoints { + if strings.TrimSpace(endpoint.Username) == "" || strings.TrimSpace(endpoint.Password) == "" { + return &ProviderError{ + Provider: "品赞", + Message: "账号密码模式返回缺少账号或密码;请移除 format=txt,让程序使用 JSON 格式提取", + Fatal: true, + } + } + } + return 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} diff --git a/internal/proxy/service_test.go b/internal/proxy/service_test.go index 653dab6..399e414 100644 --- a/internal/proxy/service_test.go +++ b/internal/proxy/service_test.go @@ -24,6 +24,35 @@ func TestIPZanRequestURLSetsRequestedCount(t *testing.T) { } } +func TestIPZanRequestURLForcesJSONForAuthMode(t *testing.T) { + raw := "https://service.ipzan.com/core-extract?num=1&mode=auth&format=txt&secret=x" + got, err := NewIPZanProvider().RequestURL(raw, 8) + if err != nil { + t.Fatal(err) + } + u, err := url.Parse(got) + if err != nil { + t.Fatal(err) + } + if mode := u.Query().Get("mode"); mode != "auth" { + t.Fatalf("mode=%q, want auth", mode) + } + if format := u.Query().Get("format"); format != "json" { + t.Fatalf("format=%q, want json", format) + } +} + +func TestIPZanParseRejectsAuthModeWithoutCredentials(t *testing.T) { + body := []byte(`{"code":0,"status":200,"data":{"list":[{"ip":"1.2.3.4","port":8001}]}}`) + _, err := NewIPZanProvider().Parse( + "https://service.ipzan.com/core-extract?mode=auth&protocol=1", + body, + ) + if err == nil || !IsFatal(err) { + t.Fatalf("expected fatal missing-credentials error, got %v", err) + } +} + func TestIPZanParseTextMultiple(t *testing.T) { body := []byte("1.2.3.4:8001\r\n5.6.7.8:8002\r\n") got, err := NewIPZanProvider().Parse( diff --git a/internal/seckill/proxy_groups.go b/internal/seckill/proxy_groups.go index 594f438..53f1eb4 100644 --- a/internal/seckill/proxy_groups.go +++ b/internal/seckill/proxy_groups.go @@ -906,20 +906,16 @@ func (s *Scheduler) validateProxy(base *client.DouyuClient, proxyURL, source str } var lastErr error for attempt := 0; attempt < proxyValidateAttempts; attempt++ { - sample, sampleErr := cli.ProbeServerTime() - if sampleErr == nil { + probe, probeErr := cli.ProbeProxyEndpoint() + if probeErr == nil { return validatedProxy{ url: proxyURL, client: cli, - rtt: sample.RTT, + rtt: probe.Latency, source: source, }, nil } - if sampleErr != nil { - lastErr = sampleErr - } else { - lastErr = errors.New("斗鱼时间接口测试失败") - } + lastErr = probeErr } if cli.HTTPClient != nil { cli.HTTPClient.CloseIdleConnections() diff --git a/internal/seckill/proxy_groups_test.go b/internal/seckill/proxy_groups_test.go index 3556a0a..008c005 100644 --- a/internal/seckill/proxy_groups_test.go +++ b/internal/seckill/proxy_groups_test.go @@ -5,7 +5,6 @@ import ( "fmt" "net/http" "net/http/httptest" - "strconv" "sync/atomic" "testing" "time" @@ -98,6 +97,7 @@ func TestPrepareProxyGroupsRoutesAccountsToDistinctClients(t *testing.T) { defer apiServer.Close() act := *activity.Get("peace") + act.StoreHomeURL = timeAPIURL base := client.New(&act, timeAPIURL, &trace.Recorder{}, "") scheduler := New(base, types.AppPaths{}, nil, 8, 1, nil, types.SeckillModeAggressive) scheduler.UpdateProxySettings(types.Config{ @@ -156,9 +156,8 @@ func TestPrepareProxyGroupsRoutesAccountsToDistinctClients(t *testing.T) { 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) + fmt.Fprint(w, `{"error":0,"msg":"","data":{"list":[{"commodityId":"bag-1","commodityName":"测试商品"}]}}`) })) t.Cleanup(server.Close) return server diff --git a/internal/types/proxy_drill.go b/internal/types/proxy_drill.go new file mode 100644 index 0000000..c0d3278 --- /dev/null +++ b/internal/types/proxy_drill.go @@ -0,0 +1,39 @@ +package types + +// ProxyDrillRequest 配置一次不依赖真实账号的代理池演练。 +type ProxyDrillRequest struct { + SimulatedAccounts int `json:"simulatedAccounts"` +} + +// ProxyDrillItem 是单个代理的演练结果。 +type ProxyDrillItem struct { + Role string `json:"role"` // 活动代理 / 备用代理 + Group int `json:"group"` + Proxy string `json:"proxy"` + IP string `json:"ip"` + Region string `json:"region"` + Provider string `json:"provider"` + Requests int `json:"requests"` + Success int `json:"success"` + Failed int `json:"failed"` + AvgLatencyMs int64 `json:"avgLatencyMs"` + MaxLatencyMs int64 `json:"maxLatencyMs"` + Bytes int64 `json:"bytes"` + ExpiresAt int64 `json:"expiresAt,omitempty"` + Error string `json:"error,omitempty"` +} + +// ProxyDrillResult 是代理池演练汇总。 +type ProxyDrillResult struct { + Success bool `json:"success"` + Error string `json:"error,omitempty"` + Endpoint string `json:"endpoint"` + SimulatedAccounts int `json:"simulatedAccounts"` + AccountsPerProxy int `json:"accountsPerProxy"` + ActiveCount int `json:"activeCount"` + StandbyCount int `json:"standbyCount"` + HealthyActive int `json:"healthyActive"` + HealthyStandby int `json:"healthyStandby"` + DurationMs int64 `json:"durationMs"` + Items []ProxyDrillItem `json:"items"` +} diff --git a/wails.json b/wails.json index 820cc3f..6122790 100644 --- a/wails.json +++ b/wails.json @@ -2,7 +2,7 @@ "$schema": "https://wails.io/schemas/config.v2.json", "name": "douyu_go", "outputfilename": "douyu_go", - "version": "0.5.0", + "version": "0.5.1", "frontend:dir": "frontend", "frontend:install": "npm install", "frontend:build": "npm run build", @@ -15,6 +15,6 @@ "email": "yml@example.com" }, "info": { - "productVersion": "0.5.0" + "productVersion": "0.5.1" } }