Multi-token support

This commit is contained in:
Ben Adrian Sarmiento
2024-06-28 04:47:43 +02:00
parent 5e06f04f33
commit 962845fb81
15 changed files with 214 additions and 108 deletions

View File

@@ -29,7 +29,7 @@ type RootResponse struct {
Infuse string `json:"infuse"`
Logs string `json:"logs"`
UserInfo *realdebrid.User `json:"user_info"`
TrafficLogged uint64 `json:"traffic_logged"`
APITrafficMB uint64 `json:"traffic_from_api"`
RequestedMB uint64 `json:"requested_mb"`
ServedMB uint64 `json:"served_mb"`
LibrarySize int `json:"library_size"` // Number of torrents in the library
@@ -82,10 +82,10 @@ func (zr *Handlers) generateResponse(resp http.ResponseWriter, req *http.Request
http.Error(resp, err.Error(), http.StatusInternalServerError)
return nil, err
}
var trafficLogged int64
trafficLogged = 0
var trafficFromAPI int64
trafficFromAPI = 0
if _, ok := trafficDetails["real-debrid.com"]; ok {
trafficLogged = trafficDetails["real-debrid.com"]
trafficFromAPI = trafficDetails["real-debrid.com"]
}
userInfo.Premium = userInfo.Premium / 86400
@@ -101,7 +101,7 @@ func (zr *Handlers) generateResponse(resp http.ResponseWriter, req *http.Request
Infuse: fmt.Sprintf("//%s/infuse/", req.Host),
Logs: fmt.Sprintf("//%s/logs/", req.Host),
UserInfo: userInfo,
TrafficLogged: bToMb(uint64(trafficLogged)),
APITrafficMB: bToMb(uint64(trafficFromAPI)),
RequestedMB: bToMb(zr.downloader.RequestedBytes.Load()),
ServedMB: bToMb(zr.downloader.TotalBytes.Load()),
LibrarySize: allTorrents.Count(),
@@ -198,10 +198,10 @@ func (zr *Handlers) handleHome(resp http.ResponseWriter, req *http.Request) {
}
efficiency := response.ServedMB * 100 / denominator
if zr.initialTraffic.Load() > response.TrafficLogged {
if zr.trafficOnStartup.Load() > response.APITrafficMB {
// it cannot be bigger than traffic logged
// so it must be a reset back to 0
zr.initialTraffic.Store(0)
zr.trafficOnStartup.Store(response.APITrafficMB * 1024 * 1024)
}
out += fmt.Sprintf(`
@@ -251,8 +251,8 @@ func (zr *Handlers) handleHome(resp http.ResponseWriter, req *http.Request) {
response.Sys,
response.NumGC,
response.PID,
response.TrafficLogged,
response.TrafficLogged-bToMb(zr.initialTraffic.Load()),
response.APITrafficMB,
response.APITrafficMB-bToMb(zr.trafficOnStartup.Load()),
response.RequestedMB,
response.ServedMB,
efficiency,

View File

@@ -21,14 +21,14 @@ import (
)
type Handlers struct {
downloader *universal.Downloader
torMgr *torrent.TorrentManager
cfg config.ConfigInterface
api *realdebrid.RealDebrid
workerPool *ants.Pool
hosts []string
initialTraffic atomic.Uint64
log *logutil.Logger
downloader *universal.Downloader
torMgr *torrent.TorrentManager
cfg config.ConfigInterface
api *realdebrid.RealDebrid
workerPool *ants.Pool
hosts []string
trafficOnStartup atomic.Uint64
log *logutil.Logger
}
func init() {
@@ -53,9 +53,9 @@ func AttachHandlers(router *chi.Mux, downloader *universal.Downloader, torMgr *t
log.Errorf("Failed to get traffic details: %v", err)
trafficDetails = make(map[string]int64)
}
hs.initialTraffic.Store(uint64(0))
hs.trafficOnStartup.Store(uint64(0))
if _, ok := trafficDetails["real-debrid.com"]; ok {
hs.initialTraffic.Store(uint64(trafficDetails["real-debrid.com"]))
hs.trafficOnStartup.Store(uint64(trafficDetails["real-debrid.com"]))
}
if cfg.GetUsername() != "" {
@@ -426,7 +426,7 @@ func (hs *Handlers) handleDownloadLink(resp http.ResponseWriter, req *http.Reque
filename = chi.URLParam(req, "filename")
}
if download, ok := hs.torMgr.DownloadMap.Get(filename); ok {
hs.downloader.DownloadLink(download.Filename, download.Download, resp, req, hs.torMgr, hs.cfg, hs.log)
hs.downloader.DownloadLink(download, resp, req, hs.torMgr, hs.cfg, hs.log)
} else {
http.NotFound(resp, req)
}