diff options
| -rw-r--r-- | roseh.moe.go | 84 |
1 files changed, 33 insertions, 51 deletions
diff --git a/roseh.moe.go b/roseh.moe.go index a512191..cb5c8d7 100644 --- a/roseh.moe.go +++ b/roseh.moe.go @@ -816,10 +816,6 @@ type notepadTemplateArgs struct { } func notepad(w http.ResponseWriter, r *http.Request) { - if !cookieAuth(w, r) { - redirectLogin(w, r, "/notepad") - return - } notepadContentsMu.Lock() currentContent := notepadContents notepadContentsMu.Unlock() @@ -828,23 +824,12 @@ func notepad(w http.ResponseWriter, r *http.Request) { } } -func saveNote(w http.ResponseWriter, r *http.Request) error { - if !cookieAuth(w, r) { - return fmt.Errorf("not logged in") - } +func autosave(w http.ResponseWriter, r *http.Request) { newContent := r.FormValue("content") notepadContentsMu.Lock() notepadContents = newContent notepadContentsMu.Unlock() - return nil -} - -func autosave(w http.ResponseWriter, r *http.Request) { - msg := "Saved ✓" - if err := saveNote(w, r); err != nil { - msg = fmt.Sprintf("Failed to save: %s", err) - } - if err := notepadTemplate.ExecuteTemplate(w, "saveIndicator", msg); err != nil { + if err := notepadTemplate.ExecuteTemplate(w, "saveIndicator", "Saved ✓"); err != nil { log.Printf("Warning: autosave: %s", err) } } @@ -930,30 +915,6 @@ func pkg(w http.ResponseWriter, r *http.Request) { } } -type nezukoHandler struct { - srv http.Handler -} - -func (h *nezukoHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) { - if !cookieAuth(w, r) { - redirectLogin(w, r, r.URL.Path) - return - } - h.srv.ServeHTTP(w, r) -} - -type jellyfinReverseProxy struct { - proxy httputil.ReverseProxy -} - -func (p *jellyfinReverseProxy) ServeHTTP(w http.ResponseWriter, r *http.Request) { - if !cookieAuth(w, r) { - redirectLogin(w, r, "https://cinema.roseh.moe") - return - } - p.proxy.ServeHTTP(w, r) -} - type nopCloser struct { io.Writer } @@ -1059,6 +1020,29 @@ func (h *gzipHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) { h.h.ServeHTTP(w, r) } +type authHandler struct { + h http.Handler + noRedirect bool +} + +func (h *authHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) { + if !cookieAuth(w, r) { + if h.noRedirect { + http.Error(w, "not logged in", http.StatusUnauthorized) + } else { + u := *r.URL + u.Host = r.Host + u.Scheme = "http" + if *https != "" { + u.Scheme = "https" + } + redirectLogin(w, r, u.String()) + } + return + } + h.h.ServeHTTP(w, r) +} + func main() { flag.Parse() @@ -1085,14 +1069,14 @@ func main() { mux.HandleFunc("GET /wormhole/{hole}", wormholeDownload) mux.HandleFunc("GET /login", serveLogin) mux.HandleFunc("POST /login", login) - mux.HandleFunc("GET /notepad", notepad) - mux.HandleFunc("POST /notepad", autosave) + mux.Handle("GET /notepad", &authHandler{h: http.HandlerFunc(notepad)}) + mux.Handle("POST /notepad", &authHandler{h: http.HandlerFunc(autosave), noRedirect: true}) mux.HandleFunc("GET /cmd/", cmdIndex) mux.HandleFunc("GET /cmd/{cmd}", cmd) mux.HandleFunc("GET /pkg/", pkgIndex) mux.HandleFunc("GET /pkg/{pkg}", pkg) mux.Handle("GET /code/", http.StripPrefix("/code/", http.FileServer(http.Dir(*codeDir)))) - mux.Handle("GET /nezuko/", &nezukoHandler{http.StripPrefix("/nezuko/", http.FileServer(http.Dir(*nezuko)))}) + mux.Handle("GET /nezuko/", &authHandler{h: http.StripPrefix("/nezuko/", http.FileServer(http.Dir(*nezuko)))}) mux.HandleFunc("GET /static/", static) mux.HandleFunc("GET /favicon.ico", favicon) apiMux := http.NewServeMux() @@ -1104,14 +1088,12 @@ func main() { if err != nil { log.Fatal(err) } - http.Handle("cinema.roseh.moe/", &jellyfinReverseProxy{ - proxy: httputil.ReverseProxy{ - Rewrite: func(r *httputil.ProxyRequest) { - r.SetURL(jellyfinURL) - r.SetXForwarded() - }, + http.Handle("cinema.roseh.moe/", &authHandler{h: &httputil.ReverseProxy{ + Rewrite: func(r *httputil.ProxyRequest) { + r.SetURL(jellyfinURL) + r.SetXForwarded() }, - }) + }}) addr := fmt.Sprintf(":%d", *port) log.Printf("Listening on %q", addr) |
