2021-07-20 05:22:29 +03:00
|
|
|
package chat
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
|
|
|
"encoding/json"
|
2022-08-02 23:03:11 +03:00
|
|
|
"net/http"
|
2022-04-16 05:30:05 +03:00
|
|
|
"sync"
|
2021-07-20 05:22:29 +03:00
|
|
|
"time"
|
|
|
|
|
|
|
|
log "github.com/sirupsen/logrus"
|
|
|
|
"golang.org/x/time/rate"
|
|
|
|
|
|
|
|
"github.com/gorilla/websocket"
|
|
|
|
"github.com/owncast/owncast/config"
|
|
|
|
"github.com/owncast/owncast/core/chat/events"
|
|
|
|
"github.com/owncast/owncast/core/user"
|
|
|
|
"github.com/owncast/owncast/geoip"
|
|
|
|
)
|
|
|
|
|
2021-09-12 10:18:15 +03:00
|
|
|
// Client represents a single chat client.
|
|
|
|
type Client struct {
|
2022-04-16 05:30:05 +03:00
|
|
|
mu sync.RWMutex
|
2022-12-14 06:17:32 +03:00
|
|
|
Id uint `json:"-"`
|
2021-07-20 05:22:29 +03:00
|
|
|
accessToken string
|
|
|
|
conn *websocket.Conn
|
|
|
|
User *user.User `json:"user"`
|
2021-09-12 10:18:15 +03:00
|
|
|
server *Server
|
2022-03-07 07:34:49 +03:00
|
|
|
IPAddress string `json:"-"`
|
2021-07-20 05:22:29 +03:00
|
|
|
// Buffered channel of outbound messages.
|
|
|
|
send chan []byte
|
|
|
|
rateLimiter *rate.Limiter
|
2021-08-28 00:43:09 +03:00
|
|
|
timeoutTimer *time.Timer
|
|
|
|
inTimeout bool
|
2021-07-20 05:22:29 +03:00
|
|
|
Geo *geoip.GeoDetails `json:"geo"`
|
|
|
|
MessageCount int `json:"messageCount"`
|
|
|
|
UserAgent string `json:"userAgent"`
|
|
|
|
ConnectedAt time.Time `json:"connectedAt"`
|
|
|
|
}
|
|
|
|
|
|
|
|
type chatClientEvent struct {
|
|
|
|
data []byte
|
2021-09-12 10:18:15 +03:00
|
|
|
client *Client
|
2021-07-20 05:22:29 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
const (
|
|
|
|
// Time allowed to write a message to the peer.
|
|
|
|
writeWait = 10 * time.Second
|
|
|
|
|
|
|
|
// Time allowed to read the next pong message from the peer.
|
|
|
|
pongWait = 60 * time.Second
|
|
|
|
|
|
|
|
// Send pings to peer with this period. Must be less than pongWait.
|
|
|
|
pingPeriod = (pongWait * 9) / 10
|
|
|
|
|
|
|
|
// Maximum message size allowed from peer.
|
|
|
|
// Larger messages get thrown away.
|
|
|
|
// Messages > *2 the socket gets closed.
|
|
|
|
maxMessageSize = config.MaxSocketPayloadSize
|
|
|
|
)
|
|
|
|
|
|
|
|
var upgrader = websocket.Upgrader{
|
|
|
|
ReadBufferSize: 1024,
|
|
|
|
WriteBufferSize: 1024,
|
2022-08-02 23:03:11 +03:00
|
|
|
|
|
|
|
// Override default origin check to allow all clients, even those that
|
|
|
|
// do not match our server.
|
|
|
|
CheckOrigin: func(r *http.Request) bool {
|
|
|
|
return true
|
|
|
|
},
|
2021-07-20 05:22:29 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
var (
|
|
|
|
newline = []byte{'\n'}
|
|
|
|
space = []byte{' '}
|
|
|
|
)
|
|
|
|
|
2021-09-12 10:18:15 +03:00
|
|
|
func (c *Client) sendConnectedClientInfo() {
|
2021-11-03 05:27:41 +03:00
|
|
|
payload := events.ConnectedClientInfo{
|
|
|
|
Event: events.Event{
|
|
|
|
Type: events.ConnectedUserInfo,
|
|
|
|
},
|
|
|
|
User: c.User,
|
2021-07-20 05:22:29 +03:00
|
|
|
}
|
|
|
|
|
2021-11-03 05:27:41 +03:00
|
|
|
payload.SetDefaults()
|
2021-07-20 05:22:29 +03:00
|
|
|
c.sendPayload(payload)
|
|
|
|
}
|
|
|
|
|
2021-09-12 10:18:15 +03:00
|
|
|
func (c *Client) readPump() {
|
2021-08-28 00:43:09 +03:00
|
|
|
// Allow 3 messages every two seconds.
|
|
|
|
limit := rate.Every(2 * time.Second / 3)
|
|
|
|
c.rateLimiter = rate.NewLimiter(limit, 1)
|
2021-07-20 05:22:29 +03:00
|
|
|
|
|
|
|
defer func() {
|
|
|
|
c.close()
|
|
|
|
}()
|
|
|
|
|
|
|
|
// If somebody is sending 2x the max message size they're likely a bad actor
|
|
|
|
// and should be disconnected. Below we throw away messages > max size.
|
|
|
|
c.conn.SetReadLimit(maxMessageSize * 2)
|
|
|
|
|
|
|
|
_ = c.conn.SetReadDeadline(time.Now().Add(pongWait))
|
|
|
|
c.conn.SetPongHandler(func(string) error { _ = c.conn.SetReadDeadline(time.Now().Add(pongWait)); return nil })
|
|
|
|
for {
|
|
|
|
_, message, err := c.conn.ReadMessage()
|
|
|
|
if err != nil {
|
|
|
|
if websocket.IsUnexpectedCloseError(err, websocket.CloseGoingAway, websocket.CloseAbnormalClosure) {
|
|
|
|
c.close()
|
|
|
|
}
|
|
|
|
break
|
|
|
|
}
|
|
|
|
|
|
|
|
// Throw away messages greater than max message size.
|
|
|
|
if len(message) > maxMessageSize {
|
|
|
|
c.sendAction("Sorry, that message exceeded the maximum size and can't be delivered.")
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
2021-08-28 00:43:09 +03:00
|
|
|
// Check if this client is temporarily blocked from sending messages.
|
|
|
|
if c.inTimeout {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
2021-07-20 05:22:29 +03:00
|
|
|
// Guard against floods.
|
|
|
|
if !c.passesRateLimit() {
|
2022-12-14 06:17:32 +03:00
|
|
|
log.Warnln("Client", c.Id, c.User.DisplayName, "has exceeded the messaging rate limiting thresholds and messages are being rejected temporarily.")
|
2021-08-28 00:43:09 +03:00
|
|
|
c.startChatRejectionTimeout()
|
|
|
|
|
2021-07-20 05:22:29 +03:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
2021-10-25 10:31:45 +03:00
|
|
|
message = bytes.TrimSpace(bytes.ReplaceAll(message, newline, space))
|
2021-07-20 05:22:29 +03:00
|
|
|
c.handleEvent(message)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-09-12 10:18:15 +03:00
|
|
|
func (c *Client) writePump() {
|
2021-07-20 05:22:29 +03:00
|
|
|
ticker := time.NewTicker(pingPeriod)
|
|
|
|
defer func() {
|
|
|
|
ticker.Stop()
|
2021-09-12 10:18:15 +03:00
|
|
|
_ = c.conn.Close()
|
2021-07-20 05:22:29 +03:00
|
|
|
}()
|
|
|
|
|
|
|
|
for {
|
|
|
|
select {
|
|
|
|
case message, ok := <-c.send:
|
|
|
|
_ = c.conn.SetWriteDeadline(time.Now().Add(writeWait))
|
|
|
|
if !ok {
|
|
|
|
// The server closed the channel.
|
|
|
|
_ = c.conn.WriteMessage(websocket.CloseMessage, []byte{})
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
w, err := c.conn.NextWriter(websocket.TextMessage)
|
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
if _, err := w.Write(message); err != nil {
|
|
|
|
log.Debugln(err)
|
|
|
|
}
|
|
|
|
|
2021-07-27 05:23:15 +03:00
|
|
|
// Optimization: Send multiple events in a single websocket message.
|
|
|
|
// Add queued chat messages to the current websocket message.
|
2022-04-16 05:30:05 +03:00
|
|
|
c.mu.RLock()
|
2021-07-27 05:23:15 +03:00
|
|
|
n := len(c.send)
|
|
|
|
for i := 0; i < n; i++ {
|
|
|
|
_, _ = w.Write(newline)
|
|
|
|
_, _ = w.Write(<-c.send)
|
|
|
|
}
|
2022-04-16 05:30:05 +03:00
|
|
|
c.mu.RUnlock()
|
2021-07-27 05:23:15 +03:00
|
|
|
|
2021-07-20 05:22:29 +03:00
|
|
|
if err := w.Close(); err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
case <-ticker.C:
|
|
|
|
_ = c.conn.SetWriteDeadline(time.Now().Add(writeWait))
|
|
|
|
if err := c.conn.WriteMessage(websocket.PingMessage, nil); err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-09-12 10:18:15 +03:00
|
|
|
func (c *Client) handleEvent(data []byte) {
|
2021-07-20 05:22:29 +03:00
|
|
|
c.server.inbound <- chatClientEvent{data: data, client: c}
|
|
|
|
}
|
|
|
|
|
2021-09-12 10:18:15 +03:00
|
|
|
func (c *Client) close() {
|
2022-12-14 06:17:32 +03:00
|
|
|
log.Traceln("client closed:", c.User.DisplayName, c.Id, c.IPAddress)
|
2021-07-20 05:22:29 +03:00
|
|
|
|
2022-04-16 05:30:05 +03:00
|
|
|
c.mu.Lock()
|
|
|
|
defer c.mu.Unlock()
|
|
|
|
|
2021-07-23 01:27:04 +03:00
|
|
|
if c.send != nil {
|
2022-04-16 05:30:05 +03:00
|
|
|
_ = c.conn.Close()
|
2022-12-14 06:17:32 +03:00
|
|
|
c.server.unregister <- c.Id
|
2021-07-23 01:27:04 +03:00
|
|
|
close(c.send)
|
|
|
|
c.send = nil
|
|
|
|
}
|
2021-07-20 05:22:29 +03:00
|
|
|
}
|
|
|
|
|
2021-09-12 10:18:15 +03:00
|
|
|
func (c *Client) passesRateLimit() bool {
|
2021-08-28 00:43:09 +03:00
|
|
|
return c.rateLimiter.Allow() && !c.inTimeout
|
|
|
|
}
|
|
|
|
|
2021-09-12 10:18:15 +03:00
|
|
|
func (c *Client) startChatRejectionTimeout() {
|
2021-08-28 00:43:09 +03:00
|
|
|
if c.timeoutTimer != nil {
|
|
|
|
return
|
2021-07-20 05:22:29 +03:00
|
|
|
}
|
|
|
|
|
2021-08-28 00:43:09 +03:00
|
|
|
c.inTimeout = true
|
|
|
|
c.timeoutTimer = time.NewTimer(10 * time.Second)
|
2021-09-12 10:18:15 +03:00
|
|
|
go func(c *Client) {
|
2021-08-28 00:43:09 +03:00
|
|
|
for range c.timeoutTimer.C {
|
|
|
|
c.inTimeout = false
|
|
|
|
c.timeoutTimer = nil
|
|
|
|
}
|
|
|
|
}(c)
|
|
|
|
|
|
|
|
c.sendAction("You are temporarily blocked from sending chat messages due to perceived flooding.")
|
2021-07-20 05:22:29 +03:00
|
|
|
}
|
|
|
|
|
2021-11-03 05:27:41 +03:00
|
|
|
func (c *Client) sendPayload(payload interface{}) {
|
2021-07-20 05:22:29 +03:00
|
|
|
var data []byte
|
|
|
|
data, err := json.Marshal(payload)
|
|
|
|
if err != nil {
|
|
|
|
log.Errorln(err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2022-04-16 05:30:05 +03:00
|
|
|
c.mu.RLock()
|
|
|
|
defer c.mu.RUnlock()
|
|
|
|
|
2021-07-23 01:27:04 +03:00
|
|
|
if c.send != nil {
|
|
|
|
c.send <- data
|
|
|
|
}
|
2021-07-20 05:22:29 +03:00
|
|
|
}
|
|
|
|
|
2021-09-12 10:18:15 +03:00
|
|
|
func (c *Client) sendAction(message string) {
|
2021-07-20 05:22:29 +03:00
|
|
|
clientMessage := events.ActionEvent{
|
|
|
|
MessageEvent: events.MessageEvent{
|
|
|
|
Body: message,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
clientMessage.SetDefaults()
|
|
|
|
clientMessage.RenderBody()
|
|
|
|
c.sendPayload(clientMessage.GetBroadcastPayload())
|
|
|
|
}
|