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
+38 -17
View File
@@ -56,10 +56,23 @@ type xiaodianEmbedCacheItem struct {
expiresAt time.Time
}
// New 创建斗鱼客户端。act 决定使用哪个活动的接口参数;
// timeAPIURL 为空时回退到 constants.DefaultTimeAPI(全局共享的服务端时间接口);
// proxyURL 为可选参数,为空时直连,非空时配置 http/https/socks5 代理。
// New 创建斗鱼客户端。proxyURL 为可选参数,为空时直连。
// 代理地址无效时会记录错误并回退直连,调用方需要严格校验时应使用 NewWithProxy。
func New(act *activity.Activity, timeAPIURL string, tr *trace.Recorder, proxyURL ...string) *DouyuClient {
var effectiveProxy string
if len(proxyURL) > 0 {
effectiveProxy = proxyURL[0]
}
cli, err := NewWithProxy(act, timeAPIURL, tr, effectiveProxy)
if err != nil {
logger.LogEvent("代理", "error", "代理地址无效,客户端已回退直连: %v", err)
cli, _ = NewWithProxy(act, timeAPIURL, tr, "")
}
return cli
}
// NewWithProxy 创建斗鱼客户端并严格校验代理地址。
func NewWithProxy(act *activity.Activity, timeAPIURL string, tr *trace.Recorder, proxyURL string) (*DouyuClient, error) {
if act == nil {
act = activity.Default()
}
@@ -69,20 +82,10 @@ func New(act *activity.Activity, timeAPIURL string, tr *trace.Recorder, proxyURL
if tr == nil {
tr = &trace.Recorder{}
}
var effectiveProxy string
if len(proxyURL) > 0 {
effectiveProxy = proxyURL[0]
}
trsp := &http.Transport{
MaxConnsPerHost: 120,
MaxIdleConns: 120,
MaxIdleConnsPerHost: 80,
IdleConnTimeout: 30 * time.Second,
}
if effectiveProxy != "" {
if u, err := ParseProxyURL(effectiveProxy); err == nil && u != nil {
trsp.Proxy = http.ProxyURL(u)
}
effectiveProxy := strings.TrimSpace(proxyURL)
trsp, err := newHTTPTransport(effectiveProxy)
if err != nil {
return nil, err
}
return &DouyuClient{
Activity: act,
@@ -94,7 +97,25 @@ func New(act *activity.Activity, timeAPIURL string, tr *trace.Recorder, proxyURL
Timeout: 8 * time.Second,
Transport: trsp,
},
}, nil
}
func newHTTPTransport(proxyURL string) (*http.Transport, error) {
trsp := &http.Transport{
MaxConnsPerHost: 120,
MaxIdleConns: 120,
MaxIdleConnsPerHost: 80,
IdleConnTimeout: 30 * time.Second,
}
if strings.TrimSpace(proxyURL) == "" {
return trsp, nil
}
u, err := ParseProxyURL(proxyURL)
if err != nil {
return nil, err
}
trsp.Proxy = http.ProxyURL(u)
return trsp, nil
}
// SetProxy 运行时更新客户端使用的代理。