Reachable hosts
This commit is contained in:
@@ -29,7 +29,7 @@ type HTTPClient struct {
|
||||
backoff func(attempt int) time.Duration
|
||||
bearerToken string
|
||||
dnsCache cmap.ConcurrentMap[string, string]
|
||||
optimalHosts []string
|
||||
hosts []string
|
||||
log *logutil.Logger
|
||||
}
|
||||
|
||||
@@ -56,7 +56,7 @@ func NewHTTPClient(
|
||||
maxRetries int,
|
||||
timeoutSecs int,
|
||||
forceIPv6 bool,
|
||||
optimalHosts []string,
|
||||
hosts []string,
|
||||
proxyURL string,
|
||||
log *logutil.Logger,
|
||||
) *HTTPClient {
|
||||
@@ -68,7 +68,7 @@ func NewHTTPClient(
|
||||
rateLimitSleepSecs: 4,
|
||||
backoff: backoffFunc,
|
||||
dnsCache: cmap.New[string](),
|
||||
optimalHosts: optimalHosts,
|
||||
hosts: hosts,
|
||||
log: log,
|
||||
}
|
||||
|
||||
@@ -152,8 +152,8 @@ func (r *HTTPClient) Do(req *http.Request) (*http.Response, error) {
|
||||
resp.Body.Close()
|
||||
}
|
||||
|
||||
if len(r.optimalHosts) > 0 {
|
||||
r.optimizeHost(req)
|
||||
if len(r.hosts) > 0 {
|
||||
r.ensureReachableHost(req)
|
||||
}
|
||||
|
||||
resp, err = r.client.Do(req)
|
||||
@@ -201,14 +201,44 @@ func (r *HTTPClient) Do(req *http.Request) (*http.Response, error) {
|
||||
return resp, err
|
||||
}
|
||||
|
||||
func (r *HTTPClient) optimizeHost(req *http.Request) {
|
||||
func (r *HTTPClient) ensureReachableHost(req *http.Request) {
|
||||
if !strings.Contains(req.Host, ".download.real-debrid.") {
|
||||
return
|
||||
}
|
||||
req.Host = r.optimalHosts[rand.Intn(len(r.optimalHosts))]
|
||||
if req.Host[0] >= 'a' && req.Host[0] <= 'z' {
|
||||
return
|
||||
}
|
||||
|
||||
// check if req.Host is in r.hosts
|
||||
if r.CheckIfHostIsReachable(req.Host) {
|
||||
return
|
||||
}
|
||||
// replace prefix of req.Host from .com to .cloud or vice versa
|
||||
var newHost string
|
||||
if strings.HasSuffix(req.Host, ".com") {
|
||||
newHost = strings.Replace(req.Host, ".com", ".cloud", 1)
|
||||
} else if strings.HasSuffix(req.Host, ".cloud") {
|
||||
newHost = strings.Replace(req.Host, ".cloud", ".com", 1)
|
||||
}
|
||||
if r.CheckIfHostIsReachable(newHost) {
|
||||
req.Host = newHost
|
||||
req.URL.Host = req.Host
|
||||
return
|
||||
}
|
||||
|
||||
req.Host = r.hosts[rand.Intn(len(r.hosts))]
|
||||
req.URL.Host = req.Host
|
||||
}
|
||||
|
||||
func (r *HTTPClient) CheckIfHostIsReachable(reqHost string) bool {
|
||||
for _, host := range r.hosts {
|
||||
if reqHost == host {
|
||||
return true
|
||||
}
|
||||
}
|
||||
return false
|
||||
}
|
||||
|
||||
func (r *HTTPClient) proxyDialer(proxyURL *url.URL) (proxy.Dialer, error) {
|
||||
if proxyURL.Scheme == "http" || proxyURL.Scheme == "https" {
|
||||
httpProxyDialer := http_dialer.New(proxyURL, http_dialer.WithConnectionTimeout(time.Duration(r.timeoutSecs)*time.Second))
|
||||
|
||||
Reference in New Issue
Block a user