Force ipv6 setting
This commit is contained in:
@@ -1,12 +1,15 @@
|
||||
package http
|
||||
|
||||
import (
|
||||
"context"
|
||||
"net"
|
||||
"net/http"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"github.com/debridmediamanager.com/zurg/internal/config"
|
||||
"github.com/debridmediamanager.com/zurg/pkg/logutil"
|
||||
cmap "github.com/orcaman/concurrent-map/v2"
|
||||
"go.uber.org/zap"
|
||||
)
|
||||
|
||||
@@ -18,6 +21,7 @@ type HTTPClient struct {
|
||||
BearerToken string
|
||||
log *zap.SugaredLogger
|
||||
config config.ConfigInterface
|
||||
IPv6 cmap.ConcurrentMap[string, net.IP]
|
||||
}
|
||||
|
||||
func (r *HTTPClient) Do(req *http.Request) (*http.Response, error) {
|
||||
@@ -30,6 +34,43 @@ func (r *HTTPClient) Do(req *http.Request) (*http.Response, error) {
|
||||
if r.BearerToken != "" {
|
||||
req.Header.Set("Authorization", "Bearer "+r.BearerToken)
|
||||
}
|
||||
|
||||
if r.config.ShouldForceIPv6() {
|
||||
dialer := &net.Dialer{}
|
||||
transport := &http.Transport{
|
||||
DialContext: func(ctx context.Context, network, addr string) (net.Conn, error) {
|
||||
host, _, 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
|
||||
}
|
||||
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
|
||||
r.log.Warnf("No IPv6 address found for %s, falling back to default", host)
|
||||
addr = net.JoinHostPort(host, req.URL.Port())
|
||||
} else {
|
||||
addr = net.JoinHostPort(ipv6.String(), req.URL.Port())
|
||||
}
|
||||
} else if ipv6 != nil && host == req.URL.Hostname() {
|
||||
addr = net.JoinHostPort(ipv6.String(), req.URL.Port())
|
||||
}
|
||||
return dialer.DialContext(ctx, network, addr)
|
||||
},
|
||||
}
|
||||
r.Client.Transport = transport
|
||||
}
|
||||
|
||||
var resp *http.Response
|
||||
var err error
|
||||
for attempt := 0; attempt < r.MaxRetries; attempt++ {
|
||||
@@ -62,5 +103,6 @@ func NewHTTPClient(token string, maxRetries int, cfg config.ConfigInterface) *HT
|
||||
},
|
||||
log: logutil.NewLogger().Named("client"),
|
||||
config: cfg,
|
||||
IPv6: cmap.New[net.IP](),
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user