2020-09-18 10:28:05 +02:00
|
|
|
package rtpconn
|
2020-04-24 19:38:21 +02:00
|
|
|
|
|
|
|
import (
|
2023-03-31 20:45:30 +02:00
|
|
|
crand "crypto/rand"
|
|
|
|
"encoding/base64"
|
2020-04-24 19:38:21 +02:00
|
|
|
"encoding/json"
|
|
|
|
"errors"
|
2020-12-04 22:42:20 +01:00
|
|
|
"fmt"
|
2020-04-24 19:38:21 +02:00
|
|
|
"log"
|
2024-04-30 18:17:00 +02:00
|
|
|
"net"
|
2020-04-24 19:38:21 +02:00
|
|
|
"os"
|
2024-11-30 17:55:24 +01:00
|
|
|
"strings"
|
2020-04-24 19:38:21 +02:00
|
|
|
"sync"
|
|
|
|
"time"
|
|
|
|
|
|
|
|
"github.com/gorilla/websocket"
|
2020-07-16 20:17:32 +02:00
|
|
|
"github.com/pion/webrtc/v3"
|
2020-04-24 19:38:21 +02:00
|
|
|
|
2020-12-19 17:37:48 +01:00
|
|
|
"github.com/jech/galene/conn"
|
|
|
|
"github.com/jech/galene/diskwriter"
|
|
|
|
"github.com/jech/galene/estimator"
|
|
|
|
"github.com/jech/galene/group"
|
2021-01-02 00:20:47 +01:00
|
|
|
"github.com/jech/galene/ice"
|
2023-03-31 20:45:30 +02:00
|
|
|
"github.com/jech/galene/token"
|
2023-12-09 16:56:33 +01:00
|
|
|
"github.com/jech/galene/unbounded"
|
2020-04-24 19:38:21 +02:00
|
|
|
)
|
2020-04-25 02:09:11 +02:00
|
|
|
|
2020-11-30 16:26:11 +01:00
|
|
|
func errorToWSCloseMessage(id string, err error) (*clientMessage, []byte) {
|
2020-04-24 19:38:21 +02:00
|
|
|
var code int
|
2020-11-30 16:26:11 +01:00
|
|
|
var m *clientMessage
|
2020-04-24 19:38:21 +02:00
|
|
|
var text string
|
|
|
|
switch e := err.(type) {
|
|
|
|
case *websocket.CloseError:
|
|
|
|
code = websocket.CloseNormalClosure
|
2020-09-13 11:56:35 +02:00
|
|
|
case group.ProtocolError:
|
2020-04-24 19:38:21 +02:00
|
|
|
code = websocket.CloseProtocolError
|
2020-11-30 16:26:11 +01:00
|
|
|
m = &clientMessage{
|
2020-12-19 17:38:47 +01:00
|
|
|
Type: "usermessage",
|
|
|
|
Kind: "error",
|
|
|
|
Dest: id,
|
|
|
|
Privileged: true,
|
2020-12-28 01:42:26 +01:00
|
|
|
Value: e.Error(),
|
2020-11-30 16:26:11 +01:00
|
|
|
}
|
|
|
|
text = e.Error()
|
|
|
|
case group.UserError, group.KickError:
|
2020-04-25 02:09:11 +02:00
|
|
|
code = websocket.CloseNormalClosure
|
2020-11-30 16:26:11 +01:00
|
|
|
m = errorMessage(id, err)
|
|
|
|
text = e.Error()
|
2020-04-24 19:38:21 +02:00
|
|
|
default:
|
|
|
|
code = websocket.CloseInternalServerErr
|
|
|
|
}
|
2020-11-30 16:26:11 +01:00
|
|
|
return m, websocket.FormatCloseMessage(code, text)
|
2020-04-24 19:38:21 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
func isWSNormalError(err error) bool {
|
|
|
|
return websocket.IsCloseError(err,
|
|
|
|
websocket.CloseNormalClosure,
|
|
|
|
websocket.CloseGoingAway)
|
|
|
|
}
|
|
|
|
|
2020-05-27 11:44:49 +02:00
|
|
|
type webClient struct {
|
2020-09-13 11:56:35 +02:00
|
|
|
group *group.Group
|
2024-04-30 18:17:00 +02:00
|
|
|
addr net.Addr
|
2020-05-27 11:44:49 +02:00
|
|
|
id string
|
2020-11-29 14:26:42 +01:00
|
|
|
username string
|
2022-02-19 23:43:44 +01:00
|
|
|
permissions []string
|
2022-01-29 22:54:44 +01:00
|
|
|
data map[string]interface{}
|
2021-04-28 17:00:50 +02:00
|
|
|
requested map[string][]string
|
2020-05-27 11:44:49 +02:00
|
|
|
done chan struct{}
|
|
|
|
writeCh chan interface{}
|
|
|
|
writerDone chan struct{}
|
2023-12-09 16:56:33 +01:00
|
|
|
actions *unbounded.Channel[any]
|
2020-05-27 11:44:49 +02:00
|
|
|
|
2021-12-11 01:33:47 +01:00
|
|
|
mu sync.Mutex
|
|
|
|
down map[string]*rtpDownConnection
|
|
|
|
up map[string]*rtpUpConnection
|
2020-05-27 11:44:49 +02:00
|
|
|
}
|
|
|
|
|
2020-09-13 11:56:35 +02:00
|
|
|
func (c *webClient) Group() *group.Group {
|
2020-05-28 02:35:09 +02:00
|
|
|
return c.group
|
|
|
|
}
|
|
|
|
|
2024-04-30 18:17:00 +02:00
|
|
|
func (c *webClient) Addr() net.Addr {
|
|
|
|
return c.addr
|
|
|
|
}
|
|
|
|
|
2020-06-08 22:14:28 +02:00
|
|
|
func (c *webClient) Id() string {
|
2020-05-28 02:35:09 +02:00
|
|
|
return c.id
|
|
|
|
}
|
|
|
|
|
2020-11-29 14:26:42 +01:00
|
|
|
func (c *webClient) Username() string {
|
|
|
|
return c.username
|
|
|
|
}
|
|
|
|
|
2022-02-20 15:32:18 +01:00
|
|
|
func (c *webClient) SetUsername(username string) {
|
|
|
|
c.username = username
|
|
|
|
}
|
|
|
|
|
2022-02-19 23:43:44 +01:00
|
|
|
func (c *webClient) Permissions() []string {
|
2021-01-14 03:56:37 +01:00
|
|
|
return c.permissions
|
|
|
|
}
|
|
|
|
|
2022-01-29 22:54:44 +01:00
|
|
|
func (c *webClient) Data() map[string]interface{} {
|
|
|
|
return c.data
|
2021-04-28 14:45:45 +02:00
|
|
|
}
|
|
|
|
|
2022-02-19 23:43:44 +01:00
|
|
|
func (c *webClient) SetPermissions(perms []string) {
|
2020-09-13 10:16:10 +02:00
|
|
|
c.permissions = perms
|
|
|
|
}
|
|
|
|
|
2023-03-22 17:41:16 +01:00
|
|
|
func (c *webClient) PushClient(group, kind, id string, username string, perms []string, data map[string]interface{}) error {
|
2023-12-09 16:56:33 +01:00
|
|
|
c.action(pushClientAction{
|
2022-02-19 23:43:44 +01:00
|
|
|
group, kind, id, username, perms, data,
|
2020-05-31 20:41:17 +02:00
|
|
|
})
|
2023-12-09 16:56:33 +01:00
|
|
|
return nil
|
2020-05-31 20:41:17 +02:00
|
|
|
}
|
|
|
|
|
2020-04-24 19:38:21 +02:00
|
|
|
type clientMessage struct {
|
2021-01-03 17:00:58 +01:00
|
|
|
Type string `json:"type"`
|
2023-04-29 19:59:36 +02:00
|
|
|
Version []string `json:"version,omitempty"`
|
2021-01-03 17:00:58 +01:00
|
|
|
Kind string `json:"kind,omitempty"`
|
2022-12-16 18:56:36 +01:00
|
|
|
Error string `json:"error,omitempty"`
|
2021-01-03 17:00:58 +01:00
|
|
|
Id string `json:"id,omitempty"`
|
2021-01-31 19:00:09 +01:00
|
|
|
Replace string `json:"replace,omitempty"`
|
2021-01-03 17:00:58 +01:00
|
|
|
Source string `json:"source,omitempty"`
|
|
|
|
Dest string `json:"dest,omitempty"`
|
2023-03-22 17:41:16 +01:00
|
|
|
Username *string `json:"username,omitempty"`
|
2021-01-03 17:00:58 +01:00
|
|
|
Password string `json:"password,omitempty"`
|
2021-10-29 23:37:05 +02:00
|
|
|
Token string `json:"token,omitempty"`
|
2021-01-03 17:00:58 +01:00
|
|
|
Privileged bool `json:"privileged,omitempty"`
|
2022-02-19 23:43:44 +01:00
|
|
|
Permissions []string `json:"permissions,omitempty"`
|
2022-01-29 22:54:44 +01:00
|
|
|
Status *group.Status `json:"status,omitempty"`
|
|
|
|
Data map[string]interface{} `json:"data,omitempty"`
|
2021-01-03 17:00:58 +01:00
|
|
|
Group string `json:"group,omitempty"`
|
|
|
|
Value interface{} `json:"value,omitempty"`
|
2021-01-11 16:22:04 +01:00
|
|
|
NoEcho bool `json:"noecho,omitempty"`
|
2022-12-16 18:56:36 +01:00
|
|
|
Time string `json:"time,omitempty"`
|
2021-01-03 17:00:58 +01:00
|
|
|
SDP string `json:"sdp,omitempty"`
|
|
|
|
Candidate *webrtc.ICECandidateInit `json:"candidate,omitempty"`
|
2021-04-28 17:00:50 +02:00
|
|
|
Label string `json:"label,omitempty"`
|
2021-05-14 18:39:39 +02:00
|
|
|
Request interface{} `json:"request,omitempty"`
|
2021-01-03 17:00:58 +01:00
|
|
|
RTCConfiguration *webrtc.Configuration `json:"rtcConfiguration,omitempty"`
|
2020-04-24 19:38:21 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
type closeMessage struct {
|
|
|
|
data []byte
|
|
|
|
}
|
|
|
|
|
2020-06-08 22:14:28 +02:00
|
|
|
func getUpConn(c *webClient, id string) *rtpUpConnection {
|
2020-04-28 14:54:50 +02:00
|
|
|
c.mu.Lock()
|
|
|
|
defer c.mu.Unlock()
|
2020-04-24 19:38:21 +02:00
|
|
|
|
|
|
|
if c.up == nil {
|
|
|
|
return nil
|
|
|
|
}
|
2020-06-08 22:14:28 +02:00
|
|
|
return c.up[id]
|
2020-04-24 19:38:21 +02:00
|
|
|
}
|
|
|
|
|
2020-06-08 22:14:28 +02:00
|
|
|
func getUpConns(c *webClient) []*rtpUpConnection {
|
2020-04-28 14:54:50 +02:00
|
|
|
c.mu.Lock()
|
|
|
|
defer c.mu.Unlock()
|
2020-06-08 22:14:28 +02:00
|
|
|
up := make([]*rtpUpConnection, 0, len(c.up))
|
2020-06-08 19:10:08 +02:00
|
|
|
for _, u := range c.up {
|
|
|
|
up = append(up, u)
|
2020-04-25 18:35:32 +02:00
|
|
|
}
|
|
|
|
return up
|
|
|
|
}
|
|
|
|
|
2021-04-28 17:00:50 +02:00
|
|
|
func addUpConn(c *webClient, id, label string, offer string) (*rtpUpConnection, bool, error) {
|
2020-04-28 23:41:18 +02:00
|
|
|
c.mu.Lock()
|
|
|
|
defer c.mu.Unlock()
|
|
|
|
|
|
|
|
if c.up == nil {
|
2020-06-08 22:14:28 +02:00
|
|
|
c.up = make(map[string]*rtpUpConnection)
|
2020-04-28 23:41:18 +02:00
|
|
|
}
|
2020-06-10 20:25:25 +02:00
|
|
|
if c.down != nil && c.down[id] != nil {
|
2022-04-23 18:22:28 +02:00
|
|
|
return nil, false, errors.New("adding duplicate connection")
|
2020-04-28 23:41:18 +02:00
|
|
|
}
|
2020-06-10 20:25:25 +02:00
|
|
|
|
|
|
|
old := c.up[id]
|
|
|
|
if old != nil {
|
2020-08-06 23:55:00 +02:00
|
|
|
return old, false, nil
|
|
|
|
}
|
|
|
|
|
2021-04-28 17:00:50 +02:00
|
|
|
conn, err := newUpConn(c, id, label, offer)
|
2020-08-06 23:55:00 +02:00
|
|
|
if err != nil {
|
|
|
|
return nil, false, err
|
2020-06-10 20:25:25 +02:00
|
|
|
}
|
|
|
|
|
2020-04-28 23:41:18 +02:00
|
|
|
c.up[id] = conn
|
|
|
|
|
2020-06-08 22:14:28 +02:00
|
|
|
conn.pc.OnICECandidate(func(candidate *webrtc.ICECandidate) {
|
2020-04-24 19:38:21 +02:00
|
|
|
sendICE(c, id, candidate)
|
|
|
|
})
|
|
|
|
|
2020-08-11 15:13:30 +02:00
|
|
|
conn.pc.OnICEConnectionStateChange(func(state webrtc.ICEConnectionState) {
|
|
|
|
if state == webrtc.ICEConnectionStateFailed {
|
|
|
|
c.action(connectionFailedAction{id: id})
|
|
|
|
}
|
|
|
|
})
|
|
|
|
|
2020-08-06 23:55:00 +02:00
|
|
|
return conn, true, nil
|
2020-04-29 01:57:37 +02:00
|
|
|
}
|
2020-04-24 19:38:21 +02:00
|
|
|
|
2021-01-31 19:00:09 +01:00
|
|
|
var ErrUserMismatch = errors.New("user id mismatch")
|
|
|
|
|
2021-02-01 14:08:54 +01:00
|
|
|
// delUpConn deletes an up connection. If push is closed, the close is
|
|
|
|
// pushed to all corresponding down connections.
|
|
|
|
func delUpConn(c *webClient, id string, userId string, push bool) error {
|
2020-04-28 14:54:50 +02:00
|
|
|
c.mu.Lock()
|
2020-04-24 19:38:21 +02:00
|
|
|
if c.up == nil {
|
2020-06-08 19:10:08 +02:00
|
|
|
c.mu.Unlock()
|
2021-02-01 14:08:54 +01:00
|
|
|
return os.ErrNotExist
|
2020-04-24 19:38:21 +02:00
|
|
|
}
|
|
|
|
conn := c.up[id]
|
|
|
|
if conn == nil {
|
2020-06-08 19:10:08 +02:00
|
|
|
c.mu.Unlock()
|
2021-02-01 14:08:54 +01:00
|
|
|
return os.ErrNotExist
|
2020-04-24 19:38:21 +02:00
|
|
|
}
|
2021-09-16 16:53:44 +02:00
|
|
|
if userId != "" {
|
|
|
|
id, _ := conn.User()
|
|
|
|
if id != userId {
|
|
|
|
c.mu.Unlock()
|
|
|
|
return ErrUserMismatch
|
|
|
|
}
|
2021-01-31 19:00:09 +01:00
|
|
|
}
|
|
|
|
|
2021-07-15 18:05:31 +02:00
|
|
|
replace := conn.getReplace(false)
|
2021-01-31 19:00:09 +01:00
|
|
|
|
2020-06-08 19:10:08 +02:00
|
|
|
delete(c.up, id)
|
2021-02-01 14:08:54 +01:00
|
|
|
g := c.group
|
2020-06-08 19:10:08 +02:00
|
|
|
c.mu.Unlock()
|
2020-04-24 19:38:21 +02:00
|
|
|
|
2021-07-15 15:58:38 +02:00
|
|
|
conn.mu.Lock()
|
|
|
|
conn.closed = true
|
|
|
|
conn.mu.Unlock()
|
|
|
|
|
2021-02-01 14:08:54 +01:00
|
|
|
conn.pc.Close()
|
|
|
|
|
|
|
|
if push && g != nil {
|
2021-02-14 17:24:42 +01:00
|
|
|
for _, c := range g.GetClients(c) {
|
|
|
|
err := c.PushConn(g, id, nil, nil, replace)
|
|
|
|
if err != nil {
|
|
|
|
log.Printf("PushConn: %v", err)
|
2020-12-01 22:42:06 +01:00
|
|
|
}
|
2021-02-14 17:24:42 +01:00
|
|
|
}
|
2020-12-01 22:42:06 +01:00
|
|
|
}
|
2020-06-10 20:25:25 +02:00
|
|
|
|
2021-02-01 14:08:54 +01:00
|
|
|
return nil
|
2020-06-10 20:25:25 +02:00
|
|
|
}
|
|
|
|
|
2020-05-27 11:44:49 +02:00
|
|
|
func getDownConn(c *webClient, id string) *rtpDownConnection {
|
2020-04-24 19:38:21 +02:00
|
|
|
if c.down == nil {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2020-04-28 14:54:50 +02:00
|
|
|
c.mu.Lock()
|
|
|
|
defer c.mu.Unlock()
|
2020-04-24 19:38:21 +02:00
|
|
|
conn := c.down[id]
|
|
|
|
if conn == nil {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
return conn
|
|
|
|
}
|
|
|
|
|
2020-05-27 11:44:49 +02:00
|
|
|
func getConn(c *webClient, id string) iceConnection {
|
2020-05-22 22:36:47 +02:00
|
|
|
up := getUpConn(c, id)
|
|
|
|
if up != nil {
|
|
|
|
return up
|
|
|
|
}
|
|
|
|
down := getDownConn(c, id)
|
|
|
|
if down != nil {
|
|
|
|
return down
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2021-02-02 19:38:15 +01:00
|
|
|
func addDownConn(c *webClient, remote conn.Up) (*rtpDownConnection, bool, error) {
|
|
|
|
id := remote.Id()
|
2020-10-01 15:48:00 +02:00
|
|
|
|
2020-06-08 22:14:28 +02:00
|
|
|
c.mu.Lock()
|
|
|
|
defer c.mu.Unlock()
|
2020-04-24 19:38:21 +02:00
|
|
|
|
2021-02-02 19:38:15 +01:00
|
|
|
if c.up != nil && c.up[id] != nil {
|
|
|
|
return nil, false, errors.New("adding duplicate connection")
|
2020-06-10 20:25:25 +02:00
|
|
|
}
|
|
|
|
|
2020-04-24 19:38:21 +02:00
|
|
|
if c.down == nil {
|
2020-05-26 17:44:21 +02:00
|
|
|
c.down = make(map[string]*rtpDownConnection)
|
2020-04-24 19:38:21 +02:00
|
|
|
}
|
2020-05-23 02:22:43 +02:00
|
|
|
|
2021-02-02 19:38:15 +01:00
|
|
|
if down := c.down[id]; down != nil {
|
|
|
|
return down, false, nil
|
2020-04-24 19:38:21 +02:00
|
|
|
}
|
2020-06-08 22:14:28 +02:00
|
|
|
|
2021-02-02 19:38:15 +01:00
|
|
|
down, err := newDownConn(c, id, remote)
|
|
|
|
if err != nil {
|
|
|
|
return nil, false, err
|
|
|
|
}
|
|
|
|
|
|
|
|
down.pc.OnICECandidate(func(candidate *webrtc.ICECandidate) {
|
|
|
|
sendICE(c, down.id, candidate)
|
2020-06-08 22:14:28 +02:00
|
|
|
})
|
|
|
|
|
2021-02-02 19:38:15 +01:00
|
|
|
down.pc.OnICEConnectionStateChange(func(state webrtc.ICEConnectionState) {
|
2020-06-11 20:22:28 +02:00
|
|
|
if state == webrtc.ICEConnectionStateFailed {
|
2021-02-02 19:38:15 +01:00
|
|
|
c.action(connectionFailedAction{id: down.id})
|
2020-06-11 20:22:28 +02:00
|
|
|
}
|
|
|
|
})
|
|
|
|
|
2021-02-02 19:38:15 +01:00
|
|
|
err = remote.AddLocal(down)
|
2020-05-30 03:36:15 +02:00
|
|
|
if err != nil {
|
2021-02-02 19:38:15 +01:00
|
|
|
down.pc.Close()
|
|
|
|
return nil, false, err
|
2020-05-30 03:36:15 +02:00
|
|
|
}
|
2020-05-23 02:22:43 +02:00
|
|
|
|
2021-02-02 19:38:15 +01:00
|
|
|
c.down[down.id] = down
|
|
|
|
|
|
|
|
go rtcpDownSender(down)
|
|
|
|
|
|
|
|
return down, true, nil
|
2020-04-24 19:38:21 +02:00
|
|
|
}
|
|
|
|
|
2021-01-31 19:00:09 +01:00
|
|
|
func delDownConn(c *webClient, id string) error {
|
2020-10-01 15:48:00 +02:00
|
|
|
conn := delDownConnHelper(c, id)
|
|
|
|
if conn != nil {
|
|
|
|
conn.pc.Close()
|
2021-01-31 19:00:09 +01:00
|
|
|
return nil
|
2020-10-01 15:48:00 +02:00
|
|
|
}
|
2021-01-31 19:00:09 +01:00
|
|
|
return os.ErrNotExist
|
2020-10-01 15:48:00 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
func delDownConnHelper(c *webClient, id string) *rtpDownConnection {
|
2020-04-28 14:54:50 +02:00
|
|
|
c.mu.Lock()
|
|
|
|
defer c.mu.Unlock()
|
2020-04-24 19:38:21 +02:00
|
|
|
|
|
|
|
if c.down == nil {
|
2020-10-01 15:48:00 +02:00
|
|
|
return nil
|
2020-04-24 19:38:21 +02:00
|
|
|
}
|
2020-04-25 02:25:51 +02:00
|
|
|
conn := c.down[id]
|
2020-04-24 19:38:21 +02:00
|
|
|
if conn == nil {
|
2020-10-01 15:48:00 +02:00
|
|
|
return nil
|
2020-04-24 19:38:21 +02:00
|
|
|
}
|
2020-04-28 14:54:50 +02:00
|
|
|
|
2020-09-13 11:04:16 +02:00
|
|
|
conn.remote.DelLocal(conn)
|
2020-04-28 14:54:50 +02:00
|
|
|
for _, track := range conn.tracks {
|
2020-05-30 12:33:30 +02:00
|
|
|
// we only insert the track after we get an answer, so
|
|
|
|
// ignore errors here.
|
2020-09-13 11:04:16 +02:00
|
|
|
track.remote.DelLocal(track)
|
2020-04-28 14:54:50 +02:00
|
|
|
}
|
2020-04-24 19:38:21 +02:00
|
|
|
delete(c.down, id)
|
2020-10-01 15:48:00 +02:00
|
|
|
return conn
|
2020-04-24 19:38:21 +02:00
|
|
|
}
|
|
|
|
|
2021-02-02 19:38:15 +01:00
|
|
|
var errUnexpectedTrackType = errors.New("unexpected track type, this shouldn't happen")
|
2021-02-02 19:06:37 +01:00
|
|
|
|
2021-08-03 02:51:44 +02:00
|
|
|
func addDownTrackUnlocked(conn *rtpDownConnection, remoteTrack *rtpUpTrack) error {
|
2021-02-02 19:06:37 +01:00
|
|
|
for _, t := range conn.tracks {
|
|
|
|
tt, ok := t.remote.(*rtpUpTrack)
|
|
|
|
if !ok {
|
2021-02-02 19:38:15 +01:00
|
|
|
return errUnexpectedTrackType
|
2021-02-02 19:06:37 +01:00
|
|
|
}
|
2021-02-02 19:38:15 +01:00
|
|
|
if tt == remoteTrack {
|
|
|
|
return os.ErrExist
|
2021-02-02 19:06:37 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-05-13 03:45:06 +02:00
|
|
|
id := remoteTrack.track.ID()
|
|
|
|
if id == "" {
|
|
|
|
log.Println("Got track with empty id")
|
|
|
|
id = remoteTrack.track.RID()
|
|
|
|
}
|
|
|
|
if id == "" {
|
|
|
|
id = remoteTrack.track.Kind().String()
|
|
|
|
}
|
|
|
|
msid := remoteTrack.track.StreamID()
|
2021-09-16 17:17:36 +02:00
|
|
|
if msid == "" || msid == "-" {
|
2021-05-13 03:45:06 +02:00
|
|
|
log.Println("Got track with empty msid")
|
2021-08-03 02:51:44 +02:00
|
|
|
msid = remoteTrack.conn.Label()
|
2021-05-13 03:45:06 +02:00
|
|
|
}
|
|
|
|
if msid == "" {
|
|
|
|
msid = "dummy"
|
|
|
|
}
|
|
|
|
|
2024-11-30 17:55:24 +01:00
|
|
|
// replace the RTCP feedback types with the ones we understand
|
|
|
|
remoteCodec := remoteTrack.Codec()
|
|
|
|
if strings.HasPrefix(strings.ToLower(remoteCodec.MimeType), "video/") {
|
|
|
|
remoteCodec.RTCPFeedback = group.VideoRTCPFeedback
|
|
|
|
} else {
|
|
|
|
remoteCodec.RTCPFeedback = group.AudioRTCPFeedback
|
|
|
|
}
|
|
|
|
|
2020-12-04 01:15:52 +01:00
|
|
|
local, err := webrtc.NewTrackLocalStaticRTP(
|
2024-11-30 17:55:24 +01:00
|
|
|
remoteCodec, id, msid,
|
2020-12-04 01:15:52 +01:00
|
|
|
)
|
2020-04-24 19:38:21 +02:00
|
|
|
if err != nil {
|
2021-02-02 19:38:15 +01:00
|
|
|
return err
|
2020-04-24 19:38:21 +02:00
|
|
|
}
|
|
|
|
|
2021-08-01 13:33:01 +02:00
|
|
|
transceiver, err := conn.pc.AddTransceiverFromTrack(local,
|
|
|
|
webrtc.RTPTransceiverInit{
|
|
|
|
Direction: webrtc.RTPTransceiverDirectionSendonly,
|
|
|
|
},
|
|
|
|
)
|
2020-04-28 14:54:50 +02:00
|
|
|
if err != nil {
|
2021-02-02 19:38:15 +01:00
|
|
|
return err
|
2020-04-28 14:54:50 +02:00
|
|
|
}
|
2020-04-26 01:33:18 +02:00
|
|
|
|
2021-08-02 15:53:33 +02:00
|
|
|
codec := local.Codec()
|
|
|
|
ptype, err := group.CodecPayloadType(local.Codec())
|
|
|
|
if err != nil {
|
|
|
|
log.Printf("Couldn't determine ptype for codec %v: %v",
|
|
|
|
codec.MimeType, err)
|
|
|
|
} else {
|
|
|
|
err := transceiver.SetCodecPreferences(
|
|
|
|
[]webrtc.RTPCodecParameters{
|
|
|
|
{
|
|
|
|
RTPCodecCapability: codec,
|
|
|
|
PayloadType: ptype,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
)
|
|
|
|
if err != nil {
|
|
|
|
log.Printf("Couldn't set ptype for codec %v: %v",
|
|
|
|
codec.MimeType, err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-08-01 13:33:01 +02:00
|
|
|
parms := transceiver.Sender().GetParameters()
|
2020-12-04 01:15:52 +01:00
|
|
|
if len(parms.Encodings) != 1 {
|
2021-02-02 19:38:15 +01:00
|
|
|
return errors.New("got multiple encodings")
|
2020-12-04 01:15:52 +01:00
|
|
|
}
|
|
|
|
|
2020-05-26 17:44:21 +02:00
|
|
|
track := &rtpDownTrack{
|
2021-04-29 17:03:25 +02:00
|
|
|
track: local,
|
2021-08-01 13:33:01 +02:00
|
|
|
sender: transceiver.Sender(),
|
2021-04-29 17:03:25 +02:00
|
|
|
ssrc: parms.Encodings[0].SSRC,
|
2021-07-14 14:05:23 +02:00
|
|
|
conn: conn,
|
2021-04-29 17:03:25 +02:00
|
|
|
remote: remoteTrack,
|
|
|
|
maxBitrate: new(bitrate),
|
|
|
|
maxREMBBitrate: new(bitrate),
|
|
|
|
stats: new(receiverStats),
|
|
|
|
rate: estimator.New(time.Second),
|
|
|
|
atomics: &downTrackAtomics{},
|
2020-04-28 15:26:50 +02:00
|
|
|
}
|
2021-01-19 23:55:33 +01:00
|
|
|
|
2020-04-28 14:54:50 +02:00
|
|
|
conn.tracks = append(conn.tracks, track)
|
|
|
|
|
2021-08-01 13:33:01 +02:00
|
|
|
go rtcpDownListener(track)
|
2020-04-24 19:38:21 +02:00
|
|
|
|
2021-02-02 19:38:15 +01:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func delDownTrackUnlocked(conn *rtpDownConnection, track *rtpDownTrack) error {
|
|
|
|
for i := range conn.tracks {
|
|
|
|
if conn.tracks[i] == track {
|
|
|
|
track.remote.DelLocal(track)
|
|
|
|
conn.tracks =
|
|
|
|
append(conn.tracks[:i], conn.tracks[i+1:]...)
|
|
|
|
return conn.pc.RemoveTrack(track.sender)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return os.ErrNotExist
|
|
|
|
}
|
|
|
|
|
2021-08-03 13:10:55 +02:00
|
|
|
func replaceTracks(conn *rtpDownConnection, remote []conn.UpTrack, limitSid bool) (bool, error) {
|
2021-02-02 19:38:15 +01:00
|
|
|
conn.mu.Lock()
|
|
|
|
defer conn.mu.Unlock()
|
|
|
|
|
|
|
|
var add []*rtpUpTrack
|
|
|
|
var del []*rtpDownTrack
|
|
|
|
|
|
|
|
outer:
|
|
|
|
for _, rtrack := range remote {
|
|
|
|
rt, ok := rtrack.(*rtpUpTrack)
|
|
|
|
if !ok {
|
2021-05-14 18:19:44 +02:00
|
|
|
return false, errUnexpectedTrackType
|
2021-02-02 19:38:15 +01:00
|
|
|
}
|
|
|
|
for _, track := range conn.tracks {
|
|
|
|
rt2, ok := track.remote.(*rtpUpTrack)
|
|
|
|
if !ok {
|
2021-05-14 18:19:44 +02:00
|
|
|
return false, errUnexpectedTrackType
|
2021-02-02 19:38:15 +01:00
|
|
|
}
|
|
|
|
if rt == rt2 {
|
|
|
|
continue outer
|
|
|
|
}
|
|
|
|
}
|
|
|
|
add = append(add, rt)
|
|
|
|
}
|
|
|
|
|
|
|
|
outer2:
|
|
|
|
for _, track := range conn.tracks {
|
|
|
|
rt, ok := track.remote.(*rtpUpTrack)
|
|
|
|
if !ok {
|
2021-05-14 18:19:44 +02:00
|
|
|
return false, errUnexpectedTrackType
|
2021-02-02 19:38:15 +01:00
|
|
|
}
|
|
|
|
for _, rtrack := range remote {
|
|
|
|
rt2, ok := rtrack.(*rtpUpTrack)
|
|
|
|
if !ok {
|
2021-05-14 18:19:44 +02:00
|
|
|
return false, errUnexpectedTrackType
|
2021-02-02 19:38:15 +01:00
|
|
|
}
|
|
|
|
if rt == rt2 {
|
|
|
|
continue outer2
|
|
|
|
}
|
|
|
|
}
|
|
|
|
del = append(del, track)
|
|
|
|
}
|
|
|
|
|
2021-08-03 13:10:55 +02:00
|
|
|
defer func() {
|
|
|
|
for _, t := range conn.tracks {
|
|
|
|
layer := t.getLayerInfo()
|
|
|
|
layer.limitSid = limitSid
|
|
|
|
if limitSid {
|
|
|
|
layer.wantedSid = 0
|
|
|
|
}
|
|
|
|
t.setLayerInfo(layer)
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
|
2021-05-14 18:19:44 +02:00
|
|
|
if len(del) == 0 && len(add) == 0 {
|
|
|
|
return false, nil
|
|
|
|
}
|
|
|
|
|
2021-02-02 19:38:15 +01:00
|
|
|
for _, t := range del {
|
|
|
|
err := delDownTrackUnlocked(conn, t)
|
|
|
|
if err != nil {
|
2021-05-14 18:19:44 +02:00
|
|
|
return false, err
|
2021-02-02 19:38:15 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, rt := range add {
|
2021-08-03 02:51:44 +02:00
|
|
|
err := addDownTrackUnlocked(conn, rt)
|
2021-02-02 19:38:15 +01:00
|
|
|
if err != nil {
|
2021-05-14 18:19:44 +02:00
|
|
|
return false, err
|
2021-02-02 19:38:15 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-05-14 18:19:44 +02:00
|
|
|
return true, nil
|
2020-04-24 19:38:21 +02:00
|
|
|
}
|
|
|
|
|
2021-01-31 19:00:09 +01:00
|
|
|
func negotiate(c *webClient, down *rtpDownConnection, restartIce bool, replace string) error {
|
2021-01-12 20:44:48 +01:00
|
|
|
if down.pc.SignalingState() == webrtc.SignalingStateHaveLocalOffer {
|
|
|
|
// avoid sending multiple offers back-to-back
|
|
|
|
if restartIce {
|
|
|
|
down.negotiationNeeded = negotiationRestartIce
|
|
|
|
} else if down.negotiationNeeded == negotiationUnneeded {
|
|
|
|
down.negotiationNeeded = negotiationNeeded
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
down.negotiationNeeded = negotiationUnneeded
|
|
|
|
|
2020-07-16 20:51:51 +02:00
|
|
|
options := webrtc.OfferOptions{ICERestart: restartIce}
|
|
|
|
offer, err := down.pc.CreateOffer(&options)
|
2020-04-24 19:38:21 +02:00
|
|
|
if err != nil {
|
2020-05-17 22:31:29 +02:00
|
|
|
return err
|
2020-04-24 19:38:21 +02:00
|
|
|
}
|
|
|
|
|
2020-05-17 22:31:29 +02:00
|
|
|
err = down.pc.SetLocalDescription(offer)
|
2020-04-24 19:38:21 +02:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2020-05-17 22:31:29 +02:00
|
|
|
|
2021-01-03 12:04:39 +01:00
|
|
|
source, username := down.remote.User()
|
|
|
|
|
2020-04-24 19:38:21 +02:00
|
|
|
return c.write(clientMessage{
|
2021-01-03 12:04:39 +01:00
|
|
|
Type: "offer",
|
|
|
|
Id: down.id,
|
2021-04-28 17:00:50 +02:00
|
|
|
Label: down.remote.Label(),
|
2021-01-31 19:00:09 +01:00
|
|
|
Replace: replace,
|
2021-01-03 12:04:39 +01:00
|
|
|
Source: source,
|
2023-03-22 17:41:16 +01:00
|
|
|
Username: &username,
|
2021-01-11 20:27:39 +01:00
|
|
|
SDP: down.pc.LocalDescription().SDP,
|
2020-04-24 19:38:21 +02:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2020-05-27 11:44:49 +02:00
|
|
|
func sendICE(c *webClient, id string, candidate *webrtc.ICECandidate) error {
|
2020-04-24 19:38:21 +02:00
|
|
|
if candidate == nil {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
cand := candidate.ToJSON()
|
|
|
|
return c.write(clientMessage{
|
|
|
|
Type: "ice",
|
|
|
|
Id: id,
|
|
|
|
Candidate: &cand,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2021-04-28 17:00:50 +02:00
|
|
|
func gotOffer(c *webClient, id, label string, sdp string, replace string) error {
|
|
|
|
up, _, err := addUpConn(c, id, label, sdp)
|
2020-06-10 20:25:25 +02:00
|
|
|
if err != nil {
|
|
|
|
return err
|
2020-04-24 19:38:21 +02:00
|
|
|
}
|
2020-08-06 23:55:00 +02:00
|
|
|
|
2021-02-01 14:08:54 +01:00
|
|
|
if replace != "" {
|
|
|
|
up.replace = replace
|
|
|
|
delUpConn(c, replace, c.Id(), false)
|
|
|
|
}
|
2021-01-03 12:04:39 +01:00
|
|
|
|
2021-01-03 17:00:58 +01:00
|
|
|
err = up.pc.SetRemoteDescription(webrtc.SessionDescription{
|
|
|
|
Type: webrtc.SDPTypeOffer,
|
|
|
|
SDP: sdp,
|
|
|
|
})
|
2020-04-24 19:38:21 +02:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
answer, err := up.pc.CreateAnswer(nil)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
err = up.pc.SetLocalDescription(answer)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2020-05-22 23:07:38 +02:00
|
|
|
err = up.flushICECandidates()
|
|
|
|
if err != nil {
|
|
|
|
log.Printf("ICE: %v", err)
|
|
|
|
}
|
|
|
|
|
2020-04-24 19:38:21 +02:00
|
|
|
return c.write(clientMessage{
|
2021-01-03 17:00:58 +01:00
|
|
|
Type: "answer",
|
|
|
|
Id: id,
|
2021-01-11 20:27:39 +01:00
|
|
|
SDP: up.pc.LocalDescription().SDP,
|
2020-04-24 19:38:21 +02:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2020-10-01 13:47:48 +02:00
|
|
|
var ErrUnknownId = errors.New("unknown id")
|
|
|
|
|
2021-01-03 17:00:58 +01:00
|
|
|
func gotAnswer(c *webClient, id string, sdp string) error {
|
2020-05-22 23:07:38 +02:00
|
|
|
down := getDownConn(c, id)
|
|
|
|
if down == nil {
|
2020-10-01 13:47:48 +02:00
|
|
|
return ErrUnknownId
|
2020-04-24 19:38:21 +02:00
|
|
|
}
|
2021-01-19 23:30:01 +01:00
|
|
|
|
2021-02-02 22:57:56 +01:00
|
|
|
err := down.pc.SetRemoteDescription(webrtc.SessionDescription{
|
|
|
|
Type: webrtc.SDPTypeAnswer,
|
|
|
|
SDP: sdp,
|
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
return err
|
2020-04-24 19:38:21 +02:00
|
|
|
}
|
2020-05-21 00:55:00 +02:00
|
|
|
|
2021-02-02 22:57:56 +01:00
|
|
|
err = down.flushICECandidates()
|
2020-05-22 23:07:38 +02:00
|
|
|
if err != nil {
|
|
|
|
log.Printf("ICE: %v", err)
|
|
|
|
}
|
|
|
|
|
2020-10-06 02:41:18 +02:00
|
|
|
add := func() {
|
|
|
|
down.pc.OnConnectionStateChange(nil)
|
|
|
|
for _, t := range down.tracks {
|
2021-07-15 15:58:38 +02:00
|
|
|
err := t.remote.AddLocal(t)
|
|
|
|
if err != nil && err != os.ErrClosed {
|
|
|
|
log.Printf("Add track: %v", err)
|
|
|
|
}
|
2020-10-06 02:41:18 +02:00
|
|
|
}
|
2020-05-21 00:55:00 +02:00
|
|
|
}
|
2020-10-06 02:41:18 +02:00
|
|
|
down.pc.OnConnectionStateChange(func(state webrtc.PeerConnectionState) {
|
|
|
|
if state == webrtc.PeerConnectionStateConnected {
|
|
|
|
add()
|
|
|
|
}
|
|
|
|
})
|
|
|
|
if down.pc.ConnectionState() == webrtc.PeerConnectionStateConnected {
|
|
|
|
add()
|
|
|
|
}
|
|
|
|
|
2020-04-24 19:38:21 +02:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2020-05-27 11:44:49 +02:00
|
|
|
func gotICE(c *webClient, candidate *webrtc.ICECandidateInit, id string) error {
|
2020-05-22 22:36:47 +02:00
|
|
|
conn := getConn(c, id)
|
|
|
|
if conn == nil {
|
|
|
|
return errors.New("unknown id in ICE")
|
2020-04-24 19:38:21 +02:00
|
|
|
}
|
2020-05-22 23:07:38 +02:00
|
|
|
return conn.addICECandidate(candidate)
|
2020-04-24 19:38:21 +02:00
|
|
|
}
|
|
|
|
|
2021-05-14 18:39:39 +02:00
|
|
|
var errBadType = errors.New("bad type")
|
|
|
|
|
|
|
|
func toStringArray(r interface{}) ([]string, error) {
|
|
|
|
if r == nil {
|
|
|
|
return nil, nil
|
|
|
|
}
|
|
|
|
rr, ok := r.([]interface{})
|
|
|
|
if !ok {
|
|
|
|
return nil, errBadType
|
|
|
|
}
|
|
|
|
if rr == nil {
|
|
|
|
return nil, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
rrr := make([]string, len(rr))
|
|
|
|
for i, s := range rr {
|
|
|
|
rrr[i], ok = s.(string)
|
|
|
|
if !ok {
|
|
|
|
return nil, errBadType
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return rrr, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func parseRequested(r interface{}) (map[string][]string, error) {
|
|
|
|
if r == nil {
|
|
|
|
return nil, nil
|
|
|
|
}
|
|
|
|
rr, ok := r.(map[string]interface{})
|
|
|
|
if !ok {
|
|
|
|
return nil, errBadType
|
|
|
|
}
|
|
|
|
if rr == nil {
|
|
|
|
return nil, nil
|
|
|
|
}
|
|
|
|
rrr := make(map[string][]string)
|
|
|
|
for k, v := range rr {
|
|
|
|
vv, err := toStringArray(v)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
rrr[k] = vv
|
|
|
|
}
|
|
|
|
return rrr, nil
|
|
|
|
}
|
|
|
|
|
2021-04-28 17:00:50 +02:00
|
|
|
func (c *webClient) setRequested(requested map[string][]string) error {
|
2021-04-15 12:07:05 +02:00
|
|
|
if c.group == nil {
|
|
|
|
return errors.New("attempted to request with no group joined")
|
|
|
|
}
|
2020-05-17 22:31:29 +02:00
|
|
|
c.requested = requested
|
2020-05-09 19:39:34 +02:00
|
|
|
|
2021-05-14 19:20:56 +02:00
|
|
|
requestConns(c, c.group, "")
|
2020-05-09 19:39:34 +02:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2021-05-14 18:39:39 +02:00
|
|
|
func (c *webClient) setRequestedStream(down *rtpDownConnection, requested []string) error {
|
|
|
|
var remoteClient group.Client
|
|
|
|
remote, ok := down.remote.(*rtpUpConnection)
|
|
|
|
if ok {
|
|
|
|
remoteClient = remote.client
|
|
|
|
}
|
|
|
|
down.requested = requested
|
|
|
|
return remoteClient.RequestConns(c, c.group, remote.id)
|
|
|
|
}
|
|
|
|
|
2021-05-14 19:20:56 +02:00
|
|
|
func (c *webClient) RequestConns(target group.Client, g *group.Group, id string) error {
|
2023-12-09 16:56:33 +01:00
|
|
|
c.action(requestConnsAction{g, target, id})
|
|
|
|
return nil
|
2021-05-14 19:20:56 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
func requestConns(target group.Client, g *group.Group, id string) {
|
|
|
|
clients := g.GetClients(target)
|
|
|
|
for _, c := range clients {
|
|
|
|
c.RequestConns(target, g, id)
|
2020-05-30 00:23:54 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-04-06 12:37:23 +02:00
|
|
|
func requestedTracks(c *webClient, requested []string, tracks []conn.UpTrack) ([]conn.UpTrack, bool) {
|
|
|
|
if len(requested) == 0 {
|
|
|
|
return nil, false
|
2021-04-28 17:00:50 +02:00
|
|
|
}
|
2021-04-29 17:03:25 +02:00
|
|
|
var audio, video, videoLow bool
|
2023-04-06 12:37:23 +02:00
|
|
|
for _, s := range requested {
|
2021-04-28 17:00:50 +02:00
|
|
|
switch s {
|
|
|
|
case "audio":
|
|
|
|
audio = true
|
|
|
|
case "video":
|
|
|
|
video = true
|
2021-04-29 17:03:25 +02:00
|
|
|
case "video-low":
|
|
|
|
videoLow = true
|
2021-04-28 17:00:50 +02:00
|
|
|
default:
|
|
|
|
log.Printf("client requested unknown value %v", s)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-04-06 12:37:23 +02:00
|
|
|
find := func(kind webrtc.RTPCodecType, last bool) (conn.UpTrack, int) {
|
|
|
|
var track conn.UpTrack
|
|
|
|
count := 0
|
2021-04-28 17:00:50 +02:00
|
|
|
for _, t := range tracks {
|
2021-04-29 17:03:25 +02:00
|
|
|
if t.Kind() != kind {
|
|
|
|
continue
|
2021-04-28 17:00:50 +02:00
|
|
|
}
|
2023-04-06 12:37:23 +02:00
|
|
|
track = t
|
|
|
|
count++
|
|
|
|
if !last {
|
|
|
|
break
|
|
|
|
}
|
2021-04-29 17:03:25 +02:00
|
|
|
}
|
2023-04-06 12:37:23 +02:00
|
|
|
return track, count
|
2021-04-29 17:03:25 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
var ts []conn.UpTrack
|
2021-08-03 13:10:55 +02:00
|
|
|
limitSid := false
|
2021-04-29 17:03:25 +02:00
|
|
|
if audio {
|
2023-04-06 12:37:23 +02:00
|
|
|
t, _ := find(webrtc.RTPCodecTypeAudio, false)
|
2021-04-29 17:03:25 +02:00
|
|
|
if t != nil {
|
|
|
|
ts = append(ts, t)
|
2021-04-28 17:00:50 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
if video {
|
2023-04-06 12:37:23 +02:00
|
|
|
t, _ := find(webrtc.RTPCodecTypeVideo, false)
|
2021-04-29 17:03:25 +02:00
|
|
|
if t != nil {
|
|
|
|
ts = append(ts, t)
|
|
|
|
}
|
|
|
|
} else if videoLow {
|
2023-04-06 12:37:23 +02:00
|
|
|
t, count := find(webrtc.RTPCodecTypeVideo, true)
|
2021-04-29 17:03:25 +02:00
|
|
|
if t != nil {
|
|
|
|
ts = append(ts, t)
|
2023-04-06 12:37:23 +02:00
|
|
|
}
|
|
|
|
if count < 2 {
|
|
|
|
limitSid = true
|
2021-04-28 17:00:50 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-08-03 13:10:55 +02:00
|
|
|
return ts, limitSid
|
2020-05-09 19:39:34 +02:00
|
|
|
}
|
|
|
|
|
2021-01-31 19:00:09 +01:00
|
|
|
func (c *webClient) PushConn(g *group.Group, id string, up conn.Up, tracks []conn.UpTrack, replace string) error {
|
2023-12-09 16:56:33 +01:00
|
|
|
c.action(pushConnAction{g, id, up, tracks, replace})
|
2020-05-28 02:35:09 +02:00
|
|
|
return nil
|
2020-05-12 18:27:40 +02:00
|
|
|
}
|
|
|
|
|
2020-12-01 22:42:06 +01:00
|
|
|
func readMessage(conn *websocket.Conn, m *clientMessage) error {
|
|
|
|
err := conn.SetReadDeadline(time.Now().Add(15 * time.Second))
|
2020-06-08 22:14:28 +02:00
|
|
|
if err != nil {
|
2020-12-01 22:42:06 +01:00
|
|
|
return err
|
2020-06-08 22:14:28 +02:00
|
|
|
}
|
2020-12-01 22:42:06 +01:00
|
|
|
defer conn.SetReadDeadline(time.Time{})
|
|
|
|
|
|
|
|
return conn.ReadJSON(&m)
|
|
|
|
}
|
|
|
|
|
2022-12-16 18:56:36 +01:00
|
|
|
const protocolVersion = "2"
|
|
|
|
|
2024-04-30 18:17:00 +02:00
|
|
|
func StartClient(conn *websocket.Conn, addr net.Addr) (err error) {
|
2020-12-01 22:42:06 +01:00
|
|
|
var m clientMessage
|
|
|
|
|
2020-12-05 01:18:27 +01:00
|
|
|
err = readMessage(conn, &m)
|
2020-06-08 22:14:28 +02:00
|
|
|
if err != nil {
|
|
|
|
conn.Close()
|
2020-12-05 01:18:27 +01:00
|
|
|
return
|
2020-06-08 22:14:28 +02:00
|
|
|
}
|
|
|
|
|
2020-12-01 22:42:06 +01:00
|
|
|
if m.Type != "handshake" {
|
2020-08-12 11:50:30 +02:00
|
|
|
conn.WriteMessage(websocket.CloseMessage,
|
|
|
|
websocket.FormatCloseMessage(
|
|
|
|
websocket.CloseProtocolError,
|
2020-12-01 22:42:06 +01:00
|
|
|
"you must handshake first",
|
2020-08-12 11:50:30 +02:00
|
|
|
),
|
|
|
|
)
|
2020-06-08 22:14:28 +02:00
|
|
|
conn.Close()
|
2020-12-05 01:18:27 +01:00
|
|
|
err = group.ProtocolError("client didn't handshake")
|
|
|
|
return
|
2020-06-08 22:14:28 +02:00
|
|
|
}
|
|
|
|
|
2022-09-02 14:30:51 +02:00
|
|
|
versionError := true
|
|
|
|
if m.Version != nil {
|
|
|
|
for _, v := range m.Version {
|
2022-12-16 18:56:36 +01:00
|
|
|
if v == protocolVersion {
|
2022-09-02 14:30:51 +02:00
|
|
|
versionError = false
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-06-08 22:14:28 +02:00
|
|
|
c := &webClient{
|
2024-04-30 18:17:00 +02:00
|
|
|
addr: addr,
|
2023-12-09 16:56:33 +01:00
|
|
|
id: m.Id,
|
|
|
|
actions: unbounded.New[any](),
|
|
|
|
done: make(chan struct{}),
|
2020-06-08 22:14:28 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
defer close(c.done)
|
|
|
|
|
2021-02-14 16:24:22 +01:00
|
|
|
c.writeCh = make(chan interface{}, 100)
|
2020-12-01 22:42:06 +01:00
|
|
|
c.writerDone = make(chan struct{})
|
|
|
|
go clientWriter(conn, c.writeCh, c.writerDone)
|
2020-06-08 22:14:28 +02:00
|
|
|
defer func() {
|
2020-12-05 01:18:27 +01:00
|
|
|
m, e := errorToWSCloseMessage(c.id, err)
|
|
|
|
if isWSNormalError(err) {
|
|
|
|
err = nil
|
|
|
|
} else if _, ok := err.(group.KickError); ok {
|
|
|
|
err = nil
|
|
|
|
}
|
|
|
|
if m != nil {
|
|
|
|
c.write(*m)
|
2020-06-08 22:14:28 +02:00
|
|
|
}
|
2020-12-01 22:42:06 +01:00
|
|
|
c.close(e)
|
2020-06-08 22:14:28 +02:00
|
|
|
}()
|
|
|
|
|
2022-09-02 14:30:51 +02:00
|
|
|
return clientLoop(c, conn, versionError)
|
2020-06-08 22:14:28 +02:00
|
|
|
}
|
|
|
|
|
2020-09-13 11:57:33 +02:00
|
|
|
type pushConnAction struct {
|
2021-01-31 19:00:09 +01:00
|
|
|
group *group.Group
|
|
|
|
id string
|
|
|
|
conn conn.Up
|
|
|
|
tracks []conn.UpTrack
|
|
|
|
replace string
|
2020-09-13 11:57:33 +02:00
|
|
|
}
|
|
|
|
|
2021-05-14 19:20:56 +02:00
|
|
|
type requestConnsAction struct {
|
2020-12-19 17:38:47 +01:00
|
|
|
group *group.Group
|
2021-05-14 19:20:56 +02:00
|
|
|
target group.Client
|
|
|
|
id string
|
2020-09-13 11:57:33 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
type connectionFailedAction struct {
|
|
|
|
id string
|
|
|
|
}
|
|
|
|
|
2021-07-16 19:41:00 +02:00
|
|
|
type pushClientAction struct {
|
|
|
|
group string
|
|
|
|
kind string
|
|
|
|
id string
|
|
|
|
username string
|
2022-02-19 23:43:44 +01:00
|
|
|
permissions []string
|
2022-01-29 22:54:44 +01:00
|
|
|
data map[string]interface{}
|
2021-07-16 19:41:00 +02:00
|
|
|
}
|
|
|
|
|
2020-09-13 11:57:33 +02:00
|
|
|
type permissionsChangedAction struct{}
|
|
|
|
|
2021-07-16 19:41:00 +02:00
|
|
|
type joinedAction struct {
|
|
|
|
group string
|
|
|
|
kind string
|
|
|
|
}
|
|
|
|
|
2020-09-13 11:57:33 +02:00
|
|
|
type kickAction struct {
|
2020-11-30 16:26:11 +01:00
|
|
|
id string
|
2023-03-22 17:41:16 +01:00
|
|
|
username *string
|
2020-11-30 16:26:11 +01:00
|
|
|
message string
|
2020-09-13 11:57:33 +02:00
|
|
|
}
|
|
|
|
|
2021-02-05 15:56:52 +01:00
|
|
|
var errEmptyId = group.ProtocolError("empty id")
|
|
|
|
|
2022-02-19 23:43:44 +01:00
|
|
|
func member(v string, l []string) bool {
|
|
|
|
for _, w := range l {
|
|
|
|
if v == w {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
func remove(v string, l []string) []string {
|
|
|
|
for i, w := range l {
|
|
|
|
if v == w {
|
|
|
|
l = append(l[:i], l[i+1:]...)
|
|
|
|
return l
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return l
|
|
|
|
}
|
|
|
|
|
|
|
|
func addnew(v string, l []string) []string {
|
|
|
|
if member(v, l) {
|
|
|
|
return l
|
|
|
|
}
|
|
|
|
l = append(l, v)
|
|
|
|
return l
|
|
|
|
}
|
|
|
|
|
2022-09-02 14:30:51 +02:00
|
|
|
func clientLoop(c *webClient, ws *websocket.Conn, versionError bool) error {
|
2020-04-24 19:38:21 +02:00
|
|
|
read := make(chan interface{}, 1)
|
2020-09-13 11:04:16 +02:00
|
|
|
go clientReader(ws, read, c.done)
|
2020-04-24 19:38:21 +02:00
|
|
|
|
2020-12-05 00:07:34 +01:00
|
|
|
defer leaveGroup(c)
|
2020-04-24 19:38:21 +02:00
|
|
|
|
2020-04-25 22:44:24 +02:00
|
|
|
readTime := time.Now()
|
|
|
|
|
2020-06-12 17:39:16 +02:00
|
|
|
ticker := time.NewTicker(10 * time.Second)
|
2020-04-24 19:38:21 +02:00
|
|
|
defer ticker.Stop()
|
|
|
|
|
2021-01-03 12:04:39 +01:00
|
|
|
err := c.write(clientMessage{
|
2022-09-02 14:30:51 +02:00
|
|
|
Type: "handshake",
|
2022-12-16 18:56:36 +01:00
|
|
|
Version: []string{protocolVersion},
|
2021-01-03 12:04:39 +01:00
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2022-09-02 14:30:51 +02:00
|
|
|
if versionError {
|
|
|
|
c.write(clientMessage{
|
|
|
|
Type: "usermessage",
|
|
|
|
Kind: "warning",
|
|
|
|
Dest: c.id,
|
|
|
|
Privileged: true,
|
|
|
|
Value: "This client is using an unknown protocol version.\n" +
|
|
|
|
"Perhaps it needs upgrading?\n" +
|
|
|
|
"Trying to continue, things may break.",
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2020-04-24 19:38:21 +02:00
|
|
|
for {
|
|
|
|
select {
|
|
|
|
case m, ok := <-read:
|
|
|
|
if !ok {
|
|
|
|
return errors.New("reader died")
|
|
|
|
}
|
|
|
|
switch m := m.(type) {
|
|
|
|
case clientMessage:
|
2020-04-25 22:44:24 +02:00
|
|
|
readTime = time.Now()
|
2020-04-24 19:38:21 +02:00
|
|
|
err := handleClientMessage(c, m)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
case error:
|
|
|
|
return m
|
|
|
|
}
|
2023-12-09 16:56:33 +01:00
|
|
|
case <-c.actions.Ch:
|
|
|
|
actions := c.actions.Get()
|
2021-02-15 00:35:09 +01:00
|
|
|
for _, a := range actions {
|
2021-02-17 20:55:11 +01:00
|
|
|
err := handleAction(c, a)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2020-04-24 19:38:21 +02:00
|
|
|
}
|
|
|
|
case <-ticker.C:
|
2024-06-10 21:32:11 +02:00
|
|
|
if time.Since(readTime) > 45*time.Second {
|
2020-04-25 22:44:24 +02:00
|
|
|
return errors.New("client is dead")
|
|
|
|
}
|
2021-01-08 14:05:02 +01:00
|
|
|
// Some reverse proxies timeout connexions at 60
|
|
|
|
// seconds, make sure we generate some activity
|
2024-06-10 21:32:11 +02:00
|
|
|
if time.Since(readTime) > 20*time.Second {
|
2020-04-25 22:44:24 +02:00
|
|
|
err := c.write(clientMessage{
|
|
|
|
Type: "ping",
|
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
2020-04-24 19:38:21 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-07-15 23:51:25 +02:00
|
|
|
func pushDownConn(c *webClient, id string, up conn.Up, tracks []conn.UpTrack, replace string) error {
|
|
|
|
var requested []conn.UpTrack
|
2021-08-03 13:10:55 +02:00
|
|
|
limitSid := false
|
2021-07-15 23:51:25 +02:00
|
|
|
if up != nil {
|
|
|
|
var old *rtpDownConnection
|
|
|
|
if replace != "" {
|
|
|
|
old = getDownConn(c, replace)
|
|
|
|
} else {
|
|
|
|
old = getDownConn(c, up.Id())
|
2021-02-14 16:34:43 +01:00
|
|
|
}
|
2023-04-06 12:37:23 +02:00
|
|
|
var req []string
|
2021-07-15 23:51:25 +02:00
|
|
|
if old != nil {
|
2023-04-06 12:37:23 +02:00
|
|
|
req = old.requested
|
|
|
|
}
|
|
|
|
if req == nil {
|
|
|
|
var ok bool
|
|
|
|
req, ok = c.requested[up.Label()]
|
|
|
|
if !ok {
|
|
|
|
req = c.requested[""]
|
|
|
|
}
|
2021-02-14 16:34:43 +01:00
|
|
|
}
|
2023-04-06 12:37:23 +02:00
|
|
|
requested, limitSid = requestedTracks(c, req, tracks)
|
2021-07-15 23:51:25 +02:00
|
|
|
}
|
2021-02-14 16:34:43 +01:00
|
|
|
|
2021-07-15 23:51:25 +02:00
|
|
|
if replace != "" {
|
|
|
|
err := delDownConn(c, replace)
|
|
|
|
if err != nil {
|
|
|
|
log.Printf("Replace: %v", err)
|
2021-07-15 18:05:31 +02:00
|
|
|
}
|
2021-07-15 23:51:25 +02:00
|
|
|
}
|
2021-07-15 18:05:31 +02:00
|
|
|
|
2021-07-15 23:51:25 +02:00
|
|
|
// closes over replace, which will be modified below
|
|
|
|
defer func() {
|
|
|
|
if replace != "" {
|
|
|
|
closeDownConn(c, replace, "")
|
2021-02-14 16:34:43 +01:00
|
|
|
}
|
2021-07-15 23:51:25 +02:00
|
|
|
}()
|
2021-02-14 16:34:43 +01:00
|
|
|
|
2021-07-15 23:51:25 +02:00
|
|
|
if len(requested) == 0 {
|
|
|
|
closeDownConn(c, id, "")
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
down, _, err := addDownConn(c, up)
|
|
|
|
if err != nil {
|
2024-03-03 13:34:18 +01:00
|
|
|
if errors.Is(err, os.ErrClosed) {
|
2021-07-15 23:51:25 +02:00
|
|
|
return nil
|
2021-02-14 16:34:43 +01:00
|
|
|
}
|
2021-07-15 23:51:25 +02:00
|
|
|
return err
|
|
|
|
}
|
2021-08-03 13:10:55 +02:00
|
|
|
done, err := replaceTracks(down, requested, limitSid)
|
2021-07-15 23:51:25 +02:00
|
|
|
if err != nil || !done {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
err = negotiate(c, down, false, replace)
|
|
|
|
if err != nil {
|
|
|
|
log.Printf("Negotiation failed: %v", err)
|
2021-08-02 20:00:23 +02:00
|
|
|
closeDownConn(c, down.id, err.Error())
|
2021-07-15 23:51:25 +02:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
replace = ""
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2023-12-09 16:56:33 +01:00
|
|
|
func handleAction(c *webClient, a any) error {
|
2021-07-15 23:51:25 +02:00
|
|
|
switch a := a.(type) {
|
|
|
|
case pushConnAction:
|
|
|
|
if c.group == nil || c.group != a.group {
|
|
|
|
log.Printf("Got connectsions for wrong group")
|
|
|
|
return nil
|
2021-02-14 16:34:43 +01:00
|
|
|
}
|
2021-07-15 23:51:25 +02:00
|
|
|
return pushDownConn(c, a.id, a.conn, a.tracks, a.replace)
|
2021-05-14 19:20:56 +02:00
|
|
|
case requestConnsAction:
|
2021-02-14 16:34:43 +01:00
|
|
|
g := c.group
|
|
|
|
if g == nil || a.group != g {
|
2021-05-14 18:39:39 +02:00
|
|
|
log.Printf("Misdirected pushConns")
|
2021-02-14 16:34:43 +01:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
for _, u := range c.up {
|
2021-05-14 19:20:56 +02:00
|
|
|
if a.id != "" && a.id != u.id {
|
|
|
|
continue
|
|
|
|
}
|
2021-02-14 16:34:43 +01:00
|
|
|
tracks := u.getTracks()
|
|
|
|
replace := u.getReplace(false)
|
|
|
|
|
|
|
|
ts := make([]conn.UpTrack, len(tracks))
|
|
|
|
for i, t := range tracks {
|
|
|
|
ts[i] = t
|
|
|
|
}
|
2021-05-14 19:20:56 +02:00
|
|
|
err := a.target.PushConn(g, u.id, u, ts, replace)
|
2021-02-14 17:24:42 +01:00
|
|
|
if err != nil {
|
|
|
|
log.Printf("PushConn: %v", err)
|
|
|
|
}
|
2021-02-14 16:34:43 +01:00
|
|
|
}
|
|
|
|
case connectionFailedAction:
|
|
|
|
if down := getDownConn(c, a.id); down != nil {
|
|
|
|
err := negotiate(c, down, true, "")
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
tracks := make(
|
|
|
|
[]conn.UpTrack, len(down.tracks),
|
|
|
|
)
|
|
|
|
for i, t := range down.tracks {
|
|
|
|
tracks[i] = t.remote
|
|
|
|
}
|
2021-02-14 17:24:42 +01:00
|
|
|
c.PushConn(
|
2021-02-14 16:34:43 +01:00
|
|
|
c.group,
|
|
|
|
down.remote.Id(), down.remote,
|
|
|
|
tracks, "",
|
|
|
|
)
|
|
|
|
} else if up := getUpConn(c, a.id); up != nil {
|
|
|
|
c.write(clientMessage{
|
|
|
|
Type: "renegotiate",
|
|
|
|
Id: a.id,
|
|
|
|
})
|
|
|
|
} else {
|
|
|
|
log.Printf("Attempting to renegotiate " +
|
|
|
|
"unknown connection")
|
|
|
|
}
|
|
|
|
|
2021-07-16 19:41:00 +02:00
|
|
|
case pushClientAction:
|
|
|
|
if a.group != c.group.Name() {
|
|
|
|
log.Printf("got client for wrong group")
|
|
|
|
return nil
|
|
|
|
}
|
2022-02-19 23:43:44 +01:00
|
|
|
perms := append([]string(nil), a.permissions...)
|
2023-03-22 17:41:16 +01:00
|
|
|
username := a.username
|
2021-07-16 19:41:00 +02:00
|
|
|
return c.write(clientMessage{
|
|
|
|
Type: "user",
|
|
|
|
Kind: a.kind,
|
|
|
|
Id: a.id,
|
2023-03-22 17:41:16 +01:00
|
|
|
Username: &username,
|
2022-02-19 23:43:44 +01:00
|
|
|
Permissions: perms,
|
2022-01-29 22:54:44 +01:00
|
|
|
Data: a.data,
|
2021-07-16 19:41:00 +02:00
|
|
|
})
|
|
|
|
case joinedAction:
|
2022-01-29 22:54:44 +01:00
|
|
|
var status *group.Status
|
|
|
|
var data map[string]interface{}
|
2022-10-07 00:21:03 +02:00
|
|
|
var g *group.Group
|
2021-07-16 19:41:00 +02:00
|
|
|
if a.group != "" {
|
2022-10-07 00:21:03 +02:00
|
|
|
g = group.Get(a.group)
|
2021-07-16 19:41:00 +02:00
|
|
|
if g != nil {
|
2024-02-24 12:09:10 +01:00
|
|
|
s := g.Status(true, nil)
|
2022-01-29 22:54:44 +01:00
|
|
|
status = &s
|
|
|
|
data = g.Data()
|
2021-07-16 19:41:00 +02:00
|
|
|
}
|
|
|
|
}
|
2022-02-19 23:43:44 +01:00
|
|
|
perms := append([]string(nil), c.permissions...)
|
2023-03-22 17:41:16 +01:00
|
|
|
username := c.username
|
2022-09-06 03:03:50 +02:00
|
|
|
err := c.write(clientMessage{
|
2021-07-16 19:41:00 +02:00
|
|
|
Type: "joined",
|
|
|
|
Kind: a.kind,
|
|
|
|
Group: a.group,
|
2023-03-22 17:41:16 +01:00
|
|
|
Username: &username,
|
2022-02-19 23:43:44 +01:00
|
|
|
Permissions: perms,
|
2021-07-16 19:41:00 +02:00
|
|
|
Status: status,
|
2022-01-29 22:54:44 +01:00
|
|
|
Data: data,
|
2021-07-16 19:41:00 +02:00
|
|
|
RTCConfiguration: ice.ICEConfiguration(),
|
|
|
|
})
|
2022-09-06 03:03:50 +02:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2022-10-07 00:21:03 +02:00
|
|
|
if a.kind == "join" {
|
|
|
|
if g == nil {
|
|
|
|
log.Println("g is null when joining" +
|
|
|
|
"this shouldn't happen")
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
h := g.GetChatHistory()
|
|
|
|
for _, m := range h {
|
|
|
|
err := c.write(clientMessage{
|
|
|
|
Type: "chathistory",
|
2024-08-11 19:00:17 +02:00
|
|
|
Id: m.Id,
|
|
|
|
Source: m.Source,
|
2022-10-07 00:21:03 +02:00
|
|
|
Username: m.User,
|
2022-12-16 18:56:36 +01:00
|
|
|
Time: m.Time.Format(time.RFC3339),
|
2022-10-07 00:21:03 +02:00
|
|
|
Value: m.Value,
|
|
|
|
Kind: m.Kind,
|
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2022-09-06 03:03:50 +02:00
|
|
|
}
|
|
|
|
}
|
2021-02-14 16:34:43 +01:00
|
|
|
case permissionsChangedAction:
|
|
|
|
g := c.Group()
|
|
|
|
if g == nil {
|
|
|
|
return errors.New("Permissions changed in no group")
|
|
|
|
}
|
2022-02-19 23:43:44 +01:00
|
|
|
perms := append([]string(nil), c.permissions...)
|
2024-02-24 12:09:10 +01:00
|
|
|
status := g.Status(true, nil)
|
2023-03-22 17:41:16 +01:00
|
|
|
username := c.username
|
2021-02-14 16:34:43 +01:00
|
|
|
c.write(clientMessage{
|
|
|
|
Type: "joined",
|
|
|
|
Kind: "change",
|
|
|
|
Group: g.Name(),
|
2023-03-22 17:41:16 +01:00
|
|
|
Username: &username,
|
2022-02-19 23:43:44 +01:00
|
|
|
Permissions: perms,
|
2022-01-29 22:54:44 +01:00
|
|
|
Status: &status,
|
2021-02-14 16:34:43 +01:00
|
|
|
RTCConfiguration: ice.ICEConfiguration(),
|
|
|
|
})
|
2022-03-04 22:23:25 +01:00
|
|
|
if !member("present", c.permissions) {
|
2021-02-14 16:34:43 +01:00
|
|
|
up := getUpConns(c)
|
|
|
|
for _, u := range up {
|
|
|
|
err := delUpConn(
|
|
|
|
c, u.id, c.id, true,
|
|
|
|
)
|
|
|
|
if err == nil {
|
|
|
|
failUpConnection(
|
|
|
|
c, u.id,
|
|
|
|
"permission denied",
|
|
|
|
)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2021-04-27 18:58:21 +02:00
|
|
|
id := c.Id()
|
|
|
|
user := c.Username()
|
2022-01-29 22:54:44 +01:00
|
|
|
d := c.Data()
|
2021-04-27 18:58:21 +02:00
|
|
|
clients := g.GetClients(nil)
|
|
|
|
go func(clients []group.Client) {
|
|
|
|
for _, cc := range clients {
|
2021-07-16 19:41:00 +02:00
|
|
|
cc.PushClient(
|
2022-01-29 22:54:44 +01:00
|
|
|
g.Name(), "change", id, user, perms, d,
|
2021-07-16 19:41:00 +02:00
|
|
|
)
|
2021-04-27 18:58:21 +02:00
|
|
|
}
|
|
|
|
}(clients)
|
2021-02-14 16:34:43 +01:00
|
|
|
case kickAction:
|
|
|
|
return group.KickError{
|
|
|
|
a.id, a.username, a.message,
|
|
|
|
}
|
|
|
|
default:
|
|
|
|
log.Printf("unexpected action %T", a)
|
|
|
|
return errors.New("unexpected action")
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2020-10-01 13:47:48 +02:00
|
|
|
func failUpConnection(c *webClient, id string, message string) error {
|
2020-05-24 13:36:42 +02:00
|
|
|
if id != "" {
|
|
|
|
err := c.write(clientMessage{
|
|
|
|
Type: "abort",
|
|
|
|
Id: id,
|
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if message != "" {
|
2020-09-13 11:56:35 +02:00
|
|
|
err := c.error(group.UserError(message))
|
2020-05-24 13:36:42 +02:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2020-12-05 00:07:34 +01:00
|
|
|
func leaveGroup(c *webClient) {
|
|
|
|
if c.group == nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
if c.up != nil {
|
|
|
|
for id := range c.up {
|
2021-02-01 14:08:54 +01:00
|
|
|
delUpConn(c, id, c.id, true)
|
2020-12-05 00:07:34 +01:00
|
|
|
}
|
|
|
|
}
|
2021-02-05 12:20:33 +01:00
|
|
|
if c.down != nil {
|
|
|
|
for id := range c.down {
|
|
|
|
delDownConn(c, id)
|
|
|
|
}
|
|
|
|
}
|
2020-12-05 00:07:34 +01:00
|
|
|
|
|
|
|
group.DelClient(c)
|
2022-02-19 23:43:44 +01:00
|
|
|
c.permissions = nil
|
2022-01-29 22:54:44 +01:00
|
|
|
c.data = nil
|
2021-04-28 17:00:50 +02:00
|
|
|
c.requested = make(map[string][]string)
|
2020-12-05 00:07:34 +01:00
|
|
|
c.group = nil
|
|
|
|
}
|
|
|
|
|
2021-02-04 23:51:51 +01:00
|
|
|
func closeDownConn(c *webClient, id string, message string) error {
|
2021-02-02 19:38:15 +01:00
|
|
|
err := delDownConn(c, id)
|
2024-04-14 13:33:52 +02:00
|
|
|
if err != nil && !errors.Is(err, os.ErrNotExist) {
|
2021-02-02 19:38:15 +01:00
|
|
|
log.Printf("Close down connection: %v", err)
|
|
|
|
}
|
2021-02-05 15:56:52 +01:00
|
|
|
err = c.write(clientMessage{
|
|
|
|
Type: "close",
|
|
|
|
Id: id,
|
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
return err
|
2020-10-01 13:47:48 +02:00
|
|
|
}
|
|
|
|
if message != "" {
|
|
|
|
err := c.error(group.UserError(message))
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2020-09-13 11:56:35 +02:00
|
|
|
func setPermissions(g *group.Group, id string, perm string) error {
|
|
|
|
client := g.GetClient(id)
|
2020-05-28 02:35:09 +02:00
|
|
|
if client == nil {
|
2020-09-13 11:56:35 +02:00
|
|
|
return group.UserError("no such user")
|
2020-05-28 02:35:09 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
c, ok := client.(*webClient)
|
|
|
|
if !ok {
|
2020-09-13 11:56:35 +02:00
|
|
|
return group.UserError("this is not a real user")
|
2020-05-28 02:35:09 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
switch perm {
|
|
|
|
case "op":
|
2022-02-19 23:43:44 +01:00
|
|
|
c.permissions = addnew("op", c.permissions)
|
2021-10-30 18:32:49 +02:00
|
|
|
if g.Description().AllowRecording {
|
2022-02-19 23:43:44 +01:00
|
|
|
c.permissions = addnew("record", c.permissions)
|
2020-05-30 00:23:54 +02:00
|
|
|
}
|
2020-05-28 02:35:09 +02:00
|
|
|
case "unop":
|
2022-02-19 23:43:44 +01:00
|
|
|
c.permissions = remove("op", c.permissions)
|
|
|
|
c.permissions = remove("record", c.permissions)
|
2020-05-28 02:35:09 +02:00
|
|
|
case "present":
|
2022-02-19 23:43:44 +01:00
|
|
|
c.permissions = addnew("present", c.permissions)
|
2020-05-28 02:35:09 +02:00
|
|
|
case "unpresent":
|
2022-02-19 23:43:44 +01:00
|
|
|
c.permissions = remove("present", c.permissions)
|
2024-05-08 16:00:18 +02:00
|
|
|
case "shutup":
|
|
|
|
c.permissions = remove("message", c.permissions)
|
|
|
|
case "unshutup":
|
|
|
|
c.permissions = addnew("message", c.permissions)
|
2020-05-28 02:35:09 +02:00
|
|
|
default:
|
2020-09-13 11:56:35 +02:00
|
|
|
return group.UserError("unknown permission")
|
2020-05-28 02:35:09 +02:00
|
|
|
}
|
2023-12-09 16:56:33 +01:00
|
|
|
c.action(permissionsChangedAction{})
|
|
|
|
return nil
|
2020-05-28 02:35:09 +02:00
|
|
|
}
|
|
|
|
|
2023-03-22 17:41:16 +01:00
|
|
|
func (c *webClient) Kick(id string, user *string, message string) error {
|
2023-12-09 16:56:33 +01:00
|
|
|
c.action(kickAction{id, user, message})
|
|
|
|
return nil
|
2020-09-12 12:42:48 +02:00
|
|
|
}
|
|
|
|
|
2021-07-16 19:41:00 +02:00
|
|
|
func (c *webClient) Joined(group, kind string) error {
|
2023-12-09 16:56:33 +01:00
|
|
|
c.action(joinedAction{group, kind})
|
|
|
|
return nil
|
2021-07-16 19:41:00 +02:00
|
|
|
}
|
|
|
|
|
2023-03-22 17:41:16 +01:00
|
|
|
func kickClient(g *group.Group, id string, user *string, dest string, message string) error {
|
2020-11-30 16:26:11 +01:00
|
|
|
client := g.GetClient(dest)
|
2020-05-28 02:35:09 +02:00
|
|
|
if client == nil {
|
2020-09-13 11:56:35 +02:00
|
|
|
return group.UserError("no such user")
|
2020-05-28 02:35:09 +02:00
|
|
|
}
|
|
|
|
|
2021-02-26 16:20:13 +01:00
|
|
|
return client.Kick(id, user, message)
|
2020-05-28 02:35:09 +02:00
|
|
|
}
|
|
|
|
|
2020-05-27 11:44:49 +02:00
|
|
|
func handleClientMessage(c *webClient, m clientMessage) error {
|
2021-01-03 12:04:39 +01:00
|
|
|
if m.Source != "" {
|
|
|
|
if m.Source != c.Id() {
|
|
|
|
return group.ProtocolError("spoofed client id")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if m.Type != "join" {
|
2023-03-22 17:41:16 +01:00
|
|
|
if m.Username != nil {
|
|
|
|
if *m.Username != c.Username() {
|
2021-01-03 12:04:39 +01:00
|
|
|
return group.ProtocolError("spoofed username")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-04-24 19:38:21 +02:00
|
|
|
switch m.Type {
|
2020-12-01 22:42:06 +01:00
|
|
|
case "join":
|
|
|
|
if m.Kind == "leave" {
|
|
|
|
if c.group == nil || c.group.Name() != m.Group {
|
2022-12-17 01:00:49 +01:00
|
|
|
return group.UserError("you are not joined")
|
2020-12-01 22:42:06 +01:00
|
|
|
}
|
2020-12-05 00:07:34 +01:00
|
|
|
leaveGroup(c)
|
2023-04-03 22:32:04 +02:00
|
|
|
return nil
|
2020-12-01 22:42:06 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
if m.Kind != "join" {
|
|
|
|
return group.ProtocolError("unknown kind")
|
|
|
|
}
|
|
|
|
|
|
|
|
if c.group != nil {
|
2022-01-25 18:16:42 +01:00
|
|
|
return group.ProtocolError(
|
|
|
|
"cannot join multiple groups",
|
|
|
|
)
|
2020-12-01 22:42:06 +01:00
|
|
|
}
|
2022-01-29 22:54:44 +01:00
|
|
|
c.data = m.Data
|
2021-10-27 04:15:44 +02:00
|
|
|
g, err := group.AddClient(m.Group, c,
|
|
|
|
group.ClientCredentials{
|
|
|
|
Username: m.Username,
|
|
|
|
Password: m.Password,
|
2021-10-29 23:37:05 +02:00
|
|
|
Token: m.Token,
|
2021-10-27 04:15:44 +02:00
|
|
|
},
|
|
|
|
)
|
2020-05-09 19:39:34 +02:00
|
|
|
if err != nil {
|
2022-12-16 18:56:36 +01:00
|
|
|
var e, s string
|
2024-03-03 13:34:18 +01:00
|
|
|
var autherr *group.NotAuthorisedError
|
2024-06-08 14:52:14 +02:00
|
|
|
if errors.Is(err, token.ErrUsernameRequired) {
|
2023-04-01 00:13:20 +02:00
|
|
|
s = err.Error()
|
|
|
|
e = "need-username"
|
2024-03-03 13:34:18 +01:00
|
|
|
} else if errors.Is(err, group.ErrDuplicateUsername) {
|
2023-04-01 00:13:20 +02:00
|
|
|
s = err.Error()
|
|
|
|
e = "duplicate-username"
|
2024-03-03 13:34:18 +01:00
|
|
|
} else if errors.As(err, &autherr) {
|
|
|
|
s = "not authorised"
|
|
|
|
time.Sleep(200 * time.Millisecond)
|
2024-03-03 15:38:08 +01:00
|
|
|
log.Printf("Join group: %v", err)
|
2024-06-08 14:52:14 +02:00
|
|
|
} else if errors.Is(err, os.ErrNotExist) {
|
|
|
|
s = "group does not exist"
|
2024-03-03 13:34:18 +01:00
|
|
|
} else if _, ok := err.(group.UserError); ok {
|
|
|
|
s = err.Error()
|
2020-12-04 22:42:20 +01:00
|
|
|
} else {
|
|
|
|
s = "internal server error"
|
2020-12-12 23:21:49 +01:00
|
|
|
log.Printf("Join group: %v", err)
|
2020-12-01 22:42:06 +01:00
|
|
|
}
|
2023-03-22 17:41:16 +01:00
|
|
|
username := c.username
|
2020-12-04 22:42:20 +01:00
|
|
|
return c.write(clientMessage{
|
2022-09-02 14:30:51 +02:00
|
|
|
Type: "joined",
|
|
|
|
Kind: "fail",
|
2022-12-16 18:56:36 +01:00
|
|
|
Error: e,
|
2022-09-02 14:30:51 +02:00
|
|
|
Group: m.Group,
|
2023-03-22 17:41:16 +01:00
|
|
|
Username: &username,
|
2022-09-02 14:30:51 +02:00
|
|
|
Value: s,
|
2020-12-04 22:42:20 +01:00
|
|
|
})
|
2020-05-09 19:39:34 +02:00
|
|
|
}
|
2021-10-30 18:32:49 +02:00
|
|
|
if redirect := g.Description().Redirect; redirect != "" {
|
2020-12-01 22:42:06 +01:00
|
|
|
// We normally redirect at the HTTP level, but the group
|
|
|
|
// description could have been edited in the meantime.
|
2023-03-22 17:41:16 +01:00
|
|
|
username := c.username
|
2020-12-04 22:42:20 +01:00
|
|
|
return c.write(clientMessage{
|
2022-09-02 14:30:51 +02:00
|
|
|
Type: "joined",
|
|
|
|
Kind: "redirect",
|
|
|
|
Group: m.Group,
|
2023-03-22 17:41:16 +01:00
|
|
|
Username: &username,
|
2022-09-02 14:30:51 +02:00
|
|
|
Value: redirect,
|
2020-12-04 22:42:20 +01:00
|
|
|
})
|
2020-12-01 22:42:06 +01:00
|
|
|
}
|
|
|
|
c.group = g
|
|
|
|
case "request":
|
2021-05-14 18:39:39 +02:00
|
|
|
requested, err := parseRequested(m.Request)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
return c.setRequested(requested)
|
|
|
|
case "requestStream":
|
|
|
|
down := getDownConn(c, m.Id)
|
|
|
|
if down == nil {
|
|
|
|
return ErrUnknownId
|
|
|
|
}
|
|
|
|
requested, err := toStringArray(m.Request)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
c.setRequestedStream(down, requested)
|
2020-04-24 19:38:21 +02:00
|
|
|
case "offer":
|
2021-02-05 15:56:52 +01:00
|
|
|
if m.Id == "" {
|
|
|
|
return errEmptyId
|
|
|
|
}
|
2022-02-19 23:43:44 +01:00
|
|
|
if !member("present", c.permissions) {
|
2021-01-31 19:00:09 +01:00
|
|
|
if m.Replace != "" {
|
2021-02-01 14:08:54 +01:00
|
|
|
delUpConn(c, m.Replace, c.id, true)
|
2021-01-31 19:00:09 +01:00
|
|
|
}
|
2020-04-25 18:29:44 +02:00
|
|
|
c.write(clientMessage{
|
|
|
|
Type: "abort",
|
|
|
|
Id: m.Id,
|
|
|
|
})
|
2020-09-13 11:56:35 +02:00
|
|
|
return c.error(group.UserError("not authorised"))
|
2020-04-25 02:25:51 +02:00
|
|
|
}
|
2021-04-28 17:00:50 +02:00
|
|
|
err := gotOffer(c, m.Id, m.Label, m.SDP, m.Replace)
|
2020-04-24 19:38:21 +02:00
|
|
|
if err != nil {
|
2020-05-24 13:36:42 +02:00
|
|
|
log.Printf("gotOffer: %v", err)
|
2021-08-02 20:00:23 +02:00
|
|
|
return failUpConnection(c, m.Id, err.Error())
|
2020-04-24 19:38:21 +02:00
|
|
|
}
|
|
|
|
case "answer":
|
2021-02-05 15:56:52 +01:00
|
|
|
if m.Id == "" {
|
|
|
|
return errEmptyId
|
|
|
|
}
|
2021-01-03 17:00:58 +01:00
|
|
|
err := gotAnswer(c, m.Id, m.SDP)
|
2020-04-24 19:38:21 +02:00
|
|
|
if err != nil {
|
2020-10-01 13:47:48 +02:00
|
|
|
log.Printf("gotAnswer: %v", err)
|
|
|
|
message := ""
|
|
|
|
if err != ErrUnknownId {
|
2021-08-02 20:00:23 +02:00
|
|
|
message = err.Error()
|
2020-10-01 13:47:48 +02:00
|
|
|
}
|
2021-02-04 23:51:51 +01:00
|
|
|
return closeDownConn(c, m.Id, message)
|
2020-04-24 19:38:21 +02:00
|
|
|
}
|
2021-01-12 20:44:48 +01:00
|
|
|
down := getDownConn(c, m.Id)
|
2021-05-11 15:28:30 +02:00
|
|
|
if down == nil {
|
|
|
|
return ErrUnknownId
|
|
|
|
}
|
2021-01-12 20:44:48 +01:00
|
|
|
if down.negotiationNeeded > negotiationUnneeded {
|
|
|
|
err := negotiate(
|
2021-01-31 19:00:09 +01:00
|
|
|
c, down,
|
2021-01-12 20:44:48 +01:00
|
|
|
down.negotiationNeeded == negotiationRestartIce,
|
2021-01-31 19:00:09 +01:00
|
|
|
"",
|
2021-01-12 20:44:48 +01:00
|
|
|
)
|
|
|
|
if err != nil {
|
2021-08-02 20:00:23 +02:00
|
|
|
return closeDownConn(c, m.Id, err.Error())
|
2021-01-12 20:44:48 +01:00
|
|
|
}
|
|
|
|
}
|
2020-08-11 15:13:30 +02:00
|
|
|
case "renegotiate":
|
2021-02-05 15:56:52 +01:00
|
|
|
if m.Id == "" {
|
|
|
|
return errEmptyId
|
|
|
|
}
|
2020-08-11 15:13:30 +02:00
|
|
|
down := getDownConn(c, m.Id)
|
|
|
|
if down != nil {
|
2021-01-31 19:00:09 +01:00
|
|
|
err := negotiate(c, down, true, "")
|
2020-08-11 15:13:30 +02:00
|
|
|
if err != nil {
|
2021-08-02 20:00:23 +02:00
|
|
|
return closeDownConn(c, m.Id, err.Error())
|
2020-08-11 15:13:30 +02:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
log.Printf("Trying to renegotiate unknown connection")
|
|
|
|
}
|
2020-04-24 19:38:21 +02:00
|
|
|
case "close":
|
2021-02-05 15:56:52 +01:00
|
|
|
if m.Id == "" {
|
|
|
|
return errEmptyId
|
|
|
|
}
|
2021-02-01 14:08:54 +01:00
|
|
|
err := delUpConn(c, m.Id, c.id, true)
|
2021-01-31 19:00:09 +01:00
|
|
|
if err != nil {
|
|
|
|
log.Printf("Deleting up connection %v: %v",
|
|
|
|
m.Id, err)
|
|
|
|
return nil
|
|
|
|
}
|
2020-12-25 18:25:55 +01:00
|
|
|
case "abort":
|
2021-02-05 15:56:52 +01:00
|
|
|
if m.Id == "" {
|
|
|
|
return errEmptyId
|
|
|
|
}
|
2021-02-04 23:51:51 +01:00
|
|
|
return closeDownConn(c, m.Id, "")
|
2020-04-24 19:38:21 +02:00
|
|
|
case "ice":
|
2021-02-05 15:56:52 +01:00
|
|
|
if m.Id == "" {
|
|
|
|
return errEmptyId
|
|
|
|
}
|
2020-04-24 19:38:21 +02:00
|
|
|
if m.Candidate == nil {
|
2020-09-13 11:56:35 +02:00
|
|
|
return group.ProtocolError("null candidate")
|
2020-04-24 19:38:21 +02:00
|
|
|
}
|
|
|
|
err := gotICE(c, m.Candidate, m.Id)
|
|
|
|
if err != nil {
|
|
|
|
log.Printf("ICE: %v", err)
|
|
|
|
}
|
2020-11-30 14:06:14 +01:00
|
|
|
case "chat", "usermessage":
|
2020-12-01 22:42:06 +01:00
|
|
|
g := c.group
|
|
|
|
if g == nil {
|
|
|
|
return c.error(group.UserError("join a group first"))
|
2020-11-30 15:22:00 +01:00
|
|
|
}
|
|
|
|
|
2024-11-08 13:08:11 +01:00
|
|
|
required := "message"
|
|
|
|
if m.Type == "chat" && m.Kind == "caption" {
|
|
|
|
required = "caption"
|
|
|
|
}
|
|
|
|
if !member(required, c.permissions) {
|
2024-05-08 16:00:18 +02:00
|
|
|
return c.error(group.UserError("not authorised"))
|
|
|
|
}
|
|
|
|
|
2024-08-11 19:00:17 +02:00
|
|
|
id := m.Id
|
|
|
|
if m.Type == "chat" && m.Dest == "" && id == "" {
|
|
|
|
buf := make([]byte, 8)
|
|
|
|
crand.Read(buf)
|
|
|
|
id = base64.RawURLEncoding.EncodeToString(buf)
|
|
|
|
}
|
2020-12-02 19:33:53 +01:00
|
|
|
|
2024-08-11 19:00:17 +02:00
|
|
|
now := time.Now()
|
2020-11-30 14:06:14 +01:00
|
|
|
if m.Type == "chat" {
|
|
|
|
if m.Dest == "" {
|
2020-12-01 22:42:06 +01:00
|
|
|
g.AddToChatHistory(
|
2024-08-11 19:00:17 +02:00
|
|
|
id, m.Source, m.Username,
|
|
|
|
now, m.Kind, m.Value,
|
2020-11-30 14:06:14 +01:00
|
|
|
)
|
|
|
|
}
|
2020-10-01 16:52:01 +02:00
|
|
|
}
|
2020-09-30 00:33:23 +02:00
|
|
|
mm := clientMessage{
|
2020-12-19 17:38:47 +01:00
|
|
|
Type: m.Type,
|
2024-08-11 19:00:17 +02:00
|
|
|
Id: id,
|
2021-01-03 12:04:39 +01:00
|
|
|
Source: m.Source,
|
2020-12-19 17:38:47 +01:00
|
|
|
Dest: m.Dest,
|
|
|
|
Username: m.Username,
|
2022-02-19 23:43:44 +01:00
|
|
|
Privileged: member("op", c.permissions),
|
2022-12-16 18:56:36 +01:00
|
|
|
Time: now.Format(time.RFC3339),
|
2020-12-19 17:38:47 +01:00
|
|
|
Kind: m.Kind,
|
2021-01-11 16:22:04 +01:00
|
|
|
NoEcho: m.NoEcho,
|
2020-12-19 17:38:47 +01:00
|
|
|
Value: m.Value,
|
2020-09-30 00:33:23 +02:00
|
|
|
}
|
2020-10-01 16:52:01 +02:00
|
|
|
if m.Dest == "" {
|
2021-01-11 16:22:04 +01:00
|
|
|
var except group.Client
|
|
|
|
if m.NoEcho {
|
|
|
|
except = c
|
|
|
|
}
|
|
|
|
err := broadcast(g.GetClients(except), mm)
|
2021-01-04 17:52:02 +01:00
|
|
|
if err != nil {
|
|
|
|
log.Printf("broadcast(chat): %v", err)
|
2020-10-01 16:52:01 +02:00
|
|
|
}
|
|
|
|
} else {
|
2020-12-01 22:42:06 +01:00
|
|
|
cc := g.GetClient(m.Dest)
|
2020-10-01 16:52:01 +02:00
|
|
|
if cc == nil {
|
|
|
|
return c.error(group.UserError("user unknown"))
|
|
|
|
}
|
|
|
|
ccc, ok := cc.(*webClient)
|
|
|
|
if !ok {
|
2021-01-04 17:52:02 +01:00
|
|
|
return c.error(group.UserError(
|
|
|
|
"this user doesn't chat",
|
|
|
|
))
|
2020-05-28 02:35:09 +02:00
|
|
|
}
|
2020-10-01 16:52:01 +02:00
|
|
|
ccc.write(mm)
|
2020-04-24 19:38:21 +02:00
|
|
|
}
|
2020-08-12 12:40:40 +02:00
|
|
|
case "groupaction":
|
2020-12-01 22:42:06 +01:00
|
|
|
g := c.group
|
|
|
|
if g == nil {
|
|
|
|
return c.error(group.UserError("join a group first"))
|
2020-11-30 15:39:44 +01:00
|
|
|
}
|
2020-08-12 12:40:40 +02:00
|
|
|
switch m.Kind {
|
|
|
|
case "clearchat":
|
2024-05-08 15:38:51 +02:00
|
|
|
if !member("op", c.permissions) {
|
|
|
|
return c.error(group.UserError("not authorised"))
|
|
|
|
}
|
2024-08-11 23:57:11 +02:00
|
|
|
var id, userId string
|
|
|
|
if m.Value != nil {
|
|
|
|
value, ok := m.Value.(map[string]any)
|
|
|
|
if !ok {
|
|
|
|
return c.error(group.UserError(
|
|
|
|
"bad value in clearchat",
|
|
|
|
))
|
|
|
|
}
|
|
|
|
id, _ = value["id"].(string)
|
|
|
|
userId, _ = value["userId"].(string)
|
|
|
|
if userId == "" && id != "" {
|
|
|
|
return c.error(group.UserError(
|
|
|
|
"bad value in clearchat",
|
|
|
|
))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
g.ClearChatHistory(id, userId)
|
2021-01-03 17:47:56 +01:00
|
|
|
m := clientMessage{
|
|
|
|
Type: "usermessage",
|
|
|
|
Kind: "clearchat",
|
2024-11-30 17:55:24 +01:00
|
|
|
Value: m.Value,
|
2021-01-03 17:47:56 +01:00
|
|
|
Privileged: true,
|
|
|
|
}
|
2021-01-04 17:52:02 +01:00
|
|
|
err := broadcast(g.GetClients(nil), m)
|
|
|
|
if err != nil {
|
|
|
|
log.Printf("broadcast(clearchat): %v", err)
|
2020-05-28 02:35:09 +02:00
|
|
|
}
|
2020-08-12 12:40:40 +02:00
|
|
|
case "lock", "unlock":
|
2022-02-19 23:43:44 +01:00
|
|
|
if !member("op", c.permissions) {
|
2020-09-13 11:56:35 +02:00
|
|
|
return c.error(group.UserError("not authorised"))
|
2020-05-30 00:23:54 +02:00
|
|
|
}
|
2020-12-02 19:33:53 +01:00
|
|
|
message := ""
|
2020-12-28 01:42:26 +01:00
|
|
|
v, ok := m.Value.(string)
|
|
|
|
if ok {
|
|
|
|
message = v
|
2020-12-02 19:33:53 +01:00
|
|
|
}
|
|
|
|
g.SetLocked(m.Kind == "lock", message)
|
2020-08-12 12:40:40 +02:00
|
|
|
case "record":
|
2022-02-19 23:43:44 +01:00
|
|
|
if !member("record", c.permissions) {
|
2020-09-13 11:56:35 +02:00
|
|
|
return c.error(group.UserError("not authorised"))
|
2020-08-12 12:40:40 +02:00
|
|
|
}
|
2020-12-01 22:42:06 +01:00
|
|
|
for _, cc := range g.GetClients(c) {
|
2020-10-04 19:01:06 +02:00
|
|
|
_, ok := cc.(*diskwriter.Client)
|
2020-08-12 12:40:40 +02:00
|
|
|
if ok {
|
2020-09-13 11:56:35 +02:00
|
|
|
return c.error(group.UserError("already recording"))
|
2020-08-12 12:40:40 +02:00
|
|
|
}
|
|
|
|
}
|
2020-12-01 22:42:06 +01:00
|
|
|
disk := diskwriter.New(g)
|
2021-10-27 04:15:44 +02:00
|
|
|
_, err := group.AddClient(g.Name(), disk,
|
|
|
|
group.ClientCredentials{
|
|
|
|
System: true,
|
|
|
|
},
|
|
|
|
)
|
2020-08-12 12:40:40 +02:00
|
|
|
if err != nil {
|
2020-05-30 00:23:54 +02:00
|
|
|
disk.Close()
|
2020-08-12 12:40:40 +02:00
|
|
|
return c.error(err)
|
2020-05-30 00:23:54 +02:00
|
|
|
}
|
2021-05-14 19:20:56 +02:00
|
|
|
requestConns(disk, c.group, "")
|
2020-08-12 12:40:40 +02:00
|
|
|
case "unrecord":
|
2022-02-19 23:43:44 +01:00
|
|
|
if !member("record", c.permissions) {
|
2020-09-13 11:56:35 +02:00
|
|
|
return c.error(group.UserError("not authorised"))
|
2020-08-12 12:40:40 +02:00
|
|
|
}
|
2020-12-01 22:42:06 +01:00
|
|
|
for _, cc := range g.GetClients(c) {
|
2020-10-04 19:01:06 +02:00
|
|
|
disk, ok := cc.(*diskwriter.Client)
|
2020-08-12 12:40:40 +02:00
|
|
|
if ok {
|
|
|
|
disk.Close()
|
2020-09-13 11:56:35 +02:00
|
|
|
group.DelClient(disk)
|
2020-08-12 12:40:40 +02:00
|
|
|
}
|
|
|
|
}
|
2020-12-02 19:47:32 +01:00
|
|
|
case "subgroups":
|
2022-02-19 23:43:44 +01:00
|
|
|
if !member("op", c.permissions) {
|
2020-12-02 19:47:32 +01:00
|
|
|
return c.error(group.UserError("not authorised"))
|
|
|
|
}
|
|
|
|
s := ""
|
|
|
|
for _, sg := range group.GetSubGroups(g.Name()) {
|
|
|
|
plural := ""
|
|
|
|
if sg.Clients > 1 {
|
|
|
|
plural = "s"
|
|
|
|
}
|
2021-11-29 17:33:46 +01:00
|
|
|
s = s + fmt.Sprintf("%v (%v client%v)\n",
|
2020-12-02 19:47:32 +01:00
|
|
|
sg.Name, sg.Clients, plural)
|
|
|
|
}
|
2023-03-22 17:41:16 +01:00
|
|
|
username := "Server"
|
2020-12-02 19:47:32 +01:00
|
|
|
c.write(clientMessage{
|
2020-12-04 22:42:20 +01:00
|
|
|
Type: "chat",
|
|
|
|
Dest: c.id,
|
2023-03-22 17:41:16 +01:00
|
|
|
Username: &username,
|
2022-12-16 18:56:36 +01:00
|
|
|
Time: time.Now().Format(time.RFC3339),
|
2020-12-28 01:42:26 +01:00
|
|
|
Value: s,
|
2020-12-02 19:47:32 +01:00
|
|
|
})
|
2022-01-29 22:54:44 +01:00
|
|
|
case "setdata":
|
2022-02-19 23:43:44 +01:00
|
|
|
if !member("op", c.permissions) {
|
2022-01-29 22:54:44 +01:00
|
|
|
return c.error(group.UserError("not authorised"))
|
|
|
|
}
|
|
|
|
data, ok := m.Value.(map[string]interface{})
|
|
|
|
if !ok {
|
|
|
|
return c.error(group.UserError(
|
|
|
|
"Bad value in setdata",
|
|
|
|
))
|
|
|
|
}
|
|
|
|
g.UpdateData(data)
|
2023-03-31 20:45:30 +02:00
|
|
|
case "maketoken":
|
|
|
|
terror := func(e, m string) error {
|
|
|
|
return c.write(clientMessage{
|
|
|
|
Type: "usermessage",
|
|
|
|
Kind: "token",
|
|
|
|
Privileged: true,
|
|
|
|
Error: e,
|
|
|
|
Value: m,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
if !member("token", c.permissions) {
|
|
|
|
return terror("not-authorised", "not authorised")
|
|
|
|
}
|
|
|
|
tok, err := parseStatefulToken(m.Value)
|
|
|
|
if err != nil {
|
2023-05-04 03:31:09 +02:00
|
|
|
return terror("error", err.Error())
|
2023-03-31 20:45:30 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
if tok.Token == "" {
|
|
|
|
buf := make([]byte, 8)
|
|
|
|
crand.Read(buf)
|
|
|
|
tok.Token =
|
|
|
|
base64.RawURLEncoding.EncodeToString(buf)
|
|
|
|
} else {
|
|
|
|
return terror("error", "client specified token")
|
|
|
|
}
|
|
|
|
|
|
|
|
if tok.Group != c.group.Name() {
|
|
|
|
return terror("error", "wrong group in token")
|
|
|
|
}
|
|
|
|
|
|
|
|
if tok.Expires == nil {
|
|
|
|
return terror("error", "token doesn't expire")
|
|
|
|
}
|
|
|
|
|
2023-04-01 00:13:20 +02:00
|
|
|
if tok.Username != nil &&
|
|
|
|
c.group.UserExists(*tok.Username) {
|
|
|
|
return terror("error", "that username is taken")
|
|
|
|
}
|
|
|
|
|
2023-03-31 20:45:30 +02:00
|
|
|
for _, p := range tok.Permissions {
|
|
|
|
if !member(p, c.permissions) {
|
|
|
|
return terror(
|
|
|
|
"not-authorised",
|
|
|
|
"not authorised",
|
|
|
|
)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-04-08 21:13:35 +02:00
|
|
|
user := c.username
|
|
|
|
if user != "" {
|
|
|
|
tok.IssuedBy = &user
|
|
|
|
}
|
|
|
|
|
2024-05-08 17:25:28 +02:00
|
|
|
now := time.Now().UTC()
|
2023-04-08 21:13:35 +02:00
|
|
|
tok.IssuedAt = &now
|
|
|
|
|
2024-04-14 20:59:46 +02:00
|
|
|
new, err := token.Update(tok, "")
|
2023-03-31 20:45:30 +02:00
|
|
|
if err != nil {
|
|
|
|
return terror("error", err.Error())
|
|
|
|
}
|
|
|
|
c.write(clientMessage{
|
|
|
|
Type: "usermessage",
|
|
|
|
Kind: "token",
|
|
|
|
Privileged: true,
|
|
|
|
Value: new,
|
|
|
|
})
|
|
|
|
case "edittoken":
|
|
|
|
terror := func(e, m string) error {
|
|
|
|
return c.write(clientMessage{
|
|
|
|
Type: "usermessage",
|
|
|
|
Kind: "token",
|
|
|
|
Privileged: true,
|
|
|
|
Error: e,
|
|
|
|
Value: m,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
if !member("op", c.permissions) ||
|
|
|
|
!member("token", c.permissions) {
|
|
|
|
return terror("not-authorised", "not authorised")
|
|
|
|
}
|
|
|
|
tok, err := parseStatefulToken(m.Value)
|
|
|
|
if err != nil {
|
|
|
|
return terror("error", err.Error())
|
|
|
|
}
|
|
|
|
if tok.Group != "" || tok.Username != nil ||
|
|
|
|
tok.Permissions != nil ||
|
2023-04-08 21:13:35 +02:00
|
|
|
tok.IssuedBy != nil ||
|
|
|
|
tok.IssuedAt != nil {
|
2023-03-31 20:45:30 +02:00
|
|
|
return terror(
|
|
|
|
"error", "this field cannot be edited",
|
|
|
|
)
|
|
|
|
}
|
2024-04-14 20:59:46 +02:00
|
|
|
|
|
|
|
old, etag, err := token.Get(tok.Token)
|
|
|
|
if err != nil {
|
|
|
|
return terror("error", err.Error())
|
2023-03-31 20:45:30 +02:00
|
|
|
}
|
2024-04-14 20:59:46 +02:00
|
|
|
t := old.Clone()
|
|
|
|
if tok.Expires != nil {
|
|
|
|
t.Expires = tok.Expires
|
|
|
|
}
|
|
|
|
if tok.NotBefore != nil {
|
|
|
|
t.NotBefore = tok.NotBefore
|
|
|
|
}
|
|
|
|
|
|
|
|
new, err := token.Update(t, etag)
|
2023-03-31 20:45:30 +02:00
|
|
|
if err != nil {
|
|
|
|
return terror("error", err.Error())
|
|
|
|
}
|
|
|
|
c.write(clientMessage{
|
|
|
|
Type: "usermessage",
|
|
|
|
Kind: "token",
|
|
|
|
Privileged: true,
|
|
|
|
Value: new,
|
|
|
|
})
|
|
|
|
case "listtokens":
|
|
|
|
terror := func(e, m string) error {
|
|
|
|
return c.write(clientMessage{
|
|
|
|
Type: "usermessage",
|
|
|
|
Kind: "tokenlist",
|
|
|
|
Privileged: true,
|
|
|
|
Error: e,
|
|
|
|
Value: m,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
if !member("op", c.permissions) ||
|
|
|
|
!member("token", c.permissions) {
|
|
|
|
return terror("not-authorised", "not authorised")
|
|
|
|
}
|
2024-04-14 20:59:46 +02:00
|
|
|
tokens, _, err := token.List(c.group.Name())
|
2023-03-31 20:45:30 +02:00
|
|
|
if err != nil {
|
|
|
|
return terror("error", err.Error())
|
|
|
|
}
|
|
|
|
c.write(clientMessage{
|
|
|
|
Type: "usermessage",
|
|
|
|
Kind: "tokenlist",
|
|
|
|
Privileged: true,
|
|
|
|
Value: tokens,
|
|
|
|
})
|
2020-08-12 12:40:40 +02:00
|
|
|
default:
|
2022-12-17 01:00:49 +01:00
|
|
|
return group.UserError("unknown group action")
|
2020-05-30 00:23:54 +02:00
|
|
|
}
|
2020-08-12 12:40:40 +02:00
|
|
|
case "useraction":
|
2020-12-01 22:42:06 +01:00
|
|
|
g := c.group
|
|
|
|
if g == nil {
|
|
|
|
return c.error(group.UserError("join a group first"))
|
2020-11-30 15:39:44 +01:00
|
|
|
}
|
2020-08-12 12:40:40 +02:00
|
|
|
switch m.Kind {
|
2024-05-08 16:00:18 +02:00
|
|
|
case "op", "unop", "present", "unpresent", "shutup", "unshutup":
|
2022-02-19 23:43:44 +01:00
|
|
|
if !member("op", c.permissions) {
|
2020-09-13 11:56:35 +02:00
|
|
|
return c.error(group.UserError("not authorised"))
|
2020-08-12 12:40:40 +02:00
|
|
|
}
|
2020-12-01 22:42:06 +01:00
|
|
|
err := setPermissions(g, m.Dest, m.Kind)
|
2020-08-12 12:40:40 +02:00
|
|
|
if err != nil {
|
|
|
|
return c.error(err)
|
|
|
|
}
|
2024-04-30 18:18:32 +02:00
|
|
|
case "identify":
|
|
|
|
if !member("op", c.permissions) {
|
|
|
|
return c.error(group.UserError("not authorised"))
|
|
|
|
}
|
|
|
|
d := g.GetClient(m.Dest)
|
|
|
|
if d == nil {
|
|
|
|
return c.error(
|
|
|
|
group.UserError("client not found"),
|
|
|
|
)
|
|
|
|
}
|
|
|
|
value := make(map[string]any)
|
|
|
|
value["id"] = d.Id()
|
|
|
|
if username := d.Username(); username != "" {
|
|
|
|
value["username"] = username
|
|
|
|
}
|
|
|
|
if addr := d.Addr(); addr != nil {
|
|
|
|
value["address"] = addr.String()
|
|
|
|
}
|
|
|
|
type warner interface {
|
|
|
|
Warn(bool, string) error
|
|
|
|
}
|
|
|
|
w, ok := d.(warner)
|
|
|
|
if ok {
|
2024-08-11 19:00:17 +02:00
|
|
|
w.Warn(false, "Your IP address has been "+
|
|
|
|
"communicated to user "+
|
|
|
|
c.Username()+".")
|
2024-04-30 18:18:32 +02:00
|
|
|
}
|
|
|
|
c.write(clientMessage{
|
2024-08-11 19:00:17 +02:00
|
|
|
Type: "usermessage",
|
|
|
|
Kind: "userinfo",
|
2024-04-30 18:18:32 +02:00
|
|
|
Privileged: true,
|
2024-08-11 19:00:17 +02:00
|
|
|
Value: value,
|
2024-04-30 18:18:32 +02:00
|
|
|
})
|
2020-08-12 12:40:40 +02:00
|
|
|
case "kick":
|
2022-02-19 23:43:44 +01:00
|
|
|
if !member("op", c.permissions) {
|
2020-09-13 11:56:35 +02:00
|
|
|
return c.error(group.UserError("not authorised"))
|
2020-08-12 12:40:40 +02:00
|
|
|
}
|
2020-12-02 19:33:53 +01:00
|
|
|
message := ""
|
2020-12-28 01:42:26 +01:00
|
|
|
v, ok := m.Value.(string)
|
|
|
|
if ok {
|
|
|
|
message = v
|
2020-12-02 19:33:53 +01:00
|
|
|
}
|
2021-01-03 12:04:39 +01:00
|
|
|
err := kickClient(g, m.Source, m.Username, m.Dest, message)
|
2020-08-12 12:40:40 +02:00
|
|
|
if err != nil {
|
|
|
|
return c.error(err)
|
|
|
|
}
|
2022-01-29 22:54:44 +01:00
|
|
|
case "setdata":
|
2021-04-28 14:45:45 +02:00
|
|
|
if m.Dest != c.Id() {
|
|
|
|
return c.error(group.UserError("not authorised"))
|
|
|
|
}
|
2022-01-29 22:54:44 +01:00
|
|
|
data, ok := m.Value.(map[string]interface{})
|
2021-04-28 14:45:45 +02:00
|
|
|
if !ok {
|
|
|
|
return c.error(group.UserError(
|
2022-01-29 22:54:44 +01:00
|
|
|
"Bad value in setdata",
|
2021-04-28 14:45:45 +02:00
|
|
|
))
|
|
|
|
}
|
2022-01-29 22:54:44 +01:00
|
|
|
if c.data == nil {
|
|
|
|
c.data = make(map[string]interface{})
|
2021-04-28 14:45:45 +02:00
|
|
|
}
|
2022-01-29 22:54:44 +01:00
|
|
|
for k, v := range data {
|
2021-04-28 14:45:45 +02:00
|
|
|
if v == nil {
|
2022-01-29 22:54:44 +01:00
|
|
|
delete(c.data, k)
|
2021-04-28 14:45:45 +02:00
|
|
|
} else {
|
2022-01-29 22:54:44 +01:00
|
|
|
c.data[k] = v
|
2021-04-28 14:45:45 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
id := c.Id()
|
|
|
|
user := c.Username()
|
|
|
|
perms := c.Permissions()
|
2022-01-29 22:54:44 +01:00
|
|
|
data = c.Data()
|
2021-04-28 14:45:45 +02:00
|
|
|
go func(clients []group.Client) {
|
|
|
|
for _, cc := range clients {
|
2021-07-16 19:41:00 +02:00
|
|
|
cc.PushClient(
|
|
|
|
g.Name(), "change",
|
2022-01-29 22:54:44 +01:00
|
|
|
id, user, perms, data,
|
2021-07-16 19:41:00 +02:00
|
|
|
)
|
2021-04-28 14:45:45 +02:00
|
|
|
}
|
|
|
|
}(g.GetClients(nil))
|
2020-08-12 12:40:40 +02:00
|
|
|
default:
|
2022-12-17 01:00:49 +01:00
|
|
|
return group.UserError("unknown user action")
|
2020-04-25 17:36:35 +02:00
|
|
|
}
|
2020-04-25 22:44:24 +02:00
|
|
|
case "pong":
|
|
|
|
// nothing
|
|
|
|
case "ping":
|
2020-12-01 22:42:06 +01:00
|
|
|
return c.write(clientMessage{
|
2020-04-25 22:44:24 +02:00
|
|
|
Type: "pong",
|
|
|
|
})
|
2020-04-24 19:38:21 +02:00
|
|
|
default:
|
|
|
|
log.Printf("unexpected message: %v", m.Type)
|
2020-09-13 11:56:35 +02:00
|
|
|
return group.ProtocolError("unexpected message")
|
2020-04-24 19:38:21 +02:00
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2023-03-31 20:45:30 +02:00
|
|
|
func parseStatefulToken(value interface{}) (*token.Stateful, error) {
|
|
|
|
data, ok := value.(map[string]interface{})
|
|
|
|
if !ok || data == nil {
|
|
|
|
return nil, errors.New("bad token value")
|
|
|
|
}
|
|
|
|
parseString := func(key string) (*string, error) {
|
|
|
|
v := data[key]
|
|
|
|
if v == nil {
|
|
|
|
return nil, nil
|
|
|
|
}
|
|
|
|
vv, ok := v.(string)
|
|
|
|
if !ok {
|
|
|
|
return nil, errors.New("bad string value")
|
|
|
|
}
|
|
|
|
return &vv, nil
|
|
|
|
}
|
|
|
|
parseStringList := func(key string) ([]string, error) {
|
|
|
|
v := data[key]
|
|
|
|
if v == nil {
|
|
|
|
return nil, nil
|
|
|
|
}
|
|
|
|
vv, ok := v.([]interface{})
|
|
|
|
if !ok {
|
|
|
|
return nil, errors.New("bad string list")
|
|
|
|
}
|
|
|
|
vvv := make([]string, 0, len(vv))
|
|
|
|
for _, s := range vv {
|
|
|
|
ss, ok := s.(string)
|
|
|
|
if !ok {
|
|
|
|
return nil, errors.New("bad string list")
|
|
|
|
}
|
|
|
|
vvv = append(vvv, ss)
|
|
|
|
}
|
|
|
|
return vvv, nil
|
|
|
|
}
|
|
|
|
parseTime := func(key string) (*time.Time, error) {
|
|
|
|
v := data[key]
|
|
|
|
if v == nil {
|
|
|
|
return nil, nil
|
|
|
|
}
|
|
|
|
switch v := v.(type) {
|
|
|
|
case string:
|
|
|
|
vv, err := time.Parse(time.RFC3339, v)
|
|
|
|
if err != nil {
|
|
|
|
return nil, errors.New("bad time value")
|
|
|
|
}
|
|
|
|
return &vv, nil
|
|
|
|
case float64: // relative time
|
|
|
|
vv := time.Now().Add(time.Duration(v) * time.Millisecond)
|
|
|
|
return &vv, nil
|
|
|
|
default:
|
|
|
|
return nil, errors.New("bad time value")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
t, err := parseString("token")
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
tt := ""
|
|
|
|
if t != nil {
|
|
|
|
tt = *t
|
|
|
|
}
|
|
|
|
u, err := parseString("username")
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
g, err := parseString("group")
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
gg := ""
|
|
|
|
if g != nil {
|
|
|
|
gg = *g
|
|
|
|
}
|
|
|
|
p, err := parseStringList("permissions")
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
e, err := parseTime("expires")
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
n, err := parseTime("not-before")
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return &token.Stateful{
|
|
|
|
Token: tt,
|
|
|
|
Group: gg,
|
|
|
|
Username: u,
|
|
|
|
Permissions: p,
|
|
|
|
Expires: e,
|
|
|
|
NotBefore: n,
|
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
|
2020-04-24 19:38:21 +02:00
|
|
|
func clientReader(conn *websocket.Conn, read chan<- interface{}, done <-chan struct{}) {
|
|
|
|
defer close(read)
|
|
|
|
for {
|
|
|
|
var m clientMessage
|
|
|
|
err := conn.ReadJSON(&m)
|
|
|
|
if err != nil {
|
|
|
|
select {
|
|
|
|
case read <- err:
|
|
|
|
return
|
|
|
|
case <-done:
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
select {
|
|
|
|
case read <- m:
|
|
|
|
case <-done:
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func clientWriter(conn *websocket.Conn, ch <-chan interface{}, done chan<- struct{}) {
|
|
|
|
defer func() {
|
|
|
|
close(done)
|
|
|
|
conn.Close()
|
|
|
|
}()
|
|
|
|
|
|
|
|
for {
|
|
|
|
m, ok := <-ch
|
|
|
|
if !ok {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
err := conn.SetWriteDeadline(
|
2021-02-14 16:24:22 +01:00
|
|
|
time.Now().Add(500 * time.Millisecond),
|
|
|
|
)
|
2020-04-24 19:38:21 +02:00
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
switch m := m.(type) {
|
|
|
|
case clientMessage:
|
|
|
|
err := conn.WriteJSON(m)
|
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
2021-01-04 17:52:02 +01:00
|
|
|
case []byte:
|
|
|
|
err := conn.WriteMessage(websocket.TextMessage, m)
|
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
2020-04-24 19:38:21 +02:00
|
|
|
case closeMessage:
|
2020-09-08 00:29:30 +02:00
|
|
|
if m.data != nil {
|
|
|
|
conn.WriteMessage(
|
|
|
|
websocket.CloseMessage,
|
|
|
|
m.data,
|
|
|
|
)
|
2020-04-24 19:38:21 +02:00
|
|
|
}
|
2020-09-08 00:29:30 +02:00
|
|
|
return
|
2020-04-24 19:38:21 +02:00
|
|
|
default:
|
2023-12-09 17:32:16 +01:00
|
|
|
log.Printf("clientWriter: unexpected message %T", m)
|
2020-04-24 19:38:21 +02:00
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2020-05-28 02:35:09 +02:00
|
|
|
|
2020-12-23 21:32:32 +01:00
|
|
|
func (c *webClient) Warn(oponly bool, message string) error {
|
2022-02-19 23:43:44 +01:00
|
|
|
if oponly && !member("op", c.permissions) {
|
2020-12-23 21:32:32 +01:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
return c.write(clientMessage{
|
2020-12-27 01:24:52 +01:00
|
|
|
Type: "usermessage",
|
|
|
|
Kind: "warning",
|
|
|
|
Dest: c.id,
|
2020-12-23 21:32:32 +01:00
|
|
|
Privileged: true,
|
2020-12-28 01:42:26 +01:00
|
|
|
Value: message,
|
2020-12-23 21:32:32 +01:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2020-09-12 19:52:36 +02:00
|
|
|
var ErrClientDead = errors.New("client is dead")
|
2020-05-30 12:38:13 +02:00
|
|
|
|
2023-12-09 16:56:33 +01:00
|
|
|
func (c *webClient) action(a interface{}) {
|
|
|
|
c.actions.Put(a)
|
2020-05-28 02:35:09 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
func (c *webClient) write(m clientMessage) error {
|
|
|
|
select {
|
|
|
|
case c.writeCh <- m:
|
|
|
|
return nil
|
|
|
|
case <-c.writerDone:
|
2020-09-12 19:52:36 +02:00
|
|
|
return ErrClientDead
|
2020-05-28 02:35:09 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-01-04 17:52:02 +01:00
|
|
|
func broadcast(cs []group.Client, m clientMessage) error {
|
|
|
|
b, err := json.Marshal(m)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
for _, c := range cs {
|
|
|
|
cc, ok := c.(*webClient)
|
|
|
|
if !ok {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
select {
|
|
|
|
case cc.writeCh <- b:
|
|
|
|
case <-cc.writerDone:
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2020-09-08 00:29:30 +02:00
|
|
|
func (c *webClient) close(data []byte) error {
|
|
|
|
select {
|
|
|
|
case c.writeCh <- closeMessage{data}:
|
|
|
|
return nil
|
|
|
|
case <-c.writerDone:
|
2020-09-12 19:52:36 +02:00
|
|
|
return ErrClientDead
|
2020-09-08 00:29:30 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-11-30 16:26:11 +01:00
|
|
|
func errorMessage(id string, err error) *clientMessage {
|
2020-05-28 02:35:09 +02:00
|
|
|
switch e := err.(type) {
|
2020-09-13 11:56:35 +02:00
|
|
|
case group.UserError:
|
2020-11-30 16:26:11 +01:00
|
|
|
return &clientMessage{
|
2020-12-19 17:38:47 +01:00
|
|
|
Type: "usermessage",
|
|
|
|
Kind: "error",
|
|
|
|
Dest: id,
|
|
|
|
Privileged: true,
|
2020-12-28 01:42:26 +01:00
|
|
|
Value: e.Error(),
|
2020-11-30 16:26:11 +01:00
|
|
|
}
|
|
|
|
case group.KickError:
|
|
|
|
message := e.Message
|
|
|
|
if message == "" {
|
|
|
|
message = "you have been kicked out"
|
|
|
|
}
|
|
|
|
return &clientMessage{
|
2020-12-19 17:38:47 +01:00
|
|
|
Type: "usermessage",
|
2021-07-30 19:38:02 +02:00
|
|
|
Kind: "kicked",
|
2020-12-19 17:38:47 +01:00
|
|
|
Id: e.Id,
|
|
|
|
Username: e.Username,
|
|
|
|
Dest: id,
|
|
|
|
Privileged: true,
|
2020-12-28 01:42:26 +01:00
|
|
|
Value: message,
|
2020-11-30 16:26:11 +01:00
|
|
|
}
|
2020-05-28 02:35:09 +02:00
|
|
|
default:
|
2020-11-30 16:26:11 +01:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *webClient) error(err error) error {
|
|
|
|
m := errorMessage(c.id, err)
|
|
|
|
if m == nil {
|
2020-05-28 02:35:09 +02:00
|
|
|
return err
|
|
|
|
}
|
2020-11-30 16:26:11 +01:00
|
|
|
return c.write(*m)
|
2020-05-28 02:35:09 +02:00
|
|
|
}
|