2020-04-24 19:38:21 +02:00
|
|
|
// Copyright (c) 2020 by Juliusz Chroboczek.
|
|
|
|
|
|
|
|
// This is not open source software. Copy it, and I'll break into your
|
|
|
|
// house and tell your three year-old that Santa doesn't exist.
|
|
|
|
|
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
2020-04-25 02:25:51 +02:00
|
|
|
"encoding/json"
|
2020-04-24 19:38:21 +02:00
|
|
|
"log"
|
2020-04-25 02:25:51 +02:00
|
|
|
"os"
|
|
|
|
"path/filepath"
|
2020-04-29 16:08:07 +02:00
|
|
|
"sort"
|
2020-04-25 04:08:43 +02:00
|
|
|
"strings"
|
2020-04-25 17:36:35 +02:00
|
|
|
"sync"
|
2020-04-28 15:26:50 +02:00
|
|
|
"sync/atomic"
|
2020-04-25 03:40:01 +02:00
|
|
|
"time"
|
2020-04-24 19:38:21 +02:00
|
|
|
|
2020-06-03 20:12:25 +02:00
|
|
|
"sfu/rtptime"
|
2020-04-27 21:43:29 +02:00
|
|
|
|
2020-04-24 19:38:21 +02:00
|
|
|
"github.com/pion/webrtc/v2"
|
|
|
|
)
|
|
|
|
|
2020-05-28 02:35:09 +02:00
|
|
|
type client interface {
|
|
|
|
getGroup() *group
|
|
|
|
getId() string
|
|
|
|
getUsername() string
|
|
|
|
pushConn(conn *upConnection, tracks []*upTrack, label string) error
|
2020-05-31 20:41:17 +02:00
|
|
|
pushClient(id, username string, add bool) error
|
2020-05-28 02:35:09 +02:00
|
|
|
}
|
|
|
|
|
2020-04-25 21:16:49 +02:00
|
|
|
type chatHistoryEntry struct {
|
|
|
|
id string
|
|
|
|
user string
|
|
|
|
value string
|
|
|
|
me bool
|
|
|
|
}
|
|
|
|
|
2020-05-09 12:06:13 +02:00
|
|
|
const (
|
2020-05-14 14:34:40 +02:00
|
|
|
minVideoRate = 200000
|
2020-05-09 12:06:13 +02:00
|
|
|
minAudioRate = 9600
|
|
|
|
)
|
|
|
|
|
2020-04-24 19:38:21 +02:00
|
|
|
type group struct {
|
2020-04-25 02:25:51 +02:00
|
|
|
name string
|
2020-04-25 03:40:01 +02:00
|
|
|
dead bool
|
2020-04-25 02:25:51 +02:00
|
|
|
description *groupDescription
|
2020-05-09 12:06:13 +02:00
|
|
|
videoCount uint32
|
2020-05-18 15:24:04 +02:00
|
|
|
locked uint32
|
2020-04-24 19:38:21 +02:00
|
|
|
|
|
|
|
mu sync.Mutex
|
2020-05-28 02:35:09 +02:00
|
|
|
clients map[string]client
|
2020-04-25 21:16:49 +02:00
|
|
|
history []chatHistoryEntry
|
2020-04-24 19:38:21 +02:00
|
|
|
}
|
|
|
|
|
2020-04-28 14:54:50 +02:00
|
|
|
type delConnAction struct {
|
2020-04-24 19:38:21 +02:00
|
|
|
id string
|
|
|
|
}
|
|
|
|
|
2020-05-17 23:51:17 +02:00
|
|
|
type addConnAction struct {
|
|
|
|
conn *upConnection
|
|
|
|
tracks []*upTrack
|
2020-04-24 19:38:21 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
type addLabelAction struct {
|
|
|
|
id string
|
|
|
|
label string
|
|
|
|
}
|
|
|
|
|
|
|
|
type getUpAction struct {
|
|
|
|
ch chan<- string
|
|
|
|
}
|
|
|
|
|
2020-05-17 23:51:17 +02:00
|
|
|
type pushConnsAction struct {
|
2020-05-28 02:35:09 +02:00
|
|
|
c client
|
2020-04-24 19:38:21 +02:00
|
|
|
}
|
|
|
|
|
2020-05-02 23:41:47 +02:00
|
|
|
type connectionFailedAction struct {
|
|
|
|
id string
|
|
|
|
}
|
|
|
|
|
2020-04-25 18:35:32 +02:00
|
|
|
type permissionsChangedAction struct{}
|
2020-04-25 17:36:35 +02:00
|
|
|
|
|
|
|
type kickAction struct{}
|
|
|
|
|
2020-04-24 19:38:21 +02:00
|
|
|
var groups struct {
|
|
|
|
mu sync.Mutex
|
|
|
|
groups map[string]*group
|
|
|
|
api *webrtc.API
|
|
|
|
}
|
|
|
|
|
2020-04-25 04:08:43 +02:00
|
|
|
func addGroup(name string, desc *groupDescription) (*group, error) {
|
2020-04-24 19:38:21 +02:00
|
|
|
groups.mu.Lock()
|
|
|
|
defer groups.mu.Unlock()
|
|
|
|
|
|
|
|
if groups.groups == nil {
|
|
|
|
groups.groups = make(map[string]*group)
|
2020-04-27 03:06:45 +02:00
|
|
|
s := webrtc.SettingEngine{}
|
2020-04-27 03:08:03 +02:00
|
|
|
s.SetTrickle(true)
|
2020-04-24 19:38:21 +02:00
|
|
|
m := webrtc.MediaEngine{}
|
2020-04-25 11:47:14 +02:00
|
|
|
m.RegisterCodec(webrtc.NewRTPVP8CodecExt(
|
|
|
|
webrtc.DefaultPayloadTypeVP8, 90000,
|
|
|
|
[]webrtc.RTCPFeedback{
|
2020-04-27 03:08:03 +02:00
|
|
|
{"goog-remb", ""},
|
2020-04-27 21:43:29 +02:00
|
|
|
{"nack", ""},
|
2020-04-27 03:08:03 +02:00
|
|
|
{"nack", "pli"},
|
2020-05-21 00:24:10 +02:00
|
|
|
{"ccm", "fir"},
|
2020-04-25 11:47:14 +02:00
|
|
|
},
|
|
|
|
"",
|
|
|
|
))
|
2020-04-24 19:38:21 +02:00
|
|
|
m.RegisterCodec(webrtc.NewRTPOpusCodec(
|
2020-04-25 11:47:14 +02:00
|
|
|
webrtc.DefaultPayloadTypeOpus, 48000,
|
|
|
|
))
|
2020-04-24 19:38:21 +02:00
|
|
|
groups.api = webrtc.NewAPI(
|
2020-04-27 03:06:45 +02:00
|
|
|
webrtc.WithSettingEngine(s),
|
2020-04-24 19:38:21 +02:00
|
|
|
webrtc.WithMediaEngine(m),
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
2020-04-25 04:08:43 +02:00
|
|
|
var err error
|
|
|
|
|
2020-04-24 19:38:21 +02:00
|
|
|
g := groups.groups[name]
|
|
|
|
if g == nil {
|
2020-04-25 17:36:35 +02:00
|
|
|
if desc == nil {
|
2020-04-25 04:08:43 +02:00
|
|
|
desc, err = getDescription(name)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2020-04-25 02:25:51 +02:00
|
|
|
}
|
2020-04-24 19:38:21 +02:00
|
|
|
g = &group{
|
2020-04-25 02:25:51 +02:00
|
|
|
name: name,
|
|
|
|
description: desc,
|
2020-05-28 02:35:09 +02:00
|
|
|
clients: make(map[string]client),
|
2020-04-24 19:38:21 +02:00
|
|
|
}
|
|
|
|
groups.groups[name] = g
|
2020-04-25 04:08:43 +02:00
|
|
|
} else if desc != nil {
|
|
|
|
g.description = desc
|
2020-04-25 03:40:01 +02:00
|
|
|
} else if g.dead || time.Since(g.description.loadTime) > 5*time.Second {
|
|
|
|
changed, err := descriptionChanged(name, g.description)
|
|
|
|
if err != nil {
|
2020-04-25 21:33:08 +02:00
|
|
|
if !os.IsNotExist(err) {
|
|
|
|
log.Printf("Reading group %v: %v", name, err)
|
2020-04-25 03:40:01 +02:00
|
|
|
}
|
2020-04-25 21:33:08 +02:00
|
|
|
g.dead = true
|
|
|
|
delGroupUnlocked(name)
|
2020-04-25 03:40:01 +02:00
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
if changed {
|
|
|
|
desc, err := getDescription(name)
|
|
|
|
if err != nil {
|
2020-04-25 21:33:08 +02:00
|
|
|
if !os.IsNotExist(err) {
|
|
|
|
log.Printf("Reading group %v: %v",
|
|
|
|
name, err)
|
2020-04-25 03:40:01 +02:00
|
|
|
}
|
2020-04-25 21:33:08 +02:00
|
|
|
g.dead = true
|
|
|
|
delGroupUnlocked(name)
|
2020-04-25 03:40:01 +02:00
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
g.dead = false
|
|
|
|
g.description = desc
|
|
|
|
} else {
|
|
|
|
g.description.loadTime = time.Now()
|
|
|
|
}
|
2020-04-24 19:38:21 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return g, nil
|
|
|
|
}
|
|
|
|
|
2020-04-29 16:08:07 +02:00
|
|
|
func getGroupNames() []string {
|
|
|
|
groups.mu.Lock()
|
|
|
|
defer groups.mu.Unlock()
|
|
|
|
|
|
|
|
names := make([]string, 0, len(groups.groups))
|
|
|
|
for name := range groups.groups {
|
|
|
|
names = append(names, name)
|
|
|
|
}
|
|
|
|
return names
|
|
|
|
}
|
|
|
|
|
|
|
|
func getGroup(name string) *group {
|
|
|
|
groups.mu.Lock()
|
|
|
|
defer groups.mu.Unlock()
|
|
|
|
|
|
|
|
return groups.groups[name]
|
|
|
|
}
|
|
|
|
|
2020-04-25 02:01:04 +02:00
|
|
|
func delGroupUnlocked(name string) bool {
|
2020-04-24 19:38:21 +02:00
|
|
|
g := groups.groups[name]
|
|
|
|
if g == nil {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(g.clients) != 0 {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
delete(groups.groups, name)
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
|
|
|
type userid struct {
|
|
|
|
id string
|
|
|
|
username string
|
|
|
|
}
|
|
|
|
|
2020-05-31 20:41:17 +02:00
|
|
|
func addClient(name string, c client, user, pass string) (*group, error) {
|
2020-04-25 04:08:43 +02:00
|
|
|
g, err := addGroup(name, nil)
|
2020-04-24 19:38:21 +02:00
|
|
|
if err != nil {
|
2020-05-31 20:41:17 +02:00
|
|
|
return nil, err
|
2020-04-24 19:38:21 +02:00
|
|
|
}
|
|
|
|
|
2020-04-25 02:25:51 +02:00
|
|
|
perms, err := getPermission(g.description, user, pass)
|
|
|
|
if err != nil {
|
2020-05-31 20:41:17 +02:00
|
|
|
return nil, err
|
2020-04-25 02:25:51 +02:00
|
|
|
}
|
2020-05-31 20:41:17 +02:00
|
|
|
w, ok := c.(*webClient)
|
2020-05-28 02:35:09 +02:00
|
|
|
if ok {
|
2020-05-31 20:41:17 +02:00
|
|
|
w.permissions = perms
|
2020-05-28 02:35:09 +02:00
|
|
|
}
|
2020-04-25 02:25:51 +02:00
|
|
|
|
2020-05-18 15:24:04 +02:00
|
|
|
if !perms.Op && atomic.LoadUint32(&g.locked) != 0 {
|
2020-05-31 20:41:17 +02:00
|
|
|
return nil, userError("group is locked")
|
2020-05-18 15:24:04 +02:00
|
|
|
}
|
|
|
|
|
2020-04-24 19:38:21 +02:00
|
|
|
g.mu.Lock()
|
|
|
|
defer g.mu.Unlock()
|
2020-04-25 21:29:21 +02:00
|
|
|
|
2020-04-25 18:09:31 +02:00
|
|
|
if !perms.Op && g.description.MaxClients > 0 {
|
2020-04-25 02:37:41 +02:00
|
|
|
if len(g.clients) >= g.description.MaxClients {
|
2020-05-31 20:41:17 +02:00
|
|
|
return nil, userError("too many users")
|
2020-04-25 02:37:41 +02:00
|
|
|
}
|
|
|
|
}
|
2020-05-31 20:41:17 +02:00
|
|
|
if g.clients[c.getId()] != nil {
|
|
|
|
return nil, protocolError("duplicate client id")
|
2020-04-25 21:29:21 +02:00
|
|
|
}
|
|
|
|
|
2020-05-31 20:41:17 +02:00
|
|
|
g.clients[c.getId()] = c
|
|
|
|
|
|
|
|
go func(clients []client) {
|
2020-06-03 21:09:24 +02:00
|
|
|
c.pushClient(c.getId(), c.getUsername(), true)
|
2020-05-31 20:41:17 +02:00
|
|
|
for _, cc := range clients {
|
|
|
|
err := c.pushClient(cc.getId(), cc.getUsername(), true)
|
|
|
|
if err == ErrClientDead {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
cc.pushClient(c.getId(), c.getUsername(), true)
|
|
|
|
}
|
|
|
|
}(g.getClientsUnlocked(c))
|
|
|
|
|
|
|
|
return g, nil
|
2020-04-24 19:38:21 +02:00
|
|
|
}
|
|
|
|
|
2020-05-28 02:35:09 +02:00
|
|
|
func delClient(c client) {
|
|
|
|
g := c.getGroup()
|
|
|
|
g.mu.Lock()
|
|
|
|
defer g.mu.Unlock()
|
2020-04-25 21:29:21 +02:00
|
|
|
|
2020-05-28 02:35:09 +02:00
|
|
|
if g.clients[c.getId()] != c {
|
2020-04-25 21:29:21 +02:00
|
|
|
log.Printf("Deleting unknown client")
|
|
|
|
return
|
|
|
|
}
|
2020-05-28 02:35:09 +02:00
|
|
|
delete(g.clients, c.getId())
|
2020-05-31 20:41:17 +02:00
|
|
|
|
|
|
|
go func(clients []client) {
|
|
|
|
for _, cc := range clients {
|
|
|
|
cc.pushClient(c.getId(), c.getUsername(), false)
|
|
|
|
}
|
2020-06-03 21:09:24 +02:00
|
|
|
}(g.getClientsUnlocked(nil))
|
2020-04-24 19:38:21 +02:00
|
|
|
}
|
|
|
|
|
2020-05-28 02:35:09 +02:00
|
|
|
func (g *group) getClients(except client) []client {
|
2020-04-24 19:38:21 +02:00
|
|
|
g.mu.Lock()
|
|
|
|
defer g.mu.Unlock()
|
2020-05-31 20:41:17 +02:00
|
|
|
return g.getClientsUnlocked(except)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (g *group) getClientsUnlocked(except client) []client {
|
2020-05-28 02:35:09 +02:00
|
|
|
clients := make([]client, 0, len(g.clients))
|
2020-04-24 19:38:21 +02:00
|
|
|
for _, c := range g.clients {
|
|
|
|
if c != except {
|
|
|
|
clients = append(clients, c)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return clients
|
|
|
|
}
|
|
|
|
|
2020-05-28 02:35:09 +02:00
|
|
|
func (g *group) getClientUnlocked(id string) client {
|
|
|
|
for idd, c := range g.clients {
|
|
|
|
if idd == id {
|
2020-04-25 17:36:35 +02:00
|
|
|
return c
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2020-05-28 02:35:09 +02:00
|
|
|
func (g *group) Range(f func(c client) bool) {
|
2020-04-24 19:38:21 +02:00
|
|
|
g.mu.Lock()
|
|
|
|
defer g.mu.Unlock()
|
|
|
|
for _, c := range g.clients {
|
|
|
|
ok := f(c)
|
2020-04-25 02:25:51 +02:00
|
|
|
if !ok {
|
|
|
|
break
|
2020-04-24 19:38:21 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-04-25 21:16:49 +02:00
|
|
|
const maxChatHistory = 20
|
|
|
|
|
2020-04-30 19:13:10 +02:00
|
|
|
func (g *group) clearChatHistory() {
|
|
|
|
g.mu.Lock()
|
|
|
|
defer g.mu.Unlock()
|
|
|
|
g.history = nil
|
|
|
|
}
|
|
|
|
|
2020-04-25 21:16:49 +02:00
|
|
|
func (g *group) addToChatHistory(id, user, value string, me bool) {
|
|
|
|
g.mu.Lock()
|
|
|
|
defer g.mu.Unlock()
|
|
|
|
|
|
|
|
if len(g.history) >= maxChatHistory {
|
|
|
|
copy(g.history, g.history[1:])
|
|
|
|
g.history = g.history[:len(g.history)-1]
|
|
|
|
}
|
|
|
|
g.history = append(g.history,
|
|
|
|
chatHistoryEntry{id: id, user: user, value: value, me: me},
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (g *group) getChatHistory() []chatHistoryEntry {
|
|
|
|
g.mu.Lock()
|
2020-05-12 12:48:56 +02:00
|
|
|
defer g.mu.Unlock()
|
2020-04-25 21:16:49 +02:00
|
|
|
|
|
|
|
h := make([]chatHistoryEntry, len(g.history))
|
|
|
|
copy(h, g.history)
|
|
|
|
return h
|
|
|
|
}
|
|
|
|
|
2020-04-25 02:25:51 +02:00
|
|
|
type groupUser struct {
|
|
|
|
Username string `json:"username,omitempty"`
|
|
|
|
Password string `json:"password,omitempty"`
|
|
|
|
}
|
|
|
|
|
|
|
|
func matchUser(user, pass string, users []groupUser) (bool, bool) {
|
|
|
|
for _, u := range users {
|
2020-04-25 19:08:14 +02:00
|
|
|
if u.Username == "" {
|
|
|
|
if u.Password == "" || u.Password == pass {
|
|
|
|
return true, true
|
|
|
|
}
|
|
|
|
} else if u.Username == user {
|
2020-04-25 02:25:51 +02:00
|
|
|
return true, (u.Password == "" || u.Password == pass)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false, false
|
|
|
|
}
|
|
|
|
|
|
|
|
type groupDescription struct {
|
2020-04-25 03:40:01 +02:00
|
|
|
loadTime time.Time `json:"-"`
|
|
|
|
modTime time.Time `json:"-"`
|
|
|
|
fileSize int64 `json:"-"`
|
2020-04-25 02:25:51 +02:00
|
|
|
Public bool `json:"public,omitempty"`
|
2020-04-25 02:37:41 +02:00
|
|
|
MaxClients int `json:"max-clients,omitempty"`
|
2020-04-25 02:25:51 +02:00
|
|
|
AllowAnonymous bool `json:"allow-anonymous,omitempty"`
|
2020-05-30 00:23:54 +02:00
|
|
|
AllowRecording bool `json:"allow-recording,omitempty"`
|
2020-04-25 18:09:31 +02:00
|
|
|
Op []groupUser `json:"op,omitempty"`
|
2020-04-25 02:25:51 +02:00
|
|
|
Presenter []groupUser `json:"presenter,omitempty"`
|
|
|
|
Other []groupUser `json:"other,omitempty"`
|
|
|
|
}
|
|
|
|
|
2020-04-25 03:40:01 +02:00
|
|
|
func descriptionChanged(name string, old *groupDescription) (bool, error) {
|
|
|
|
fi, err := os.Stat(filepath.Join(groupsDir, name+".json"))
|
|
|
|
if err != nil {
|
|
|
|
if os.IsNotExist(err) {
|
|
|
|
err = userError("group does not exist")
|
|
|
|
}
|
|
|
|
return false, err
|
|
|
|
}
|
|
|
|
if fi.Size() != old.fileSize || fi.ModTime() != old.modTime {
|
|
|
|
return true, err
|
|
|
|
}
|
|
|
|
return false, err
|
|
|
|
}
|
|
|
|
|
2020-04-25 02:25:51 +02:00
|
|
|
func getDescription(name string) (*groupDescription, error) {
|
|
|
|
r, err := os.Open(filepath.Join(groupsDir, name+".json"))
|
|
|
|
if err != nil {
|
|
|
|
if os.IsNotExist(err) {
|
|
|
|
err = userError("group does not exist")
|
|
|
|
}
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
defer r.Close()
|
|
|
|
|
|
|
|
var desc groupDescription
|
2020-04-25 03:40:01 +02:00
|
|
|
|
|
|
|
fi, err := r.Stat()
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
desc.fileSize = fi.Size()
|
|
|
|
desc.modTime = fi.ModTime()
|
|
|
|
|
2020-04-25 02:25:51 +02:00
|
|
|
d := json.NewDecoder(r)
|
|
|
|
err = d.Decode(&desc)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2020-04-25 03:40:01 +02:00
|
|
|
desc.loadTime = time.Now()
|
2020-04-25 02:25:51 +02:00
|
|
|
return &desc, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
type userPermission struct {
|
2020-04-25 18:09:31 +02:00
|
|
|
Op bool `json:"op,omitempty"`
|
2020-04-25 02:25:51 +02:00
|
|
|
Present bool `json:"present,omitempty"`
|
2020-05-30 00:23:54 +02:00
|
|
|
Record bool `json:"record,omitempty"`
|
2020-04-25 02:25:51 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
func getPermission(desc *groupDescription, user, pass string) (userPermission, error) {
|
|
|
|
var p userPermission
|
|
|
|
if !desc.AllowAnonymous && user == "" {
|
2020-04-25 16:54:20 +02:00
|
|
|
return p, userError("anonymous users not allowed in this group, please choose a username")
|
2020-04-25 02:25:51 +02:00
|
|
|
}
|
2020-04-25 18:09:31 +02:00
|
|
|
if found, good := matchUser(user, pass, desc.Op); found {
|
2020-04-25 02:25:51 +02:00
|
|
|
if good {
|
2020-04-25 18:09:31 +02:00
|
|
|
p.Op = true
|
2020-04-25 02:25:51 +02:00
|
|
|
p.Present = true
|
2020-05-30 00:23:54 +02:00
|
|
|
if desc.AllowRecording {
|
|
|
|
p.Record = true
|
|
|
|
}
|
2020-04-25 02:25:51 +02:00
|
|
|
return p, nil
|
|
|
|
}
|
2020-04-25 17:36:35 +02:00
|
|
|
return p, userError("not authorised")
|
2020-04-25 02:25:51 +02:00
|
|
|
}
|
|
|
|
if found, good := matchUser(user, pass, desc.Presenter); found {
|
|
|
|
if good {
|
|
|
|
p.Present = true
|
|
|
|
return p, nil
|
|
|
|
}
|
2020-04-25 17:36:35 +02:00
|
|
|
return p, userError("not authorised")
|
2020-04-25 02:25:51 +02:00
|
|
|
}
|
|
|
|
if found, good := matchUser(user, pass, desc.Other); found {
|
|
|
|
if good {
|
|
|
|
return p, nil
|
|
|
|
}
|
2020-04-25 17:36:35 +02:00
|
|
|
return p, userError("not authorised")
|
2020-04-25 02:25:51 +02:00
|
|
|
}
|
2020-04-25 17:36:35 +02:00
|
|
|
return p, userError("not authorised")
|
|
|
|
}
|
|
|
|
|
2020-04-24 19:38:21 +02:00
|
|
|
type publicGroup struct {
|
|
|
|
Name string `json:"name"`
|
|
|
|
ClientCount int `json:"clientCount"`
|
|
|
|
}
|
|
|
|
|
|
|
|
func getPublicGroups() []publicGroup {
|
|
|
|
gs := make([]publicGroup, 0)
|
|
|
|
groups.mu.Lock()
|
|
|
|
defer groups.mu.Unlock()
|
|
|
|
for _, g := range groups.groups {
|
2020-04-25 02:25:51 +02:00
|
|
|
if g.description.Public {
|
2020-04-24 19:38:21 +02:00
|
|
|
gs = append(gs, publicGroup{
|
|
|
|
Name: g.name,
|
|
|
|
ClientCount: len(g.clients),
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
2020-04-30 22:32:44 +02:00
|
|
|
sort.Slice(gs, func(i, j int) bool {
|
|
|
|
return gs[i].Name < gs[j].Name
|
|
|
|
})
|
2020-04-24 19:38:21 +02:00
|
|
|
return gs
|
|
|
|
}
|
2020-04-25 04:08:43 +02:00
|
|
|
|
|
|
|
func readPublicGroups() {
|
|
|
|
dir, err := os.Open(groupsDir)
|
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
defer dir.Close()
|
|
|
|
|
|
|
|
fis, err := dir.Readdir(-1)
|
|
|
|
if err != nil {
|
|
|
|
log.Printf("readPublicGroups: %v", err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, fi := range fis {
|
|
|
|
if !strings.HasSuffix(fi.Name(), ".json") {
|
|
|
|
continue
|
|
|
|
}
|
2020-04-25 17:36:35 +02:00
|
|
|
name := fi.Name()[:len(fi.Name())-5]
|
2020-04-25 04:08:43 +02:00
|
|
|
desc, err := getDescription(name)
|
|
|
|
if err != nil {
|
2020-04-25 21:33:08 +02:00
|
|
|
if !os.IsNotExist(err) {
|
|
|
|
log.Printf("Reading group %v: %v", name, err)
|
|
|
|
}
|
2020-04-25 04:08:43 +02:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
if desc.Public {
|
|
|
|
addGroup(name, desc)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2020-04-29 16:08:07 +02:00
|
|
|
|
|
|
|
type groupStats struct {
|
|
|
|
name string
|
|
|
|
clients []clientStats
|
|
|
|
}
|
|
|
|
|
|
|
|
type clientStats struct {
|
|
|
|
id string
|
|
|
|
up, down []connStats
|
|
|
|
}
|
|
|
|
|
|
|
|
type connStats struct {
|
|
|
|
id string
|
|
|
|
tracks []trackStats
|
|
|
|
}
|
|
|
|
|
|
|
|
type trackStats struct {
|
2020-04-30 20:33:04 +02:00
|
|
|
bitrate uint64
|
|
|
|
maxBitrate uint64
|
|
|
|
loss uint8
|
2020-06-03 21:48:20 +02:00
|
|
|
rtt time.Duration
|
2020-05-02 16:32:34 +02:00
|
|
|
jitter time.Duration
|
2020-04-29 16:08:07 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
func getGroupStats() []groupStats {
|
|
|
|
names := getGroupNames()
|
|
|
|
|
|
|
|
gs := make([]groupStats, 0, len(names))
|
|
|
|
for _, name := range names {
|
|
|
|
g := getGroup(name)
|
|
|
|
if g == nil {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
clients := g.getClients(nil)
|
|
|
|
stats := groupStats{
|
|
|
|
name: name,
|
|
|
|
clients: make([]clientStats, 0, len(clients)),
|
|
|
|
}
|
|
|
|
for _, c := range clients {
|
2020-05-28 02:35:09 +02:00
|
|
|
c, ok := c.(*webClient)
|
|
|
|
if ok {
|
|
|
|
cs := getClientStats(c)
|
|
|
|
stats.clients = append(stats.clients, cs)
|
|
|
|
}
|
2020-04-29 16:08:07 +02:00
|
|
|
}
|
|
|
|
sort.Slice(stats.clients, func(i, j int) bool {
|
|
|
|
return stats.clients[i].id < stats.clients[j].id
|
|
|
|
})
|
|
|
|
gs = append(gs, stats)
|
|
|
|
}
|
|
|
|
sort.Slice(gs, func(i, j int) bool {
|
|
|
|
return gs[i].name < gs[j].name
|
|
|
|
})
|
|
|
|
|
|
|
|
return gs
|
|
|
|
}
|
|
|
|
|
2020-05-27 11:44:49 +02:00
|
|
|
func getClientStats(c *webClient) clientStats {
|
2020-04-29 16:08:07 +02:00
|
|
|
c.mu.Lock()
|
|
|
|
defer c.mu.Unlock()
|
|
|
|
|
|
|
|
cs := clientStats{
|
|
|
|
id: c.id,
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, up := range c.up {
|
|
|
|
conns := connStats{id: up.id}
|
|
|
|
for _, t := range up.tracks {
|
2020-05-01 04:41:15 +02:00
|
|
|
expected, lost, _, _ := t.cache.GetStats(false)
|
2020-04-29 16:08:07 +02:00
|
|
|
if expected == 0 {
|
|
|
|
expected = 1
|
|
|
|
}
|
2020-05-02 16:32:34 +02:00
|
|
|
loss := uint8(lost * 100 / expected)
|
|
|
|
jitter := time.Duration(t.jitter.Jitter()) *
|
|
|
|
(time.Second / time.Duration(t.jitter.HZ()))
|
2020-06-03 22:37:43 +02:00
|
|
|
rate, _ := t.rate.Estimate()
|
2020-04-29 16:08:07 +02:00
|
|
|
conns.tracks = append(conns.tracks, trackStats{
|
2020-06-03 22:37:43 +02:00
|
|
|
bitrate: uint64(rate) * 8,
|
2020-04-30 20:33:04 +02:00
|
|
|
maxBitrate: atomic.LoadUint64(&t.maxBitrate),
|
2020-05-02 16:32:34 +02:00
|
|
|
loss: loss,
|
|
|
|
jitter: jitter,
|
2020-04-29 16:08:07 +02:00
|
|
|
})
|
|
|
|
}
|
|
|
|
cs.up = append(cs.up, conns)
|
|
|
|
}
|
|
|
|
sort.Slice(cs.up, func(i, j int) bool {
|
|
|
|
return cs.up[i].id < cs.up[j].id
|
|
|
|
})
|
|
|
|
|
|
|
|
for _, down := range c.down {
|
|
|
|
conns := connStats{id: down.id}
|
|
|
|
for _, t := range down.tracks {
|
2020-06-03 20:26:54 +02:00
|
|
|
jiffies := rtptime.Jiffies()
|
2020-06-03 22:37:43 +02:00
|
|
|
rate, _ := t.rate.Estimate()
|
2020-06-03 21:48:20 +02:00
|
|
|
rtt := rtptime.ToDuration(atomic.LoadUint64(&t.rtt),
|
|
|
|
rtptime.JiffiesPerSec)
|
2020-06-03 20:26:54 +02:00
|
|
|
loss, jitter := t.stats.Get(jiffies)
|
2020-05-03 12:25:10 +02:00
|
|
|
j := time.Duration(jitter) * time.Second /
|
2020-05-02 16:32:34 +02:00
|
|
|
time.Duration(t.track.Codec().ClockRate)
|
2020-04-29 16:08:07 +02:00
|
|
|
conns.tracks = append(conns.tracks, trackStats{
|
2020-06-03 22:37:43 +02:00
|
|
|
bitrate: uint64(rate) * 8,
|
2020-06-03 20:26:54 +02:00
|
|
|
maxBitrate: t.GetMaxBitrate(jiffies),
|
2020-05-03 12:25:10 +02:00
|
|
|
loss: uint8(uint32(loss) * 100 / 256),
|
2020-06-03 21:48:20 +02:00
|
|
|
rtt: rtt,
|
2020-05-03 12:25:10 +02:00
|
|
|
jitter: j,
|
2020-04-29 16:08:07 +02:00
|
|
|
})
|
|
|
|
}
|
|
|
|
cs.down = append(cs.down, conns)
|
|
|
|
}
|
|
|
|
sort.Slice(cs.down, func(i, j int) bool {
|
|
|
|
return cs.down[i].id < cs.down[j].id
|
|
|
|
})
|
|
|
|
|
|
|
|
return cs
|
|
|
|
}
|