ghostream/web/handler.go

174 lines
4.6 KiB
Go
Raw Normal View History

2020-10-14 19:37:05 +00:00
// Package web serves the JavaScript player and WebRTC negotiation
2020-09-28 16:06:10 +00:00
package web
import (
"bytes"
2020-09-28 16:06:10 +00:00
"encoding/json"
"html/template"
2020-09-28 16:06:10 +00:00
"log"
2020-10-04 15:56:03 +00:00
"net"
2020-09-28 16:06:10 +00:00
"net/http"
2020-10-16 19:23:13 +00:00
"regexp"
"strings"
2020-09-28 16:06:10 +00:00
"github.com/markbates/pkger"
"gitlab.crans.org/nounous/ghostream/internal/monitoring"
2020-09-29 16:17:55 +00:00
"gitlab.crans.org/nounous/ghostream/stream/webrtc"
2020-09-28 16:06:10 +00:00
)
2020-10-16 19:23:13 +00:00
var (
// Precompile regex
2020-10-17 10:38:18 +00:00
validPath = regexp.MustCompile("^/[a-z0-9@_-]*$")
2020-10-16 19:23:13 +00:00
)
2020-09-28 16:06:10 +00:00
// Handle WebRTC session description exchange via POST
func viewerPostHandler(w http.ResponseWriter, r *http.Request) {
// Limit response body to 128KB
r.Body = http.MaxBytesReader(w, r.Body, 131072)
// Get stream ID from URL, or from domain name
path := r.URL.Path[1:]
host := r.Host
if strings.Contains(host, ":") {
realHost, _, err := net.SplitHostPort(r.Host)
if err != nil {
log.Printf("Failed to split host and port from %s", r.Host)
return
}
host = realHost
}
host = strings.Replace(host, ".", "-", -1)
if streamID, ok := cfg.MapDomainToStream[host]; ok {
path = streamID
}
2020-09-28 16:06:10 +00:00
// Decode client description
dec := json.NewDecoder(r.Body)
dec.DisallowUnknownFields()
remoteDescription := webrtc.SessionDescription{}
if err := dec.Decode(&remoteDescription); err != nil {
http.Error(w, "The JSON WebRTC offer is malformed", http.StatusBadRequest)
return
}
// Exchange session descriptions with WebRTC stream server
remoteSdpChan <- struct {
StreamID string
RemoteDescription webrtc.SessionDescription
}{StreamID: path, RemoteDescription: remoteDescription}
2020-09-28 16:06:10 +00:00
localDescription := <-localSdpChan
// Send server description as JSON
jsonDesc, err := json.Marshal(localDescription)
if err != nil {
http.Error(w, "An error occurred while formating response", http.StatusInternalServerError)
log.Println("An error occurred while sending session description", err)
return
}
w.Header().Set("Content-Type", "application/json")
_, err = w.Write(jsonDesc)
if err != nil {
log.Println("An error occurred while sending session description", err)
}
2020-09-28 16:06:10 +00:00
// Increment monitoring
monitoring.WebSessions.Inc()
}
func viewerGetHandler(w http.ResponseWriter, r *http.Request) {
2020-10-04 15:56:03 +00:00
// Get stream ID from URL, or from domain name
path := r.URL.Path[1:]
host := r.Host
if strings.Contains(host, ":") {
realHost, _, err := net.SplitHostPort(r.Host)
if err != nil {
log.Printf("Failed to split host and port from %s", r.Host)
return
2020-10-04 15:56:03 +00:00
}
host = realHost
}
host = strings.Replace(host, ".", "-", -1)
if streamID, ok := cfg.MapDomainToStream[host]; ok {
2020-10-13 17:36:28 +00:00
// Move home page to /about
if path == "about" {
path = ""
} else {
path = streamID
}
2020-10-04 15:56:03 +00:00
}
2020-09-28 16:06:10 +00:00
// Render template
data := struct {
Cfg *Options
Path string
WidgetURL string
2020-10-13 17:36:28 +00:00
}{Path: path, Cfg: cfg, WidgetURL: ""}
2020-10-04 15:56:03 +00:00
2020-10-13 17:36:28 +00:00
// Load widget is user does not disable it with ?nowidget
if _, ok := r.URL.Query()["nowidget"]; !ok {
// Compute the WidgetURL with the stream path
b := &bytes.Buffer{}
_ = template.Must(template.New("").Parse(cfg.WidgetURL)).Execute(b, data)
data.WidgetURL = b.String()
}
2020-09-28 16:06:10 +00:00
if err := templates.ExecuteTemplate(w, "base", data); err != nil {
log.Println(err.Error())
http.Error(w, "Internal Server Error", http.StatusInternalServerError)
return
}
// Increment monitoring
monitoring.WebViewerServed.Inc()
}
// Handle site index and viewer pages
// POST requests are used to exchange WebRTC session descriptions
func viewerHandler(w http.ResponseWriter, r *http.Request) {
// Validation on path
if validPath.FindStringSubmatch(r.URL.Path) == nil {
http.NotFound(w, r)
2020-10-16 19:26:48 +00:00
log.Printf("Replied not found on %s", r.URL.Path)
2020-09-28 16:06:10 +00:00
return
}
// Route depending on HTTP method
switch r.Method {
case http.MethodGet:
viewerGetHandler(w, r)
case http.MethodPost:
viewerPostHandler(w, r)
default:
http.Error(w, "Sorry, only GET and POST methods are supported.", http.StatusBadRequest)
}
}
func staticHandler() http.Handler {
// Set up static files server
staticFs := http.FileServer(pkger.Dir("/web/static"))
return http.StripPrefix("/static/", staticFs)
}
func statisticsHandler(w http.ResponseWriter, r *http.Request) {
name := strings.SplitN(strings.Replace(r.URL.Path[7:], "/", "", -1), "@", 2)[0]
2020-10-17 10:38:18 +00:00
userCount := 0
// Get all substreams
for _, outputType := range []string{"", "@720p", "@480p", "@360p", "@240p", "@text"} {
// Get requested stream
stream, ok := streams[name+outputType]
if ok {
// Get number of output channels
userCount += stream.ClientCount()
}
2020-10-17 10:38:18 +00:00
}
// Display connected users statistics
enc := json.NewEncoder(w)
2020-10-17 10:38:18 +00:00
err := enc.Encode(struct{ ConnectedViewers int }{userCount})
2020-09-29 16:20:24 +00:00
if err != nil {
http.Error(w, "Failed to generate JSON.", http.StatusInternalServerError)
log.Printf("Failed to generate JSON: %s", err)
}
}