2020-09-18 13:11:21 +02:00
|
|
|
package webserver
|
2020-05-31 16:46:41 +02:00
|
|
|
|
|
|
|
import (
|
|
|
|
"bufio"
|
2020-09-12 12:42:48 +02:00
|
|
|
"context"
|
2020-05-31 16:46:41 +02:00
|
|
|
"encoding/json"
|
|
|
|
"errors"
|
|
|
|
"fmt"
|
|
|
|
"html"
|
|
|
|
"io"
|
|
|
|
"log"
|
|
|
|
"net/http"
|
2020-05-31 20:03:22 +02:00
|
|
|
"net/url"
|
2020-05-31 16:46:41 +02:00
|
|
|
"os"
|
2020-05-31 20:03:22 +02:00
|
|
|
"path"
|
2020-05-31 16:46:41 +02:00
|
|
|
"path/filepath"
|
|
|
|
"strings"
|
|
|
|
"time"
|
|
|
|
|
|
|
|
"github.com/gorilla/websocket"
|
2020-09-13 11:56:35 +02:00
|
|
|
|
2020-09-13 14:12:00 +02:00
|
|
|
"sfu/disk"
|
2020-09-13 11:56:35 +02:00
|
|
|
"sfu/group"
|
2020-09-18 10:28:05 +02:00
|
|
|
"sfu/rtpconn"
|
2020-09-18 10:14:57 +02:00
|
|
|
"sfu/stats"
|
2020-05-31 16:46:41 +02:00
|
|
|
)
|
|
|
|
|
2020-09-12 12:42:48 +02:00
|
|
|
var server *http.Server
|
|
|
|
|
2020-09-18 13:11:21 +02:00
|
|
|
var StaticRoot string
|
|
|
|
|
|
|
|
func Serve(address string, dataDir string) {
|
2020-09-18 13:12:39 +02:00
|
|
|
http.Handle("/", &fileHandler{http.Dir(StaticRoot)})
|
2020-09-10 13:39:38 +02:00
|
|
|
http.HandleFunc("/group/", groupHandler)
|
2020-05-31 20:03:22 +02:00
|
|
|
http.HandleFunc("/recordings",
|
|
|
|
func(w http.ResponseWriter, r *http.Request) {
|
|
|
|
http.Redirect(w, r,
|
|
|
|
"/recordings/", http.StatusPermanentRedirect)
|
|
|
|
})
|
|
|
|
http.HandleFunc("/recordings/", recordingsHandler)
|
2020-05-31 16:46:41 +02:00
|
|
|
http.HandleFunc("/ws", wsHandler)
|
2020-08-07 11:14:34 +02:00
|
|
|
http.HandleFunc("/ice-servers.json",
|
|
|
|
func(w http.ResponseWriter, r *http.Request) {
|
|
|
|
mungeHeader(w)
|
|
|
|
http.ServeFile(w, r,
|
|
|
|
filepath.Join(dataDir, "ice-servers.json"))
|
|
|
|
})
|
2020-05-31 16:46:41 +02:00
|
|
|
http.HandleFunc("/public-groups.json", publicHandler)
|
2020-09-18 13:11:21 +02:00
|
|
|
http.HandleFunc("/stats", func(w http.ResponseWriter, r *http.Request) {
|
|
|
|
statsHandler(w, r, dataDir)
|
|
|
|
})
|
2020-05-31 16:46:41 +02:00
|
|
|
|
2020-09-12 21:19:40 +02:00
|
|
|
server = &http.Server{
|
2020-09-18 13:11:21 +02:00
|
|
|
Addr: address,
|
2020-09-12 21:19:40 +02:00
|
|
|
ReadHeaderTimeout: 60 * time.Second,
|
|
|
|
IdleTimeout: 120 * time.Second,
|
|
|
|
}
|
|
|
|
server.RegisterOnShutdown(func() {
|
2020-09-18 10:14:57 +02:00
|
|
|
group.Range(func(g *group.Group) bool {
|
2020-09-13 11:56:35 +02:00
|
|
|
go g.Shutdown("server is shutting down")
|
2020-09-13 12:24:06 +02:00
|
|
|
return true
|
|
|
|
})
|
2020-09-12 21:19:40 +02:00
|
|
|
})
|
|
|
|
go func() {
|
2020-05-31 16:46:41 +02:00
|
|
|
var err error
|
|
|
|
err = server.ListenAndServeTLS(
|
|
|
|
filepath.Join(dataDir, "cert.pem"),
|
|
|
|
filepath.Join(dataDir, "key.pem"),
|
|
|
|
)
|
2020-09-12 12:42:48 +02:00
|
|
|
if err != nil && err != http.ErrServerClosed {
|
2020-05-31 16:46:41 +02:00
|
|
|
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'")
|
|
|
|
}
|
|
|
|
|
2020-09-11 17:37:35 +02:00
|
|
|
func notFound(w http.ResponseWriter) {
|
|
|
|
w.Header().Set("Content-Type", "text/html; charset=utf-8")
|
|
|
|
w.WriteHeader(http.StatusNotFound)
|
|
|
|
|
2020-09-18 13:11:21 +02:00
|
|
|
f, err := os.Open(path.Join(StaticRoot, "404.html"))
|
2020-09-11 17:37:35 +02:00
|
|
|
if err != nil {
|
|
|
|
fmt.Fprintln(w, "<p>Not found</p>")
|
|
|
|
return
|
|
|
|
}
|
|
|
|
defer f.Close()
|
|
|
|
|
|
|
|
io.Copy(w, f)
|
|
|
|
}
|
|
|
|
|
2020-09-18 13:12:39 +02:00
|
|
|
var ErrIsDirectory = errors.New("is a directory")
|
|
|
|
|
|
|
|
func httpError(w http.ResponseWriter, err error) {
|
|
|
|
if os.IsNotExist(err) {
|
|
|
|
notFound(w)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
if os.IsPermission(err) {
|
|
|
|
http.Error(w, "403 forbidden", http.StatusForbidden)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
http.Error(w, "500 Internal Server Error",
|
|
|
|
http.StatusInternalServerError)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
// fileHandler is our custom reimplementation of http.FileServer
|
|
|
|
type fileHandler struct {
|
|
|
|
root http.FileSystem
|
2020-05-31 16:46:41 +02:00
|
|
|
}
|
|
|
|
|
2020-09-18 13:12:39 +02:00
|
|
|
func (fh *fileHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
|
2020-05-31 16:46:41 +02:00
|
|
|
mungeHeader(w)
|
2020-09-18 13:12:39 +02:00
|
|
|
p := r.URL.Path
|
|
|
|
// this ensures any leading .. are removed by path.Clean below
|
|
|
|
if !strings.HasPrefix(p, "/") {
|
|
|
|
p = "/" + p
|
|
|
|
r.URL.Path = p
|
|
|
|
}
|
|
|
|
p = path.Clean(p)
|
|
|
|
|
|
|
|
f, err := fh.root.Open(p)
|
|
|
|
if err != nil {
|
|
|
|
httpError(w, err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
defer f.Close()
|
|
|
|
d, err := f.Stat()
|
|
|
|
if err != nil {
|
|
|
|
httpError(w, err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
if d.IsDir() {
|
|
|
|
u := r.URL.Path
|
|
|
|
if u[len(u)-1] != '/' {
|
|
|
|
http.Redirect(w, r, u+"/", http.StatusPermanentRedirect)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
index := path.Join(p, "index.html")
|
|
|
|
ff, err := fh.root.Open(index)
|
|
|
|
if err != nil {
|
|
|
|
if os.IsNotExist(err) {
|
|
|
|
err = os.ErrPermission
|
|
|
|
}
|
|
|
|
httpError(w, err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
defer ff.Close()
|
|
|
|
dd, err := ff.Stat()
|
|
|
|
if err != nil {
|
|
|
|
httpError(w, err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
if dd.IsDir() {
|
|
|
|
httpError(w, ErrIsDirectory)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
f, d = ff, dd
|
|
|
|
p = index
|
|
|
|
}
|
|
|
|
|
|
|
|
http.ServeContent(w, r, d.Name(), d.ModTime(), f)
|
2020-05-31 16:46:41 +02:00
|
|
|
}
|
|
|
|
|
2020-09-10 13:39:38 +02:00
|
|
|
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 == "" {
|
2020-09-11 17:37:35 +02:00
|
|
|
notFound(w)
|
2020-09-10 13:39:38 +02:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2020-09-13 11:56:35 +02:00
|
|
|
g, err := group.Add(name, nil)
|
2020-09-10 13:39:38 +02:00
|
|
|
if err != nil {
|
|
|
|
if os.IsNotExist(err) {
|
2020-09-11 17:37:35 +02:00
|
|
|
notFound(w)
|
2020-09-10 13:39:38 +02:00
|
|
|
} else {
|
2020-09-14 15:48:16 +02:00
|
|
|
log.Printf("addGroup: %v", err)
|
2020-09-10 13:39:38 +02:00
|
|
|
http.Error(w, "Internal server error",
|
|
|
|
http.StatusInternalServerError)
|
|
|
|
}
|
|
|
|
return
|
|
|
|
}
|
2020-09-10 13:55:57 +02:00
|
|
|
|
2020-09-13 13:24:05 +02:00
|
|
|
if redirect := g.Redirect(); redirect != "" {
|
|
|
|
http.Redirect(w, r, redirect,
|
2020-09-10 13:55:57 +02:00
|
|
|
http.StatusPermanentRedirect)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2020-09-18 13:11:21 +02:00
|
|
|
http.ServeFile(w, r, filepath.Join(StaticRoot, "sfu.html"))
|
2020-09-10 13:39:38 +02:00
|
|
|
}
|
|
|
|
|
2020-05-31 16:46:41 +02:00
|
|
|
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
|
|
|
|
}
|
|
|
|
|
2020-09-13 11:56:35 +02:00
|
|
|
g := group.GetPublic()
|
2020-05-31 16:46:41 +02:00
|
|
|
e := json.NewEncoder(w)
|
|
|
|
e.Encode(g)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2020-09-18 13:11:21 +02:00
|
|
|
func getPassword(dataDir string) (string, string, error) {
|
2020-05-31 16:46:41 +02:00
|
|
|
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
|
|
|
|
}
|
|
|
|
|
2020-05-31 20:03:22 +02:00
|
|
|
func failAuthentication(w http.ResponseWriter, realm string) {
|
|
|
|
w.Header().Set("www-authenticate",
|
|
|
|
fmt.Sprintf("basic realm=\"%v\"", realm))
|
|
|
|
http.Error(w, "Haha!", http.StatusUnauthorized)
|
|
|
|
}
|
2020-05-31 16:46:41 +02:00
|
|
|
|
2020-09-18 13:11:21 +02:00
|
|
|
func statsHandler(w http.ResponseWriter, r *http.Request, dataDir string) {
|
|
|
|
u, p, err := getPassword(dataDir)
|
2020-05-31 16:46:41 +02:00
|
|
|
if err != nil {
|
|
|
|
log.Printf("Passwd: %v", err)
|
2020-05-31 20:03:22 +02:00
|
|
|
failAuthentication(w, "stats")
|
2020-05-31 16:46:41 +02:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
username, password, ok := r.BasicAuth()
|
|
|
|
if !ok || username != u || password != p {
|
2020-05-31 20:03:22 +02:00
|
|
|
failAuthentication(w, "stats")
|
2020-05-31 16:46:41 +02:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
w.Header().Set("content-type", "text/html; charset=utf-8")
|
|
|
|
w.Header().Set("cache-control", "no-cache")
|
|
|
|
if r.Method == "HEAD" {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2020-09-18 10:14:57 +02:00
|
|
|
ss := stats.GetGroups()
|
2020-05-31 16:46:41 +02:00
|
|
|
|
|
|
|
fmt.Fprintf(w, "<!DOCTYPE html>\n<html><head>\n")
|
|
|
|
fmt.Fprintf(w, "<title>Stats</title>\n")
|
2020-05-31 20:03:22 +02:00
|
|
|
fmt.Fprintf(w, "<link rel=\"stylesheet\" type=\"text/css\" href=\"/common.css\"/>")
|
2020-05-31 16:46:41 +02:00
|
|
|
fmt.Fprintf(w, "<head><body>\n")
|
|
|
|
|
|
|
|
printBitrate := func(w io.Writer, rate, maxRate uint64) error {
|
|
|
|
var err error
|
|
|
|
if maxRate != 0 && maxRate != ^uint64(0) {
|
|
|
|
_, err = fmt.Fprintf(w, "%v/%v", rate, maxRate)
|
|
|
|
} else {
|
|
|
|
_, err = fmt.Fprintf(w, "%v", rate)
|
|
|
|
}
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2020-09-18 10:14:57 +02:00
|
|
|
printTrack := func(w io.Writer, t stats.Track) {
|
2020-05-31 16:46:41 +02:00
|
|
|
fmt.Fprintf(w, "<tr><td></td><td></td><td></td>")
|
|
|
|
fmt.Fprintf(w, "<td>")
|
2020-09-18 10:14:57 +02:00
|
|
|
printBitrate(w, t.Bitrate, t.MaxBitrate)
|
2020-05-31 16:46:41 +02:00
|
|
|
fmt.Fprintf(w, "</td>")
|
|
|
|
fmt.Fprintf(w, "<td>%d%%</td>",
|
2020-09-18 10:14:57 +02:00
|
|
|
t.Loss,
|
2020-05-31 16:46:41 +02:00
|
|
|
)
|
2020-06-03 21:48:20 +02:00
|
|
|
fmt.Fprintf(w, "<td>")
|
2020-09-18 10:14:57 +02:00
|
|
|
if t.Rtt > 0 {
|
|
|
|
fmt.Fprintf(w, "%v", t.Rtt)
|
2020-06-03 21:48:20 +02:00
|
|
|
}
|
2020-09-18 10:14:57 +02:00
|
|
|
if t.Jitter > 0 {
|
|
|
|
fmt.Fprintf(w, "±%v", t.Jitter)
|
2020-05-31 16:46:41 +02:00
|
|
|
}
|
2020-06-03 21:48:20 +02:00
|
|
|
fmt.Fprintf(w, "</td>")
|
2020-05-31 16:46:41 +02:00
|
|
|
fmt.Fprintf(w, "</tr>")
|
|
|
|
}
|
|
|
|
|
2020-09-18 10:14:57 +02:00
|
|
|
for _, gs := range ss {
|
|
|
|
fmt.Fprintf(w, "<p>%v</p>\n", html.EscapeString(gs.Name))
|
2020-05-31 16:46:41 +02:00
|
|
|
fmt.Fprintf(w, "<table>")
|
2020-09-18 10:14:57 +02:00
|
|
|
for _, cs := range gs.Clients {
|
|
|
|
fmt.Fprintf(w, "<tr><td>%v</td></tr>\n", cs.Id)
|
|
|
|
for _, up := range cs.Up {
|
2020-06-12 17:39:16 +02:00
|
|
|
fmt.Fprintf(w, "<tr><td></td><td>Up</td><td>%v</td>",
|
2020-09-18 10:14:57 +02:00
|
|
|
up.Id)
|
|
|
|
if up.MaxBitrate > 0 {
|
2020-06-12 17:39:16 +02:00
|
|
|
fmt.Fprintf(w, "<td>%v</td>",
|
2020-09-18 10:14:57 +02:00
|
|
|
up.MaxBitrate)
|
2020-06-12 17:39:16 +02:00
|
|
|
}
|
|
|
|
fmt.Fprintf(w, "</tr>\n")
|
2020-09-18 10:14:57 +02:00
|
|
|
for _, t := range up.Tracks {
|
2020-05-31 16:46:41 +02:00
|
|
|
printTrack(w, t)
|
|
|
|
}
|
|
|
|
}
|
2020-09-18 10:14:57 +02:00
|
|
|
for _, down := range cs.Down {
|
2020-06-12 17:39:16 +02:00
|
|
|
fmt.Fprintf(w, "<tr><td></td><td>Down</td><td> %v</td>",
|
2020-09-18 10:14:57 +02:00
|
|
|
down.Id)
|
|
|
|
if down.MaxBitrate > 0 {
|
2020-06-12 17:39:16 +02:00
|
|
|
fmt.Fprintf(w, "<td>%v</td>",
|
2020-09-18 10:14:57 +02:00
|
|
|
down.MaxBitrate)
|
2020-06-12 17:39:16 +02:00
|
|
|
}
|
|
|
|
fmt.Fprintf(w, "</tr>\n")
|
2020-09-18 10:14:57 +02:00
|
|
|
for _, t := range down.Tracks {
|
2020-05-31 16:46:41 +02:00
|
|
|
printTrack(w, t)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
fmt.Fprintf(w, "</table>\n")
|
|
|
|
}
|
|
|
|
fmt.Fprintf(w, "</body></html>\n")
|
|
|
|
}
|
|
|
|
|
|
|
|
var upgrader websocket.Upgrader
|
|
|
|
|
|
|
|
func wsHandler(w http.ResponseWriter, r *http.Request) {
|
|
|
|
conn, err := upgrader.Upgrade(w, r, nil)
|
|
|
|
if err != nil {
|
|
|
|
log.Printf("Websocket upgrade: %v", err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
go func() {
|
2020-09-18 10:28:05 +02:00
|
|
|
err := rtpconn.StartClient(conn)
|
2020-05-31 16:46:41 +02:00
|
|
|
if err != nil {
|
|
|
|
log.Printf("client: %v", err)
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
}
|
2020-05-31 20:03:22 +02:00
|
|
|
|
|
|
|
func recordingsHandler(w http.ResponseWriter, r *http.Request) {
|
|
|
|
if len(r.URL.Path) < 12 || r.URL.Path[:12] != "/recordings/" {
|
|
|
|
http.Error(w, "server error", http.StatusInternalServerError)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
pth := r.URL.Path[12:]
|
|
|
|
|
|
|
|
if pth == "" {
|
|
|
|
http.Error(w, "nothing to see", http.StatusNotImplemented)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2020-09-13 14:12:00 +02:00
|
|
|
f, err := os.Open(filepath.Join(disk.Directory, pth))
|
2020-05-31 20:03:22 +02:00
|
|
|
if err != nil {
|
|
|
|
if os.IsNotExist(err) {
|
2020-09-11 17:37:35 +02:00
|
|
|
notFound(w)
|
2020-05-31 20:03:22 +02:00
|
|
|
} else {
|
|
|
|
http.Error(w, "server error",
|
|
|
|
http.StatusInternalServerError)
|
|
|
|
}
|
|
|
|
return
|
|
|
|
}
|
|
|
|
defer f.Close()
|
|
|
|
|
|
|
|
fi, err := f.Stat()
|
|
|
|
if err != nil {
|
|
|
|
http.Error(w, "server error", http.StatusInternalServerError)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
if fi.IsDir() {
|
|
|
|
if pth[len(pth)-1] != '/' {
|
|
|
|
http.Redirect(w, r,
|
|
|
|
r.URL.Path+"/", http.StatusPermanentRedirect)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
ok := checkGroupPermissions(w, r, path.Dir(pth))
|
|
|
|
if !ok {
|
|
|
|
failAuthentication(w, "recordings/"+path.Dir(pth))
|
|
|
|
return
|
|
|
|
}
|
|
|
|
if r.Method == "POST" {
|
|
|
|
handleGroupAction(w, r, path.Dir(pth))
|
|
|
|
} else {
|
|
|
|
serveGroupRecordings(w, r, f, path.Dir(pth))
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
ok := checkGroupPermissions(w, r, path.Dir(pth))
|
|
|
|
if !ok {
|
|
|
|
failAuthentication(w, "recordings/"+path.Dir(pth))
|
|
|
|
return
|
|
|
|
}
|
|
|
|
http.ServeContent(w, r, r.URL.Path, fi.ModTime(), f)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func handleGroupAction(w http.ResponseWriter, r *http.Request, group string) {
|
|
|
|
if r.Method != "POST" {
|
|
|
|
http.Error(w, "method not allowed", http.StatusMethodNotAllowed)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
err := r.ParseForm()
|
|
|
|
if err != nil {
|
|
|
|
http.Error(w, "couldn't parse request", http.StatusBadRequest)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
q := r.Form.Get("q")
|
|
|
|
|
|
|
|
switch q {
|
|
|
|
case "delete":
|
|
|
|
filename := r.Form.Get("filename")
|
|
|
|
if group == "" || filename == "" {
|
|
|
|
http.Error(w, "no filename provided",
|
|
|
|
http.StatusBadRequest)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
err := os.Remove(
|
2020-09-13 14:12:00 +02:00
|
|
|
filepath.Join(disk.Directory, group+"/"+filename),
|
2020-05-31 20:03:22 +02:00
|
|
|
)
|
|
|
|
if err != nil {
|
|
|
|
if os.IsPermission(err) {
|
|
|
|
http.Error(w, "unauthorized",
|
|
|
|
http.StatusForbidden)
|
|
|
|
} else {
|
|
|
|
http.Error(w, "server error",
|
|
|
|
http.StatusInternalServerError)
|
|
|
|
}
|
|
|
|
return
|
|
|
|
}
|
|
|
|
http.Redirect(w, r, "/recordings/"+group+"/",
|
|
|
|
http.StatusSeeOther)
|
|
|
|
return
|
|
|
|
default:
|
|
|
|
http.Error(w, "unknown query", http.StatusBadRequest)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-09-13 11:56:35 +02:00
|
|
|
func checkGroupPermissions(w http.ResponseWriter, r *http.Request, groupname string) bool {
|
|
|
|
desc, err := group.GetDescription(groupname)
|
2020-05-31 20:03:22 +02:00
|
|
|
if err != nil {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
user, pass, ok := r.BasicAuth()
|
|
|
|
if !ok {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
2020-09-13 11:56:35 +02:00
|
|
|
p, err := desc.GetPermission(group.ClientCredentials{user, pass})
|
2020-05-31 20:03:22 +02:00
|
|
|
if err != nil || !p.Record {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
|
|
|
func serveGroupRecordings(w http.ResponseWriter, r *http.Request, f *os.File, group string) {
|
|
|
|
fis, err := f.Readdir(-1)
|
|
|
|
if err != nil {
|
|
|
|
http.Error(w, "server error", http.StatusInternalServerError)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
w.Header().Set("content-type", "text/html; charset=utf-8")
|
|
|
|
w.Header().Set("cache-control", "no-cache")
|
|
|
|
|
|
|
|
if r.Method == "HEAD" {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
fmt.Fprintf(w, "<!DOCTYPE html>\n<html><head>\n")
|
|
|
|
fmt.Fprintf(w, "<title>Recordings for group %v</title>\n", group)
|
|
|
|
fmt.Fprintf(w, "<link rel=\"stylesheet\" type=\"text/css\" href=\"/common.css\"/>")
|
|
|
|
fmt.Fprintf(w, "<head><body>\n")
|
|
|
|
|
|
|
|
fmt.Fprintf(w, "<table>\n")
|
|
|
|
for _, fi := range fis {
|
|
|
|
if fi.IsDir() {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
fmt.Fprintf(w, "<tr><td><a href=\"%v\">%v</a></td><td>%d</td>",
|
|
|
|
html.EscapeString(fi.Name()),
|
|
|
|
html.EscapeString(fi.Name()),
|
|
|
|
fi.Size(),
|
|
|
|
)
|
|
|
|
fmt.Fprintf(w,
|
|
|
|
"<td><form action=\"/recordings/%v/?q=delete\" method=\"post\">"+
|
|
|
|
"<button type=\"submit\" name=\"filename\" value=\"%v\">Delete</button>"+
|
|
|
|
"</form></td></tr>\n",
|
|
|
|
url.PathEscape(group), fi.Name())
|
|
|
|
}
|
|
|
|
fmt.Fprintf(w, "</table>\n")
|
|
|
|
fmt.Fprintf(w, "</body></html>\n")
|
|
|
|
}
|
2020-09-12 12:42:48 +02:00
|
|
|
|
2020-09-18 13:11:21 +02:00
|
|
|
func Shutdown() {
|
2020-09-18 10:36:05 +02:00
|
|
|
ctx, cancel := context.WithTimeout(context.Background(), 2*time.Second)
|
|
|
|
defer cancel()
|
2020-09-12 12:42:48 +02:00
|
|
|
server.Shutdown(ctx)
|
|
|
|
}
|