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