2020-06-23 04:11:56 +03:00
|
|
|
package chat
|
|
|
|
|
|
|
|
import (
|
2021-07-20 05:22:29 +03:00
|
|
|
"encoding/json"
|
2020-06-23 04:11:56 +03:00
|
|
|
"net/http"
|
2020-12-22 06:42:47 +03:00
|
|
|
"sync"
|
2020-06-23 04:11:56 +03:00
|
|
|
"time"
|
|
|
|
|
|
|
|
log "github.com/sirupsen/logrus"
|
2020-06-23 09:52:50 +03:00
|
|
|
|
2021-07-20 05:22:29 +03:00
|
|
|
"github.com/gorilla/websocket"
|
|
|
|
|
|
|
|
"github.com/owncast/owncast/core/chat/events"
|
2021-02-19 10:05:52 +03:00
|
|
|
"github.com/owncast/owncast/core/data"
|
2021-07-20 05:22:29 +03:00
|
|
|
"github.com/owncast/owncast/core/user"
|
2021-02-19 10:05:52 +03:00
|
|
|
"github.com/owncast/owncast/core/webhooks"
|
2021-08-13 00:55:21 +03:00
|
|
|
"github.com/owncast/owncast/geoip"
|
2021-07-20 05:22:29 +03:00
|
|
|
"github.com/owncast/owncast/utils"
|
2020-06-23 04:11:56 +03:00
|
|
|
)
|
|
|
|
|
2021-07-20 05:22:29 +03:00
|
|
|
var _server *ChatServer
|
2020-06-23 23:11:01 +03:00
|
|
|
|
2021-07-20 05:22:29 +03:00
|
|
|
type ChatServer struct {
|
2021-08-14 01:26:44 +03:00
|
|
|
mu sync.RWMutex
|
|
|
|
seq uint
|
|
|
|
clients map[uint]*ChatClient
|
|
|
|
maxSocketConnectionLimit int64
|
2020-12-22 06:42:47 +03:00
|
|
|
|
2021-07-20 05:22:29 +03:00
|
|
|
// send outbound message payload to all clients
|
|
|
|
outbound chan []byte
|
2020-06-23 04:11:56 +03:00
|
|
|
|
2021-07-20 05:22:29 +03:00
|
|
|
// receive inbound message payload from all clients
|
|
|
|
inbound chan chatClientEvent
|
2020-06-23 23:11:01 +03:00
|
|
|
|
2021-07-20 05:22:29 +03:00
|
|
|
// unregister requests from clients.
|
2021-07-27 03:30:28 +03:00
|
|
|
unregister chan uint // the ChatClient id
|
2020-06-23 04:11:56 +03:00
|
|
|
}
|
|
|
|
|
2021-07-20 05:22:29 +03:00
|
|
|
func NewChat() *ChatServer {
|
2021-08-14 01:26:44 +03:00
|
|
|
maximumConcurrentConnectionLimit := getMaximumConcurrentConnectionLimit()
|
|
|
|
setSystemConcurrentConnectionLimit(maximumConcurrentConnectionLimit)
|
|
|
|
|
2021-07-20 05:22:29 +03:00
|
|
|
server := &ChatServer{
|
2021-08-14 01:26:44 +03:00
|
|
|
clients: map[uint]*ChatClient{},
|
|
|
|
outbound: make(chan []byte),
|
|
|
|
inbound: make(chan chatClientEvent),
|
|
|
|
unregister: make(chan uint),
|
|
|
|
maxSocketConnectionLimit: maximumConcurrentConnectionLimit,
|
2021-07-20 05:22:29 +03:00
|
|
|
}
|
2020-06-23 04:11:56 +03:00
|
|
|
|
2021-07-20 05:22:29 +03:00
|
|
|
return server
|
2020-06-23 04:11:56 +03:00
|
|
|
}
|
|
|
|
|
2021-07-20 05:22:29 +03:00
|
|
|
func (s *ChatServer) Run() {
|
|
|
|
for {
|
|
|
|
select {
|
2021-07-27 03:30:28 +03:00
|
|
|
case clientId := <-s.unregister:
|
|
|
|
if _, ok := s.clients[clientId]; ok {
|
2021-07-20 05:22:29 +03:00
|
|
|
s.mu.Lock()
|
2021-07-27 03:30:28 +03:00
|
|
|
delete(s.clients, clientId)
|
2021-07-20 05:22:29 +03:00
|
|
|
s.mu.Unlock()
|
|
|
|
}
|
|
|
|
|
|
|
|
case message := <-s.inbound:
|
|
|
|
s.eventReceived(message)
|
|
|
|
}
|
|
|
|
}
|
2020-06-23 04:11:56 +03:00
|
|
|
}
|
|
|
|
|
2021-07-20 05:22:29 +03:00
|
|
|
// Addclient registers new connection as a User.
|
2021-08-13 00:55:21 +03:00
|
|
|
func (s *ChatServer) Addclient(conn *websocket.Conn, user *user.User, accessToken string, userAgent string, ipAddress string) *ChatClient {
|
2021-07-20 05:22:29 +03:00
|
|
|
client := &ChatClient{
|
|
|
|
server: s,
|
|
|
|
conn: conn,
|
|
|
|
User: user,
|
2021-08-13 00:55:21 +03:00
|
|
|
ipAddress: ipAddress,
|
2021-07-20 05:22:29 +03:00
|
|
|
accessToken: accessToken,
|
2021-07-31 22:48:42 +03:00
|
|
|
send: make(chan []byte, 256),
|
2021-07-20 05:22:29 +03:00
|
|
|
UserAgent: userAgent,
|
|
|
|
ConnectedAt: time.Now(),
|
|
|
|
}
|
|
|
|
|
|
|
|
s.mu.Lock()
|
|
|
|
{
|
|
|
|
client.id = s.seq
|
|
|
|
s.clients[client.id] = client
|
|
|
|
s.seq++
|
|
|
|
}
|
|
|
|
s.mu.Unlock()
|
|
|
|
|
|
|
|
log.Traceln("Adding client", client.id, "total count:", len(s.clients))
|
|
|
|
|
|
|
|
go client.writePump()
|
|
|
|
go client.readPump()
|
|
|
|
|
|
|
|
client.sendConnectedClientInfo()
|
|
|
|
|
|
|
|
if getStatus().Online {
|
|
|
|
s.sendUserJoinedMessage(client)
|
|
|
|
s.sendWelcomeMessageToClient(client)
|
|
|
|
}
|
|
|
|
|
2021-08-13 00:55:21 +03:00
|
|
|
// Asynchronously, optionally, fetch GeoIP data.
|
|
|
|
go func(client *ChatClient) {
|
|
|
|
client.Geo = geoip.GetGeoFromIP(ipAddress)
|
|
|
|
}(client)
|
|
|
|
|
2021-07-20 05:22:29 +03:00
|
|
|
return client
|
2020-06-23 04:11:56 +03:00
|
|
|
}
|
|
|
|
|
2021-07-20 05:22:29 +03:00
|
|
|
func (s *ChatServer) sendUserJoinedMessage(c *ChatClient) {
|
|
|
|
userJoinedEvent := events.UserJoinedEvent{}
|
|
|
|
userJoinedEvent.SetDefaults()
|
|
|
|
userJoinedEvent.User = c.User
|
|
|
|
|
|
|
|
if err := s.Broadcast(userJoinedEvent.GetBroadcastPayload()); err != nil {
|
|
|
|
log.Errorln("error adding client to chat server", err)
|
2020-06-23 04:11:56 +03:00
|
|
|
}
|
2021-07-20 05:22:29 +03:00
|
|
|
|
|
|
|
// Send chat user joined webhook
|
|
|
|
webhooks.SendChatEventUserJoined(userJoinedEvent)
|
2020-06-23 04:11:56 +03:00
|
|
|
}
|
|
|
|
|
2021-07-20 05:22:29 +03:00
|
|
|
func (s *ChatServer) ClientClosed(c *ChatClient) {
|
|
|
|
s.mu.Lock()
|
|
|
|
defer s.mu.Unlock()
|
|
|
|
c.close()
|
2021-03-04 07:44:13 +03:00
|
|
|
|
2021-07-20 05:22:29 +03:00
|
|
|
if _, ok := s.clients[c.id]; ok {
|
|
|
|
log.Debugln("Deleting", c.id)
|
|
|
|
delete(s.clients, c.id)
|
2020-06-23 04:11:56 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-07-20 05:22:29 +03:00
|
|
|
func (s *ChatServer) HandleClientConnection(w http.ResponseWriter, r *http.Request) {
|
|
|
|
if data.GetChatDisabled() {
|
|
|
|
_, _ = w.Write([]byte(events.ChatDisabled))
|
|
|
|
return
|
2020-07-29 07:30:03 +03:00
|
|
|
}
|
2021-02-19 10:05:52 +03:00
|
|
|
|
2021-07-20 05:22:29 +03:00
|
|
|
// Limit concurrent chat connections
|
2021-08-14 01:26:44 +03:00
|
|
|
if int64(len(s.clients)) >= s.maxSocketConnectionLimit {
|
|
|
|
log.Warnln("rejecting incoming client connection as it exceeds the max client count of", s.maxSocketConnectionLimit)
|
2021-07-20 05:22:29 +03:00
|
|
|
_, _ = w.Write([]byte(events.ErrorMaxConnectionsExceeded))
|
|
|
|
return
|
|
|
|
}
|
2021-02-19 10:05:52 +03:00
|
|
|
|
2021-07-20 05:22:29 +03:00
|
|
|
conn, err := upgrader.Upgrade(w, r, nil)
|
|
|
|
if err != nil {
|
|
|
|
log.Debugln(err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
accessToken := r.URL.Query().Get("accessToken")
|
|
|
|
if accessToken == "" {
|
|
|
|
log.Errorln("Access token is required")
|
|
|
|
// Return HTTP status code
|
|
|
|
conn.Close()
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
// A user is required to use the websocket
|
|
|
|
user := user.GetUserByToken(accessToken)
|
|
|
|
if user == nil {
|
|
|
|
_ = conn.WriteJSON(events.EventPayload{
|
|
|
|
"type": events.ErrorNeedsRegistration,
|
|
|
|
})
|
|
|
|
// Send error that registration is required
|
|
|
|
conn.Close()
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
// User is disabled therefore we should disconnect.
|
|
|
|
if user.DisabledAt != nil {
|
|
|
|
log.Traceln("Disabled user", user.Id, user.DisplayName, "rejected")
|
|
|
|
_ = conn.WriteJSON(events.EventPayload{
|
|
|
|
"type": events.ErrorUserDisabled,
|
|
|
|
})
|
|
|
|
conn.Close()
|
|
|
|
return
|
2021-02-19 10:05:52 +03:00
|
|
|
}
|
2021-03-04 07:44:13 +03:00
|
|
|
|
2021-07-20 05:22:29 +03:00
|
|
|
userAgent := r.UserAgent()
|
2021-08-13 00:55:21 +03:00
|
|
|
ipAddress := utils.GetIPAddressFromRequest(r)
|
2021-07-20 05:22:29 +03:00
|
|
|
|
2021-08-13 00:55:21 +03:00
|
|
|
s.Addclient(conn, user, accessToken, userAgent, ipAddress)
|
2020-07-29 07:30:03 +03:00
|
|
|
}
|
|
|
|
|
2021-07-20 05:22:29 +03:00
|
|
|
// Broadcast sends message to all connected clients.
|
|
|
|
func (s *ChatServer) Broadcast(payload events.EventPayload) error {
|
|
|
|
data, err := json.Marshal(payload)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
s.mu.Lock()
|
|
|
|
defer s.mu.Unlock()
|
2020-06-23 09:52:50 +03:00
|
|
|
|
2021-07-20 05:22:29 +03:00
|
|
|
for _, client := range s.clients {
|
|
|
|
if client == nil {
|
|
|
|
continue
|
|
|
|
}
|
2020-06-23 09:52:50 +03:00
|
|
|
|
2021-07-20 05:22:29 +03:00
|
|
|
select {
|
|
|
|
case client.send <- data:
|
|
|
|
default:
|
2021-07-23 01:27:04 +03:00
|
|
|
client.close()
|
2021-07-20 05:22:29 +03:00
|
|
|
delete(s.clients, client.id)
|
2020-06-23 09:52:50 +03:00
|
|
|
}
|
2021-07-20 05:22:29 +03:00
|
|
|
}
|
2020-06-23 09:52:50 +03:00
|
|
|
|
2021-07-20 05:22:29 +03:00
|
|
|
return nil
|
2020-06-23 09:52:50 +03:00
|
|
|
}
|
|
|
|
|
2021-07-20 05:22:29 +03:00
|
|
|
func (s *ChatServer) Send(payload events.EventPayload, client *ChatClient) {
|
|
|
|
data, err := json.Marshal(payload)
|
|
|
|
if err != nil {
|
|
|
|
log.Errorln(err)
|
|
|
|
return
|
|
|
|
}
|
2020-06-23 04:11:56 +03:00
|
|
|
|
2021-07-20 05:22:29 +03:00
|
|
|
client.send <- data
|
|
|
|
}
|
2020-06-23 04:11:56 +03:00
|
|
|
|
2021-07-20 05:22:29 +03:00
|
|
|
// DisconnectUser will forcefully disconnect all clients belonging to a user by ID.
|
|
|
|
func (s *ChatServer) DisconnectUser(userID string) {
|
|
|
|
s.mu.Lock()
|
|
|
|
clients, err := GetClientsForUser(userID)
|
|
|
|
s.mu.Unlock()
|
|
|
|
|
|
|
|
if err != nil || clients == nil || len(clients) == 0 {
|
|
|
|
log.Debugln("Requested to disconnect user", userID, err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, client := range clients {
|
|
|
|
log.Traceln("Disconnecting client", client.User.Id, "owned by", client.User.DisplayName)
|
2021-03-14 21:46:27 +03:00
|
|
|
|
2021-07-20 05:22:29 +03:00
|
|
|
go func(client *ChatClient) {
|
|
|
|
event := events.UserDisabledEvent{}
|
|
|
|
event.SetDefaults()
|
2021-03-10 22:07:47 +03:00
|
|
|
|
2021-07-20 05:22:29 +03:00
|
|
|
// Send this disabled event specifically to this single connected client
|
|
|
|
// to let them know they've been banned.
|
|
|
|
_server.Send(event.GetBroadcastPayload(), client)
|
2020-10-14 02:45:52 +03:00
|
|
|
|
2021-07-20 05:22:29 +03:00
|
|
|
// Give the socket time to send out the above message.
|
|
|
|
// Unfortunately I don't know of any way to get a real callback to know when
|
|
|
|
// the message was successfully sent, so give it a couple seconds.
|
|
|
|
time.Sleep(2 * time.Second)
|
2021-02-19 10:05:52 +03:00
|
|
|
|
2021-07-20 05:22:29 +03:00
|
|
|
// Forcefully disconnect if still valid.
|
|
|
|
if client != nil {
|
|
|
|
client.close()
|
2021-02-01 01:57:50 +03:00
|
|
|
}
|
2021-07-20 05:22:29 +03:00
|
|
|
}(client)
|
|
|
|
}
|
|
|
|
}
|
2020-06-23 04:11:56 +03:00
|
|
|
|
2021-07-20 05:22:29 +03:00
|
|
|
func (s *ChatServer) eventReceived(event chatClientEvent) {
|
|
|
|
var typecheck map[string]interface{}
|
|
|
|
if err := json.Unmarshal(event.data, &typecheck); err != nil {
|
|
|
|
log.Debugln(err)
|
|
|
|
}
|
2020-06-23 04:11:56 +03:00
|
|
|
|
2021-07-20 05:22:29 +03:00
|
|
|
eventType := typecheck["type"]
|
|
|
|
|
|
|
|
switch eventType {
|
|
|
|
case events.MessageSent:
|
|
|
|
s.userMessageSent(event)
|
|
|
|
|
|
|
|
case events.UserNameChanged:
|
|
|
|
s.userNameChanged(event)
|
|
|
|
|
|
|
|
default:
|
|
|
|
log.Debugln(eventType, "event not found:", typecheck)
|
2020-06-23 04:11:56 +03:00
|
|
|
}
|
|
|
|
}
|
2020-07-20 01:14:51 +03:00
|
|
|
|
2021-07-20 05:22:29 +03:00
|
|
|
func (s *ChatServer) sendWelcomeMessageToClient(c *ChatClient) {
|
|
|
|
// Add an artificial delay so people notice this message come in.
|
|
|
|
time.Sleep(7 * time.Second)
|
|
|
|
|
|
|
|
welcomeMessage := utils.RenderSimpleMarkdown(data.GetServerWelcomeMessage())
|
2020-12-22 06:42:47 +03:00
|
|
|
|
2021-07-20 05:22:29 +03:00
|
|
|
if welcomeMessage != "" {
|
|
|
|
s.sendSystemMessageToClient(c, welcomeMessage)
|
2020-12-22 06:42:47 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-07-20 05:22:29 +03:00
|
|
|
func (s *ChatServer) sendAllWelcomeMessage() {
|
|
|
|
welcomeMessage := utils.RenderSimpleMarkdown(data.GetServerWelcomeMessage())
|
2020-07-20 01:14:51 +03:00
|
|
|
|
2021-07-20 05:22:29 +03:00
|
|
|
if welcomeMessage != "" {
|
|
|
|
clientMessage := events.SystemMessageEvent{
|
|
|
|
Event: events.Event{},
|
|
|
|
MessageEvent: events.MessageEvent{
|
|
|
|
Body: welcomeMessage,
|
|
|
|
},
|
2021-03-22 00:10:56 +03:00
|
|
|
}
|
2021-07-20 05:22:29 +03:00
|
|
|
clientMessage.SetDefaults()
|
|
|
|
_ = s.Broadcast(clientMessage.GetBroadcastPayload())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *ChatServer) sendSystemMessageToClient(c *ChatClient, message string) {
|
|
|
|
clientMessage := events.SystemMessageEvent{
|
|
|
|
Event: events.Event{},
|
|
|
|
MessageEvent: events.MessageEvent{
|
|
|
|
Body: message,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
clientMessage.SetDefaults()
|
|
|
|
s.Send(clientMessage.GetBroadcastPayload(), c)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *ChatServer) sendActionToClient(c *ChatClient, message string) {
|
|
|
|
clientMessage := events.ActionEvent{
|
|
|
|
MessageEvent: events.MessageEvent{
|
|
|
|
Body: message,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
clientMessage.SetDefaults()
|
|
|
|
clientMessage.RenderBody()
|
|
|
|
s.Send(clientMessage.GetBroadcastPayload(), c)
|
2020-10-07 09:14:33 +03:00
|
|
|
}
|