406 lines
10 KiB
Go
406 lines
10 KiB
Go
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 := `<table border="1px">
|
|
<tr>
|
|
<th colspan="3">zurg</th>
|
|
</tr>
|
|
<tr>
|
|
<td>Version</td>
|
|
<td colspan="2">%s</td>
|
|
</tr>
|
|
<tr>
|
|
<td>Built At</td>
|
|
<td colspan="2">%s</td>
|
|
</tr>
|
|
<tr>
|
|
<td>Git Commit</td>
|
|
<td colspan="2">%s</td>
|
|
</tr>
|
|
<tr>
|
|
<td>HTML</td>
|
|
<td colspan="2"><a href="%s">%s</a></td>
|
|
</tr>
|
|
<tr>
|
|
<td>DAV</td>
|
|
<td colspan="2"><a href="%s">%s</a></td>
|
|
</tr>
|
|
<tr>
|
|
<td>Infuse</td>
|
|
<td colspan="2"><a href="%s">%s</a></td>
|
|
</tr>
|
|
<tr>
|
|
<td>Logs</td>
|
|
<td colspan="2"><a href="%s">%s</a></td>
|
|
</tr>
|
|
<tr>
|
|
<td>Library Size</td>
|
|
<td colspan="2">%d items</td>
|
|
</tr>
|
|
<tr>
|
|
<td>Memory Allocation</td>
|
|
<td colspan="2">%d MB</td>
|
|
</tr>
|
|
<tr>
|
|
<td>Total Memory Allocated</td>
|
|
<td colspan="2">%d MB</td>
|
|
</tr>
|
|
<tr>
|
|
<td>System Memory</td>
|
|
<td colspan="2">%d MB</td>
|
|
</tr>
|
|
<tr>
|
|
<td>Number of GC Cycles</td>
|
|
<td colspan="2">%d</td>
|
|
</tr>
|
|
<tr>
|
|
<td>Process ID</td>
|
|
<td colspan="2">%d</td>
|
|
</tr>
|
|
<tr>
|
|
<td rowspan="3">Sponsor Zurg</td>
|
|
<td>Patreon</td>
|
|
<td><a href="%s">%s</a></td>
|
|
</tr>
|
|
<tr>
|
|
<td>Github</td>
|
|
<td><a href="%s">%s</a></td>
|
|
</tr>
|
|
<tr>
|
|
<td>Paypal</td>
|
|
<td><a href="%s">%s</a></td>
|
|
</tr>
|
|
<tr>
|
|
<td rowspan="6">User Info</td>
|
|
<td>Username</td>
|
|
<td>%s</td>
|
|
</tr>
|
|
<tr>
|
|
<td>Points</td>
|
|
<td>%d</td>
|
|
</tr>
|
|
<tr>
|
|
<td>Locale</td>
|
|
<td>%s</td>
|
|
</tr>
|
|
<tr>
|
|
<td>Type</td>
|
|
<td>%s</td>
|
|
</tr>
|
|
<tr>
|
|
<td>Premium</td>
|
|
<td>%d days</td>
|
|
</tr>
|
|
<tr>
|
|
<td>Expiration</td>
|
|
<td>%s</td>
|
|
</tr>
|
|
<tr>
|
|
<td rowspan="20">Config</td>
|
|
<td>Version</td>
|
|
<td>%s</td>
|
|
</tr>
|
|
<tr>
|
|
<td>Token</td>
|
|
<td>%s</td>
|
|
</tr>
|
|
<tr>
|
|
<td>Host</td>
|
|
<td>%s</td>
|
|
</tr>
|
|
<tr>
|
|
<td>Port</td>
|
|
<td>%s</td>
|
|
</tr>
|
|
<tr>
|
|
<td>Workers</td>
|
|
<td>%d running / %d free / %d total</td>
|
|
</tr>
|
|
<tr>
|
|
<td>Refresh Every...</td>
|
|
<td>%d secs</td>
|
|
</tr>
|
|
<tr>
|
|
<td>Retain RD Torrent Name</td>
|
|
<td>%t</td>
|
|
</tr>
|
|
<tr>
|
|
<td>Retain Folder Name Extension</td>
|
|
<td>%t</td>
|
|
</tr>
|
|
<tr>
|
|
<td>Can Repair</td>
|
|
<td>%t</td>
|
|
</tr>
|
|
<tr>
|
|
<td>Action to take on RAR'ed torrents</td>
|
|
<td>%s</td>
|
|
</tr>
|
|
<tr>
|
|
<td>Repair Every...</td>
|
|
<td>%d mins</td>
|
|
</tr>
|
|
<tr>
|
|
<td>Refresh Download Mount Every...</td>
|
|
<td>%d mins</td>
|
|
</tr>
|
|
<tr>
|
|
<td>Dump Torrents Every...</td>
|
|
<td>%d mins</td>
|
|
</tr>
|
|
<tr>
|
|
<td>API Timeout</td>
|
|
<td>%d secs</td>
|
|
</tr>
|
|
<tr>
|
|
<td>Download Timeout</td>
|
|
<td>%d secs</td>
|
|
</tr>
|
|
<tr>
|
|
<td>Retries Until Failed</td>
|
|
<td>%d</td>
|
|
</tr>
|
|
<tr>
|
|
<td>Network Buffer Size</td>
|
|
<td>%d bytes</td>
|
|
</tr>
|
|
<tr>
|
|
<td>Serve From Rclone</td>
|
|
<td>%t</td>
|
|
</tr>
|
|
<tr>
|
|
<td>Force IPv6</td>
|
|
<td>%t</td>
|
|
</tr>
|
|
<tr>
|
|
<td>On Library Update</td>
|
|
<td>%v</td>
|
|
</tr>
|
|
<tr>
|
|
<td>Immediate Bin</td>
|
|
<td colspan="2">%v</td>
|
|
</tr>
|
|
<tr>
|
|
<td>Once Done Bin</td>
|
|
<td colspan="2">%v</td>
|
|
</tr>
|
|
<tr>
|
|
<td>Utilities</td>
|
|
<td colspan="2">
|
|
<form method="get" action="/logs/upload">
|
|
<input type="submit" value="Upload logs" />
|
|
</form>
|
|
<form method="post" action="/reboot-worker">
|
|
<input type="submit" value="Reboot worker pool" />
|
|
</form>
|
|
<form method="post" action="/reboot-refresh">
|
|
<input type="submit" value="Reboot refresh worker" />
|
|
</form>
|
|
<form method="post" action="/reboot-repair">
|
|
<input type="submit" value="Reboot repair worker" />
|
|
</form>
|
|
<form method="post" action="/downloads/remount">
|
|
<input type="submit" value="Remount downloads" />
|
|
</form>
|
|
<form method="post" action="/torrents/dump">
|
|
<input type="submit" value="Dump torrents" />
|
|
</form>
|
|
<form method="post" action="/torrents/analyze">
|
|
<input type="submit" value="Analyze torrents" />
|
|
</form>
|
|
<form method="post" action="/torrents/repair">
|
|
<input type="submit" value="Repair torrents" />
|
|
</form>
|
|
</td>
|
|
</tr>
|
|
</table>
|
|
`
|
|
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.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
|
|
}
|