package handlers import ( "fmt" "net/http" "os" "runtime" "strings" "github.com/debridmediamanager/zurg/internal/config" "github.com/debridmediamanager/zurg/internal/version" "github.com/debridmediamanager/zurg/pkg/realdebrid" ) 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"` // Replace UserInfoType with the actual type LibrarySize int `json:"library_size"` // Number of torrents in the library 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"` ImmediateBin []string `json:"immediate_bin"` OnceDoneBin []string `json:"once_done_bin"` } func (zr *Handlers) handleHome(resp http.ResponseWriter, req *http.Request) { userInfo, err := zr.api.GetUserInformation() if err != nil { http.Error(resp, err.Error(), http.StatusInternalServerError) return } var mem runtime.MemStats runtime.ReadMemStats(&mem) allTorrents, _ := zr.torMgr.DirectoryMap.Get(config.ALL_TORRENTS) response := 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, LibrarySize: allTorrents.Count(), 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(), ImmediateBin: zr.torMgr.ImmediateBin.ToSlice(), OnceDoneBin: zr.torMgr.OnceDoneBin.ToSlice(), } out := `
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
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
Rate Limit Sleep for... %d secs
Retries Until Failed %d
Network Buffer Size %d bytes
Serve From Rclone %t
Force IPv6 %t
On Library Update %v
Immediate Bin %v
Once Done Bin %v
Utilities
` out = fmt.Sprintf(out, response.Version, response.BuiltAt, response.GitCommit, response.Html, response.Html, response.Dav, response.Dav, response.Infuse, response.Infuse, response.Logs, response.Logs, response.LibrarySize, response.MemAlloc, response.TotalAlloc, response.Sys, response.NumGC, response.PID, response.Sponsor.Patreon, response.Sponsor.Patreon, response.Sponsor.Github, response.Sponsor.Github, response.Sponsor.Paypal, response.Sponsor.Paypal, response.UserInfo.Username, response.UserInfo.Points, response.UserInfo.Locale, response.UserInfo.Type, response.UserInfo.Premium/86400, strings.Replace(response.UserInfo.Expiration, "Z", "+01:00", 1), response.Config.Version, strings.Replace(response.Config.Token, response.Config.Token[len(response.Config.Token)-48:], "*****", 1), 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.GetRateLimitSleepSecs(), response.Config.GetRetriesUntilFailed(), response.Config.GetNetworkBufferSize(), response.Config.ShouldServeFromRclone(), response.Config.ShouldForceIPv6(), response.Config.GetOnLibraryUpdate(), response.ImmediateBin, response.OnceDoneBin, ) fmt.Fprint(resp, out) } func (zr *Handlers) handleRebootWorkerPool(resp http.ResponseWriter, req *http.Request) { resp.Header().Set("Refresh", "2; url=/") zr.workerPool.Release() zr.workerPool.Reboot() zr.log.Infof("Rebooted worker pool") fmt.Fprint(resp, "Rebooting worker pool...") } func (zr *Handlers) handleRebootRefreshWorker(resp http.ResponseWriter, req *http.Request) { resp.Header().Set("Refresh", "2; url=/") zr.torMgr.RefreshWorkerKillSwitch <- struct{}{} zr.torMgr.StartRefreshJob() zr.log.Infof("Rebooted refresh worker") fmt.Fprint(resp, "Rebooting refresh worker...") } func (zr *Handlers) handleRebootRepairWorker(resp http.ResponseWriter, req *http.Request) { resp.Header().Set("Refresh", "2; url=/") zr.torMgr.RepairWorkerKillSwitch <- struct{}{} zr.torMgr.StartRepairJob() zr.log.Infof("Rebooted repair worker") fmt.Fprint(resp, "Rebooting repair worker...") } func (zr *Handlers) handleRemountDownloads(resp http.ResponseWriter, req *http.Request) { resp.Header().Set("Refresh", "2; url=/") zr.torMgr.RemountTrigger <- struct{}{} zr.log.Infof("Triggered remount of downloads") fmt.Fprint(resp, "Remounting downloads...") } func (zr *Handlers) handleDumpTorrents(resp http.ResponseWriter, req *http.Request) { resp.Header().Set("Refresh", "2; url=/") zr.torMgr.DumpTrigger <- struct{}{} zr.log.Infof("Triggered dump of torrents") fmt.Fprint(resp, "Dumping torrents...") } func (zr *Handlers) handleAnalyzeTorrents(resp http.ResponseWriter, req *http.Request) { resp.Header().Set("Refresh", "2; url=/") zr.torMgr.AnalyzeTrigger <- struct{}{} zr.log.Infof("Triggered media analysis of torrents") fmt.Fprint(resp, "Analyzing all torrents...") } func (zr *Handlers) handleTriggerRepairAll(resp http.ResponseWriter, req *http.Request) { resp.Header().Set("Refresh", "2; url=/") zr.torMgr.RepairAllTrigger <- struct{}{} zr.log.Infof("Triggered repair of all torrents") fmt.Fprint(resp, "Repairing all torrents...") } func bToMb(b uint64) uint64 { return b / 1024 / 1024 }