2020-06-23 04:11:56 +03:00
|
|
|
package chat
|
2020-05-24 03:57:49 +03:00
|
|
|
|
|
|
|
import (
|
2020-07-29 07:30:03 +03:00
|
|
|
"encoding/json"
|
2020-05-24 03:57:49 +03:00
|
|
|
"fmt"
|
|
|
|
"io"
|
2020-06-23 09:52:50 +03:00
|
|
|
"time"
|
2020-05-24 03:57:49 +03:00
|
|
|
|
2020-06-18 09:01:49 +03:00
|
|
|
log "github.com/sirupsen/logrus"
|
2020-05-24 03:57:49 +03:00
|
|
|
"golang.org/x/net/websocket"
|
2020-06-23 04:11:56 +03:00
|
|
|
|
2020-10-07 09:14:33 +03:00
|
|
|
"github.com/owncast/owncast/geoip"
|
2020-10-05 20:07:09 +03:00
|
|
|
"github.com/owncast/owncast/models"
|
|
|
|
"github.com/owncast/owncast/utils"
|
2020-06-25 08:00:49 +03:00
|
|
|
|
|
|
|
"github.com/teris-io/shortid"
|
2021-01-02 03:55:04 +03:00
|
|
|
"golang.org/x/time/rate"
|
2020-05-24 03:57:49 +03:00
|
|
|
)
|
|
|
|
|
|
|
|
const channelBufSize = 100
|
|
|
|
|
2020-06-23 04:11:56 +03:00
|
|
|
//Client represents a chat client.
|
2020-05-24 03:57:49 +03:00
|
|
|
type Client struct {
|
2020-06-23 09:52:50 +03:00
|
|
|
ConnectedAt time.Time
|
|
|
|
MessageCount int
|
2020-10-07 09:14:33 +03:00
|
|
|
UserAgent string
|
|
|
|
IPAddress string
|
|
|
|
Username *string
|
|
|
|
ClientID string // How we identify unique viewers when counting viewer counts.
|
|
|
|
Geo *geoip.GeoDetails `json:"geo"`
|
2021-02-19 10:05:52 +03:00
|
|
|
Ignore bool // If set to true this will not be treated as a viewer
|
2020-06-23 09:52:50 +03:00
|
|
|
|
2020-07-29 07:30:03 +03:00
|
|
|
socketID string // How we identify a single websocket client.
|
|
|
|
ws *websocket.Conn
|
2020-12-30 00:35:33 +03:00
|
|
|
ch chan models.ChatEvent
|
2020-07-29 07:30:03 +03:00
|
|
|
pingch chan models.PingMessage
|
|
|
|
usernameChangeChannel chan models.NameChangeEvent
|
2021-02-19 10:05:52 +03:00
|
|
|
userJoinedChannel chan models.UserJoinedEvent
|
2020-06-15 02:44:38 +03:00
|
|
|
|
2020-05-24 03:57:49 +03:00
|
|
|
doneCh chan bool
|
2021-01-02 03:55:04 +03:00
|
|
|
|
|
|
|
rateLimiter *rate.Limiter
|
2020-05-24 03:57:49 +03:00
|
|
|
}
|
|
|
|
|
2020-11-13 02:14:59 +03:00
|
|
|
// NewClient creates a new chat client.
|
2020-06-23 23:11:01 +03:00
|
|
|
func NewClient(ws *websocket.Conn) *Client {
|
2020-05-24 03:57:49 +03:00
|
|
|
if ws == nil {
|
2020-06-18 09:01:49 +03:00
|
|
|
log.Panicln("ws cannot be nil")
|
2020-05-24 03:57:49 +03:00
|
|
|
}
|
|
|
|
|
2021-02-19 10:05:52 +03:00
|
|
|
var ignoreClient = false
|
|
|
|
for _, extraData := range ws.Config().Protocol {
|
|
|
|
if extraData == "IGNORE_CLIENT" {
|
|
|
|
ignoreClient = true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-12-30 00:35:33 +03:00
|
|
|
ch := make(chan models.ChatEvent, channelBufSize)
|
2020-05-24 03:57:49 +03:00
|
|
|
doneCh := make(chan bool)
|
2020-06-23 04:11:56 +03:00
|
|
|
pingch := make(chan models.PingMessage)
|
2020-07-29 07:30:03 +03:00
|
|
|
usernameChangeChannel := make(chan models.NameChangeEvent)
|
2021-02-19 10:05:52 +03:00
|
|
|
userJoinedChannel := make(chan models.UserJoinedEvent)
|
2020-07-29 07:30:03 +03:00
|
|
|
|
2020-10-07 09:14:33 +03:00
|
|
|
ipAddress := utils.GetIPAddressFromRequest(ws.Request())
|
|
|
|
userAgent := ws.Request().UserAgent()
|
2020-07-29 07:30:03 +03:00
|
|
|
socketID, _ := shortid.Generate()
|
2020-11-05 11:14:51 +03:00
|
|
|
clientID := socketID
|
2020-06-23 04:11:56 +03:00
|
|
|
|
2021-01-02 03:55:04 +03:00
|
|
|
rateLimiter := rate.NewLimiter(0.6, 5)
|
|
|
|
|
2021-02-19 10:05:52 +03:00
|
|
|
return &Client{time.Now(), 0, userAgent, ipAddress, nil, clientID, nil, ignoreClient, socketID, ws, ch, pingch, usernameChangeChannel, userJoinedChannel, doneCh, rateLimiter}
|
2020-05-24 03:57:49 +03:00
|
|
|
}
|
|
|
|
|
2021-01-02 02:39:41 +03:00
|
|
|
func (c *Client) write(msg models.ChatEvent) {
|
2020-05-24 03:57:49 +03:00
|
|
|
select {
|
|
|
|
case c.ch <- msg:
|
|
|
|
default:
|
2020-12-22 06:42:47 +03:00
|
|
|
_server.removeClient(c)
|
2020-10-07 09:14:33 +03:00
|
|
|
_server.err(fmt.Errorf("client %s is disconnected", c.ClientID))
|
2020-05-24 03:57:49 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-11-13 02:14:59 +03:00
|
|
|
// Listen Write and Read request via channel.
|
2021-01-02 02:39:41 +03:00
|
|
|
func (c *Client) listen() {
|
2020-05-24 03:57:49 +03:00
|
|
|
go c.listenWrite()
|
|
|
|
c.listenRead()
|
|
|
|
}
|
|
|
|
|
2020-11-13 02:14:59 +03:00
|
|
|
// Listen write request via channel.
|
2020-05-24 03:57:49 +03:00
|
|
|
func (c *Client) listenWrite() {
|
|
|
|
for {
|
|
|
|
select {
|
2020-06-15 02:44:38 +03:00
|
|
|
// Send a PING keepalive
|
|
|
|
case msg := <-c.pingch:
|
2020-11-15 05:39:53 +03:00
|
|
|
err := websocket.JSON.Send(c.ws, msg)
|
|
|
|
if err != nil {
|
2020-12-22 06:42:47 +03:00
|
|
|
c.handleClientSocketError(err)
|
2020-11-15 05:39:53 +03:00
|
|
|
}
|
2020-05-24 03:57:49 +03:00
|
|
|
// send message to the client
|
|
|
|
case msg := <-c.ch:
|
2020-11-15 05:39:53 +03:00
|
|
|
err := websocket.JSON.Send(c.ws, msg)
|
|
|
|
if err != nil {
|
2020-12-22 06:42:47 +03:00
|
|
|
c.handleClientSocketError(err)
|
2020-11-15 05:39:53 +03:00
|
|
|
}
|
2020-07-29 07:30:03 +03:00
|
|
|
case msg := <-c.usernameChangeChannel:
|
2020-11-15 05:39:53 +03:00
|
|
|
err := websocket.JSON.Send(c.ws, msg)
|
|
|
|
if err != nil {
|
2020-12-22 06:42:47 +03:00
|
|
|
c.handleClientSocketError(err)
|
2020-11-15 05:39:53 +03:00
|
|
|
}
|
2021-02-19 10:05:52 +03:00
|
|
|
case msg := <-c.userJoinedChannel:
|
|
|
|
err := websocket.JSON.Send(c.ws, msg)
|
|
|
|
if err != nil {
|
|
|
|
c.handleClientSocketError(err)
|
|
|
|
}
|
|
|
|
|
2020-05-24 03:57:49 +03:00
|
|
|
// receive done request
|
|
|
|
case <-c.doneCh:
|
2020-12-22 06:42:47 +03:00
|
|
|
_server.removeClient(c)
|
2020-05-24 03:57:49 +03:00
|
|
|
c.doneCh <- true // for listenRead method
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-12-22 06:42:47 +03:00
|
|
|
func (c *Client) handleClientSocketError(err error) {
|
2021-01-23 00:50:23 +03:00
|
|
|
log.Warnln("Websocket client error: ", err.Error())
|
2020-12-22 06:42:47 +03:00
|
|
|
_server.removeClient(c)
|
|
|
|
}
|
|
|
|
|
2021-01-02 03:55:04 +03:00
|
|
|
func (c *Client) passesRateLimit() bool {
|
|
|
|
if !c.rateLimiter.Allow() {
|
2021-01-27 06:52:32 +03:00
|
|
|
log.Debugln("Client", c.ClientID, "has exceeded the messaging rate limiting thresholds.")
|
2021-01-02 03:55:04 +03:00
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
2020-11-13 02:14:59 +03:00
|
|
|
// Listen read request via channel.
|
2020-05-24 03:57:49 +03:00
|
|
|
func (c *Client) listenRead() {
|
|
|
|
for {
|
|
|
|
select {
|
|
|
|
// receive done request
|
|
|
|
case <-c.doneCh:
|
2020-06-23 23:11:01 +03:00
|
|
|
_server.remove(c)
|
2020-05-24 03:57:49 +03:00
|
|
|
c.doneCh <- true // for listenWrite method
|
|
|
|
return
|
|
|
|
|
|
|
|
// read data from websocket connection
|
|
|
|
default:
|
2020-07-29 07:30:03 +03:00
|
|
|
var data []byte
|
|
|
|
err := websocket.Message.Receive(c.ws, &data)
|
2020-06-25 08:00:49 +03:00
|
|
|
if err != nil {
|
2020-07-29 07:30:03 +03:00
|
|
|
if err == io.EOF {
|
|
|
|
c.doneCh <- true
|
2021-03-04 07:44:13 +03:00
|
|
|
return
|
2020-07-29 07:30:03 +03:00
|
|
|
}
|
2021-03-04 07:44:13 +03:00
|
|
|
c.handleClientSocketError(err)
|
2020-06-25 08:00:49 +03:00
|
|
|
}
|
|
|
|
|
2020-07-29 07:30:03 +03:00
|
|
|
var messageTypeCheck map[string]interface{}
|
|
|
|
err = json.Unmarshal(data, &messageTypeCheck)
|
|
|
|
if err != nil {
|
|
|
|
log.Errorln(err)
|
|
|
|
}
|
2020-06-23 04:11:56 +03:00
|
|
|
|
2021-01-02 03:55:04 +03:00
|
|
|
if !c.passesRateLimit() {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
2021-03-04 07:44:13 +03:00
|
|
|
messageType := messageTypeCheck["type"].(string)
|
|
|
|
|
2021-02-19 10:05:52 +03:00
|
|
|
if messageType == models.MessageSent {
|
2020-07-29 07:30:03 +03:00
|
|
|
c.chatMessageReceived(data)
|
2021-02-19 10:05:52 +03:00
|
|
|
} else if messageType == models.UserNameChanged {
|
2020-07-29 07:30:03 +03:00
|
|
|
c.userChangedName(data)
|
2021-02-19 10:05:52 +03:00
|
|
|
} else if messageType == models.UserJoined {
|
|
|
|
c.userJoined(data)
|
2020-05-24 03:57:49 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2020-07-29 07:30:03 +03:00
|
|
|
|
2021-02-19 10:05:52 +03:00
|
|
|
func (c *Client) userJoined(data []byte) {
|
|
|
|
var msg models.UserJoinedEvent
|
|
|
|
if err := json.Unmarshal(data, &msg); err != nil {
|
|
|
|
log.Errorln(err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
msg.ID = shortid.MustGenerate()
|
|
|
|
msg.Type = models.UserJoined
|
|
|
|
msg.Timestamp = time.Now()
|
|
|
|
|
|
|
|
c.Username = &msg.Username
|
|
|
|
|
|
|
|
_server.userJoined(msg)
|
|
|
|
}
|
|
|
|
|
2020-07-29 07:30:03 +03:00
|
|
|
func (c *Client) userChangedName(data []byte) {
|
|
|
|
var msg models.NameChangeEvent
|
|
|
|
err := json.Unmarshal(data, &msg)
|
|
|
|
if err != nil {
|
|
|
|
log.Errorln(err)
|
|
|
|
}
|
2021-02-19 10:05:52 +03:00
|
|
|
msg.Type = models.UserNameChanged
|
2020-07-29 07:30:03 +03:00
|
|
|
msg.ID = shortid.MustGenerate()
|
|
|
|
_server.usernameChanged(msg)
|
2020-10-07 09:14:33 +03:00
|
|
|
c.Username = &msg.NewName
|
2020-07-29 07:30:03 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
func (c *Client) chatMessageReceived(data []byte) {
|
2020-12-30 00:35:33 +03:00
|
|
|
var msg models.ChatEvent
|
2020-07-29 07:30:03 +03:00
|
|
|
err := json.Unmarshal(data, &msg)
|
|
|
|
if err != nil {
|
|
|
|
log.Errorln(err)
|
|
|
|
}
|
|
|
|
|
2021-02-19 10:05:52 +03:00
|
|
|
msg.SetDefaults()
|
2020-07-29 07:30:03 +03:00
|
|
|
|
|
|
|
c.MessageCount++
|
2020-10-07 09:14:33 +03:00
|
|
|
c.Username = &msg.Author
|
2020-07-29 07:30:03 +03:00
|
|
|
|
2020-10-07 09:14:33 +03:00
|
|
|
msg.ClientID = c.ClientID
|
2020-07-29 07:30:03 +03:00
|
|
|
_server.SendToAll(msg)
|
|
|
|
}
|
2020-10-07 09:14:33 +03:00
|
|
|
|
|
|
|
// GetViewerClientFromChatClient returns a general models.Client from a chat websocket client.
|
|
|
|
func (c *Client) GetViewerClientFromChatClient() models.Client {
|
|
|
|
return models.Client{
|
|
|
|
ConnectedAt: c.ConnectedAt,
|
|
|
|
MessageCount: c.MessageCount,
|
|
|
|
UserAgent: c.UserAgent,
|
|
|
|
IPAddress: c.IPAddress,
|
|
|
|
Username: c.Username,
|
|
|
|
ClientID: c.ClientID,
|
|
|
|
Geo: geoip.GetGeoFromIP(c.IPAddress),
|
|
|
|
}
|
|
|
|
}
|