ghostream/web/web.go

169 lines
4.1 KiB
Go
Raw Normal View History

2020-09-21 15:47:31 +00:00
package web
import (
2020-09-23 11:52:12 +00:00
"encoding/json"
2020-09-21 15:47:31 +00:00
"html/template"
2020-09-28 15:36:40 +00:00
"io/ioutil"
2020-09-21 15:47:31 +00:00
"log"
"net/http"
"os"
2020-09-24 14:13:21 +00:00
"regexp"
2020-09-28 15:36:40 +00:00
"strings"
2020-09-21 17:59:41 +00:00
2020-09-28 15:36:40 +00:00
"github.com/markbates/pkger"
2020-09-23 11:52:12 +00:00
"github.com/pion/webrtc/v3"
2020-09-21 19:38:11 +00:00
"gitlab.crans.org/nounous/ghostream/internal/monitoring"
2020-09-21 15:47:31 +00:00
)
2020-09-22 09:42:57 +00:00
// Options holds web package configuration
type Options struct {
ListenAddress string
Name string
Hostname string
Favicon string
WidgetURL string
}
2020-09-24 09:24:13 +00:00
var (
cfg *Options
// WebRTC session description channels
remoteSdpChan chan webrtc.SessionDescription
localSdpChan chan webrtc.SessionDescription
// Preload templates
2020-09-28 15:36:40 +00:00
templates *template.Template
2020-09-24 14:13:21 +00:00
// Precompile regex
validPath = regexp.MustCompile("^\\/[a-z0-9_-]*\\/?$")
2020-09-24 09:24:13 +00:00
)
2020-09-21 15:47:31 +00:00
2020-09-23 11:52:12 +00:00
// Handle WebRTC session description exchange via POST
2020-09-24 09:24:13 +00:00
func viewerPostHandler(w http.ResponseWriter, r *http.Request) {
2020-09-23 11:52:12 +00:00
// 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
}
2020-09-24 09:24:13 +00:00
// Exchange session descriptions with WebRTC stream server
remoteSdpChan <- remoteDescription
localDescription := <-localSdpChan
2020-09-23 11:52:12 +00:00
// Send server description as JSON
jsonDesc, err := json.Marshal(localDescription)
if err != nil {
2020-09-28 08:54:02 +00:00
http.Error(w, "An error occurred while formating response", http.StatusInternalServerError)
log.Println("An error occurred while sending session description", err)
2020-09-23 11:52:12 +00:00
return
}
w.Header().Set("Content-Type", "application/json")
w.Write(jsonDesc)
2020-09-24 09:24:13 +00:00
// Increment monitoring
monitoring.WebSessions.Inc()
}
func viewerGetHandler(w http.ResponseWriter, r *http.Request) {
// Render template
data := struct {
Path string
Cfg *Options
}{Path: r.URL.Path[1:], Cfg: cfg}
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()
2020-09-23 11:52:12 +00:00
}
2020-09-21 15:47:31 +00:00
// Handle site index and viewer pages
2020-09-23 11:52:12 +00:00
// POST requests are used to exchange WebRTC session descriptions
2020-09-24 09:24:13 +00:00
func viewerHandler(w http.ResponseWriter, r *http.Request) {
2020-09-24 14:13:21 +00:00
// Validation on path
if validPath.FindStringSubmatch(r.URL.Path) == nil {
http.NotFound(w, r)
log.Print(r.URL.Path)
return
}
2020-09-21 15:47:31 +00:00
2020-09-24 09:24:13 +00:00
// Route depending on HTTP method
2020-09-23 11:52:12 +00:00
switch r.Method {
2020-09-24 09:24:13 +00:00
case http.MethodGet:
viewerGetHandler(w, r)
case http.MethodPost:
viewerPostHandler(w, r)
2020-09-23 11:52:12 +00:00
default:
http.Error(w, "Sorry, only GET and POST methods are supported.", http.StatusBadRequest)
2020-09-21 15:47:31 +00:00
}
}
// Handle static files
// We do not use http.FileServer as we do not want directory listing
2020-09-24 09:24:13 +00:00
func staticHandler(w http.ResponseWriter, r *http.Request) {
2020-09-21 15:47:31 +00:00
path := "./web/" + r.URL.Path
if f, err := os.Stat(path); err == nil && !f.IsDir() {
http.ServeFile(w, r, path)
} else {
http.NotFound(w, r)
}
}
2020-09-28 15:36:40 +00:00
// Load templates with pkger
// templates will be packed in the compiled binary
func loadTemplates() error {
templates = template.New("")
return pkger.Walk("/web/template", func(path string, info os.FileInfo, err error) error {
if err != nil {
return err
}
// Skip non-templates
if info.IsDir() || !strings.HasSuffix(path, ".html") {
return nil
}
// Open file with pkger
f, err := pkger.Open(path)
if err != nil {
return err
}
// Read and parse template
temp, err := ioutil.ReadAll(f)
if err != nil {
return err
}
templates, err = templates.Parse(string(temp))
return err
})
}
2020-09-24 09:24:13 +00:00
// Serve HTTP server
func Serve(rSdpChan chan webrtc.SessionDescription, lSdpChan chan webrtc.SessionDescription, c *Options) {
remoteSdpChan = rSdpChan
localSdpChan = lSdpChan
cfg = c
2020-09-21 18:38:21 +00:00
2020-09-28 15:36:40 +00:00
// Load templates
if err := loadTemplates(); err != nil {
log.Fatalln("Failed to load templates:", err)
}
2020-09-21 15:47:31 +00:00
// Set up HTTP router and server
2020-09-21 19:33:32 +00:00
mux := http.NewServeMux()
2020-09-24 09:24:13 +00:00
mux.HandleFunc("/", viewerHandler)
mux.HandleFunc("/static/", staticHandler)
2020-09-22 09:42:57 +00:00
log.Printf("HTTP server listening on %s", cfg.ListenAddress)
log.Fatal(http.ListenAndServe(cfg.ListenAddress, mux))
2020-09-21 15:47:31 +00:00
}