2020-06-23 03:11:56 +02:00
|
|
|
package chat
|
2020-05-24 02:57:49 +02:00
|
|
|
|
|
|
|
import (
|
2020-07-29 06:30:03 +02:00
|
|
|
"encoding/json"
|
2020-05-24 02:57:49 +02:00
|
|
|
"fmt"
|
|
|
|
"io"
|
2020-06-23 08:52:50 +02:00
|
|
|
"time"
|
2020-05-24 02:57:49 +02:00
|
|
|
|
2020-06-18 08:01:49 +02:00
|
|
|
log "github.com/sirupsen/logrus"
|
2020-05-24 02:57:49 +02:00
|
|
|
"golang.org/x/net/websocket"
|
2020-06-23 03:11:56 +02:00
|
|
|
|
2020-10-07 08:14:33 +02:00
|
|
|
"github.com/owncast/owncast/geoip"
|
2020-10-05 19:07:09 +02:00
|
|
|
"github.com/owncast/owncast/models"
|
|
|
|
"github.com/owncast/owncast/utils"
|
2020-06-25 07:00:49 +02:00
|
|
|
|
|
|
|
"github.com/teris-io/shortid"
|
2020-05-24 02:57:49 +02:00
|
|
|
)
|
|
|
|
|
|
|
|
const channelBufSize = 100
|
|
|
|
|
2020-06-23 03:11:56 +02:00
|
|
|
//Client represents a chat client.
|
2020-05-24 02:57:49 +02:00
|
|
|
type Client struct {
|
2020-06-23 08:52:50 +02:00
|
|
|
ConnectedAt time.Time
|
|
|
|
MessageCount int
|
2020-10-07 08:14:33 +02:00
|
|
|
UserAgent string
|
|
|
|
IPAddress string
|
|
|
|
Username *string
|
|
|
|
ClientID string // How we identify unique viewers when counting viewer counts.
|
|
|
|
Geo *geoip.GeoDetails `json:"geo"`
|
2020-06-23 08:52:50 +02:00
|
|
|
|
2020-07-29 06:30:03 +02:00
|
|
|
socketID string // How we identify a single websocket client.
|
|
|
|
ws *websocket.Conn
|
|
|
|
ch chan models.ChatMessage
|
|
|
|
pingch chan models.PingMessage
|
|
|
|
usernameChangeChannel chan models.NameChangeEvent
|
2020-06-15 01:44:38 +02:00
|
|
|
|
2020-05-24 02:57:49 +02:00
|
|
|
doneCh chan bool
|
|
|
|
}
|
|
|
|
|
2020-07-29 06:30:03 +02:00
|
|
|
const (
|
|
|
|
CHAT = "CHAT"
|
|
|
|
NAMECHANGE = "NAME_CHANGE"
|
|
|
|
PING = "PING"
|
|
|
|
PONG = "PONG"
|
|
|
|
)
|
|
|
|
|
2020-06-23 03:11:56 +02:00
|
|
|
//NewClient creates a new chat client
|
2020-06-23 22:11:01 +02:00
|
|
|
func NewClient(ws *websocket.Conn) *Client {
|
2020-05-24 02:57:49 +02:00
|
|
|
if ws == nil {
|
2020-06-18 08:01:49 +02:00
|
|
|
log.Panicln("ws cannot be nil")
|
2020-05-24 02:57:49 +02:00
|
|
|
}
|
|
|
|
|
2020-06-23 03:11:56 +02:00
|
|
|
ch := make(chan models.ChatMessage, channelBufSize)
|
2020-05-24 02:57:49 +02:00
|
|
|
doneCh := make(chan bool)
|
2020-06-23 03:11:56 +02:00
|
|
|
pingch := make(chan models.PingMessage)
|
2020-07-29 06:30:03 +02:00
|
|
|
usernameChangeChannel := make(chan models.NameChangeEvent)
|
|
|
|
|
2020-10-07 08:14:33 +02:00
|
|
|
ipAddress := utils.GetIPAddressFromRequest(ws.Request())
|
|
|
|
userAgent := ws.Request().UserAgent()
|
2020-07-29 06:30:03 +02:00
|
|
|
socketID, _ := shortid.Generate()
|
2020-11-05 09:14:51 +01:00
|
|
|
clientID := socketID
|
2020-06-23 03:11:56 +02:00
|
|
|
|
2020-10-07 08:14:33 +02:00
|
|
|
return &Client{time.Now(), 0, userAgent, ipAddress, nil, clientID, nil, socketID, ws, ch, pingch, usernameChangeChannel, doneCh}
|
2020-05-24 02:57:49 +02:00
|
|
|
}
|
|
|
|
|
2020-06-23 03:11:56 +02:00
|
|
|
//GetConnection gets the connection for the client
|
|
|
|
func (c *Client) GetConnection() *websocket.Conn {
|
2020-05-24 02:57:49 +02:00
|
|
|
return c.ws
|
|
|
|
}
|
|
|
|
|
2020-06-23 03:11:56 +02:00
|
|
|
func (c *Client) Write(msg models.ChatMessage) {
|
2020-05-24 02:57:49 +02:00
|
|
|
select {
|
|
|
|
case c.ch <- msg:
|
|
|
|
default:
|
2020-06-23 22:11:01 +02:00
|
|
|
_server.remove(c)
|
2020-10-07 08:14:33 +02:00
|
|
|
_server.err(fmt.Errorf("client %s is disconnected", c.ClientID))
|
2020-05-24 02:57:49 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-06-23 03:11:56 +02:00
|
|
|
//Done marks the client as done
|
2020-05-24 02:57:49 +02:00
|
|
|
func (c *Client) Done() {
|
|
|
|
c.doneCh <- true
|
|
|
|
}
|
|
|
|
|
|
|
|
// Listen Write and Read request via chanel
|
|
|
|
func (c *Client) Listen() {
|
|
|
|
go c.listenWrite()
|
|
|
|
c.listenRead()
|
|
|
|
}
|
|
|
|
|
|
|
|
// Listen write request via chanel
|
|
|
|
func (c *Client) listenWrite() {
|
|
|
|
for {
|
|
|
|
select {
|
2020-06-15 01:44:38 +02:00
|
|
|
// Send a PING keepalive
|
|
|
|
case msg := <-c.pingch:
|
|
|
|
websocket.JSON.Send(c.ws, msg)
|
2020-05-24 02:57:49 +02:00
|
|
|
// send message to the client
|
|
|
|
case msg := <-c.ch:
|
2020-06-17 04:08:34 +02:00
|
|
|
// log.Println("Send:", msg)
|
2020-05-24 02:57:49 +02:00
|
|
|
websocket.JSON.Send(c.ws, msg)
|
2020-07-29 06:30:03 +02:00
|
|
|
case msg := <-c.usernameChangeChannel:
|
|
|
|
websocket.JSON.Send(c.ws, msg)
|
2020-05-24 02:57:49 +02:00
|
|
|
// receive done request
|
|
|
|
case <-c.doneCh:
|
2020-06-23 22:11:01 +02:00
|
|
|
_server.remove(c)
|
2020-05-24 02:57:49 +02:00
|
|
|
c.doneCh <- true // for listenRead method
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Listen read request via chanel
|
|
|
|
func (c *Client) listenRead() {
|
|
|
|
for {
|
|
|
|
select {
|
|
|
|
|
|
|
|
// receive done request
|
|
|
|
case <-c.doneCh:
|
2020-06-23 22:11:01 +02:00
|
|
|
_server.remove(c)
|
2020-05-24 02:57:49 +02:00
|
|
|
c.doneCh <- true // for listenWrite method
|
|
|
|
return
|
|
|
|
|
|
|
|
// read data from websocket connection
|
|
|
|
default:
|
2020-07-29 06:30:03 +02:00
|
|
|
var data []byte
|
|
|
|
err := websocket.Message.Receive(c.ws, &data)
|
2020-06-25 07:00:49 +02:00
|
|
|
if err != nil {
|
2020-07-29 06:30:03 +02:00
|
|
|
if err == io.EOF {
|
|
|
|
c.doneCh <- true
|
|
|
|
} else {
|
|
|
|
log.Errorln(err)
|
|
|
|
}
|
|
|
|
return
|
2020-06-25 07:00:49 +02:00
|
|
|
}
|
|
|
|
|
2020-07-29 06:30:03 +02:00
|
|
|
var messageTypeCheck map[string]interface{}
|
|
|
|
err = json.Unmarshal(data, &messageTypeCheck)
|
|
|
|
if err != nil {
|
|
|
|
log.Errorln(err)
|
|
|
|
}
|
2020-06-23 03:11:56 +02:00
|
|
|
|
2020-07-29 06:30:03 +02:00
|
|
|
messageType := messageTypeCheck["type"]
|
2020-06-23 22:11:01 +02:00
|
|
|
|
2020-07-29 06:30:03 +02:00
|
|
|
if messageType == CHAT {
|
|
|
|
c.chatMessageReceived(data)
|
|
|
|
} else if messageType == NAMECHANGE {
|
|
|
|
c.userChangedName(data)
|
2020-05-24 02:57:49 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2020-07-29 06:30:03 +02:00
|
|
|
|
|
|
|
func (c *Client) userChangedName(data []byte) {
|
|
|
|
var msg models.NameChangeEvent
|
|
|
|
err := json.Unmarshal(data, &msg)
|
|
|
|
if err != nil {
|
|
|
|
log.Errorln(err)
|
|
|
|
}
|
|
|
|
msg.Type = NAMECHANGE
|
|
|
|
msg.ID = shortid.MustGenerate()
|
|
|
|
_server.usernameChanged(msg)
|
2020-10-07 08:14:33 +02:00
|
|
|
c.Username = &msg.NewName
|
2020-07-29 06:30:03 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
func (c *Client) chatMessageReceived(data []byte) {
|
|
|
|
var msg models.ChatMessage
|
|
|
|
err := json.Unmarshal(data, &msg)
|
|
|
|
if err != nil {
|
|
|
|
log.Errorln(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
id, _ := shortid.Generate()
|
|
|
|
msg.ID = id
|
|
|
|
msg.Timestamp = time.Now()
|
|
|
|
msg.Visible = true
|
|
|
|
|
|
|
|
c.MessageCount++
|
2020-10-07 08:14:33 +02:00
|
|
|
c.Username = &msg.Author
|
2020-07-29 06:30:03 +02:00
|
|
|
|
2020-10-07 08:14:33 +02:00
|
|
|
msg.ClientID = c.ClientID
|
2020-07-29 06:30:03 +02:00
|
|
|
_server.SendToAll(msg)
|
|
|
|
}
|
2020-10-07 08:14:33 +02: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),
|
|
|
|
}
|
|
|
|
}
|