Only test for ipv6 if network supports it

This commit is contained in:
Ben Adrian Sarmiento
2024-08-25 13:29:50 +02:00
parent f5cbf150ef
commit 11e9c5d431
3 changed files with 92 additions and 94 deletions

View File

@@ -7,6 +7,7 @@ import (
"fmt"
"io"
"math"
"math/rand"
"net"
"net/http"
"net/url"
@@ -156,7 +157,7 @@ func (r *HTTPClient) Do(req *http.Request) (*http.Response, error) {
}
if len(r.hosts) > 0 {
r.ensureReachableHost(req)
r.ensureReachableDownloadServer(req)
}
if r.rateLimiter != nil {
@@ -199,14 +200,14 @@ func (r *HTTPClient) Do(req *http.Request) (*http.Response, error) {
return resp, err
}
// ensureReachableHost ensures that the request is sent to a reachable host
// ensureReachableDownloadServer ensures that the request is sent to a reachable host
// if not, it will replace the host with a reachable one
func (r *HTTPClient) ensureReachableHost(req *http.Request) {
func (r *HTTPClient) ensureReachableDownloadServer(req *http.Request) {
// skip if not a download server
if !strings.Contains(req.Host, ".download.real-debrid.") {
return
}
// skip CDN servers
// skip if CDN servers
if req.Host[0] >= 'a' && req.Host[0] <= 'z' {
return
}
@@ -221,19 +222,17 @@ func (r *HTTPClient) ensureReachableHost(req *http.Request) {
} else if strings.HasSuffix(req.Host, ".cloud") {
newHost = strings.Replace(req.Host, ".cloud", ".com", 1)
}
// check if newHost is reachable
if r.CheckIfHostIsReachable(newHost) {
req.Host = newHost
req.URL.Host = req.Host
return
} else {
// just pick a random host
req.Host = r.hosts[rand.Intn(len(r.hosts))]
}
// // just pick a random host
// req.Host = r.hosts[rand.Intn(len(r.hosts))]
// req.URL.Host = req.Host
req.URL.Host = req.Host
// just retain the original host if not in the list of reachable hosts
r.log.Debugf("Host %s is not found on the list of reachable hosts", req.Host)
// r.log.Debugf("Host %s is not found on the list of reachable hosts", req.Host)
}
// CheckIfHostIsReachable checks if the given host is passed in the list of reachable hosts