2020-09-28 16:06:10 +00:00
|
|
|
package web
|
|
|
|
|
|
|
|
import (
|
2020-10-02 19:13:50 +00:00
|
|
|
"bytes"
|
2020-09-28 16:06:10 +00:00
|
|
|
"encoding/json"
|
2020-10-02 19:13:50 +00:00
|
|
|
"html/template"
|
2020-09-28 16:06:10 +00:00
|
|
|
"log"
|
|
|
|
"net/http"
|
|
|
|
|
|
|
|
"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
|
|
|
)
|
|
|
|
|
|
|
|
// 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)
|
|
|
|
|
|
|
|
// 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 <- remoteDescription
|
|
|
|
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")
|
2020-09-30 14:09:25 +00:00
|
|
|
_, _ = w.Write(jsonDesc)
|
2020-09-28 16:06:10 +00:00
|
|
|
|
|
|
|
// Increment monitoring
|
|
|
|
monitoring.WebSessions.Inc()
|
|
|
|
}
|
|
|
|
|
|
|
|
func viewerGetHandler(w http.ResponseWriter, r *http.Request) {
|
|
|
|
// Render template
|
|
|
|
data := struct {
|
2020-10-02 19:13:50 +00:00
|
|
|
Cfg *Options
|
|
|
|
Path string
|
|
|
|
WidgetURL string
|
2020-09-28 16:06:10 +00:00
|
|
|
}{Path: r.URL.Path[1:], Cfg: cfg}
|
2020-10-02 19:13:50 +00:00
|
|
|
b := &bytes.Buffer{}
|
|
|
|
// Update the WidgetURL with the stream path
|
|
|
|
_ = 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)
|
|
|
|
log.Print(r.URL.Path)
|
|
|
|
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)
|
|
|
|
}
|
2020-09-29 16:03:28 +00:00
|
|
|
|
|
|
|
func statisticsHandler(w http.ResponseWriter, r *http.Request) {
|
|
|
|
// Display connected users stats
|
|
|
|
enc := json.NewEncoder(w)
|
2020-09-29 16:20:24 +00:00
|
|
|
err := enc.Encode(struct {
|
2020-09-29 16:03:28 +00:00
|
|
|
ConnectedViewers int
|
2020-09-29 16:17:55 +00:00
|
|
|
}{webrtc.GetNumberConnectedSessions()})
|
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)
|
|
|
|
}
|
2020-09-29 16:03:28 +00:00
|
|
|
}
|