Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 9 additions & 2 deletions server/internal/session/session.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,12 +49,19 @@ func (session *SessionCtx) profileChanged() {
session.ClearHost()
}

if (!session.profile.CanConnect || !session.profile.CanLogin || !session.profile.CanWatch) && session.state.IsWatching {
if !session.profile.CanConnect || !session.profile.CanLogin || !session.profile.CanWatch {
// TODO: Needed for legacy implementation. Websocket must die before webrtc and deliver signal close message
// otherwise webrtc destroy would trigger websocket reconnect. In case of kick event, webrtc destroy is called
// before websocket destroy that delivers the information about the kick.
time.AfterFunc(time.Second, func() {
if peer := session.GetWebRTCPeer(); peer != nil {
// Select the peer before allowing a profile update to grant access
// again. A peer installed after that grant must not be closed here.
session.manager.sessionsMu.Lock()
profile := session.profile
peer := session.GetWebRTCPeer()
session.manager.sessionsMu.Unlock()

if peer != nil && (!profile.CanConnect || !profile.CanLogin || !profile.CanWatch) {
peer.Destroy()
}
})
Expand Down
72 changes: 72 additions & 0 deletions server/internal/session/session_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,11 @@ type fakeWebRTCPeer struct {

func (p *fakeWebRTCPeer) Destroy() { p.destroyed = true }

// A change of profile also sets the paused state of the peer.
type pausableWebRTCPeer struct{ fakeWebRTCPeer }

func (*pausableWebRTCPeer) SetPaused(isPaused bool) error { return nil }

func newTestSession(t *testing.T) *SessionCtx {
t.Helper()
manager := New(&config.Session{})
Expand Down Expand Up @@ -258,6 +263,73 @@ func TestSessionThatCountsAsWatchingWithoutAPeerCanLoseItsLogin(t *testing.T) {
})
}

// A session that may no longer watch keeps its websocket, so a peer that has
// not connected is not taken with it. Left alone it could still connect, and
// the session would watch without being allowed to.
func TestWebRTCPeerThatNeverConnectedIsDestroyedWhenItsSessionMayNoLongerWatch(t *testing.T) {
synctest.Test(t, func(t *testing.T) {
session, _ := newConnectedTestSession(t)
peer := &pausableWebRTCPeer{}
session.SetWebRTCPeer(peer)

profile := session.Profile()
profile.CanWatch = false
if err := session.manager.Update(session.ID(), profile); err != nil {
t.Fatalf("update session: %v", err)
}

// the peer is destroyed from a timer
time.Sleep(2 * time.Second)
synctest.Wait()
if !peer.destroyed {
t.Error("peer was not destroyed")
}
})
}

// The peer of a session that may no longer watch is destroyed a moment later.
// A session that may watch again by then keeps the peer it has.
func TestWebRTCPeerIsKeptWhenItsSessionMayWatchAgain(t *testing.T) {
tests := []struct {
name string
connected bool
}{
{name: "peer that has not connected"},
{name: "peer that is streaming", connected: true},
}

for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
synctest.Test(t, func(t *testing.T) {
session, _ := newConnectedTestSession(t)
session.SetWebRTCPeer(&pausableWebRTCPeer{})

profile := session.Profile()
revoked := profile
revoked.CanWatch = false
if err := session.manager.Update(session.ID(), revoked); err != nil {
t.Fatalf("update session: %v", err)
}
if err := session.manager.Update(session.ID(), profile); err != nil {
t.Fatalf("update session: %v", err)
}

peer := &pausableWebRTCPeer{}
session.SetWebRTCPeer(peer)
if tt.connected {
session.SetWebRTCConnected(peer, true)
}

time.Sleep(2 * time.Second)
synctest.Wait()
if peer.destroyed {
t.Error("peer was destroyed")
}
})
})
}
}

// A client can be back on a new websocket before the old one is done leaving,
// because the session's listeners run in between. A peer the new websocket
// asked for is not the old one's to destroy.
Expand Down
Loading