feat: 完善代理池与抢购链路,发布 0.5.0
This commit is contained in:
+73
-16
@@ -2,6 +2,8 @@ package app
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"douyu_go/internal/client"
|
||||
@@ -24,27 +26,12 @@ func (a *App) TestProxy(proxyURL string) types.ProxyTestResult {
|
||||
|
||||
// FetchProxy 从指定 API 提取一个代理 IP。
|
||||
func (a *App) FetchProxy(apiURL string) types.ProxyFetchResult {
|
||||
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,
|
||||
})
|
||||
endpoints, err := extractProxyEndpoints(cfg, apiURL, 1)
|
||||
if err != nil {
|
||||
logger.LogEvent("代理", "warn", "代理提取失败: %v", err)
|
||||
return types.ProxyFetchResult{Error: err.Error()}
|
||||
}
|
||||
if len(endpoints) == 0 {
|
||||
return types.ProxyFetchResult{
|
||||
Success: false,
|
||||
Error: "代理 API 未返回有效地址",
|
||||
}
|
||||
}
|
||||
endpoint := endpoints[0]
|
||||
expiresAt := int64(0)
|
||||
if !endpoint.ExpiresAt.IsZero() {
|
||||
@@ -67,3 +54,73 @@ func (a *App) FetchProxy(apiURL string) types.ProxyFetchResult {
|
||||
ExpiresAt: expiresAt,
|
||||
}
|
||||
}
|
||||
|
||||
func extractProxyEndpoints(cfg types.Config, apiURL string, count int) ([]proxyprovider.Endpoint, error) {
|
||||
ctx, cancel := context.WithTimeout(context.Background(), 30*time.Second)
|
||||
defer cancel()
|
||||
service := proxyprovider.NewService()
|
||||
endpoints, err := service.ExtractWithWhitelist(ctx, apiURL, count, proxyprovider.WhitelistConfig{
|
||||
APIURL: apiURL,
|
||||
LoginPassword: cfg.ProxyWhitelistPassword,
|
||||
SignKey: cfg.ProxyWhitelistSignKey,
|
||||
ReplaceOldest: cfg.ProxyWhitelistReplace,
|
||||
AutoWhitelist: cfg.ProxyAutoWhitelist,
|
||||
})
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if len(endpoints) == 0 {
|
||||
return nil, fmt.Errorf("代理 API 未返回有效地址")
|
||||
}
|
||||
return endpoints, nil
|
||||
}
|
||||
|
||||
func proxyNeedsStartupRefresh(cfg types.Config, now time.Time) bool {
|
||||
return cfg.ProxyEnabled &&
|
||||
strings.TrimSpace(cfg.ProxyAPI) != "" &&
|
||||
cfg.ProxyExpiresAt > 0 &&
|
||||
now.Add(30*time.Second).UnixMilli() >= cfg.ProxyExpiresAt
|
||||
}
|
||||
|
||||
func isStartupProxyFailure(err error) bool {
|
||||
if err == nil {
|
||||
return false
|
||||
}
|
||||
message := strings.ToLower(err.Error())
|
||||
for _, hint := range []string{
|
||||
"407", "proxy authentication required", "proxyconnect", "connection refused",
|
||||
"connection reset", "unexpected eof", "context deadline exceeded", "timeout",
|
||||
"can't assign requested address",
|
||||
} {
|
||||
if strings.Contains(message, hint) {
|
||||
return true
|
||||
}
|
||||
}
|
||||
return false
|
||||
}
|
||||
|
||||
func (a *App) refreshStartupProxy(cfg *types.Config, reason string) error {
|
||||
if !cfg.ProxyEnabled || strings.TrimSpace(cfg.ProxyAPI) == "" {
|
||||
return fmt.Errorf("代理提取 API 未配置")
|
||||
}
|
||||
endpoints, err := extractProxyEndpoints(*cfg, cfg.ProxyAPI, 1)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
endpoint := endpoints[0]
|
||||
if probe := client.TestProxyConnection(endpoint.URL); !probe.Success {
|
||||
return fmt.Errorf("新代理连通性校验失败: %s", probe.Error)
|
||||
}
|
||||
cfg.ProxyURL = endpoint.URL
|
||||
cfg.ProxyExpiresAt = 0
|
||||
if !endpoint.ExpiresAt.IsZero() {
|
||||
cfg.ProxyExpiresAt = endpoint.ExpiresAt.UnixMilli()
|
||||
}
|
||||
expiresLabel := "未知"
|
||||
if !endpoint.ExpiresAt.IsZero() {
|
||||
expiresLabel = endpoint.ExpiresAt.In(constants.CST).Format("2006-01-02 15:04:05")
|
||||
}
|
||||
logger.LogEvent("代理", "success", "启动时检测到%s,已提取并校验新代理: %s,过期=%s",
|
||||
reason, client.MaskProxyURL(cfg.ProxyURL), expiresLabel)
|
||||
return nil
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user