Add basic auth and fix repair issues
This commit is contained in:
17
internal/handlers/basicauth.go
Normal file
17
internal/handlers/basicauth.go
Normal 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)
|
||||
})
|
||||
}
|
||||
@@ -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)
|
||||
|
||||
13
internal/handlers/options.go
Normal file
13
internal/handlers/options.go
Normal 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)
|
||||
})
|
||||
}
|
||||
@@ -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) {
|
||||
|
||||
Reference in New Issue
Block a user