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
+41 -4
View File
@@ -1,8 +1,13 @@
package app
import (
"context"
"time"
"douyu_go/internal/client"
"douyu_go/internal/constants"
"douyu_go/internal/logger"
proxyprovider "douyu_go/internal/proxy"
"douyu_go/internal/types"
)
@@ -19,14 +24,46 @@ func (a *App) TestProxy(proxyURL string) types.ProxyTestResult {
// FetchProxy 从指定 API 提取一个代理 IP。
func (a *App) FetchProxy(apiURL string) types.ProxyFetchResult {
res, err := client.FetchProxyFromAPI(apiURL)
ctx, cancel := context.WithTimeout(context.Background(), 30*time.Second)
defer cancel()
service := proxyprovider.NewService()
cfg := a.getCfg()
endpoints, err := service.ExtractWithWhitelist(ctx, apiURL, 1, proxyprovider.WhitelistConfig{
APIURL: apiURL,
LoginPassword: cfg.ProxyWhitelistPassword,
SignKey: cfg.ProxyWhitelistSignKey,
ReplaceOldest: cfg.ProxyWhitelistReplace,
AutoWhitelist: cfg.ProxyAutoWhitelist,
})
if err != nil {
logger.LogEvent("代理", "warn", "代理提取失败: %v", err)
return types.ProxyFetchResult{Error: err.Error()}
}
if len(endpoints) == 0 {
return types.ProxyFetchResult{
Success: false,
Error: err.Error(),
Error: "代理 API 未返回有效地址",
}
}
logger.LogEvent("代理", "success", "代理提取成功: %s (IP=%s 端口=%s 地区=%s)", res.ProxyURL, res.IP, res.Port, res.Region)
return *res
endpoint := endpoints[0]
expiresAt := int64(0)
if !endpoint.ExpiresAt.IsZero() {
expiresAt = endpoint.ExpiresAt.UnixMilli()
}
expiresLabel := "未知"
if !endpoint.ExpiresAt.IsZero() {
expiresLabel = endpoint.ExpiresAt.In(constants.CST).Format("2006-01-02 15:04:05")
}
logger.LogEvent("代理", "success", "代理提取成功: provider=%s %s (IP=%s 端口=%s 地区=%s 过期=%s)",
endpoint.Provider, client.MaskProxyURL(endpoint.URL), endpoint.IP, endpoint.Port, endpoint.Region,
expiresLabel)
return types.ProxyFetchResult{
Success: true,
ProxyURL: endpoint.URL,
IP: endpoint.IP,
Port: endpoint.Port,
Region: endpoint.Region,
Provider: endpoint.Provider,
ExpiresAt: expiresAt,
}
}