feat(proxy): 增加代理池演练并修复账号密码提取,发布 0.5.1
This commit is contained in:
+3
-2
@@ -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 &&
|
||||
|
||||
@@ -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()
|
||||
}
|
||||
}
|
||||
@@ -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))
|
||||
}
|
||||
}
|
||||
@@ -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()
|
||||
}
|
||||
@@ -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("<html>proxy error</html>", 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)
|
||||
}
|
||||
}
|
||||
@@ -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"
|
||||
|
||||
@@ -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}
|
||||
|
||||
@@ -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(
|
||||
|
||||
@@ -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()
|
||||
|
||||
@@ -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
|
||||
|
||||
@@ -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"`
|
||||
}
|
||||
Reference in New Issue
Block a user