Check if host works first before assigning random host
This commit is contained in:
@@ -79,7 +79,7 @@ func (dl *Downloader) DownloadFile(directory, torrentName, fileName string, resp
|
|||||||
}
|
}
|
||||||
if cfg.ShouldServeFromRclone() {
|
if cfg.ShouldServeFromRclone() {
|
||||||
if cfg.ShouldVerifyDownloadLink() {
|
if cfg.ShouldVerifyDownloadLink() {
|
||||||
if !torMgr.Api.CanFetchFirstByte(unrestrict.Download) {
|
if !dl.client.CanFetchFirstByte(unrestrict.Download) {
|
||||||
log.Warnf("File %s is not available", fileName)
|
log.Warnf("File %s is not available", fileName)
|
||||||
http.Error(resp, "File is not available", http.StatusNotFound)
|
http.Error(resp, "File is not available", http.StatusNotFound)
|
||||||
return
|
return
|
||||||
@@ -123,7 +123,7 @@ func (dl *Downloader) DownloadLink(fileName, link string, resp http.ResponseWrit
|
|||||||
}
|
}
|
||||||
if cfg.ShouldServeFromRclone() {
|
if cfg.ShouldServeFromRclone() {
|
||||||
if cfg.ShouldVerifyDownloadLink() {
|
if cfg.ShouldVerifyDownloadLink() {
|
||||||
if !torMgr.Api.CanFetchFirstByte(unrestrict.Download) {
|
if !dl.client.CanFetchFirstByte(unrestrict.Download) {
|
||||||
log.Warnf("File %s is not available", fileName)
|
log.Warnf("File %s is not available", fileName)
|
||||||
http.Error(resp, "File is not available", http.StatusNotFound)
|
http.Error(resp, "File is not available", http.StatusNotFound)
|
||||||
return
|
return
|
||||||
|
|||||||
@@ -198,11 +198,12 @@ func (r *HTTPClient) replaceHostIfNeeded(req *http.Request) {
|
|||||||
break
|
break
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
if !found {
|
if !found && !r.CanFetchFirstByte(req.URL.String()) {
|
||||||
r.log.Debug("Not found, assigning random IPv6 host")
|
|
||||||
// random IPv6 host
|
|
||||||
req.Host = r.ipv6Hosts[rand.Intn(len(r.ipv6Hosts))]
|
req.Host = r.ipv6Hosts[rand.Intn(len(r.ipv6Hosts))]
|
||||||
req.URL.Host = req.Host
|
req.URL.Host = req.Host
|
||||||
|
r.log.Debugf("Host is not a valid IPv6 host, assigning a random IPv6 host: %s", req.Host)
|
||||||
|
} else {
|
||||||
|
r.ipv6Hosts = append(r.ipv6Hosts, req.Host)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -267,3 +268,28 @@ func backoffFunc(attempt int) time.Duration {
|
|||||||
}
|
}
|
||||||
return time.Duration(backoff) * time.Second
|
return time.Duration(backoff) * time.Second
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (r *HTTPClient) CanFetchFirstByte(url string) bool {
|
||||||
|
req, err := http.NewRequest("GET", url, nil)
|
||||||
|
if err != nil {
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
|
||||||
|
req.Header.Set("Range", "bytes=0-0")
|
||||||
|
|
||||||
|
resp, err := r.client.Do(req)
|
||||||
|
if err != nil {
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
defer resp.Body.Close()
|
||||||
|
|
||||||
|
// If server supports partial content
|
||||||
|
if resp.StatusCode/100 == 2 {
|
||||||
|
buffer := make([]byte, 1)
|
||||||
|
_, err = resp.Body.Read(buffer)
|
||||||
|
if err == nil {
|
||||||
|
return true
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
|||||||
@@ -308,7 +308,7 @@ func (rd *RealDebrid) UnrestrictLink(link string, checkFirstByte bool) (*Downloa
|
|||||||
return nil, fmt.Errorf("undecodable response: %v", err)
|
return nil, fmt.Errorf("undecodable response: %v", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
if checkFirstByte && !rd.CanFetchFirstByte(response.Download) {
|
if checkFirstByte && !rd.client.CanFetchFirstByte(response.Download) {
|
||||||
return nil, fmt.Errorf("can't fetch first byte")
|
return nil, fmt.Errorf("can't fetch first byte")
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -1,30 +0,0 @@
|
|||||||
package realdebrid
|
|
||||||
|
|
||||||
import (
|
|
||||||
"net/http"
|
|
||||||
)
|
|
||||||
|
|
||||||
func (rd *RealDebrid) CanFetchFirstByte(url string) bool {
|
|
||||||
req, err := http.NewRequest("GET", url, nil)
|
|
||||||
if err != nil {
|
|
||||||
return false
|
|
||||||
}
|
|
||||||
|
|
||||||
req.Header.Set("Range", "bytes=0-0")
|
|
||||||
|
|
||||||
resp, err := rd.client.Do(req)
|
|
||||||
if err != nil {
|
|
||||||
return false
|
|
||||||
}
|
|
||||||
defer resp.Body.Close()
|
|
||||||
|
|
||||||
// If server supports partial content
|
|
||||||
if resp.StatusCode/100 == 2 {
|
|
||||||
buffer := make([]byte, 1)
|
|
||||||
_, err = resp.Body.Read(buffer)
|
|
||||||
if err == nil {
|
|
||||||
return true
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return false
|
|
||||||
}
|
|
||||||
Reference in New Issue
Block a user