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"` 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"` DownloadTokens []string `json:"download_tokens"` IDsToDelete []string `json:"ids_to_delete"` Hosts []string `json:"hosts"` TrafficServedPerAPI map[string]uint64 `json:"traffic_served_per_api"` } 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) trafficFromAPI := make(map[string]uint64) for _, token := range zr.rd.TokenManager.GetAllTokens() { trafficDetails, err := zr.rd.GetTrafficDetails(token) if err != nil { trafficDetails = make(map[string]uint64) } trafficFromAPI[token] = 0 if _, ok := trafficDetails["real-debrid.com"]; ok { trafficFromAPI[token] = trafficDetails["real-debrid.com"] } } userInfo.Premium = userInfo.Premium / 86400 userInfo.Expiration = strings.Replace(userInfo.Expiration, "Z", "+01:00", 1) token := utils.MaskToken(zr.cfg.GetToken()) var downloadTokens []string for _, t := range zr.cfg.GetDownloadTokens() { downloadTokens = append(downloadTokens, utils.MaskToken(t)) } 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, TrafficServedPerAPI: trafficFromAPI, 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: token, DownloadTokens: downloadTokens, 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, ) out += fmt.Sprintf(` `, response.LibrarySize, response.MemAlloc, response.TotalAlloc, response.Sys, response.NumGC, response.PID, bToMb(zr.downloader.TrafficServed.Load()), // traffic served *zurg* ) for token, traffic := range response.TrafficServedPerAPI { trafficOnStartup, _ := zr.downloader.TrafficOnStartup.Get(token) out += fmt.Sprintf(` `, utils.MaskToken(token)[40:], bToMb(traffic), // traffic served *api* bToMb(traffic-trafficOnStartup.Load()), // traffic served *api* since startup ) } 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.DownloadTokens, 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.ShouldLogRequests(), 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 Served (zurg) %d MB
Traffic Served (%s) %d MB (%d MB since startup)
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 (token) %s
Download Tokens (download_tokens) %v
Host (host) %s
Port (port) %s
Workers (concurrent_workers) %d running / %d free / %d total
Refresh Every... (check_for_changes_every_secs) %d secs
Retain RD Torrent Name (retain_rd_torrent_name) %t
Retain Folder Name Extension (retain_folder_name_extension) %t
Can Repair (enable_repair) %t
Action to take on RAR'ed torrents (rar_action) %s
Repair Every... (repair_every_mins) %d mins
Refresh Download Mount Every... (downloads_every_mins) %d mins
Dump Torrents Every... (dump_torrents_every_mins) %d mins
API Timeout (api_timeout_secs) %d secs
Download Timeout (download_timeout_secs) %d secs
Retries Until Failed (retries_until_failed) %d
Auto-Analyze New Torrents (auto_analyze_new_torrents) %t
Cache Network Test Results (cache_network_test_results) %t
Additional Playable Extensions (addl_playable_extensions) %s
Serve From Rclone (serve_from_rclone) %t
Force IPv6 (force_ipv6) %t
Log Requests (log_requests) %t
On Library Update (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 }