Properly set transport
This commit is contained in:
@@ -38,35 +38,34 @@ func (r *HTTPClient) Do(req *http.Request) (*http.Response, error) {
|
||||
|
||||
if r.config.ShouldForceIPv6() {
|
||||
dialer := &net.Dialer{}
|
||||
transport := &http.Transport{
|
||||
DialContext: func(ctx context.Context, network, addr string) (net.Conn, error) {
|
||||
host, port, err := net.SplitHostPort(addr)
|
||||
transport := r.Client.Transport.(*http.Transport).Clone()
|
||||
transport.DialContext = func(ctx context.Context, network, addr string) (net.Conn, error) {
|
||||
host, port, err := net.SplitHostPort(addr)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if ipv6, ok := r.IPv6.Get(host); !ok {
|
||||
// Lookup IP address if not found in map
|
||||
ips, err := net.LookupIP(host)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if ipv6, ok := r.IPv6.Get(host); !ok {
|
||||
// Lookup IP address if not found in map
|
||||
ips, err := net.LookupIP(host)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
for _, ip := range ips {
|
||||
if ip.To4() == nil { // IPv6
|
||||
ipv6 = ip
|
||||
r.IPv6.Set(host, ipv6)
|
||||
break
|
||||
}
|
||||
for _, ip := range ips {
|
||||
if ip.To4() == nil { // IPv6
|
||||
ipv6 = ip
|
||||
r.IPv6.Set(host, ipv6)
|
||||
break
|
||||
}
|
||||
}
|
||||
if ipv6 == nil { // No IPv6 address found, fallback to default
|
||||
addr = net.JoinHostPort(host, port)
|
||||
} else {
|
||||
addr = net.JoinHostPort(ipv6.String(), port)
|
||||
}
|
||||
} else if ipv6 != nil && host == req.URL.Hostname() {
|
||||
}
|
||||
if ipv6 == nil { // No IPv6 address found, fallback to default
|
||||
addr = net.JoinHostPort(host, port)
|
||||
} else {
|
||||
addr = net.JoinHostPort(ipv6.String(), port)
|
||||
}
|
||||
return dialer.DialContext(ctx, network, addr)
|
||||
},
|
||||
} else if ipv6 != nil && host == req.URL.Hostname() {
|
||||
addr = net.JoinHostPort(ipv6.String(), port)
|
||||
}
|
||||
return dialer.DialContext(ctx, network, addr)
|
||||
}
|
||||
r.Client.Transport = transport
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user