Files
dygou_go/internal/app/app_proxy.go
T

127 lines
3.9 KiB
Go

package app
import (
"context"
"fmt"
"strings"
"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 {
cfg := a.getCfg()
endpoints, err := extractProxyEndpoints(cfg, apiURL, 1)
if err != nil {
logger.LogEvent("代理", "warn", "代理提取失败: %v", err)
return types.ProxyFetchResult{Error: err.Error()}
}
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,
}
}
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
}