feat(proxy): 支持品赞分组代理与自动白名单
This commit is contained in:
+9
-125
@@ -2,7 +2,6 @@ package client
|
||||
|
||||
import (
|
||||
"context"
|
||||
"encoding/json"
|
||||
"errors"
|
||||
"fmt"
|
||||
"io"
|
||||
@@ -15,8 +14,6 @@ import (
|
||||
"douyu_go/internal/types"
|
||||
)
|
||||
|
||||
var ipPortRegex = regexp.MustCompile(`(\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}):(\d{2,5})`)
|
||||
|
||||
// ParseProxyURL 解析并规范化代理字符串为 *url.URL。
|
||||
// 支持 http://, https://, socks5://;若未带协议头,默认补齐 http://。
|
||||
func ParseProxyURL(proxyStr string) (*url.URL, error) {
|
||||
@@ -41,133 +38,20 @@ func ParseProxyURL(proxyStr string) (*url.URL, error) {
|
||||
return u, nil
|
||||
}
|
||||
|
||||
// FetchProxyFromAPI 从指定的代理提供商 API 拉取一个代理 IP。
|
||||
// 兼容星空代理等常见 JSON 格式,以及纯文本 IP:Port 格式。
|
||||
func FetchProxyFromAPI(apiURL string) (*types.ProxyFetchResult, error) {
|
||||
trimmed := strings.TrimSpace(apiURL)
|
||||
// MaskProxyURL 隐藏代理地址中的用户名和密码,供日志与界面提示使用。
|
||||
func MaskProxyURL(proxyStr string) string {
|
||||
trimmed := strings.TrimSpace(proxyStr)
|
||||
if trimmed == "" {
|
||||
return nil, errors.New("代理提取 API 地址为空")
|
||||
return ""
|
||||
}
|
||||
if !strings.HasPrefix(trimmed, "http://") && !strings.HasPrefix(trimmed, "https://") {
|
||||
return nil, errors.New("代理提取 API 地址必须以 http:// 或 https:// 开头")
|
||||
}
|
||||
|
||||
req, err := http.NewRequestWithContext(context.Background(), http.MethodGet, trimmed, nil)
|
||||
u, err := ParseProxyURL(trimmed)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("创建提取请求失败: %w", err)
|
||||
return "<invalid-proxy>"
|
||||
}
|
||||
req.Header.Set("User-Agent", "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36")
|
||||
|
||||
client := &http.Client{
|
||||
Timeout: 8 * time.Second,
|
||||
if u.User != nil {
|
||||
u.User = nil
|
||||
}
|
||||
resp, err := client.Do(req)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("请求代理 API 失败: %w", err)
|
||||
}
|
||||
defer resp.Body.Close()
|
||||
|
||||
bodyBytes, err := io.ReadAll(io.LimitReader(resp.Body, 128*1024))
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("读取代理 API 响应失败: %w", err)
|
||||
}
|
||||
|
||||
bodyStr := strings.TrimSpace(string(bodyBytes))
|
||||
if bodyStr == "" {
|
||||
return nil, errors.New("代理 API 返回内容为空")
|
||||
}
|
||||
|
||||
// 尝试优先按照 JSON 结构体解析
|
||||
var jsonObj struct {
|
||||
Status int `json:"status"`
|
||||
Code *int `json:"code"`
|
||||
Info string `json:"info"`
|
||||
Msg string `json:"msg"`
|
||||
Data json.RawMessage `json:"data"`
|
||||
}
|
||||
|
||||
if jsonErr := json.Unmarshal([]byte(bodyStr), &jsonObj); jsonErr == nil {
|
||||
// 判断是否有错误提示
|
||||
errInfo := jsonObj.Info
|
||||
if errInfo == "" {
|
||||
errInfo = jsonObj.Msg
|
||||
}
|
||||
// 星空代理成功状态为 100,其它平台通用成功状态为 0
|
||||
hasSuccessStatus := jsonObj.Status == 100 || (jsonObj.Code != nil && *jsonObj.Code == 0)
|
||||
hasErrorStatus := (jsonObj.Status != 0 && jsonObj.Status != 100) || (jsonObj.Code != nil && *jsonObj.Code != 0)
|
||||
|
||||
if hasErrorStatus && errInfo != "" {
|
||||
return nil, fmt.Errorf("代理平台返回: %s (状态码=%d)", errInfo, jsonObj.Status)
|
||||
}
|
||||
|
||||
if len(jsonObj.Data) > 0 {
|
||||
// 1. 尝试解析为对象数组: [{"ip":"...", "port":"...", "prov":"...", "city":"..."}]
|
||||
type nodeItem struct {
|
||||
IP string `json:"ip"`
|
||||
Port any `json:"port"`
|
||||
Prov string `json:"prov"`
|
||||
City string `json:"city"`
|
||||
}
|
||||
var items []nodeItem
|
||||
if err := json.Unmarshal(jsonObj.Data, &items); err == nil && len(items) > 0 {
|
||||
ip := strings.TrimSpace(items[0].IP)
|
||||
portStr := fmt.Sprintf("%v", items[0].Port)
|
||||
region := strings.TrimSpace(items[0].Prov + " " + items[0].City)
|
||||
if ip != "" && portStr != "" {
|
||||
return &types.ProxyFetchResult{
|
||||
Success: true,
|
||||
ProxyURL: fmt.Sprintf("http://%s:%s", ip, portStr),
|
||||
IP: ip,
|
||||
Port: portStr,
|
||||
Region: region,
|
||||
}, nil
|
||||
}
|
||||
}
|
||||
|
||||
// 2. 尝试解析为字符串数组: ["ip:port"]
|
||||
var strArr []string
|
||||
if err := json.Unmarshal(jsonObj.Data, &strArr); err == nil && len(strArr) > 0 {
|
||||
if m := ipPortRegex.FindStringSubmatch(strArr[0]); len(m) == 3 {
|
||||
return &types.ProxyFetchResult{
|
||||
Success: true,
|
||||
ProxyURL: fmt.Sprintf("http://%s:%s", m[1], m[2]),
|
||||
IP: m[1],
|
||||
Port: m[2],
|
||||
}, nil
|
||||
}
|
||||
}
|
||||
|
||||
// 3. 尝试解析为单字符串
|
||||
var singleStr string
|
||||
if err := json.Unmarshal(jsonObj.Data, &singleStr); err == nil {
|
||||
if m := ipPortRegex.FindStringSubmatch(singleStr); len(m) == 3 {
|
||||
return &types.ProxyFetchResult{
|
||||
Success: true,
|
||||
ProxyURL: fmt.Sprintf("http://%s:%s", m[1], m[2]),
|
||||
IP: m[1],
|
||||
Port: m[2],
|
||||
}, nil
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if !hasSuccessStatus && errInfo != "" {
|
||||
return nil, fmt.Errorf("代理平台提示: %s", errInfo)
|
||||
}
|
||||
}
|
||||
|
||||
// 兜底正则提取 IP:Port 文本格式
|
||||
matches := ipPortRegex.FindStringSubmatch(bodyStr)
|
||||
if len(matches) == 3 {
|
||||
return &types.ProxyFetchResult{
|
||||
Success: true,
|
||||
ProxyURL: fmt.Sprintf("http://%s:%s", matches[1], matches[2]),
|
||||
IP: matches[1],
|
||||
Port: matches[2],
|
||||
}, nil
|
||||
}
|
||||
|
||||
return nil, fmt.Errorf("未能从 API 返回中识别出有效 IP:端口,原始返回: %s", truncateString(bodyStr, 120))
|
||||
return u.String()
|
||||
}
|
||||
|
||||
// TestProxyConnection 测试指定代理的连通性、出口公网 IP 与往返延迟。
|
||||
|
||||
Reference in New Issue
Block a user