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"` DownloadTokens []string `json:"download_tokens"` 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 := 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, 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: 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(`
| 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 (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 | ||
| Debug | ||