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