mirror of
https://github.com/livekit/livekit.git
synced 2026-08-28 00:44:12 +00:00
connection validation
This commit is contained in:
+31
-14
@@ -8,12 +8,12 @@ import (
|
||||
"strings"
|
||||
|
||||
"github.com/gorilla/websocket"
|
||||
"github.com/livekit/livekit-server/pkg/rtc/types"
|
||||
|
||||
"github.com/livekit/livekit-server/pkg/config"
|
||||
"github.com/livekit/livekit-server/pkg/logger"
|
||||
"github.com/livekit/livekit-server/pkg/routing"
|
||||
"github.com/livekit/livekit-server/pkg/rtc"
|
||||
"github.com/livekit/livekit-server/pkg/rtc/types"
|
||||
livekit "github.com/livekit/livekit-server/proto"
|
||||
)
|
||||
|
||||
@@ -25,7 +25,7 @@ type RTCService struct {
|
||||
isDev bool
|
||||
}
|
||||
|
||||
func NewRTCService(conf *config.Config, roomStore RoomStore, roomManager *RoomManager, router routing.Router, currentNode routing.LocalNode) *RTCService {
|
||||
func NewRTCService(conf *config.Config, roomManager *RoomManager, router routing.Router, currentNode routing.LocalNode) *RTCService {
|
||||
s := &RTCService{
|
||||
router: router,
|
||||
roomManager: roomManager,
|
||||
@@ -43,13 +43,17 @@ func NewRTCService(conf *config.Config, roomStore RoomStore, roomManager *RoomMa
|
||||
return s
|
||||
}
|
||||
|
||||
func (s *RTCService) ServeHTTP(w http.ResponseWriter, r *http.Request) {
|
||||
// reject non websocket requests
|
||||
if !websocket.IsWebSocketUpgrade(r) {
|
||||
w.WriteHeader(404)
|
||||
func (s *RTCService) Validate(w http.ResponseWriter, r *http.Request) {
|
||||
_, _, code, err := s.validate(r)
|
||||
w.Header().Set("Access-Control-Allow-Origin", "*")
|
||||
if err != nil {
|
||||
handleError(w, code, err.Error())
|
||||
return
|
||||
}
|
||||
_, _ = w.Write([]byte("success"))
|
||||
}
|
||||
|
||||
func (s *RTCService) validate(r *http.Request) (string, routing.ParticipantInit, int, error) {
|
||||
roomName := r.FormValue("room")
|
||||
reconnectParam := r.FormValue("reconnect")
|
||||
protocolParam := r.FormValue("protocol")
|
||||
@@ -60,14 +64,15 @@ func (s *RTCService) ServeHTTP(w http.ResponseWriter, r *http.Request) {
|
||||
claims := GetGrants(r.Context())
|
||||
// require a claim
|
||||
if claims == nil || claims.Video == nil {
|
||||
handleError(w, http.StatusUnauthorized, rtc.ErrPermissionDenied.Error())
|
||||
return
|
||||
return "", routing.ParticipantInit{}, http.StatusUnauthorized, rtc.ErrPermissionDenied
|
||||
}
|
||||
|
||||
pi := routing.ParticipantInit{
|
||||
Reconnect: boolValue(reconnectParam),
|
||||
Identity: claims.Identity,
|
||||
UsePlanB: boolValue(planBParam),
|
||||
AutoSubscribe: true,
|
||||
Metadata: claims.Metadata,
|
||||
}
|
||||
if autoSubParam != "" {
|
||||
pi.AutoSubscribe = boolValue(autoSubParam)
|
||||
@@ -86,13 +91,29 @@ func (s *RTCService) ServeHTTP(w http.ResponseWriter, r *http.Request) {
|
||||
|
||||
onlyName, err := EnsureJoinPermission(r.Context())
|
||||
if err != nil {
|
||||
handleError(w, http.StatusUnauthorized, err.Error())
|
||||
return
|
||||
return "", routing.ParticipantInit{}, http.StatusUnauthorized, err
|
||||
}
|
||||
|
||||
if onlyName != "" {
|
||||
roomName = onlyName
|
||||
}
|
||||
|
||||
return roomName, pi, http.StatusOK, nil
|
||||
}
|
||||
|
||||
func (s *RTCService) ServeHTTP(w http.ResponseWriter, r *http.Request) {
|
||||
// reject non websocket requests
|
||||
if !websocket.IsWebSocketUpgrade(r) {
|
||||
w.WriteHeader(404)
|
||||
return
|
||||
}
|
||||
|
||||
roomName, pi, code, err := s.validate(r)
|
||||
if err != nil {
|
||||
handleError(w, code, err.Error())
|
||||
return
|
||||
}
|
||||
|
||||
// create room if it doesn't exist, also assigns an RTC node for the room
|
||||
rm, err := s.roomManager.CreateRoom(&livekit.CreateRoomRequest{Name: roomName})
|
||||
if err != nil {
|
||||
@@ -100,10 +121,6 @@ func (s *RTCService) ServeHTTP(w http.ResponseWriter, r *http.Request) {
|
||||
return
|
||||
}
|
||||
|
||||
if claims.Metadata != "" {
|
||||
pi.Metadata = claims.Metadata
|
||||
}
|
||||
|
||||
// this needs to be started first *before* using router functions on this node
|
||||
connId, reqSink, resSource, err := s.router.StartParticipantSignal(roomName, pi)
|
||||
if err != nil {
|
||||
|
||||
@@ -71,6 +71,7 @@ func NewLivekitServer(conf *config.Config,
|
||||
mux := http.NewServeMux()
|
||||
mux.Handle(s.roomServer.PathPrefix(), s.roomServer)
|
||||
mux.Handle("/rtc", rtcService)
|
||||
mux.HandleFunc("/rtc/validate", rtcService.Validate)
|
||||
mux.HandleFunc("/", s.healthCheck)
|
||||
if conf.Development {
|
||||
mux.HandleFunc("/debug/goroutine", s.debugGoroutines)
|
||||
|
||||
@@ -22,7 +22,7 @@ func InitializeServer(conf *config.Config, keyProvider auth.KeyProvider, roomSto
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
rtcService := NewRTCService(conf, roomStore, roomManager, router, currentNode)
|
||||
rtcService := NewRTCService(conf, roomManager, router, currentNode)
|
||||
server, err := NewTurnServer(conf, roomStore, currentNode)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
|
||||
Reference in New Issue
Block a user