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 (
|
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"
|
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"
|
2020-10-04 16:10:11 +00:00
|
|
|
"strings"
|
2020-11-09 17:11:42 +00:00
|
|
|
"sync"
|
2020-11-09 16:57:55 +00:00
|
|
|
"time"
|
2020-11-09 17:11:42 +00:00
|
|
|
|
|
|
|
"github.com/markbates/pkger"
|
|
|
|
"gitlab.crans.org/nounous/ghostream/internal/monitoring"
|
|
|
|
"gitlab.crans.org/nounous/ghostream/stream/ovenmediaengine"
|
|
|
|
"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
|
2021-02-25 16:23:11 +00:00
|
|
|
validPath = regexp.MustCompile("^/[a-zA-Z0-9@_-]*$")
|
2020-11-09 16:57:55 +00:00
|
|
|
|
2020-11-09 17:11:42 +00:00
|
|
|
counterMutex = new(sync.Mutex)
|
2020-11-09 16:57:55 +00:00
|
|
|
connectedClients = make(map[string]map[string]int64)
|
2020-10-16 19:23:13 +00:00
|
|
|
)
|
|
|
|
|
2020-10-20 17:12:15 +00:00
|
|
|
// Handle site index and viewer pages
|
|
|
|
func viewerHandler(w http.ResponseWriter, r *http.Request) {
|
|
|
|
// Validation on path
|
|
|
|
if validPath.FindStringSubmatch(r.URL.Path) == nil {
|
|
|
|
http.NotFound(w, r)
|
|
|
|
log.Printf("Replied not found on %s", r.URL.Path)
|
2020-10-19 19:45:23 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2020-10-20 17:12:15 +00:00
|
|
|
// Check method
|
|
|
|
if r.Method != http.MethodGet {
|
|
|
|
http.Error(w, "Method not allowed.", http.StatusMethodNotAllowed)
|
2020-10-05 20:00:08 +00:00
|
|
|
}
|
2020-09-28 16:06:10 +00:00
|
|
|
|
2020-10-04 15:56:03 +00:00
|
|
|
// Get stream ID from URL, or from domain name
|
|
|
|
path := r.URL.Path[1:]
|
2020-10-13 15:12:19 +00:00
|
|
|
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
|
|
|
}
|
2020-10-13 15:12:19 +00:00
|
|
|
host = realHost
|
|
|
|
}
|
2021-02-25 16:38:09 +00:00
|
|
|
host = strings.ToLower(strings.Replace(host, ".", "-", -1))
|
2020-10-13 15:12:19 +00:00
|
|
|
if streamID, ok := cfg.MapDomainToStream[host]; ok {
|
2020-10-13 17:36:28 +00:00
|
|
|
// Move home page to /about
|
2020-10-11 21:09:48 +00:00
|
|
|
if path == "about" {
|
|
|
|
path = ""
|
|
|
|
} else {
|
2020-10-13 15:12:19 +00:00
|
|
|
path = streamID
|
2020-10-11 21:09:48 +00:00
|
|
|
}
|
2020-10-04 15:56:03 +00:00
|
|
|
}
|
|
|
|
|
2020-09-28 16:06:10 +00:00
|
|
|
// Render template
|
|
|
|
data := struct {
|
2020-10-02 19:13:50 +00:00
|
|
|
Cfg *Options
|
|
|
|
Path string
|
|
|
|
WidgetURL string
|
2020-11-09 16:31:58 +00:00
|
|
|
OMECfg *ovenmediaengine.Options
|
|
|
|
}{Path: path, Cfg: cfg, WidgetURL: "", OMECfg: omeCfg}
|
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-10-02 19:13:50 +00:00
|
|
|
|
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()
|
|
|
|
}
|
|
|
|
|
|
|
|
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) {
|
2020-11-09 16:57:55 +00:00
|
|
|
// Retrieve stream name from URL
|
2020-10-18 18:44:31 +00:00
|
|
|
name := strings.SplitN(strings.Replace(r.URL.Path[7:], "/", "", -1), "@", 2)[0]
|
2021-02-25 16:38:09 +00:00
|
|
|
name = strings.ToLower(name)
|
2020-10-17 10:38:18 +00:00
|
|
|
userCount := 0
|
|
|
|
|
2020-11-09 16:57:55 +00:00
|
|
|
// Clients have a unique generated identifier per session, that expires in 40 seconds.
|
|
|
|
// Each time the client connects to this page, the identifier is renewed.
|
|
|
|
// Yeah, that's not a good way to have stats, but it works...
|
|
|
|
if connectedClients[name] == nil {
|
2020-11-09 17:11:42 +00:00
|
|
|
counterMutex.Lock()
|
2020-11-09 16:57:55 +00:00
|
|
|
connectedClients[name] = make(map[string]int64)
|
2020-11-09 17:11:42 +00:00
|
|
|
counterMutex.Unlock()
|
2020-11-09 16:57:55 +00:00
|
|
|
}
|
|
|
|
currentTime := time.Now().Unix()
|
|
|
|
if _, ok := r.URL.Query()["uid"]; ok {
|
|
|
|
uid := r.URL.Query()["uid"][0]
|
2020-11-09 17:11:42 +00:00
|
|
|
counterMutex.Lock()
|
2020-11-09 16:57:55 +00:00
|
|
|
connectedClients[name][uid] = currentTime
|
2020-11-09 17:11:42 +00:00
|
|
|
counterMutex.Unlock()
|
2020-11-09 16:57:55 +00:00
|
|
|
}
|
2020-11-09 17:03:15 +00:00
|
|
|
toDelete := make([]string, 0)
|
2020-11-09 17:11:42 +00:00
|
|
|
counterMutex.Lock()
|
2020-11-09 16:57:55 +00:00
|
|
|
for uid, oldTime := range connectedClients[name] {
|
|
|
|
if currentTime-oldTime > 40 {
|
2020-11-09 17:03:15 +00:00
|
|
|
toDelete = append(toDelete, uid)
|
2020-11-09 16:57:55 +00:00
|
|
|
}
|
|
|
|
}
|
2020-11-09 17:03:15 +00:00
|
|
|
for _, uid := range toDelete {
|
|
|
|
delete(connectedClients[name], uid)
|
|
|
|
}
|
2020-11-09 17:11:42 +00:00
|
|
|
counterMutex.Unlock()
|
2020-11-09 16:57:55 +00:00
|
|
|
|
2020-10-19 17:57:04 +00:00
|
|
|
// Get requested stream
|
|
|
|
stream, err := streams.Get(name)
|
|
|
|
if err == nil {
|
|
|
|
userCount = stream.ClientCount()
|
2020-10-19 18:05:20 +00:00
|
|
|
userCount += webrtc.GetNumberConnectedSessions(name)
|
2020-11-09 16:57:55 +00:00
|
|
|
userCount += len(connectedClients[name])
|
2020-10-17 10:38:18 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Display connected users statistics
|
2020-09-29 16:03:28 +00:00
|
|
|
enc := json.NewEncoder(w)
|
2020-10-19 17:57:04 +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)
|
|
|
|
}
|
2020-09-29 16:03:28 +00:00
|
|
|
}
|