578 lines
17 KiB
Go
578 lines
17 KiB
Go
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"`
|
|
VidHub string `json:"vidhub"`
|
|
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),
|
|
VidHub: fmt.Sprintf("//%s/vidhub/", 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(`<head>
|
|
<link
|
|
rel="stylesheet"
|
|
href="https://cdn.jsdelivr.net/npm/bulma@1.0.1/css/bulma.min.css">
|
|
<style>
|
|
body {
|
|
padding: 15px;
|
|
background-color: #212A31; /* Dark background */
|
|
color: #D3D9D4; /* Light text color for contrast */
|
|
font-family: 'Roboto', sans-serif; /* Modern font choice */
|
|
}
|
|
|
|
table {
|
|
width: 100%%;
|
|
border-collapse: collapse;
|
|
background-color: #2E3944; /* Dark gray background for contrast */
|
|
box-shadow: 0 2px 5px rgba(0, 0, 0, 0.5); /* Subtle shadow */
|
|
}
|
|
|
|
th, td {
|
|
border: 1px solid #124E66; /* Border color from the palette */
|
|
padding: 10px;
|
|
text-align: left;
|
|
}
|
|
|
|
th {
|
|
background-color: #124E66; /* Dark header background */
|
|
color: #D3D9D4; /* Light text on header */
|
|
}
|
|
|
|
tr:nth-child(even) {
|
|
background-color: #2E3944; /* Subtle alternating row color */
|
|
}
|
|
|
|
tr:hover {
|
|
background-color: #1A252C; /* Darker shade on hover */
|
|
}
|
|
|
|
a {
|
|
color: #03A9F4; /* Slightly lighter blue for links */
|
|
text-decoration: none;
|
|
}
|
|
|
|
a:hover {
|
|
text-decoration: underline;
|
|
}
|
|
|
|
/* Button Styling */
|
|
input[type="submit"] {
|
|
background-color: #124E66; /* Button color from the palette */
|
|
color: #D3D9D4;
|
|
padding: 8px 15px;
|
|
border: none;
|
|
border-radius: 4px;
|
|
cursor: pointer;
|
|
transition: background-color 0.3s;
|
|
}
|
|
|
|
input[type="submit"]:hover {
|
|
background-color: #2E3944; /* Darker on hover */
|
|
}
|
|
|
|
/* Link Styling */
|
|
a {
|
|
color: #03A9F4; /* Slightly lighter blue for links */
|
|
text-decoration: none;
|
|
transition: color 0.3s;
|
|
}
|
|
|
|
a:hover {
|
|
color: #D3D9D4; /* Light color on hover for contrast */
|
|
}
|
|
</style>
|
|
</head>
|
|
|
|
<body>
|
|
<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>VidHub</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>`,
|
|
response.Version,
|
|
response.BuiltAt,
|
|
response.GitCommit,
|
|
response.Html,
|
|
response.Html,
|
|
response.Dav,
|
|
response.Dav,
|
|
response.Infuse,
|
|
response.Infuse,
|
|
response.VidHub,
|
|
response.VidHub,
|
|
response.Logs,
|
|
response.Logs,
|
|
)
|
|
|
|
out += fmt.Sprintf(`
|
|
<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>Traffic Served (zurg)</td>
|
|
<td colspan="2">%d MB</td>
|
|
</tr>`,
|
|
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(`
|
|
<tr>
|
|
<td>Traffic Served (%s)</td>
|
|
<td colspan="2">%d MB (%d MB since startup)</td>
|
|
</tr>`,
|
|
utils.MaskToken(token)[40:],
|
|
bToMb(traffic), // traffic served *api*
|
|
bToMb(traffic-trafficOnStartup.Load()), // traffic served *api* since startup
|
|
)
|
|
}
|
|
|
|
out += fmt.Sprintf(`
|
|
<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>`,
|
|
response.Sponsor.Patreon,
|
|
response.Sponsor.Patreon,
|
|
response.Sponsor.Github,
|
|
response.Sponsor.Github,
|
|
response.Sponsor.Paypal,
|
|
response.Sponsor.Paypal,
|
|
)
|
|
|
|
out += fmt.Sprintf(`
|
|
<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>`,
|
|
response.UserInfo.Username,
|
|
response.UserInfo.Points,
|
|
response.UserInfo.Locale,
|
|
response.UserInfo.Type,
|
|
response.UserInfo.Premium,
|
|
response.UserInfo.Expiration,
|
|
)
|
|
|
|
out += fmt.Sprintf(`
|
|
<tr>
|
|
<td rowspan="24">Config</td>
|
|
<td>Version</td>
|
|
<td>%s</td>
|
|
</tr>
|
|
<tr>
|
|
<td>Token (token)</td>
|
|
<td>%s</td>
|
|
</tr>
|
|
<tr>
|
|
<td>Download Tokens (download_tokens)</td>
|
|
<td>%v</td>
|
|
</tr>
|
|
<tr>
|
|
<td>Host (host)</td>
|
|
<td>%s</td>
|
|
</tr>
|
|
<tr>
|
|
<td>Port (port)</td>
|
|
<td>%s</td>
|
|
</tr>
|
|
<tr>
|
|
<td>Workers (concurrent_workers)</td>
|
|
<td>%d running / %d free / %d total</td>
|
|
</tr>
|
|
<tr>
|
|
<td>Refresh Every... (check_for_changes_every_secs)</td>
|
|
<td>%d secs</td>
|
|
</tr>
|
|
<tr>
|
|
<td>Retain RD Torrent Name (retain_rd_torrent_name)</td>
|
|
<td>%t</td>
|
|
</tr>
|
|
<tr>
|
|
<td>Retain Folder Name Extension (retain_folder_name_extension)</td>
|
|
<td>%t</td>
|
|
</tr>
|
|
<tr>
|
|
<td>Can Repair (enable_repair)</td>
|
|
<td>%t</td>
|
|
</tr>
|
|
<tr>
|
|
<td>Action to take on RAR'ed torrents (rar_action)</td>
|
|
<td>%s</td>
|
|
</tr>
|
|
<tr>
|
|
<td>Repair Every... (repair_every_mins)</td>
|
|
<td>%d mins</td>
|
|
</tr>
|
|
<tr>
|
|
<td>Refresh Download Mount Every... (downloads_every_mins)</td>
|
|
<td>%d mins</td>
|
|
</tr>
|
|
<tr>
|
|
<td>Dump Torrents Every... (dump_torrents_every_mins)</td>
|
|
<td>%d mins</td>
|
|
</tr>
|
|
<tr>
|
|
<td>API Timeout (api_timeout_secs)</td>
|
|
<td>%d secs</td>
|
|
</tr>
|
|
<tr>
|
|
<td>Download Timeout (download_timeout_secs)</td>
|
|
<td>%d secs</td>
|
|
</tr>
|
|
<tr>
|
|
<td>Retries Until Failed (retries_until_failed)</td>
|
|
<td>%d</td>
|
|
</tr>
|
|
<tr>
|
|
<td>Auto-Analyze New Torrents (auto_analyze_new_torrents)</td>
|
|
<td>%t</td>
|
|
</tr>
|
|
<tr>
|
|
<td>Cache Network Test Results (cache_network_test_results)</td>
|
|
<td>%t</td>
|
|
</tr>
|
|
<tr>
|
|
<td>Additional Playable Extensions (addl_playable_extensions)</td>
|
|
<td>%s</td>
|
|
</tr>
|
|
<tr>
|
|
<td>Serve From Rclone (serve_from_rclone)</td>
|
|
<td>%t</td>
|
|
</tr>
|
|
<tr>
|
|
<td>Force IPv6 (force_ipv6)</td>
|
|
<td>%t</td>
|
|
</tr>
|
|
<tr>
|
|
<td>Log Requests (log_requests)</td>
|
|
<td>%t</td>
|
|
</tr>
|
|
<tr>
|
|
<td>On Library Update (on_library_update)</td>
|
|
<td>%v</td>
|
|
</tr>`,
|
|
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(`
|
|
<tr>
|
|
<td>IDs to be deleted</td>
|
|
<td colspan="2">%v</td>
|
|
</tr>
|
|
<tr>
|
|
<td>Torrents to be repaired</td>
|
|
<td colspan="2">%s</td>
|
|
</tr>
|
|
<tr>
|
|
<td>Hosts</td>
|
|
<td colspan="2">%v</td>
|
|
</tr>
|
|
<tr>
|
|
<td>Utilities</td>
|
|
<td colspan="2">
|
|
<form method="post" action="/torrents/dump">
|
|
<input type="submit" value="Dump torrents" /> Copy all your zurgtorrent files to your dump folder
|
|
</form>
|
|
<form method="post" action="/torrents/analyze">
|
|
<input type="submit" value="Analyze torrents" /> Required to use media_info_* filters
|
|
</form>
|
|
<form method="post" action="/torrents/repair">
|
|
<input type="submit" value="Repair torrents" /> Trigger repair of all torrents
|
|
</form>
|
|
<form method="post" action="/torrents/reset-repair-state">
|
|
<input type="submit" value="Reset repair state" /> Reset repair state of all torrents so they can be repaired again
|
|
</form>
|
|
</td>
|
|
</tr>
|
|
<tr>
|
|
<td>Debug</td>
|
|
<td colspan="2">
|
|
<form method="get" action="/logs/upload">
|
|
<input type="submit" value="Upload logs" /> Share the link to get help
|
|
</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>
|
|
</td>
|
|
</tr>
|
|
</table>`,
|
|
response.IDsToDelete,
|
|
response.TorrentsToRepair,
|
|
response.Hosts,
|
|
)
|
|
|
|
fmt.Fprint(resp, out)
|
|
}
|
|
|
|
func bToMb(b uint64) uint64 {
|
|
return b / 1024 / 1024
|
|
}
|