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" ) // TestProxy 测试指定代理的连通性、出口公网 IP 与往返延迟。 func (a *App) TestProxy(proxyURL string) types.ProxyTestResult { res := client.TestProxyConnection(proxyURL) if res.Success { logger.LogEvent("代理", "success", "代理测试成功: IP=%s 归属=%s 延迟=%dms", res.IP, res.Region, res.Latency) } else { logger.LogEvent("代理", "warn", "代理测试失败: %s", res.Error) } return res } // 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, }) 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() { 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, } }