package handlers import ( "encoding/json" "fmt" "net/http" "os" "runtime" "sort" "strings" "github.com/debridmediamanager/zurg/internal/config" "github.com/debridmediamanager/zurg/internal/version" "github.com/debridmediamanager/zurg/pkg/realdebrid" "github.com/debridmediamanager/zurg/pkg/utils" ) type SponsorResponse struct { Patreon string `json:"patreon"` Github string `json:"github"` Paypal string `json:"paypal"` } type RootResponse struct { Version string `json:"version"` BuiltAt string `json:"built_at"` GitCommit string `json:"git_commit"` Html string `json:"html"` Dav string `json:"dav"` Infuse string `json:"infuse"` Logs string `json:"logs"` UserInfo *realdebrid.User `json:"user_info"` APITraffic uint64 `json:"traffic_from_api"` ServedMB uint64 `json:"served_mb"` LibrarySize int `json:"library_size"` // Number of torrents in the library TorrentsToRepair string `json:"repair_queue"` // List of torrents in the repair queue MemAlloc uint64 `json:"mem_alloc"` // Memory allocation in MB TotalAlloc uint64 `json:"total_alloc"` // Total memory allocated in MB Sys uint64 `json:"sys"` // System memory in MB NumGC uint32 `json:"num_gc"` // Number of completed GC cycles PID int `json:"pid"` // Process ID Sponsor SponsorResponse `json:"sponsor_zurg"` // Sponsorship links Config config.ZurgConfig `json:"config"` Token string `json:"token"` IDsToDelete []string `json:"ids_to_delete"` Hosts []string `json:"hosts"` } func (zr *Handlers) generateResponse(resp http.ResponseWriter, req *http.Request) (*RootResponse, error) { userInfo, err := zr.rd.GetUserInformation() if err != nil { http.Error(resp, err.Error(), http.StatusInternalServerError) return nil, err } var mem runtime.MemStats runtime.ReadMemStats(&mem) allTorrents, _ := zr.torMgr.DirectoryMap.Get(config.ALL_TORRENTS) repairQueueStr := "" if zr.torMgr.RepairQueue == nil { repairQueueStr = "uninitialized" } else if zr.torMgr.RepairQueue.IsEmpty() { repairQueueStr = "empty" } else { var repairList []string for _, torrent := range zr.torMgr.RepairQueue.ToSlice() { if torrent != nil { repairList = append(repairList, zr.torMgr.GetKey(torrent)) } } sort.Strings(repairList) repairQueueStr = strings.Join(repairList, ", ") } sortedIDs := zr.torMgr.OnceDoneBin.ToSlice() sort.Strings(sortedIDs) trafficDetails, err := zr.rd.GetTrafficDetails() if err != nil { http.Error(resp, err.Error(), http.StatusInternalServerError) return nil, err } var trafficFromAPI int64 trafficFromAPI = 0 if _, ok := trafficDetails["real-debrid.com"]; ok { trafficFromAPI = trafficDetails["real-debrid.com"] } userInfo.Premium = userInfo.Premium / 86400 userInfo.Expiration = strings.Replace(userInfo.Expiration, "Z", "+01:00", 1) token := zr.cfg.GetToken() return &RootResponse{ Version: version.GetVersion(), BuiltAt: version.GetBuiltAt(), GitCommit: version.GetGitCommit(), Html: fmt.Sprintf("//%s/http/", req.Host), Dav: fmt.Sprintf("//%s/dav/", req.Host), Infuse: fmt.Sprintf("//%s/infuse/", req.Host), Logs: fmt.Sprintf("//%s/logs/", req.Host), UserInfo: userInfo, APITraffic: uint64(trafficFromAPI), ServedMB: bToMb(zr.downloader.TotalBytes.Load()), LibrarySize: allTorrents.Count(), TorrentsToRepair: repairQueueStr, MemAlloc: bToMb(mem.Alloc), TotalAlloc: bToMb(mem.TotalAlloc), Sys: bToMb(mem.Sys), NumGC: mem.NumGC, PID: os.Getpid(), Sponsor: SponsorResponse{ Patreon: "https://www.patreon.com/debridmediamanager", Github: "https://github.com/sponsors/debridmediamanager", Paypal: "https://paypal.me/yowmamasita", }, Config: zr.cfg.GetConfig(), Token: utils.MaskToken(token), IDsToDelete: sortedIDs, Hosts: zr.hosts, }, nil } func (zr *Handlers) handleHomeJson(resp http.ResponseWriter, req *http.Request) { response, err := zr.generateResponse(resp, req) if err != nil { return } // Send the response as JSON by marshalling it jsonResponse, err := json.Marshal(response) if err != nil { http.Error(resp, err.Error(), http.StatusInternalServerError) return } // write the response resp.Header().Set("Content-Type", "application/json") resp.Write(jsonResponse) } func (zr *Handlers) handleHome(resp http.ResponseWriter, req *http.Request) { response, err := zr.generateResponse(resp, req) if err != nil { return } out := fmt.Sprintf(``, response.Version, response.BuiltAt, response.GitCommit, response.Html, response.Html, response.Dav, response.Dav, response.Infuse, response.Infuse, response.Logs, response.Logs, ) denominator := bToMb(response.APITraffic - zr.trafficOnStartup.Load()) if denominator == 0 { denominator = 1 } efficiency := response.ServedMB * 100 / denominator if zr.trafficOnStartup.Load() > response.APITraffic { // it cannot be bigger than traffic logged // so it must be a reset back to 0 zr.trafficOnStartup.Store(0) } out += fmt.Sprintf(` `, response.LibrarySize, response.MemAlloc, response.TotalAlloc, response.Sys, response.NumGC, response.PID, bToMb(response.APITraffic), bToMb(response.APITraffic-zr.trafficOnStartup.Load()), response.ServedMB, efficiency, bToMb(response.APITraffic-zr.trafficOnStartup.Load())-response.ServedMB, ) out += fmt.Sprintf(` `, response.Sponsor.Patreon, response.Sponsor.Patreon, response.Sponsor.Github, response.Sponsor.Github, response.Sponsor.Paypal, response.Sponsor.Paypal, ) out += fmt.Sprintf(` `, response.UserInfo.Username, response.UserInfo.Points, response.UserInfo.Locale, response.UserInfo.Type, response.UserInfo.Premium, response.UserInfo.Expiration, ) out += fmt.Sprintf(` `, response.Config.Version, response.Token, response.Config.GetHost(), response.Config.GetPort(), zr.workerPool.Running(), zr.workerPool.Free(), zr.workerPool.Cap(), response.Config.GetRefreshEverySecs(), response.Config.EnableRetainRDTorrentName(), response.Config.EnableRetainFolderNameExtension(), response.Config.EnableRepair(), response.Config.GetRarAction(), response.Config.GetRepairEveryMins(), response.Config.GetDownloadsEveryMins(), response.Config.GetDumpTorrentsEveryMins(), response.Config.GetApiTimeoutSecs(), response.Config.GetDownloadTimeoutSecs(), response.Config.GetRetriesUntilFailed(), response.Config.ShouldAutoAnalyzeNewTorrents(), response.Config.ShouldCacheNetworkTestResults(), response.Config.GetPlayableExtensions(), response.Config.ShouldServeFromRclone(), response.Config.ShouldForceIPv6(), response.Config.GetOnLibraryUpdate(), ) out += fmt.Sprintf(`
zurg
Version %s
Built At %s
Git Commit %s
HTML %s
DAV %s
Infuse %s
Logs %s
Library Size %d items
Memory Allocation %d MB
Total Memory Allocated %d MB
System Memory %d MB
Number of GC Cycles %d
Process ID %d
Traffic Logged %d MB (%d MB added)
Traffic Served %d MB
Traffic Efficiency %d%% (wasted %d MB) disclaimer: this assumes all RD traffic comes from this zurg instance
Sponsor Zurg Patreon %s
Github %s
Paypal %s
User Info Username %s
Points %d
Locale %s
Type %s
Premium %d days
Expiration %s
Config Version %s
Token %s
Host %s
Port %s
Workers %d running / %d free / %d total
Refresh Every... %d secs
Retain RD Torrent Name %t
Retain Folder Name Extension %t
Can Repair %t
Action to take on RAR'ed torrents %s
Repair Every... %d mins
Refresh Download Mount Every... %d mins
Dump Torrents Every... %d mins
API Timeout %d secs
Download Timeout %d secs
Retries Until Failed %d
Auto-Analyze New Torrents %t
Cache Network Test Results %t
Additional Playable Extensions %s
Serve From Rclone %t
Force IPv6 %t
On Library Update %v
IDs to be deleted %v
Torrents to be repaired %s
Hosts %v
Utilities
Copy all your zurgtorrent files to your dump folder
Required to use media_info_* filters
Trigger repair of all torrents
Reset repair state of all torrents so they can be repaired again
Debug
Share the link to get help
`, response.IDsToDelete, response.TorrentsToRepair, response.Hosts, ) fmt.Fprint(resp, out) } func bToMb(b uint64) uint64 { return b / 1024 / 1024 }