82 lines
3.0 KiB
Go
82 lines
3.0 KiB
Go
// Copyright 2026 Sverse contributors. Licensed under the Apache License, Version 2.0.
|
|
// spatial-test serves a loopback-only browser laboratory and issues short-lived
|
|
// tokens with server-side credentials. It is not a production token service.
|
|
package main
|
|
|
|
import (
|
|
"embed"
|
|
"encoding/json"
|
|
"flag"
|
|
"fmt"
|
|
"io/fs"
|
|
"log"
|
|
"net"
|
|
"net/http"
|
|
"os"
|
|
"strings"
|
|
"time"
|
|
|
|
"github.com/livekit/protocol/auth"
|
|
)
|
|
|
|
//go:embed web/*
|
|
var web embed.FS
|
|
|
|
func main() {
|
|
listen := flag.String("listen", "127.0.0.1:8899", "loopback HTTP address")
|
|
url := flag.String("livekit-url", "ws://127.0.0.1:7880", "LiveKit websocket URL")
|
|
prefix := flag.String("room-prefix", "spatial-", "allowed laboratory room prefix")
|
|
flag.Parse()
|
|
host, _, err := net.SplitHostPort(*listen)
|
|
if err != nil || net.ParseIP(host) == nil || !net.ParseIP(host).IsLoopback() {
|
|
log.Fatal("spatial-test must bind to a loopback IP")
|
|
}
|
|
key, secret := os.Getenv("LIVEKIT_API_KEY"), os.Getenv("LIVEKIT_API_SECRET")
|
|
if key == "" || secret == "" {
|
|
log.Fatal("set LIVEKIT_API_KEY and LIVEKIT_API_SECRET")
|
|
}
|
|
files, _ := fs.Sub(web, "web")
|
|
mux := http.NewServeMux()
|
|
mux.Handle("/", http.FileServer(http.FS(files)))
|
|
mux.HandleFunc("POST /token", func(w http.ResponseWriter, r *http.Request) {
|
|
if origin := r.Header.Get("Origin"); origin != "" && origin != "http://"+r.Host {
|
|
http.Error(w, "origin not allowed", http.StatusForbidden)
|
|
return
|
|
}
|
|
var request struct {
|
|
Room string `json:"room"`
|
|
Identity string `json:"identity"`
|
|
SceneController bool `json:"scene_controller"`
|
|
}
|
|
r.Body = http.MaxBytesReader(w, r.Body, 1024)
|
|
if json.NewDecoder(r.Body).Decode(&request) != nil || !strings.HasPrefix(request.Room, *prefix) || len(request.Room) > 100 || len(request.Identity) < 1 || len(request.Identity) > 100 || strings.HasPrefix(request.Identity, "__spatial_") {
|
|
http.Error(w, "invalid room or identity", http.StatusBadRequest)
|
|
return
|
|
}
|
|
allow := true
|
|
grant := &auth.VideoGrant{RoomJoin: true, Room: request.Room, CanPublish: &allow, CanSubscribe: &allow, CanPublishData: &allow}
|
|
if request.SceneController {
|
|
if !strings.HasPrefix(request.Identity, "scene-controller-") {
|
|
http.Error(w, "invalid scene controller identity", http.StatusBadRequest)
|
|
return
|
|
}
|
|
deny := false
|
|
grant.RoomAdmin = true
|
|
grant.Hidden = true
|
|
grant.CanPublish = &deny
|
|
grant.CanSubscribe = &deny
|
|
}
|
|
token, err := auth.NewAccessToken(key, secret).SetIdentity(request.Identity).SetValidFor(15 * time.Minute).SetVideoGrant(grant).ToJWT()
|
|
if err != nil {
|
|
http.Error(w, "token generation failed", http.StatusInternalServerError)
|
|
return
|
|
}
|
|
w.Header().Set("Content-Type", "application/json")
|
|
w.Header().Set("Cache-Control", "no-store")
|
|
_ = json.NewEncoder(w).Encode(map[string]string{"url": *url, "token": token})
|
|
})
|
|
server := &http.Server{Addr: *listen, Handler: mux, ReadHeaderTimeout: 5 * time.Second, ReadTimeout: 10 * time.Second, WriteTimeout: 10 * time.Second}
|
|
fmt.Printf("Spatial audio laboratory: http://%s\n", *listen)
|
|
log.Fatal(server.ListenAndServe())
|
|
}
|