perf(web): do not redirect unauthorized TDBMS requests
This commit is contained in:
parent
dfe53d3e16
commit
dc937444e5
@ -44,15 +44,20 @@ func TokenGenerate(seed []byte) {
|
|||||||
defer log.Println("Token generated")
|
defer log.Println("Token generated")
|
||||||
}
|
}
|
||||||
|
|
||||||
func Auth(handler http.HandlerFunc) http.HandlerFunc {
|
func Auth(handler http.HandlerFunc, redirect bool) http.HandlerFunc {
|
||||||
return func(w http.ResponseWriter, r *http.Request) {
|
return func(w http.ResponseWriter, r *http.Request) {
|
||||||
authorized := false
|
authorized := false
|
||||||
defer func() {
|
defer func() {
|
||||||
if authorized {
|
if authorized {
|
||||||
handler.ServeHTTP(w, r)
|
handler.ServeHTTP(w, r)
|
||||||
} else {
|
} else {
|
||||||
http.Redirect(w, r, "/auth", http.StatusSeeOther)
|
if redirect {
|
||||||
defer log.Println("Unauthorized request, redirecting to /auth")
|
http.Redirect(w, r, "/auth", http.StatusSeeOther)
|
||||||
|
defer log.Println("Unauthorized request, redirecting to /auth")
|
||||||
|
} else {
|
||||||
|
http.Error(w, "Unauthorized", http.StatusUnauthorized)
|
||||||
|
defer log.Println("Unauthorized request, status 401")
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}()
|
}()
|
||||||
token, err := r.Cookie("token")
|
token, err := r.Cookie("token")
|
||||||
@ -223,20 +228,20 @@ func main() {
|
|||||||
public_fs.ServeHTTP(w, r)
|
public_fs.ServeHTTP(w, r)
|
||||||
})
|
})
|
||||||
http.HandleFunc("/AUTH", HandlerAuth)
|
http.HandleFunc("/AUTH", HandlerAuth)
|
||||||
http.HandleFunc("/TDBMS", Auth(HandlerTDBMS))
|
http.HandleFunc("/TDBMS", Auth(HandlerTDBMS, false))
|
||||||
tfm_fs := http.FileServer(http.Dir("/srv/data/tfm"))
|
tfm_fs := http.FileServer(http.Dir("/srv/data/tfm"))
|
||||||
http.Handle("/files/", Auth(func(w http.ResponseWriter, r *http.Request) {
|
http.Handle("/files/", Auth(func(w http.ResponseWriter, r *http.Request) {
|
||||||
http.StripPrefix("/files", tfm_fs).ServeHTTP(w, r)
|
http.StripPrefix("/files", tfm_fs).ServeHTTP(w, r)
|
||||||
}))
|
}, true))
|
||||||
http.Handle("/thumbs/", Auth(func(w http.ResponseWriter, r *http.Request) {
|
http.Handle("/thumbs/", Auth(func(w http.ResponseWriter, r *http.Request) {
|
||||||
thumb_path := strings.Split(r.URL.Path, "/")
|
thumb_path := strings.Split(r.URL.Path, "/")
|
||||||
thumb_path[len(thumb_path)-1] = ".thumb-" + thumb_path[len(thumb_path)-1]
|
thumb_path[len(thumb_path)-1] = ".thumb-" + thumb_path[len(thumb_path)-1]
|
||||||
r.URL.Path = strings.Join(thumb_path, "/")
|
r.URL.Path = strings.Join(thumb_path, "/")
|
||||||
http.StripPrefix("/thumbs", tfm_fs).ServeHTTP(w, r)
|
http.StripPrefix("/thumbs", tfm_fs).ServeHTTP(w, r)
|
||||||
}))
|
}, true))
|
||||||
http.Handle("/preview/", Auth(func(w http.ResponseWriter, r *http.Request) {
|
http.Handle("/preview/", Auth(func(w http.ResponseWriter, r *http.Request) {
|
||||||
http.StripPrefix("/preview", tfm_fs).ServeHTTP(w, r)
|
http.StripPrefix("/preview", tfm_fs).ServeHTTP(w, r)
|
||||||
}))
|
}, true))
|
||||||
log.Println("Running...")
|
log.Println("Running...")
|
||||||
err = server.ListenAndServeTLS("/etc/ssl/certs/web-global.crt", "/etc/ssl/private/web-global.key")
|
err = server.ListenAndServeTLS("/etc/ssl/certs/web-global.crt", "/etc/ssl/private/web-global.key")
|
||||||
if errors.Is(err, http.ErrServerClosed) {
|
if errors.Is(err, http.ErrServerClosed) {
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user