fixes here and there

This commit is contained in:
Ben Sarmiento
2023-11-11 02:34:46 +01:00
parent 147c0bd444
commit cd96c7bd38
14 changed files with 181 additions and 155 deletions

View File

@@ -13,31 +13,23 @@ import (
"github.com/debridmediamanager.com/zurg/internal/net"
"github.com/debridmediamanager.com/zurg/internal/torrent"
"github.com/debridmediamanager.com/zurg/pkg/logutil"
"github.com/debridmediamanager.com/zurg/pkg/realdebrid"
"github.com/hashicorp/golang-lru/v2/expirable"
"github.com/nutsdb/nutsdb"
)
func main() {
rlog := logutil.NewLogger()
log := rlog.Named("zurg")
log := logutil.NewLogger().Named("zurg")
config, configErr := config.LoadZurgConfig("./config.yml")
if configErr != nil {
log.Panicf("Config failed to load: %v", configErr)
}
db, err := nutsdb.Open(
nutsdb.DefaultOptions,
nutsdb.WithDir("/tmp/nutsdb"),
)
if err != nil {
log.Fatal(err)
}
defer db.Close()
cache := expirable.NewLRU[string, string](1e4, nil, time.Hour)
torrentMgr := torrent.NewTorrentManager(config, db)
rd := realdebrid.NewRealDebrid(config.GetToken(), config, logutil.NewLogger().Named("realdebrid"))
torrentMgr := torrent.NewTorrentManager(config, rd)
mux := http.NewServeMux()
net.Router(mux, config, torrentMgr, cache)

15
go.mod
View File

@@ -5,21 +5,8 @@ go 1.21.3
require (
github.com/elliotchance/orderedmap/v2 v2.2.0
github.com/hashicorp/golang-lru/v2 v2.0.7
github.com/nutsdb/nutsdb v0.14.1
go.uber.org/zap v1.26.0
gopkg.in/yaml.v3 v3.0.1
)
require (
github.com/antlabs/stl v0.0.1 // indirect
github.com/antlabs/timer v0.0.11 // indirect
github.com/bwmarrin/snowflake v0.3.0 // indirect
github.com/emirpasic/gods v1.18.1 // indirect
github.com/gofrs/flock v0.8.1 // indirect
github.com/pkg/errors v0.9.1 // indirect
github.com/tidwall/btree v1.6.0 // indirect
github.com/xujiajun/mmap-go v1.0.1 // indirect
github.com/xujiajun/utils v0.0.0-20220904132955-5f7c5b914235 // indirect
go.uber.org/multierr v1.10.0 // indirect
golang.org/x/sys v0.10.0 // indirect
)
require go.uber.org/multierr v1.10.0 // indirect

29
go.sum
View File

@@ -1,49 +1,20 @@
github.com/antlabs/stl v0.0.1 h1:TRD3csCrjREeLhLoQ/supaoCvFhNLBTNIwuRGrDIs6Q=
github.com/antlabs/stl v0.0.1/go.mod h1:wvVwP1loadLG3cRjxUxK8RL4Co5xujGaZlhbztmUEqQ=
github.com/antlabs/timer v0.0.11 h1:z75oGFLeTqJHMOcWzUPBKsBbQAz4Ske3AfqJ7bsdcwU=
github.com/antlabs/timer v0.0.11/go.mod h1:JNV8J3yGvMKhCavGXgj9HXrVZkfdQyKCcqXBT8RdyuU=
github.com/bwmarrin/snowflake v0.3.0 h1:xm67bEhkKh6ij1790JB83OujPR5CzNe8QuQqAgISZN0=
github.com/bwmarrin/snowflake v0.3.0/go.mod h1:NdZxfVWX+oR6y2K0o6qAYv6gIOP9rjG0/E9WsDpxqwE=
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/elliotchance/orderedmap/v2 v2.2.0 h1:7/2iwO98kYT4XkOjA9mBEIwvi4KpGB4cyHeOFOnj4Vk=
github.com/elliotchance/orderedmap/v2 v2.2.0/go.mod h1:85lZyVbpGaGvHvnKa7Qhx7zncAdBIBq6u56Hb1PRU5Q=
github.com/emirpasic/gods v1.18.1 h1:FXtiHYKDGKCW2KzwZKx0iC0PQmdlorYgdFG9jPXJ1Bc=
github.com/emirpasic/gods v1.18.1/go.mod h1:8tpGGwCnJ5H4r6BWwaV6OrWmMoPhUl5jm/FMNAnJvWQ=
github.com/gofrs/flock v0.8.1 h1:+gYjHKf32LDeiEEFhQaotPbLuUXjY5ZqxKgXy7n59aw=
github.com/gofrs/flock v0.8.1/go.mod h1:F1TvTiK9OcQqauNUHlbJvyl9Qa1QvF/gOUDKA14jxHU=
github.com/hashicorp/golang-lru/v2 v2.0.7 h1:a+bsQ5rvGLjzHuww6tVxozPZFVghXaHOwFs4luLUK2k=
github.com/hashicorp/golang-lru/v2 v2.0.7/go.mod h1:QeFd9opnmA6QUJc5vARoKUSoFhyfM2/ZepoAG6RGpeM=
github.com/nutsdb/nutsdb v0.14.1 h1:z+Kth/kz2oYqKmOMBZho1YK2183xjrcl6KExRtCFl18=
github.com/nutsdb/nutsdb v0.14.1/go.mod h1:6inOji9rFBporXeHDjJny4g50RpQbkjSK5jI1hht0j8=
github.com/pkg/errors v0.9.1 h1:FEBLx1zS214owpjy7qsBeixbURkuhQAwrK5UwLGTwt4=
github.com/pkg/errors v0.9.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0=
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
github.com/stretchr/testify v1.6.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
github.com/stretchr/testify v1.8.1 h1:w7B6lhMri9wdJUVmEZPGGhZzrYTPvgJArz7wNPgYKsk=
github.com/stretchr/testify v1.8.1/go.mod h1:w2LPCIKwWwSfY2zedu0+kehJoqGctiVI29o6fzry7u4=
github.com/tidwall/btree v1.6.0 h1:LDZfKfQIBHGHWSwckhXI0RPSXzlo+KYdjK7FWSqOzzg=
github.com/tidwall/btree v1.6.0/go.mod h1:twD9XRA5jj9VUQGELzDO4HPQTNJsoWWfYEL+EUQ2cKY=
github.com/xujiajun/mmap-go v1.0.1 h1:7Se7ss1fLPPRW+ePgqGpCkfGIZzJV6JPq9Wq9iv/WHc=
github.com/xujiajun/mmap-go v1.0.1/go.mod h1:CNN6Sw4SL69Sui00p0zEzcZKbt+5HtEnYUsc6BKKRMg=
github.com/xujiajun/utils v0.0.0-20220904132955-5f7c5b914235 h1:w0si+uee0iAaCJO9q86T6yrhdadgcsoNuh47LrUykzg=
github.com/xujiajun/utils v0.0.0-20220904132955-5f7c5b914235/go.mod h1:MR4+0R6A9NS5IABnIM3384FfOq8QFVnm7WDrBOhIaMU=
go.uber.org/goleak v1.2.0 h1:xqgm/S+aQvhWFTtR0XK3Jvg7z8kGV8P4X14IzwN3Eqk=
go.uber.org/goleak v1.2.0/go.mod h1:XJYK+MuIchqpmGmUSAzotztawfKvYLUIgg7guXrwVUo=
go.uber.org/multierr v1.10.0 h1:S0h4aNzvfcFsC3dRF1jLoaov7oRaKqRGC/pUEJ2yvPQ=
go.uber.org/multierr v1.10.0/go.mod h1:20+QtiLqy0Nd6FdQB9TLXag12DsQkrbs3htMFfDN80Y=
go.uber.org/zap v1.26.0 h1:sI7k6L95XOKS281NhVKOFCUNIvv9e0w4BF8N3u+tCRo=
go.uber.org/zap v1.26.0/go.mod h1:dtElttAiwGvoJ/vj4IwHBS/gXsEu/pZ50mUIRWuG0so=
golang.org/x/sys v0.0.0-20181221143128-b4a75ba826a6/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
golang.org/x/sys v0.10.0 h1:SqMFp9UcQJZa+pmYuAKjd9xq1f0j5rLcDIk0mj4qAsA=
golang.org/x/sys v0.10.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/go-playground/assert.v1 v1.2.1 h1:xoYuJVE7KT85PYWrN730RguIQO0ePzVRfFMXadIrXTM=
gopkg.in/go-playground/assert.v1 v1.2.1/go.mod h1:9RXL0bg/zibRAgZUYszZSwO/z8Y/a8bDuhia5mkpMnE=
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=

76
hosts.txt Normal file
View File

@@ -0,0 +1,76 @@
20.download.real-debrid.cloud
21.download.real-debrid.cloud
22.download.real-debrid.cloud
23.download.real-debrid.cloud
30.download.real-debrid.cloud
31.download.real-debrid.cloud
32.download.real-debrid.cloud
34.download.real-debrid.cloud
40.download.real-debrid.cloud
41.download.real-debrid.cloud
42.download.real-debrid.cloud
43.download.real-debrid.cloud
44.download.real-debrid.cloud
45.download.real-debrid.cloud
50.download.real-debrid.cloud
51.download.real-debrid.cloud
52.download.real-debrid.cloud
53.download.real-debrid.cloud
54.download.real-debrid.cloud
55.download.real-debrid.cloud
56.download.real-debrid.cloud
57.download.real-debrid.cloud
58.download.real-debrid.cloud
59.download.real-debrid.cloud
60.download.real-debrid.cloud
61.download.real-debrid.cloud
62.download.real-debrid.cloud
63.download.real-debrid.cloud
64.download.real-debrid.cloud
65.download.real-debrid.cloud
66.download.real-debrid.cloud
67.download.real-debrid.cloud
68.download.real-debrid.cloud
69.download.real-debrid.cloud
20.download.real-debrid.com
21.download.real-debrid.com
22.download.real-debrid.com
23.download.real-debrid.com
30.download.real-debrid.com
31.download.real-debrid.com
32.download.real-debrid.com
34.download.real-debrid.com
40.download.real-debrid.com
41.download.real-debrid.com
42.download.real-debrid.com
43.download.real-debrid.com
44.download.real-debrid.com
45.download.real-debrid.com
50.download.real-debrid.com
51.download.real-debrid.com
52.download.real-debrid.com
53.download.real-debrid.com
54.download.real-debrid.com
55.download.real-debrid.com
56.download.real-debrid.com
57.download.real-debrid.com
58.download.real-debrid.com
59.download.real-debrid.com
60.download.real-debrid.com
61.download.real-debrid.com
62.download.real-debrid.com
63.download.real-debrid.com
64.download.real-debrid.com
65.download.real-debrid.com
66.download.real-debrid.com
67.download.real-debrid.com
68.download.real-debrid.com
69.download.real-debrid.com
hkg1.download.real-debrid.com
lax1.download.real-debrid.com
lon1.download.real-debrid.com
mum1.download.real-debrid.com
rbx.download.real-debrid.com
sgp1.download.real-debrid.com
tlv1.download.real-debrid.com
tyo1.download.real-debrid.com

View File

@@ -23,6 +23,7 @@ type ConfigInterface interface {
GetNetworkBufferSize() int
GetMountPoint() string
EnableRetainFolderNameExtension() bool
GetRandomPreferredHost() string
}
func LoadZurgConfig(filename string) (ConfigInterface, error) {

View File

@@ -1,18 +1,21 @@
package config
import "math/rand"
type ZurgConfig struct {
Version string `yaml:"zurg"`
Token string `yaml:"token"`
Host string `yaml:"host"`
Port string `yaml:"port"`
NumOfWorkers int `yaml:"concurrent_workers"`
RefreshEverySeconds int `yaml:"check_for_changes_every_secs"`
CacheTimeHours int `yaml:"info_cache_time_hours"`
CanRepair bool `yaml:"enable_repair"`
OnLibraryUpdate string `yaml:"on_library_update"`
NetworkBufferSize int `yaml:"network_buffer_size"`
MountPoint string `yaml:"mount_point"`
RetainFolderNameExtension bool `yaml:"retain_folder_name_extension"`
Version string `yaml:"zurg"`
Token string `yaml:"token"`
Host string `yaml:"host"`
Port string `yaml:"port"`
NumOfWorkers int `yaml:"concurrent_workers"`
RefreshEverySeconds int `yaml:"check_for_changes_every_secs"`
CacheTimeHours int `yaml:"info_cache_time_hours"`
CanRepair bool `yaml:"enable_repair"`
OnLibraryUpdate string `yaml:"on_library_update"`
NetworkBufferSize int `yaml:"network_buffer_size"`
MountPoint string `yaml:"mount_point"`
RetainFolderNameExtension bool `yaml:"retain_folder_name_extension"`
PreferredHosts []string `yaml:"preferred_hosts"`
}
func (z *ZurgConfig) GetToken() string {
@@ -73,3 +76,11 @@ func (z *ZurgConfig) GetMountPoint() string {
func (z *ZurgConfig) EnableRetainFolderNameExtension() bool {
return z.RetainFolderNameExtension
}
func (z *ZurgConfig) GetRandomPreferredHost() string {
if len(z.PreferredHosts) == 0 {
return ""
}
randomIndex := rand.Intn(len(z.PreferredHosts))
return z.PreferredHosts[randomIndex]
}

View File

@@ -33,7 +33,7 @@ func HandlePropfindRequest(w http.ResponseWriter, r *http.Request, t *torrent.To
case len(filteredSegments) == 2:
output, err = handleSingleTorrent(requestPath, w, r, t)
default:
log.Errorf("Request %s %s not found", r.Method, requestPath)
log.Warnf("Request %s %s not found", r.Method, requestPath)
http.Error(w, "Not Found", http.StatusNotFound)
return
}
@@ -54,9 +54,9 @@ func HandlePropfindRequest(w http.ResponseWriter, r *http.Request, t *torrent.To
func handleRoot(w http.ResponseWriter, r *http.Request, c config.ConfigInterface) ([]byte, error) {
var responses []dav.Response
responses = append(responses, dav.Directory("/"))
responses = append(responses, dav.Directory(""))
for _, directory := range c.GetDirectories() {
responses = append(responses, dav.Directory("/"+directory))
responses = append(responses, dav.Directory(directory))
}
rootResponse := dav.MultiStatus{
XMLNS: "DAV:",
@@ -104,7 +104,7 @@ func handleSingleTorrent(requestPath string, w http.ResponseWriter, r *http.Requ
accessKey := path.Base(requestPath)
torrent, exists := t.TorrentMap.Get(accessKey)
if !exists {
return nil, fmt.Errorf("cannot find torrent %s", requestPath)
return nil, fmt.Errorf("cannot find torrent %s", accessKey)
}
var responses []dav.Response

View File

@@ -30,7 +30,7 @@ func HandleDirectoryListing(w http.ResponseWriter, r *http.Request, t *torrent.T
case len(filteredSegments) == 3:
output, err = handleSingleTorrent(requestPath, w, r, t)
default:
log.Errorf("Request %s %s not found", r.Method, requestPath)
log.Warnf("Request %s %s not found", r.Method, requestPath)
http.Error(w, "Not Found", http.StatusNotFound)
return
}
@@ -89,7 +89,7 @@ func handleSingleTorrent(requestPath string, w http.ResponseWriter, r *http.Requ
accessKey := path.Base(requestPath)
torrent, _ := t.TorrentMap.Get(accessKey)
if torrent == nil {
return nil, fmt.Errorf("cannot find torrent %s", requestPath)
return nil, fmt.Errorf("cannot find torrent %s", accessKey)
}
htmlDoc := "<ol>"

View File

@@ -14,17 +14,16 @@ import (
"github.com/debridmediamanager.com/zurg/pkg/logutil"
"github.com/debridmediamanager.com/zurg/pkg/realdebrid"
"github.com/elliotchance/orderedmap/v2"
"github.com/nutsdb/nutsdb"
"go.uber.org/zap"
)
type TorrentManager struct {
TorrentMap *orderedmap.OrderedMap[string, *Torrent] // accessKey -> Torrent
repairMap *orderedmap.OrderedMap[string, bool] // accessKey -> bool
requiredVersion string
rd *realdebrid.RealDebrid
checksum string
config config.ConfigInterface
db *nutsdb.DB
workerPool chan bool
mu *sync.Mutex
log *zap.SugaredLogger
@@ -33,19 +32,18 @@ type TorrentManager struct {
// NewTorrentManager creates a new torrent manager
// it will fetch all torrents and their info in the background
// and store them in-memory and cached in files
func NewTorrentManager(config config.ConfigInterface, db *nutsdb.DB) *TorrentManager {
func NewTorrentManager(config config.ConfigInterface, rd *realdebrid.RealDebrid) *TorrentManager {
t := &TorrentManager{
TorrentMap: orderedmap.NewOrderedMap[string, *Torrent](),
repairMap: orderedmap.NewOrderedMap[string, bool](),
requiredVersion: "10.11.2023",
rd: realdebrid.NewRealDebrid(config.GetToken(), logutil.NewLogger().Named("realdebrid")),
rd: rd,
config: config,
db: db,
workerPool: make(chan bool, config.GetNumOfWorkers()),
mu: &sync.Mutex{},
log: logutil.NewLogger().Named("manager"),
}
// start with a clean slate
t.mu.Lock()
newTorrents, _, err := t.rd.GetTorrents(0)
@@ -170,7 +168,7 @@ func (t *TorrentManager) getChecksum() string {
totalCount = torrentsResp.totalCount
case count = <-countChan:
case err := <-errChan:
t.log.Errorf("Checksum API Error: %v\n", err)
t.log.Warnf("Checksum API Error: %v\n", err)
return ""
}
}
@@ -199,7 +197,7 @@ func (t *TorrentManager) startRefreshJob() {
newTorrents, _, err := t.rd.GetTorrents(0)
if err != nil {
t.log.Errorf("Cannot get torrents: %v\n", err)
t.log.Warnf("Cannot get torrents: %v\n", err)
continue
}
t.log.Infof("Detected changes! Refreshing %d torrents", len(newTorrents))
@@ -271,7 +269,7 @@ func (t *TorrentManager) getMoreInfo(rdTorrent realdebrid.Torrent) *Torrent {
if info == nil {
info, err = t.rd.GetTorrentInfo(rdTorrent.ID)
if err != nil {
t.log.Errorf("Cannot get info for id=%s: %v\n", rdTorrent.ID, err)
t.log.Warnf("Cannot get info for id=%s: %v\n", rdTorrent.ID, err)
return nil
}
}
@@ -296,14 +294,13 @@ func (t *TorrentManager) getMoreInfo(rdTorrent realdebrid.Torrent) *Torrent {
})
}
if selectedFiles.Len() > len(info.Links) && info.Progress == 100 {
t.log.Debugf("%d links has expired for %s %s", selectedFiles.Len()-len(info.Links), info.ID, info.Name)
// chaotic file means RD will not output the desired file selection
// e.g. even if we select just a single mkv, it will output a rar
var isChaotic bool
selectedFiles, isChaotic = t.organizeChaos(info.Links, selectedFiles)
if isChaotic {
t.log.Errorf("Torrent id=%s %s is unfixable, it is always returning an unstreamable link (it is no longer shown in your directories)", info.ID, info.Name)
t.log.Debugf("You can try fixing it yourself magnet:?xt=urn:btih:%s", info.Hash)
// t.log.Warnf("Torrent id=%s %s is unfixable, it is always returning an unstreamable link (it is no longer shown in your directories)", info.ID, info.Name)
// t.log.Debugf("You can try fixing it yourself magnet:?xt=urn:btih:%s", info.Hash)
return nil
} else {
if streamableCount > 1 {
@@ -314,8 +311,8 @@ func (t *TorrentManager) getMoreInfo(rdTorrent realdebrid.Torrent) *Torrent {
t.log.Infof("Torrent id=%s %s marked for repair", info.ID, info.Name)
forRepair = true
} else {
t.log.Errorf("Torrent id=%s %s is unfixable, the lone streamable link has expired (it is no longer shown in your directories)", info.ID, info.Name)
t.log.Debugf("You can try fixing it yourself magnet:?xt=urn:btih:%s", info.Hash)
// t.log.Warnf("Torrent id=%s %s is unfixable, the lone streamable link has expired (it is no longer shown in your directories)", info.ID, info.Name)
// t.log.Debugf("You can try fixing it yourself magnet:?xt=urn:btih:%s", info.Hash)
return nil
}
}
@@ -525,9 +522,19 @@ func (t *TorrentManager) repairAll() {
}
func (t *TorrentManager) Repair(accessKey string) {
if _, exists := t.repairMap.Get(accessKey); exists {
return
}
t.repairMap.Set(accessKey, true)
if !t.config.EnableRepair() {
t.log.Warn("Repair is disabled; if you do not have other zurg instances running, you should enable repair")
return
}
torrent, _ := t.TorrentMap.Get(accessKey)
if torrent == nil {
t.log.Errorf("Cannot find torrent %s anymore to repair it", accessKey)
t.log.Warnf("Cannot find torrent %s anymore to repair it", accessKey)
return
}
if torrent.InProgress {
@@ -602,7 +609,7 @@ func (t *TorrentManager) reinsertTorrent(torrent *Torrent, missingFiles string)
// redownload torrent
resp, err := t.rd.AddMagnetHash(torrent.Instances[0].Hash)
if err != nil {
t.log.Errorf("Cannot redownload torrent: %v", err)
t.log.Warnf("Cannot redownload torrent: %v", err)
return false
}
time.Sleep(1 * time.Second)
@@ -611,7 +618,7 @@ func (t *TorrentManager) reinsertTorrent(torrent *Torrent, missingFiles string)
newTorrentID := resp.ID
err = t.rd.SelectTorrentFiles(newTorrentID, missingFiles)
if err != nil {
t.log.Errorf("Cannot start redownloading: %v", err)
t.log.Warnf("Cannot start redownloading: %v", err)
t.rd.DeleteTorrent(newTorrentID)
return false
}
@@ -620,13 +627,13 @@ func (t *TorrentManager) reinsertTorrent(torrent *Torrent, missingFiles string)
// see if the torrent is ready
info, err := t.rd.GetTorrentInfo(newTorrentID)
if err != nil {
t.log.Errorf("Cannot get info on redownloaded torrent id=%s : %v", newTorrentID, err)
t.log.Warnf("Cannot get info on redownloaded torrent id=%s : %v", newTorrentID, err)
t.rd.DeleteTorrent(newTorrentID)
return false
}
if info.Status == "magnet_error" || info.Status == "error" || info.Status == "virus" || info.Status == "dead" {
t.log.Errorf("Redownloaded torrent id=%s is in error state: %s", newTorrentID, info.Status)
t.log.Warnf("Redownloaded torrent id=%s is in error state: %s", newTorrentID, info.Status)
t.rd.DeleteTorrent(newTorrentID)
return false
}
@@ -656,7 +663,7 @@ func (t *TorrentManager) canCapacityHandle() bool {
for {
count, err := t.rd.GetActiveTorrentCount()
if err != nil {
t.log.Errorf("Cannot get active downloads count: %v", err)
t.log.Warnf("Cannot get active downloads count: %v", err)
if retryCount >= maxRetries {
t.log.Error("Max retries reached. Exiting.")
return false

View File

@@ -47,14 +47,14 @@ func HandleGetRequest(w http.ResponseWriter, r *http.Request, t *torrent.Torrent
torrent, _ := t.TorrentMap.Get(accessKey)
if torrent == nil {
log.Errorf("Cannot find torrent %s in the directory %s", accessKey, baseDirectory)
log.Warnf("Cannot find torrent %s in the directory %s", accessKey, baseDirectory)
http.Error(w, "File not found", http.StatusNotFound)
return
}
file, _ := torrent.SelectedFiles.Get(filename)
if file == nil {
log.Errorf("Cannot find file from path %s", requestPath)
log.Warnf("Cannot find file from path %s", requestPath)
http.Error(w, "File not found", http.StatusNotFound)
return
}
@@ -66,7 +66,7 @@ func HandleGetRequest(w http.ResponseWriter, r *http.Request, t *torrent.Torrent
if file.Link == "" {
// This is a dead file, serve an alternate file
log.Errorf("File %s is not yet available, zurg is repairing the torrent", filename)
log.Warnf("File %s is not yet available, zurg is repairing the torrent", filename)
streamErrorVideo("https://www.youtube.com/watch?v=bGTqwt6vdcY", w, r, t, c, log)
return
}
@@ -75,18 +75,18 @@ func HandleGetRequest(w http.ResponseWriter, r *http.Request, t *torrent.Torrent
resp := t.UnrestrictUntilOk(link)
if resp == nil {
go t.Repair(torrent.AccessKey)
log.Errorf("File %s is no longer available, torrent is marked for repair", file.Path)
log.Warnf("File %s is no longer available, torrent is marked for repair", file.Path)
streamErrorVideo("https://www.youtube.com/watch?v=gea_FJrtFVA", w, r, t, c, log)
return
} else if resp.Filename != filename {
actualExt := filepath.Ext(resp.Filename)
expectedExt := filepath.Ext(filename)
if actualExt != expectedExt && resp.Streamable != 1 {
log.Errorf("File extension mismatch: %s and %s", filename, resp.Filename)
log.Warnf("File was changed and is not streamable: %s and %s", filename, resp.Filename)
streamErrorVideo("https://www.youtube.com/watch?v=t9VgOriBHwE", w, r, t, c, log)
return
} else {
log.Errorf("Filename mismatch: %s and %s", filename, resp.Filename)
log.Warnf("Filename mismatch: %s and %s", filename, resp.Filename)
}
}
cache.Add(requestPath, resp.Download)
@@ -108,11 +108,11 @@ func streamFileToResponse(torrent *torrent.Torrent, url string, w http.ResponseW
}
// Create a custom HTTP client
client := zurghttp.NewHTTPClient(c.GetToken(), 10)
client := zurghttp.NewHTTPClient(c.GetToken(), 10, c)
resp, err := client.Do(req)
if err != nil {
log.Errorf("Error downloading file %v ; torrent is marked for repair", err)
log.Warnf("Cannot download file %v ; torrent is marked for repair", err)
if torrent != nil {
go t.Repair(torrent.AccessKey)
}
@@ -122,7 +122,7 @@ func streamFileToResponse(torrent *torrent.Torrent, url string, w http.ResponseW
defer resp.Body.Close()
if resp.StatusCode != http.StatusOK && resp.StatusCode != http.StatusPartialContent {
log.Errorf("Received a %s status code ; torrent is marked for repair", resp.Status)
log.Warnf("Received a %s status code ; torrent is marked for repair", resp.Status)
if torrent != nil {
go t.Repair(torrent.AccessKey)
}

View File

@@ -46,20 +46,20 @@ func HandleHeadRequest(w http.ResponseWriter, r *http.Request, t *torrent.Torren
torrent, _ := t.TorrentMap.Get(accessKey)
if torrent == nil {
log.Errorf("Cannot find torrent %s in the directory %s", accessKey, baseDirectory)
log.Warnf("Cannot find torrent %s in the directory %s", accessKey, baseDirectory)
http.Error(w, "File not found", http.StatusNotFound)
return
}
file, _ := torrent.SelectedFiles.Get(filename)
if file == nil {
log.Errorf("Cannot find file from path %s", requestPath)
log.Warnf("Cannot find file from path %s", requestPath)
http.Error(w, "Cannot find file", http.StatusNotFound)
return
}
if file.Link == "" {
// This is a dead file, serve an alternate file
log.Errorf("File %s is no longer available", filename)
log.Warnf("File %s is no longer available", filename)
http.Error(w, "Cannot find file", http.StatusNotFound)
return
}

View File

@@ -2,7 +2,7 @@ package dav
func Directory(path string) Response {
return Response{
Href: customPathEscape(path),
Href: "/" + customPathEscape(path),
Propstat: PropStat{
Prop: Prop{
ResourceType: ResourceType{Value: "<d:collection/>"},
@@ -14,7 +14,7 @@ func Directory(path string) Response {
func File(path string, fileSize int64, added string, link string) Response {
return Response{
Href: customPathEscape(path),
Href: "/" + customPathEscape(path),
Propstat: PropStat{
Prop: Prop{
ContentLength: fileSize,

View File

@@ -1,19 +1,32 @@
package http
import (
"io"
"net/http"
"strings"
"time"
"github.com/debridmediamanager.com/zurg/internal/config"
"github.com/debridmediamanager.com/zurg/pkg/logutil"
"go.uber.org/zap"
)
type HTTPClient struct {
Client *http.Client
MaxRetries int
Backoff func(attempt int) time.Duration
CheckRespStatus func(resp *http.Response, err error) bool
CheckRespStatus func(resp *http.Response, err error, log *zap.SugaredLogger) bool
BearerToken string
log *zap.SugaredLogger
config config.ConfigInterface
}
func (r *HTTPClient) Do(req *http.Request) (*http.Response, error) {
if r.config != nil && strings.Contains(req.Host, "download.real-debrid.") {
if host := r.config.GetRandomPreferredHost(); host != "" {
req.Host = r.config.GetRandomPreferredHost()
}
}
if r.BearerToken != "" {
req.Header.Set("Authorization", "Bearer "+r.BearerToken)
}
@@ -21,7 +34,7 @@ func (r *HTTPClient) Do(req *http.Request) (*http.Response, error) {
var err error
for attempt := 0; attempt < r.MaxRetries; attempt++ {
resp, err = r.Client.Do(req)
if !r.CheckRespStatus(resp, err) {
if !r.CheckRespStatus(resp, err, r.log) {
return resp, err
}
time.Sleep(r.Backoff(attempt))
@@ -29,7 +42,7 @@ func (r *HTTPClient) Do(req *http.Request) (*http.Response, error) {
return resp, err
}
func NewHTTPClient(token string, maxRetries int) *HTTPClient {
func NewHTTPClient(token string, maxRetries int, c config.ConfigInterface) *HTTPClient {
return &HTTPClient{
BearerToken: token,
Client: &http.Client{},
@@ -37,15 +50,21 @@ func NewHTTPClient(token string, maxRetries int) *HTTPClient {
Backoff: func(attempt int) time.Duration {
return time.Duration(attempt) * time.Second
},
CheckRespStatus: func(resp *http.Response, err error) bool {
CheckRespStatus: func(resp *http.Response, err error, log *zap.SugaredLogger) bool {
if err != nil {
return true
}
if resp.StatusCode < 200 || resp.StatusCode >= 300 {
body, _ := io.ReadAll(resp.Body)
body2, _ := io.ReadAll(resp.Request.Body)
log.Errorf("Received a %s %s from %s", resp.Status, string(body), resp.Request.URL)
log.Errorf("request %s %s", string(body2), resp.Request.URL)
return true
}
// no need to retry because the status code is 2XX
return false
},
log: logutil.NewLogger().Named("http"),
config: c,
}
}

View File

@@ -10,6 +10,7 @@ import (
"strconv"
"strings"
"github.com/debridmediamanager.com/zurg/internal/config"
zurghttp "github.com/debridmediamanager.com/zurg/pkg/http"
"go.uber.org/zap"
)
@@ -19,9 +20,9 @@ type RealDebrid struct {
client *zurghttp.HTTPClient
}
func NewRealDebrid(accessToken string, log *zap.SugaredLogger) *RealDebrid {
func NewRealDebrid(accessToken string, config config.ConfigInterface, log *zap.SugaredLogger) *RealDebrid {
maxRetries := 10
client := zurghttp.NewHTTPClient(accessToken, maxRetries)
client := zurghttp.NewHTTPClient(accessToken, maxRetries, nil)
return &RealDebrid{
log: log,
client: client,
@@ -52,11 +53,6 @@ func (rd *RealDebrid) UnrestrictCheck(link string) (*UnrestrictResponse, error)
return nil, err
}
if resp.StatusCode < 200 || resp.StatusCode >= 300 {
rd.log.Errorf("Received a %s from unrestrict check endpoint", resp.Status)
return nil, fmt.Errorf("HTTP error: %s", resp.Status)
}
var response UnrestrictResponse
err = json.Unmarshal(body, &response)
if err != nil {
@@ -101,11 +97,7 @@ func (rd *RealDebrid) GetTorrents(customLimit int) ([]Torrent, int, error) {
}
defer resp.Body.Close()
// if status code is not 2xx, return error
if resp.StatusCode < 200 || resp.StatusCode >= 300 {
rd.log.Errorf("Received a %s from get torrents endpoint", resp.Status)
return nil, 0, fmt.Errorf("HTTP error: %s", resp.Status)
}
// if status code is not 2xx, return erro
var torrents []Torrent
decoder := json.NewDecoder(resp.Body)
@@ -154,11 +146,6 @@ func (rd *RealDebrid) GetTorrentInfo(id string) (*TorrentInfo, error) {
return nil, err
}
if resp.StatusCode < 200 || resp.StatusCode >= 300 {
rd.log.Errorf("Received a %s from get info endpoint", resp.Status)
return nil, fmt.Errorf("HTTP error: %s", resp.Status)
}
var response TorrentInfo
err = json.Unmarshal(body, &response)
if err != nil {
@@ -191,11 +178,6 @@ func (rd *RealDebrid) SelectTorrentFiles(id string, files string) error {
}
defer resp.Body.Close()
if resp.StatusCode < 200 || resp.StatusCode >= 300 {
rd.log.Errorf("Received a %s from select files endpoint", resp.Status)
return fmt.Errorf("HTTP error: %s", resp.Status)
}
rd.log.Debugf("Selected files %s for torrent id=%s", len(strings.Split(files, ",")), id)
return nil
}
@@ -218,11 +200,6 @@ func (rd *RealDebrid) DeleteTorrent(id string) error {
}
defer resp.Body.Close()
if resp.StatusCode < 200 || resp.StatusCode >= 300 {
rd.log.Errorf("Received a %s from delete torrent endpoint", resp.Status)
return fmt.Errorf("HTTP error: %s", resp.Status)
}
rd.log.Debugf("Deleted torrent with id=%s", id)
return nil
}
@@ -251,11 +228,6 @@ func (rd *RealDebrid) AddMagnetHash(magnet string) (*MagnetResponse, error) {
}
defer resp.Body.Close()
if resp.StatusCode < 200 || resp.StatusCode >= 300 {
rd.log.Errorf("Received a %s from add magnet endpoint", resp.Status)
return nil, fmt.Errorf("HTTP error: %s", resp.Status)
}
var response MagnetResponse
err = json.NewDecoder(resp.Body).Decode(&response)
if err != nil {
@@ -285,11 +257,6 @@ func (rd *RealDebrid) GetActiveTorrentCount() (*ActiveTorrentCountResponse, erro
}
defer resp.Body.Close()
if resp.StatusCode < 200 || resp.StatusCode >= 300 {
rd.log.Errorf("Received a %s from active torrents endpoint", resp.Status)
return nil, fmt.Errorf("HTTP error: %s", resp.Status)
}
var response ActiveTorrentCountResponse
err = json.NewDecoder(resp.Body).Decode(&response)
if err != nil {
@@ -324,11 +291,6 @@ func (rd *RealDebrid) UnrestrictLink(link string) (*UnrestrictResponse, error) {
return nil, err
}
if resp.StatusCode < 200 || resp.StatusCode >= 300 {
rd.log.Errorf("Received a %s from unrestrict link endpoint", resp.Status)
return nil, fmt.Errorf("HTTP error: %s", resp.Status)
}
var response UnrestrictResponse
err = json.Unmarshal(body, &response)
if err != nil {
@@ -340,6 +302,6 @@ func (rd *RealDebrid) UnrestrictLink(link string) (*UnrestrictResponse, error) {
return nil, fmt.Errorf("can't fetch first byte")
}
rd.log.Debugf("Unrestricted link %s into %s", link, response.Download)
// rd.log.Debugf("Unrestricted link %s into %s", link, response.Download)
return &response, nil
}