From 00c6015148e84c362714793f4f706c5b7bca7f9b Mon Sep 17 00:00:00 2001 From: Noah Petherbridge Date: Sun, 10 Dec 2023 15:30:25 -0800 Subject: [PATCH] Server-side IsVideoNotAllowed validation checks --- pkg/handlers.go | 51 +++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 51 insertions(+) diff --git a/pkg/handlers.go b/pkg/handlers.go index c861b9c..e076f42 100644 --- a/pkg/handlers.go +++ b/pkg/handlers.go @@ -412,6 +412,14 @@ func (s *Server) OnOpen(sub *Subscriber, msg messages.Message) { return } + // Enforce whether the viewer has permission to see this camera. + if ok, reason := s.IsVideoNotAllowed(sub, other); !ok { + sub.ChatServer( + "Could not open that video: %s", reason, + ) + return + } + // Make up a WebRTC shared secret and send it to both of them. secret := util.RandomString(16) log.Info("WebRTC: %s opens %s with secret %s", sub.Username, other.Username, secret) @@ -440,6 +448,49 @@ func (s *Server) OnOpen(sub *Subscriber, msg messages.Message) { }) } +// IsVideoNotAllowed verifies whether a viewer can open a broadcaster's camera. +// +// Returns a boolean and an error message to return if false. +func (s *Server) IsVideoNotAllowed(sub *Subscriber, other *Subscriber) (bool, string) { + var ( + ourVideoActive = (sub.VideoStatus & messages.VideoFlagActive) == messages.VideoFlagActive + theirVideoActive = (other.VideoStatus & messages.VideoFlagActive) == messages.VideoFlagActive + theirMutualRequired = (other.VideoStatus & messages.VideoFlagMutualRequired) == messages.VideoFlagMutualRequired + theirVIPRequired = (other.VideoStatus & messages.VideoFlagOnlyVIP) == messages.VideoFlagOnlyVIP + ) + + // Conditions in which we can not watch their video. + var conditions = []struct { + If bool + Error string + }{ + { + If: !theirVideoActive, + Error: "Their video is not currently enabled.", + }, + { + If: theirMutualRequired && !ourVideoActive, + Error: fmt.Sprintf("%s has requested that you should share your own camera too before opening theirs.", other.Username), + }, + { + If: theirVIPRequired && !sub.IsVIP(), + Error: "You do not have permission to view that camera.", + }, + { + If: other.Mutes(sub.Username) || other.Blocks(sub), + Error: "You do not have permission to view that camera.", + }, + } + + for _, c := range conditions { + if c.If { + return false, c.Error + } + } + + return true, "" +} + // OnBoot is a user kicking you off their video stream. func (s *Server) OnBoot(sub *Subscriber, msg messages.Message, boot bool) { sub.muteMu.Lock()