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"
mapset "github.com/deckarep/golang-set/v2"
)
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
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 mapset.Set[string] `json:"immediate_bin"`
OnceDoneBin mapset.Set[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)
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,
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,
OnceDoneBin: zr.torMgr.OnceDoneBin,
}
out := `
| zurg |
| Version |
%s |
| Built At |
%s |
| Git Commit |
%s |
| HTML |
%s |
| DAV |
%s |
| Infuse |
%s |
| Logs |
%s |
| 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 |
| Repair Every... |
%d mins |
| Delete Rar Files |
%t |
| API Timeout |
%d secs |
| Get torrents count |
%d at a time |
| Download Timeout |
%d secs |
| Refresh Download Mount Every... |
%d mins |
| Get downloads limit |
%d items |
| Dump Torrents Every... |
%d mins |
| 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 |
%s |
| Once Done Bin |
%s |
| 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.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.GetRepairEveryMins(),
response.Config.ShouldDeleteRarFiles(),
response.Config.GetApiTimeoutSecs(),
response.Config.GetTorrentsCount(),
response.Config.GetDownloadTimeoutSecs(),
response.Config.GetDownloadsEveryMins(),
response.Config.GetDownloadsLimit(),
response.Config.GetDumpTorrentsEveryMins(),
response.Config.GetRateLimitSleepSecs(),
response.Config.GetRetriesUntilFailed(),
response.Config.GetNetworkBufferSize(),
response.Config.ShouldServeFromRclone(),
response.Config.ShouldForceIPv6(),
response.Config.GetOnLibraryUpdate(),
response.ImmediateBin.String(),
response.OnceDoneBin.String(),
)
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.RefreshKillSwitch <- 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.RepairKillSwitch <- 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 bToMb(b uint64) uint64 {
return b / 1024 / 1024
}