Fix ipv6 client

This commit is contained in:
Ben Sarmiento
2023-12-01 13:29:59 +01:00
parent 33ffa9154c
commit 45ca65110e

View File

@@ -24,7 +24,7 @@ type HTTPClient struct {
getRetryIncr func(resp *http.Response, err error) int getRetryIncr func(resp *http.Response, err error) int
bearerToken string bearerToken string
cfg config.ConfigInterface cfg config.ConfigInterface
ipv6 cmap.ConcurrentMap[string, net.IP] ipv6 cmap.ConcurrentMap[string, string]
log *zap.SugaredLogger log *zap.SugaredLogger
} }
@@ -60,26 +60,32 @@ func NewHTTPClient(token string, maxRetries int, timeoutSecs int, cfg config.Con
return RATE_LIMIT_FACTOR // retry and increment attempt return RATE_LIMIT_FACTOR // retry and increment attempt
}, },
cfg: cfg, cfg: cfg,
ipv6: cmap.New[net.IP](), ipv6: cmap.New[string](),
log: log, log: log,
} }
if cfg.ShouldForceIPv6() { if cfg.ShouldForceIPv6() {
dialer := &net.Dialer{} dialer := &net.Dialer{}
dialContext := func(ctx context.Context, network, address string) (net.Conn, error) { dialContext := func(ctx context.Context, network, address string) (net.Conn, error) {
ips, err := net.DefaultResolver.LookupIPAddr(ctx, address) host, port, _ := net.SplitHostPort(address)
if ipv6Address, ok := client.ipv6.Get(address); ok {
return dialer.DialContext(ctx, network, ipv6Address)
}
ips, err := net.DefaultResolver.LookupIPAddr(ctx, host)
if err != nil { if err != nil {
return nil, err return nil, err
} }
for _, ip := range ips { for _, ip := range ips {
if ip.IP.To4() == nil { // IPv6 address found if ip.IP.To4() == nil { // IPv6 address found
ipv6Address := "[" + ip.IP.String() + "]" ip6Host := ip.IP.String()
ipv6Address := net.JoinHostPort(ip6Host, port)
client.ipv6.Set(address, ipv6Address)
return dialer.DialContext(ctx, network, ipv6Address) return dialer.DialContext(ctx, network, ipv6Address)
} }
} }
return nil, net.UnknownNetworkError("no IPv6 address found") return dialer.DialContext(ctx, network, address)
} }
transport := &http.Transport{ transport := &http.Transport{