ipv6 network test

This commit is contained in:
Ben Sarmiento
2024-06-16 06:36:44 +00:00
parent e50806d8e1
commit 5aea569be7
3 changed files with 153 additions and 178 deletions

View File

@@ -15,7 +15,6 @@ import (
"time"
"github.com/debridmediamanager/zurg/internal/config"
"github.com/debridmediamanager/zurg/pkg/hosts"
"github.com/debridmediamanager/zurg/pkg/logutil"
http_dialer "github.com/mwitkow/go-http-dialer"
"golang.org/x/net/proxy"
@@ -24,16 +23,15 @@ import (
)
type HTTPClient struct {
client *http.Client
maxRetries int
timeoutSecs int
backoff func(attempt int) time.Duration
bearerToken string
isDownloadClient bool
cfg config.ConfigInterface
ipv6 cmap.ConcurrentMap[string, string]
ipv6Hosts []string
log *logutil.Logger
client *http.Client
maxRetries int
timeoutSecs int
backoff func(attempt int) time.Duration
bearerToken string
cfg config.ConfigInterface
dnsCache cmap.ConcurrentMap[string, string]
ipv6Hosts []string
log *logutil.Logger
}
type ApiErrorResponse struct {
@@ -49,20 +47,20 @@ func NewHTTPClient(
token string,
maxRetries int,
timeoutSecs int,
isDownloadClient bool,
forceIPv6 bool,
cfg config.ConfigInterface,
log *logutil.Logger,
) *HTTPClient {
client := HTTPClient{
bearerToken: token,
client: &http.Client{},
maxRetries: maxRetries,
timeoutSecs: timeoutSecs,
backoff: backoffFunc,
isDownloadClient: isDownloadClient,
cfg: cfg,
ipv6: cmap.New[string](),
log: log,
bearerToken: token,
client: &http.Client{},
maxRetries: maxRetries,
timeoutSecs: timeoutSecs,
backoff: backoffFunc,
cfg: cfg,
dnsCache: cmap.New[string](),
ipv6Hosts: []string{},
log: log,
}
var dialer proxy.Dialer = &net.Dialer{
@@ -96,21 +94,11 @@ func NewHTTPClient(
},
}
if cfg.ShouldForceIPv6() {
// fetch IPv6 hosts
ipv6List, err := hosts.FetchHosts(hosts.IPV6)
if err != nil {
log.Warnf("Failed to fetch IPv6 hosts: %v", err)
// Decide if you want to return nil here or continue without IPv6
} else {
client.ipv6Hosts = ipv6List
log.Debugf("Fetched %d IPv6 hosts", len(ipv6List))
}
if forceIPv6 {
// replace the default dialer with a custom one that resolves hostnames to IPv6 addresses
client.client.Transport.(*http.Transport).DialContext = func(ctx context.Context, network, address string) (net.Conn, error) {
// if address is already cached, use it
if ipv6Address, ok := client.ipv6.Get(address); ok {
if ipv6Address, ok := client.dnsCache.Get(address); ok {
return dialer.Dial(network, ipv6Address)
}
@@ -126,24 +114,13 @@ func NewHTTPClient(
for _, ip := range ips {
if ip.IP.To4() == nil { // IPv6 address found
ipv6Address := net.JoinHostPort(ip.IP.String(), port)
client.ipv6.Set(address, ipv6Address)
client.dnsCache.Set(address, ipv6Address)
return dialer.Dial(network, ipv6Address)
}
}
// no IPv6 address found, use the original address
log.Warnf("No IPv6 address found for host %s", host)
for _, ip := range ips {
if ip.IP.To4() != nil { // IPv4 address found
ipV4Address := net.JoinHostPort(ip.IP.String(), port)
client.ipv6.Set(address, ipV4Address)
return dialer.Dial(network, ipV4Address)
}
}
return dialer.Dial(network, address)
return nil, fmt.Errorf("no ipv6 address found")
}
}
return &client
@@ -171,7 +148,7 @@ func (r *HTTPClient) Do(req *http.Request) (*http.Response, error) {
resp.Body.Close()
}
r.replaceWithIPv6Host(req) // needed for ipv6
// r.optimizeHost(req)
resp, err = r.client.Do(req)
@@ -210,12 +187,7 @@ func (r *HTTPClient) Do(req *http.Request) (*http.Response, error) {
return resp, err
}
func (r *HTTPClient) replaceWithIPv6Host(req *http.Request) {
// don't replace host if IPv6 is not supported or not forced
if !r.isDownloadClient || !r.cfg.ShouldForceIPv6() {
return
}
// this host should be replaced
func (r *HTTPClient) optimizeHost(req *http.Request) {
if !strings.HasSuffix(req.Host, ".download.real-debrid.com") {
return
}
@@ -233,7 +205,6 @@ func (r *HTTPClient) replaceWithIPv6Host(req *http.Request) {
req.Host = r.ipv6Hosts[rand.Intn(len(r.ipv6Hosts))]
req.URL.Host = req.Host
r.log.Debugf("Host %s is not a valid IPv6 host, assigning a random IPv6 host: %s", req.URL.Host, req.Host)
// if !found && !r.CanFetchFirstByte(req.URL.String()) {
}
func (r *HTTPClient) proxyDialer(proxyURL *url.URL) (proxy.Dialer, error) {