2023-01-11 06:38:48 +00:00
|
|
|
package barertc
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"encoding/json"
|
2023-01-27 06:54:02 +00:00
|
|
|
"errors"
|
2023-01-11 06:38:48 +00:00
|
|
|
"fmt"
|
|
|
|
"net/http"
|
2023-02-05 08:53:50 +00:00
|
|
|
"strings"
|
2023-03-23 03:21:04 +00:00
|
|
|
"sync"
|
2023-01-11 06:38:48 +00:00
|
|
|
"time"
|
|
|
|
|
2023-03-22 04:29:24 +00:00
|
|
|
"git.kirsle.net/apps/barertc/pkg/config"
|
2023-02-06 01:42:09 +00:00
|
|
|
"git.kirsle.net/apps/barertc/pkg/jwt"
|
2023-01-11 06:38:48 +00:00
|
|
|
"git.kirsle.net/apps/barertc/pkg/log"
|
2023-02-09 04:01:06 +00:00
|
|
|
"git.kirsle.net/apps/barertc/pkg/util"
|
2023-01-11 06:38:48 +00:00
|
|
|
"nhooyr.io/websocket"
|
|
|
|
)
|
|
|
|
|
|
|
|
// Subscriber represents a connected WebSocket session.
|
|
|
|
type Subscriber struct {
|
2023-01-27 04:34:58 +00:00
|
|
|
// User properties
|
2023-02-06 01:42:09 +00:00
|
|
|
ID int // ID assigned by server
|
|
|
|
Username string
|
|
|
|
VideoActive bool
|
2023-02-11 06:46:39 +00:00
|
|
|
VideoNSFW bool
|
2023-03-28 04:13:04 +00:00
|
|
|
ChatStatus string
|
2023-02-06 01:42:09 +00:00
|
|
|
JWTClaims *jwt.Claims
|
|
|
|
authenticated bool // has passed the login step
|
|
|
|
conn *websocket.Conn
|
|
|
|
ctx context.Context
|
|
|
|
cancel context.CancelFunc
|
2023-03-31 19:40:15 +00:00
|
|
|
messages chan []byte
|
2023-02-06 01:42:09 +00:00
|
|
|
closeSlow func()
|
2023-03-23 03:21:04 +00:00
|
|
|
|
|
|
|
muteMu sync.RWMutex
|
|
|
|
booted map[string]struct{} // usernames booted off your camera
|
|
|
|
muted map[string]struct{} // usernames you muted
|
2023-01-11 06:38:48 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// ReadLoop spawns a goroutine that reads from the websocket connection.
|
|
|
|
func (sub *Subscriber) ReadLoop(s *Server) {
|
|
|
|
go func() {
|
|
|
|
for {
|
|
|
|
msgType, data, err := sub.conn.Read(sub.ctx)
|
|
|
|
if err != nil {
|
2023-02-09 04:01:06 +00:00
|
|
|
log.Error("ReadLoop error(%d=%s): %+v", sub.ID, sub.Username, err)
|
2023-01-27 04:34:58 +00:00
|
|
|
s.DeleteSubscriber(sub)
|
2023-02-09 04:01:06 +00:00
|
|
|
|
2023-03-29 01:09:13 +00:00
|
|
|
// Notify if this user was auth'd and not hidden
|
|
|
|
if sub.authenticated && sub.ChatStatus != "hidden" {
|
2023-02-09 04:01:06 +00:00
|
|
|
s.Broadcast(Message{
|
|
|
|
Action: ActionPresence,
|
|
|
|
Username: sub.Username,
|
|
|
|
Message: "has exited the room!",
|
|
|
|
})
|
|
|
|
s.SendWhoList()
|
|
|
|
}
|
2023-01-11 06:38:48 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
if msgType != websocket.MessageText {
|
|
|
|
log.Error("Unexpected MessageType")
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
// Read the user's posted message.
|
|
|
|
var msg Message
|
|
|
|
if err := json.Unmarshal(data, &msg); err != nil {
|
2023-02-09 04:01:06 +00:00
|
|
|
log.Error("Read(%d=%s) Message error: %s", sub.ID, sub.Username, err)
|
2023-01-11 06:38:48 +00:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
2023-03-22 04:29:24 +00:00
|
|
|
if msg.Action != ActionFile {
|
|
|
|
log.Debug("Read(%d=%s): %s", sub.ID, sub.Username, data)
|
|
|
|
}
|
|
|
|
|
2023-01-11 06:38:48 +00:00
|
|
|
// What action are they performing?
|
|
|
|
switch msg.Action {
|
|
|
|
case ActionLogin:
|
2023-01-27 04:34:58 +00:00
|
|
|
s.OnLogin(sub, msg)
|
2023-01-11 06:38:48 +00:00
|
|
|
case ActionMessage:
|
2023-01-27 04:34:58 +00:00
|
|
|
s.OnMessage(sub, msg)
|
2023-03-22 04:29:24 +00:00
|
|
|
case ActionFile:
|
|
|
|
s.OnFile(sub, msg)
|
2023-01-27 04:34:58 +00:00
|
|
|
case ActionMe:
|
|
|
|
s.OnMe(sub, msg)
|
2023-01-27 06:54:02 +00:00
|
|
|
case ActionOpen:
|
|
|
|
s.OnOpen(sub, msg)
|
2023-03-23 03:21:04 +00:00
|
|
|
case ActionBoot:
|
|
|
|
s.OnBoot(sub, msg)
|
|
|
|
case ActionMute, ActionUnmute:
|
|
|
|
s.OnMute(sub, msg, msg.Action == ActionMute)
|
2023-01-27 06:54:02 +00:00
|
|
|
case ActionCandidate:
|
|
|
|
s.OnCandidate(sub, msg)
|
|
|
|
case ActionSDP:
|
|
|
|
s.OnSDP(sub, msg)
|
2023-02-06 04:26:00 +00:00
|
|
|
case ActionWatch:
|
|
|
|
s.OnWatch(sub, msg)
|
|
|
|
case ActionUnwatch:
|
|
|
|
s.OnUnwatch(sub, msg)
|
2023-01-11 06:38:48 +00:00
|
|
|
default:
|
2023-01-27 06:54:02 +00:00
|
|
|
sub.ChatServer("Unsupported message type.")
|
2023-01-11 06:38:48 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
}
|
|
|
|
|
|
|
|
// SendJSON sends a JSON message to the websocket client.
|
|
|
|
func (sub *Subscriber) SendJSON(v interface{}) error {
|
|
|
|
data, err := json.Marshal(v)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2023-03-23 03:21:04 +00:00
|
|
|
log.Debug("SendJSON(%d=%s): %s", sub.ID, sub.Username, data)
|
2023-03-31 19:40:15 +00:00
|
|
|
return sub.conn.Write(sub.ctx, websocket.MessageText, data)
|
2023-01-11 06:38:48 +00:00
|
|
|
}
|
|
|
|
|
2023-01-27 04:34:58 +00:00
|
|
|
// SendMe sends the current user state to the client.
|
|
|
|
func (sub *Subscriber) SendMe() {
|
|
|
|
sub.SendJSON(Message{
|
|
|
|
Action: ActionMe,
|
|
|
|
Username: sub.Username,
|
|
|
|
VideoActive: sub.VideoActive,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2023-01-27 06:54:02 +00:00
|
|
|
// ChatServer is a convenience function to deliver a ChatServer error to the client.
|
|
|
|
func (sub *Subscriber) ChatServer(message string, v ...interface{}) {
|
|
|
|
sub.SendJSON(Message{
|
|
|
|
Action: ActionError,
|
|
|
|
Username: "ChatServer",
|
|
|
|
Message: fmt.Sprintf(message, v...),
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2023-03-22 04:29:24 +00:00
|
|
|
// WebSocket handles the /ws websocket connection endpoint.
|
2023-01-11 06:38:48 +00:00
|
|
|
func (s *Server) WebSocket() http.HandlerFunc {
|
|
|
|
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
2023-02-09 04:01:06 +00:00
|
|
|
ip := util.IPAddress(r)
|
|
|
|
log.Info("WebSocket connection from %s - %s", ip, r.Header.Get("User-Agent"))
|
|
|
|
log.Debug("Headers: %+v", r.Header)
|
2023-03-31 19:40:55 +00:00
|
|
|
c, err := websocket.Accept(w, r, &websocket.AcceptOptions{
|
|
|
|
CompressionMode: websocket.CompressionDisabled,
|
|
|
|
})
|
2023-01-11 06:38:48 +00:00
|
|
|
if err != nil {
|
|
|
|
w.WriteHeader(http.StatusInternalServerError)
|
2023-02-09 04:01:06 +00:00
|
|
|
fmt.Fprintf(w, "Could not accept websocket connection: %s", err)
|
2023-01-11 06:38:48 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
defer c.Close(websocket.StatusInternalError, "the sky is falling")
|
|
|
|
|
2023-02-09 04:01:06 +00:00
|
|
|
log.Debug("WebSocket: %s has connected", ip)
|
2023-03-22 04:29:24 +00:00
|
|
|
c.SetReadLimit(config.Current.WebSocketReadLimit)
|
2023-01-11 06:38:48 +00:00
|
|
|
|
|
|
|
// CloseRead starts a goroutine that will read from the connection
|
|
|
|
// until it is closed.
|
|
|
|
// ctx := c.CloseRead(r.Context())
|
2023-02-05 05:00:01 +00:00
|
|
|
ctx, cancel := context.WithCancel(r.Context())
|
2023-01-11 06:38:48 +00:00
|
|
|
|
|
|
|
sub := &Subscriber{
|
|
|
|
conn: c,
|
|
|
|
ctx: ctx,
|
2023-02-05 05:00:01 +00:00
|
|
|
cancel: cancel,
|
2023-01-11 06:38:48 +00:00
|
|
|
messages: make(chan []byte, s.subscriberMessageBuffer),
|
|
|
|
closeSlow: func() {
|
|
|
|
c.Close(websocket.StatusPolicyViolation, "connection too slow to keep up with messages")
|
|
|
|
},
|
2023-03-28 04:13:04 +00:00
|
|
|
booted: make(map[string]struct{}),
|
|
|
|
muted: make(map[string]struct{}),
|
|
|
|
ChatStatus: "online",
|
2023-01-11 06:38:48 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
s.AddSubscriber(sub)
|
2023-02-09 04:01:06 +00:00
|
|
|
defer s.DeleteSubscriber(sub)
|
2023-01-11 06:38:48 +00:00
|
|
|
|
|
|
|
go sub.ReadLoop(s)
|
2023-02-05 05:00:01 +00:00
|
|
|
pinger := time.NewTicker(PingInterval)
|
2023-01-11 06:38:48 +00:00
|
|
|
for {
|
|
|
|
select {
|
|
|
|
case msg := <-sub.messages:
|
|
|
|
err = writeTimeout(ctx, time.Second*5, c, msg)
|
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
2023-02-11 06:46:39 +00:00
|
|
|
case <-pinger.C:
|
2023-02-05 05:00:01 +00:00
|
|
|
sub.SendJSON(Message{
|
2023-02-11 06:46:39 +00:00
|
|
|
Action: ActionPing,
|
2023-02-05 05:00:01 +00:00
|
|
|
})
|
2023-01-11 06:38:48 +00:00
|
|
|
case <-ctx.Done():
|
2023-02-05 05:00:01 +00:00
|
|
|
pinger.Stop()
|
2023-01-11 06:38:48 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2023-01-27 04:34:58 +00:00
|
|
|
// Auto incrementing Subscriber ID, assigned in AddSubscriber.
|
|
|
|
var SubscriberID int
|
|
|
|
|
2023-01-11 06:38:48 +00:00
|
|
|
// AddSubscriber adds a WebSocket subscriber to the server.
|
|
|
|
func (s *Server) AddSubscriber(sub *Subscriber) {
|
2023-01-27 04:34:58 +00:00
|
|
|
// Assign a unique ID.
|
|
|
|
SubscriberID++
|
|
|
|
sub.ID = SubscriberID
|
2023-02-09 04:01:06 +00:00
|
|
|
log.Debug("AddSubscriber: ID #%d", sub.ID)
|
2023-01-27 04:34:58 +00:00
|
|
|
|
2023-01-11 06:38:48 +00:00
|
|
|
s.subscribersMu.Lock()
|
|
|
|
s.subscribers[sub] = struct{}{}
|
|
|
|
s.subscribersMu.Unlock()
|
|
|
|
}
|
|
|
|
|
2023-01-27 06:54:02 +00:00
|
|
|
// GetSubscriber by username.
|
|
|
|
func (s *Server) GetSubscriber(username string) (*Subscriber, error) {
|
|
|
|
s.subscribersMu.RLock()
|
|
|
|
defer s.subscribersMu.RUnlock()
|
|
|
|
for _, sub := range s.IterSubscribers(true) {
|
|
|
|
if sub.Username == username {
|
|
|
|
return sub, nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil, errors.New("not found")
|
|
|
|
}
|
|
|
|
|
2023-01-11 06:38:48 +00:00
|
|
|
// DeleteSubscriber removes a subscriber from the server.
|
|
|
|
func (s *Server) DeleteSubscriber(sub *Subscriber) {
|
2023-01-27 04:34:58 +00:00
|
|
|
log.Error("DeleteSubscriber: %s", sub.Username)
|
2023-02-05 05:00:01 +00:00
|
|
|
|
|
|
|
// Cancel its context to clean up the for-loop goroutine.
|
|
|
|
if sub.cancel != nil {
|
|
|
|
sub.cancel()
|
|
|
|
}
|
|
|
|
|
2023-01-11 06:38:48 +00:00
|
|
|
s.subscribersMu.Lock()
|
|
|
|
delete(s.subscribers, sub)
|
|
|
|
s.subscribersMu.Unlock()
|
|
|
|
}
|
|
|
|
|
2023-01-27 04:34:58 +00:00
|
|
|
// IterSubscribers loops over the subscriber list with a read lock. If the
|
|
|
|
// caller already holds a lock, pass the optional `true` parameter for isLocked.
|
2023-01-27 06:54:02 +00:00
|
|
|
func (s *Server) IterSubscribers(isLocked ...bool) []*Subscriber {
|
|
|
|
var result = []*Subscriber{}
|
2023-01-27 04:34:58 +00:00
|
|
|
|
2023-01-27 06:54:02 +00:00
|
|
|
// Has the caller already taken the read lock or do we get it?
|
|
|
|
if locked := len(isLocked) > 0 && isLocked[0]; !locked {
|
|
|
|
s.subscribersMu.RLock()
|
|
|
|
defer s.subscribersMu.RUnlock()
|
|
|
|
}
|
2023-01-27 04:34:58 +00:00
|
|
|
|
2023-01-27 06:54:02 +00:00
|
|
|
for sub := range s.subscribers {
|
|
|
|
result = append(result, sub)
|
|
|
|
}
|
2023-01-27 04:34:58 +00:00
|
|
|
|
2023-01-27 06:54:02 +00:00
|
|
|
return result
|
2023-01-27 04:34:58 +00:00
|
|
|
}
|
|
|
|
|
2023-01-11 06:38:48 +00:00
|
|
|
// Broadcast a message to the chat room.
|
|
|
|
func (s *Server) Broadcast(msg Message) {
|
2023-03-22 04:29:24 +00:00
|
|
|
if len(msg.Message) < 1024 {
|
|
|
|
log.Debug("Broadcast: %+v", msg)
|
|
|
|
}
|
|
|
|
|
2023-01-11 06:38:48 +00:00
|
|
|
s.subscribersMu.RLock()
|
|
|
|
defer s.subscribersMu.RUnlock()
|
2023-01-27 06:54:02 +00:00
|
|
|
for _, sub := range s.IterSubscribers(true) {
|
2023-02-06 01:42:09 +00:00
|
|
|
if !sub.authenticated {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
2023-03-23 03:21:04 +00:00
|
|
|
// Don't deliver it if the receiver has muted us.
|
|
|
|
if sub.Mutes(msg.Username) {
|
|
|
|
log.Debug("Do not broadcast message to %s: they have muted or booted %s", sub.Username, msg.Username)
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
2023-02-06 21:27:29 +00:00
|
|
|
sub.SendJSON(msg)
|
2023-01-11 06:38:48 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-02-05 08:53:50 +00:00
|
|
|
// SendTo sends a message to a given username.
|
2023-02-06 21:27:29 +00:00
|
|
|
func (s *Server) SendTo(username string, msg Message) error {
|
2023-02-05 08:53:50 +00:00
|
|
|
log.Debug("SendTo(%s): %+v", username, msg)
|
|
|
|
username = strings.TrimPrefix(username, "@")
|
|
|
|
s.subscribersMu.RLock()
|
|
|
|
defer s.subscribersMu.RUnlock()
|
2023-02-06 21:27:29 +00:00
|
|
|
|
|
|
|
var found bool
|
2023-02-05 08:53:50 +00:00
|
|
|
for _, sub := range s.IterSubscribers(true) {
|
|
|
|
if sub.Username == username {
|
2023-02-06 21:27:29 +00:00
|
|
|
found = true
|
2023-02-05 08:53:50 +00:00
|
|
|
sub.SendJSON(Message{
|
|
|
|
Action: msg.Action,
|
|
|
|
Channel: msg.Channel,
|
|
|
|
Username: msg.Username,
|
|
|
|
Message: msg.Message,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
2023-02-06 21:27:29 +00:00
|
|
|
|
|
|
|
if !found {
|
|
|
|
return fmt.Errorf("%s is not online", username)
|
|
|
|
}
|
|
|
|
return nil
|
2023-02-05 08:53:50 +00:00
|
|
|
}
|
|
|
|
|
2023-01-27 04:34:58 +00:00
|
|
|
// SendWhoList broadcasts the connected members to everybody in the room.
|
|
|
|
func (s *Server) SendWhoList() {
|
2023-01-27 06:54:02 +00:00
|
|
|
var (
|
|
|
|
subscribers = s.IterSubscribers()
|
|
|
|
)
|
|
|
|
|
2023-03-23 03:21:04 +00:00
|
|
|
// Build the WhoList for each subscriber.
|
|
|
|
// TODO: it's the only way to fake videoActive for booted user views.
|
2023-01-27 06:54:02 +00:00
|
|
|
for _, sub := range subscribers {
|
2023-02-06 01:42:09 +00:00
|
|
|
if !sub.authenticated {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
2023-03-23 03:21:04 +00:00
|
|
|
var users = []WhoList{}
|
|
|
|
for _, user := range subscribers {
|
2023-03-29 01:09:13 +00:00
|
|
|
if user.ChatStatus == "hidden" {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
2023-03-23 03:21:04 +00:00
|
|
|
who := WhoList{
|
|
|
|
Username: user.Username,
|
2023-03-28 04:13:04 +00:00
|
|
|
Status: user.ChatStatus,
|
2023-03-23 03:21:04 +00:00
|
|
|
VideoActive: user.VideoActive,
|
|
|
|
NSFW: user.VideoNSFW,
|
|
|
|
}
|
|
|
|
|
|
|
|
// If this person had booted us, force their camera to "off"
|
|
|
|
if user.Boots(sub.Username) || user.Mutes(sub.Username) {
|
|
|
|
who.VideoActive = false
|
|
|
|
who.NSFW = false
|
|
|
|
}
|
|
|
|
|
2023-03-29 01:09:13 +00:00
|
|
|
if user.JWTClaims != nil {
|
2023-03-23 03:21:04 +00:00
|
|
|
who.Operator = user.JWTClaims.IsAdmin
|
|
|
|
who.Avatar = user.JWTClaims.Avatar
|
|
|
|
who.ProfileURL = user.JWTClaims.ProfileURL
|
|
|
|
}
|
|
|
|
users = append(users, who)
|
2023-02-06 01:42:09 +00:00
|
|
|
}
|
2023-01-27 04:34:58 +00:00
|
|
|
|
|
|
|
sub.SendJSON(Message{
|
2023-02-11 06:46:39 +00:00
|
|
|
Action: ActionWhoList,
|
|
|
|
WhoList: users,
|
2023-01-27 04:34:58 +00:00
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-03-23 03:21:04 +00:00
|
|
|
// Boots checks whether the subscriber has blocked username from their camera.
|
|
|
|
func (s *Subscriber) Boots(username string) bool {
|
|
|
|
s.muteMu.RLock()
|
|
|
|
defer s.muteMu.RUnlock()
|
|
|
|
_, ok := s.booted[username]
|
|
|
|
return ok
|
|
|
|
}
|
|
|
|
|
|
|
|
// Mutes checks whether the subscriber has muted username.
|
|
|
|
func (s *Subscriber) Mutes(username string) bool {
|
|
|
|
s.muteMu.RLock()
|
|
|
|
defer s.muteMu.RUnlock()
|
|
|
|
_, ok := s.muted[username]
|
|
|
|
return ok
|
|
|
|
}
|
|
|
|
|
2023-01-11 06:38:48 +00:00
|
|
|
func writeTimeout(ctx context.Context, timeout time.Duration, c *websocket.Conn, msg []byte) error {
|
|
|
|
ctx, cancel := context.WithTimeout(ctx, timeout)
|
|
|
|
defer cancel()
|
|
|
|
return c.Write(ctx, websocket.MessageText, msg)
|
|
|
|
}
|