- 新增 proxyutil 包,统一 HTTP/HTTPS/SOCKS5/SOCKS5H 代理配置逻辑 - SOCKS5H 支持服务端 DNS 解析,避免本地 DNS 泄露 - 移除 ProxyStrict 宽松模式,代理失败直接返回错误不回退直连 - 前端代理管理页面支持 SOCKS5H 协议的添加/编辑/批量导入 - 补充 IPv6 地址和特殊字符密码的边界测试
139 lines
4.0 KiB
Go
139 lines
4.0 KiB
Go
// Package httpclient 提供共享 HTTP 客户端池
|
||
//
|
||
// 性能优化说明:
|
||
// 原实现在多个服务中重复创建 http.Client:
|
||
// 1. proxy_probe_service.go: 每次探测创建新客户端
|
||
// 2. pricing_service.go: 每次请求创建新客户端
|
||
// 3. turnstile_service.go: 每次验证创建新客户端
|
||
// 4. github_release_service.go: 每次请求创建新客户端
|
||
// 5. claude_usage_service.go: 每次请求创建新客户端
|
||
//
|
||
// 新实现使用统一的客户端池:
|
||
// 1. 相同配置复用同一 http.Client 实例
|
||
// 2. 复用 Transport 连接池,减少 TCP/TLS 握手开销
|
||
// 3. 支持 HTTP/HTTPS/SOCKS5/SOCKS5H 代理
|
||
// 4. 代理配置失败时直接返回错误,不会回退到直连(避免 IP 关联风险)
|
||
package httpclient
|
||
|
||
import (
|
||
"crypto/tls"
|
||
"fmt"
|
||
"net/http"
|
||
"net/url"
|
||
"strings"
|
||
"sync"
|
||
"time"
|
||
|
||
"github.com/Wei-Shaw/sub2api/internal/pkg/proxyutil"
|
||
)
|
||
|
||
// Transport 连接池默认配置
|
||
const (
|
||
defaultMaxIdleConns = 100 // 最大空闲连接数
|
||
defaultMaxIdleConnsPerHost = 10 // 每个主机最大空闲连接数
|
||
defaultIdleConnTimeout = 90 * time.Second // 空闲连接超时时间
|
||
)
|
||
|
||
// Options 定义共享 HTTP 客户端的构建参数
|
||
type Options struct {
|
||
ProxyURL string // 代理 URL(支持 http/https/socks5/socks5h)
|
||
Timeout time.Duration // 请求总超时时间
|
||
ResponseHeaderTimeout time.Duration // 等待响应头超时时间
|
||
InsecureSkipVerify bool // 是否跳过 TLS 证书验证
|
||
|
||
// 可选的连接池参数(不设置则使用默认值)
|
||
MaxIdleConns int // 最大空闲连接总数(默认 100)
|
||
MaxIdleConnsPerHost int // 每主机最大空闲连接(默认 10)
|
||
MaxConnsPerHost int // 每主机最大连接数(默认 0 无限制)
|
||
}
|
||
|
||
// sharedClients 存储按配置参数缓存的 http.Client 实例
|
||
var sharedClients sync.Map
|
||
|
||
// GetClient 返回共享的 HTTP 客户端实例
|
||
// 性能优化:相同配置复用同一客户端,避免重复创建 Transport
|
||
// 安全说明:代理配置失败时直接返回错误,不会回退到直连,避免 IP 关联风险
|
||
func GetClient(opts Options) (*http.Client, error) {
|
||
key := buildClientKey(opts)
|
||
if cached, ok := sharedClients.Load(key); ok {
|
||
if client, ok := cached.(*http.Client); ok {
|
||
return client, nil
|
||
}
|
||
}
|
||
|
||
client, err := buildClient(opts)
|
||
if err != nil {
|
||
return nil, err
|
||
}
|
||
|
||
actual, _ := sharedClients.LoadOrStore(key, client)
|
||
if c, ok := actual.(*http.Client); ok {
|
||
return c, nil
|
||
}
|
||
return client, nil
|
||
}
|
||
|
||
func buildClient(opts Options) (*http.Client, error) {
|
||
transport, err := buildTransport(opts)
|
||
if err != nil {
|
||
return nil, err
|
||
}
|
||
|
||
return &http.Client{
|
||
Transport: transport,
|
||
Timeout: opts.Timeout,
|
||
}, nil
|
||
}
|
||
|
||
func buildTransport(opts Options) (*http.Transport, error) {
|
||
// 使用自定义值或默认值
|
||
maxIdleConns := opts.MaxIdleConns
|
||
if maxIdleConns <= 0 {
|
||
maxIdleConns = defaultMaxIdleConns
|
||
}
|
||
maxIdleConnsPerHost := opts.MaxIdleConnsPerHost
|
||
if maxIdleConnsPerHost <= 0 {
|
||
maxIdleConnsPerHost = defaultMaxIdleConnsPerHost
|
||
}
|
||
|
||
transport := &http.Transport{
|
||
MaxIdleConns: maxIdleConns,
|
||
MaxIdleConnsPerHost: maxIdleConnsPerHost,
|
||
MaxConnsPerHost: opts.MaxConnsPerHost, // 0 表示无限制
|
||
IdleConnTimeout: defaultIdleConnTimeout,
|
||
ResponseHeaderTimeout: opts.ResponseHeaderTimeout,
|
||
}
|
||
|
||
if opts.InsecureSkipVerify {
|
||
transport.TLSClientConfig = &tls.Config{InsecureSkipVerify: true}
|
||
}
|
||
|
||
proxyURL := strings.TrimSpace(opts.ProxyURL)
|
||
if proxyURL == "" {
|
||
return transport, nil
|
||
}
|
||
|
||
parsed, err := url.Parse(proxyURL)
|
||
if err != nil {
|
||
return nil, err
|
||
}
|
||
|
||
if err := proxyutil.ConfigureTransportProxy(transport, parsed); err != nil {
|
||
return nil, err
|
||
}
|
||
|
||
return transport, nil
|
||
}
|
||
|
||
func buildClientKey(opts Options) string {
|
||
return fmt.Sprintf("%s|%s|%s|%t|%d|%d|%d",
|
||
strings.TrimSpace(opts.ProxyURL),
|
||
opts.Timeout.String(),
|
||
opts.ResponseHeaderTimeout.String(),
|
||
opts.InsecureSkipVerify,
|
||
opts.MaxIdleConns,
|
||
opts.MaxIdleConnsPerHost,
|
||
opts.MaxConnsPerHost,
|
||
)
|
||
}
|