Add basic auth and fix repair issues

This commit is contained in:
Ben Sarmiento
2024-01-09 16:53:12 +01:00
parent eb3f2785ce
commit 91f6d35831
7 changed files with 73 additions and 39 deletions

View File

@@ -0,0 +1,17 @@
package handlers
import "net/http"
func (hs *Handlers) basicAuth(next http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
username, password, ok := r.BasicAuth()
if !ok || username != hs.cfg.GetUsername() || password != hs.cfg.GetPassword() {
w.Header().Set("WWW-Authenticate", `Basic realm="restricted"`)
w.WriteHeader(http.StatusUnauthorized)
w.Write([]byte(http.StatusText(http.StatusUnauthorized)))
return
}
next.ServeHTTP(w, r)
})
}

View File

@@ -151,7 +151,7 @@ func (zr *Handlers) handleHome(resp http.ResponseWriter, req *http.Request) {
</tr>
<tr>
<td>Premium</td>
<td>%d seconds</td>
<td>%d days</td>
</tr>
<tr>
<td>Expiration</td>
@@ -267,28 +267,28 @@ func (zr *Handlers) handleHome(resp http.ResponseWriter, req *http.Request) {
response.UserInfo.Points,
response.UserInfo.Locale,
response.UserInfo.Type,
response.UserInfo.Premium,
response.UserInfo.Expiration,
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.Host,
response.Config.Port,
response.Config.NumOfWorkers,
response.Config.RefreshEverySeconds,
response.Config.RetainRDTorrentName,
response.Config.RetainFolderNameExtension,
response.Config.CanRepair,
response.Config.DeleteRarFiles,
response.Config.RealDebridTimeout,
response.Config.UseDownloadCache,
response.Config.RateLimitSleepSeconds,
response.Config.RetriesUntilFailed,
response.Config.GetHost(),
response.Config.GetPort(),
response.Config.GetNumOfWorkers(),
response.Config.GetRefreshEverySeconds(),
response.Config.EnableRetainRDTorrentName(),
response.Config.EnableRetainFolderNameExtension(),
response.Config.EnableRepair(),
response.Config.ShouldDeleteRarFiles(),
response.Config.GetRealDebridTimeout(),
response.Config.EnableDownloadCache(),
response.Config.GetRateLimitSleepSeconds(),
response.Config.GetRetriesUntilFailed(),
strings.Join(response.Config.PreferredHosts, ", "),
response.Config.NetworkBufferSize,
response.Config.ServeFromRclone,
response.Config.VerifyDownloadLink,
response.Config.ForceIPv6,
response.Config.OnLibraryUpdate,
response.Config.GetNetworkBufferSize(),
response.Config.ShouldServeFromRclone(),
response.Config.ShouldVerifyDownloadLink(),
response.Config.ShouldForceIPv6(),
response.Config.GetOnLibraryUpdate(),
)
fmt.Fprint(resp, out)

View File

@@ -0,0 +1,13 @@
package handlers
import "net/http"
func (hs *Handlers) options(next http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
if r.Method == "OPTIONS" {
w.WriteHeader(http.StatusOK)
return
}
next.ServeHTTP(w, r)
})
}

View File

@@ -39,7 +39,10 @@ func AttachHandlers(router *chi.Mux, downloader *universal.Downloader, torMgr *t
log: log,
}
router.Use(optionsMiddleware)
if cfg.GetUsername() != "" {
router.Use(hs.basicAuth)
}
router.Use(hs.options)
router.Get("/", hs.handleHome)
// version
@@ -283,16 +286,6 @@ func (hs *Handlers) moveTorrentHandler(resp http.ResponseWriter, req *http.Reque
resp.WriteHeader(http.StatusNoContent)
}
func optionsMiddleware(next http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
if r.Method == "OPTIONS" {
w.WriteHeader(http.StatusOK)
return
}
next.ServeHTTP(w, r)
})
}
// universal handlers
func (hs *Handlers) handleDownloadFile(resp http.ResponseWriter, req *http.Request) {