Files
NTrace-core/util/http_client_geo.go
T

59 lines
1.4 KiB
Go
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
package util
import (
"context"
"fmt"
"net"
"net/http"
"time"
)
// NewGeoHTTPClient 返回一个使用 Geo DNS 解析策略的 *http.Client。
//
// 内部 Transport.DialContext 会通过 LookupHostForGeo 解析目标 host,
// 然后按 IP 拨号,保持请求 URL Host 不变(即 TLS SNI 不受影响)。
func NewGeoHTTPClient(timeout time.Duration) *http.Client {
dialer := &net.Dialer{
Timeout: timeout,
KeepAlive: 30 * time.Second,
}
transport := &http.Transport{}
if base, ok := http.DefaultTransport.(*http.Transport); ok && base != nil {
transport = base.Clone()
}
transport.DialContext = func(ctx context.Context, network, addr string) (net.Conn, error) {
host, port, err := net.SplitHostPort(addr)
if err != nil {
// addr 可能不含端口号;原样拨号
return dialer.DialContext(ctx, network, addr)
}
// 用 Geo DNS 策略解析 host
ips, err := LookupHostForGeo(ctx, host)
if err != nil {
return nil, err
}
// 依次尝试解析到的 IP,优先使用地址族匹配的
var lastErr error
for _, ip := range ips {
target := net.JoinHostPort(ip.String(), port)
conn, dialErr := dialer.DialContext(ctx, network, target)
if dialErr == nil {
return conn, nil
}
lastErr = dialErr
}
if lastErr == nil {
return nil, fmt.Errorf("geo DNS returned no IPs for host %q", host)
}
return nil, lastErr
}
return &http.Client{
Timeout: timeout,
Transport: transport,
}
}