package main import ( "bufio" "context" "encoding/json" "errors" "fmt" "html" "io" "log" "net/http" "net/url" "os" "path" "path/filepath" "strings" "time" "github.com/gorilla/websocket" ) var server *http.Server func webserver() { http.Handle("/", mungeHandler{http.FileServer(http.Dir(staticRoot))}) http.HandleFunc("/group/", groupHandler) http.HandleFunc("/recordings", func(w http.ResponseWriter, r *http.Request) { http.Redirect(w, r, "/recordings/", http.StatusPermanentRedirect) }) http.HandleFunc("/recordings/", recordingsHandler) http.HandleFunc("/ws", wsHandler) http.HandleFunc("/ice-servers.json", func(w http.ResponseWriter, r *http.Request) { mungeHeader(w) http.ServeFile(w, r, filepath.Join(dataDir, "ice-servers.json")) }) http.HandleFunc("/public-groups.json", publicHandler) http.HandleFunc("/stats", statsHandler) server = &http.Server{ Addr: httpAddr, ReadHeaderTimeout: 60 * time.Second, IdleTimeout: 120 * time.Second, } server.RegisterOnShutdown(func() { rangeGroups(func (g *group) bool { go g.shutdown("server is shutting down") return true }) }) go func() { var err error err = server.ListenAndServeTLS( filepath.Join(dataDir, "cert.pem"), filepath.Join(dataDir, "key.pem"), ) if err != nil && err != http.ErrServerClosed { log.Printf("ListenAndServeTLS: %v", err) } }() } func mungeHeader(w http.ResponseWriter) { w.Header().Add("Content-Security-Policy", "connect-src ws: wss: 'self'; img-src data: 'self'; default-src 'self'") } // mungeResponseWriter redirects 404 replies to our custom 404 page. type mungeResponseWriter struct { http.ResponseWriter active bool } func (w *mungeResponseWriter) WriteHeader(status int) { if status == http.StatusNotFound { w.active = true notFound(w.ResponseWriter) return } w.ResponseWriter.WriteHeader(status) } func (w *mungeResponseWriter) Write(p []byte) (int, error) { if w.active { return len(p), nil } return w.ResponseWriter.Write(p) } func notFound(w http.ResponseWriter) { w.Header().Set("Content-Type", "text/html; charset=utf-8") w.WriteHeader(http.StatusNotFound) f, err := os.Open(path.Join(staticRoot, "404.html")) if err != nil { fmt.Fprintln(w, "
Not found
") return } defer f.Close() io.Copy(w, f) } // mungeHandler adds our custom headers and redirects 404 replies type mungeHandler struct { h http.Handler } func (h mungeHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) { mungeHeader(w) h.h.ServeHTTP(&mungeResponseWriter{ResponseWriter: w}, r) } func parseGroupName(path string) string { if !strings.HasPrefix(path, "/group/") { return "" } name := path[len("/group/"):] if name == "" { return "" } if name[len(name)-1] == '/' { name = name[:len(name)-1] } return name } func groupHandler(w http.ResponseWriter, r *http.Request) { mungeHeader(w) name := parseGroupName(r.URL.Path) if name == "" { notFound(w) return } g, err := addGroup(name, nil) if err != nil { if os.IsNotExist(err) { notFound(w) } else { log.Printf("addGroup: %v", err) http.Error(w, "Internal server error", http.StatusInternalServerError) } return } if redirect := g.Redirect(); redirect != "" { http.Redirect(w, r, redirect, http.StatusPermanentRedirect) return } http.ServeFile(w, r, filepath.Join(staticRoot, "sfu.html")) } func publicHandler(w http.ResponseWriter, r *http.Request) { w.Header().Set("content-type", "application/json") w.Header().Set("cache-control", "no-cache") if r.Method == "HEAD" { return } g := getPublicGroups() e := json.NewEncoder(w) e.Encode(g) return } func getPassword() (string, string, error) { f, err := os.Open(filepath.Join(dataDir, "passwd")) if err != nil { return "", "", err } defer f.Close() r := bufio.NewReader(f) s, err := r.ReadString('\n') if err != nil { return "", "", err } l := strings.SplitN(strings.TrimSpace(s), ":", 2) if len(l) != 2 { return "", "", errors.New("couldn't parse passwords") } return l[0], l[1], nil } func failAuthentication(w http.ResponseWriter, realm string) { w.Header().Set("www-authenticate", fmt.Sprintf("basic realm=\"%v\"", realm)) http.Error(w, "Haha!", http.StatusUnauthorized) } func statsHandler(w http.ResponseWriter, r *http.Request) { u, p, err := getPassword() if err != nil { log.Printf("Passwd: %v", err) failAuthentication(w, "stats") return } username, password, ok := r.BasicAuth() if !ok || username != u || password != p { failAuthentication(w, "stats") return } w.Header().Set("content-type", "text/html; charset=utf-8") w.Header().Set("cache-control", "no-cache") if r.Method == "HEAD" { return } stats := getGroupStats() fmt.Fprintf(w, "\n\n") fmt.Fprintf(w, "%v
\n", html.EscapeString(gs.name)) fmt.Fprintf(w, "%v | |||
Up | %v | ", up.id) if up.maxBitrate > 0 { fmt.Fprintf(w, "%v | ", up.maxBitrate) } fmt.Fprintf(w, "|
Down | %v | ", down.id) if down.maxBitrate > 0 { fmt.Fprintf(w, "%v | ", down.maxBitrate) } fmt.Fprintf(w, "
%v | %d | ", html.EscapeString(fi.Name()), html.EscapeString(fi.Name()), fi.Size(), ) fmt.Fprintf(w, "