1
Fork 0
mirror of https://github.com/jech/galene.git synced 2024-11-14 04:35:57 +01:00
galene/group.go

489 lines
9.4 KiB
Go
Raw Normal View History

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-25 04:08:43 +02:00
"strings"
2020-04-25 17:36:35 +02:00
"sync"
"time"
2020-04-24 19:38:21 +02:00
"github.com/pion/webrtc/v2"
)
type trackPair struct {
remote, local *webrtc.Track
}
type upConnection struct {
id string
label string
pc *webrtc.PeerConnection
maxAudioBitrate uint32
maxVideoBitrate uint32
streamCount int
pairs []trackPair
}
type downConnection struct {
id string
pc *webrtc.PeerConnection
remote *upConnection
maxBitrate uint32
}
type client struct {
2020-04-25 02:25:51 +02:00
group *group
id string
username string
permissions userPermission
done chan struct{}
writeCh chan interface{}
writerDone chan struct{}
actionCh chan interface{}
down map[string]*downConnection
up map[string]*upConnection
2020-04-24 19:38:21 +02:00
}
type group struct {
2020-04-25 02:25:51 +02:00
name string
dead bool
2020-04-25 02:25:51 +02:00
description *groupDescription
2020-04-24 19:38:21 +02:00
mu sync.Mutex
clients []*client
}
type delPCAction struct {
id string
}
type addTrackAction struct {
id string
track *webrtc.Track
remote *upConnection
done bool
}
type addLabelAction struct {
id string
label string
}
type getUpAction struct {
ch chan<- string
}
type pushTracksAction struct {
c *client
}
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)
m := webrtc.MediaEngine{}
m.RegisterCodec(webrtc.NewRTPVP8CodecExt(
webrtc.DefaultPayloadTypeVP8, 90000,
[]webrtc.RTCPFeedback{
webrtc.RTCPFeedback{"goog-remb", ""},
webrtc.RTCPFeedback{"nack", "pli"},
},
"",
))
2020-04-24 19:38:21 +02:00
m.RegisterCodec(webrtc.NewRTPOpusCodec(
webrtc.DefaultPayloadTypeOpus, 48000,
))
2020-04-24 19:38:21 +02:00
groups.api = webrtc.NewAPI(
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-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
} else if g.dead || time.Since(g.description.loadTime) > 5*time.Second {
changed, err := descriptionChanged(name, g.description)
if err != nil {
g.dead = true
if !g.description.Public {
delGroupUnlocked(name)
}
return nil, err
}
if changed {
desc, err := getDescription(name)
if err != nil {
g.dead = true
if !g.description.Public {
delGroupUnlocked(name)
}
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-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-04-25 02:25:51 +02:00
func addClient(name string, client *client, user, pass string) (*group, []userid, 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 {
return nil, nil, err
}
2020-04-25 02:25:51 +02:00
perms, err := getPermission(g.description, user, pass)
if err != nil {
return nil, nil, err
}
client.permissions = perms
2020-04-24 19:38:21 +02:00
var users []userid
g.mu.Lock()
defer g.mu.Unlock()
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 {
return nil, nil, userError("too many users")
}
}
2020-04-24 19:38:21 +02:00
for _, c := range g.clients {
users = append(users, userid{c.id, c.username})
}
g.clients = append(g.clients, client)
return g, users, nil
}
func delClient(c *client) {
c.group.mu.Lock()
defer c.group.mu.Unlock()
g := c.group
for i, cc := range g.clients {
if cc == c {
g.clients =
append(g.clients[:i], g.clients[i+1:]...)
c.group = nil
if len(g.clients) == 0 && !g.description.Public {
2020-04-25 02:01:04 +02:00
delGroupUnlocked(g.name)
}
2020-04-24 19:38:21 +02:00
return
}
}
log.Printf("Deleting unknown client")
c.group = nil
}
func (g *group) getClients(except *client) []*client {
g.mu.Lock()
defer g.mu.Unlock()
clients := make([]*client, 0, len(g.clients))
for _, c := range g.clients {
if c != except {
clients = append(clients, c)
}
}
return clients
}
2020-04-25 17:36:35 +02:00
func (g *group) getClientUnlocked(id string) *client {
for _, c := range g.clients {
if c.id == id {
return c
}
}
return nil
}
2020-04-24 19:38:21 +02:00
func (g *group) Range(f func(c *client) bool) {
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
}
}
}
type writerDeadError int
func (err writerDeadError) Error() string {
return "client writer died"
}
func (c *client) write(m clientMessage) error {
select {
case c.writeCh <- m:
return nil
case <-c.writerDone:
return writerDeadError(0)
}
}
2020-04-25 17:30:19 +02:00
func (c *client) error(err error) error {
switch e := err.(type) {
case userError:
return c.write(clientMessage{
2020-04-25 17:56:22 +02:00
Type: "error",
2020-04-25 17:30:19 +02:00
Message: "The server said: " + string(e),
})
default:
return err
}
}
2020-04-24 19:38:21 +02:00
type clientDeadError int
func (err clientDeadError) Error() string {
return "client dead"
}
func (c *client) action(m interface{}) error {
select {
case c.actionCh <- m:
return nil
case <-c.done:
return clientDeadError(0)
}
}
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 17:36:35 +02:00
if u.Username == "" || u.Username == user {
2020-04-25 02:25:51 +02:00
return true, (u.Password == "" || u.Password == pass)
}
}
return false, false
}
type groupDescription struct {
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-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"`
}
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
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
}
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"`
}
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
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")
}
func setPermission(g *group, id string, perm string) error {
g.mu.Lock()
defer g.mu.Unlock()
c := g.getClientUnlocked(id)
if c == nil {
return userError("no such user")
}
switch perm {
case "op":
2020-04-25 18:09:31 +02:00
c.permissions.Op = true
2020-04-25 17:36:35 +02:00
case "unop":
2020-04-25 18:09:31 +02:00
c.permissions.Op = false
2020-04-25 17:36:35 +02:00
case "present":
c.permissions.Present = true
case "unpresent":
c.permissions.Present = false
default:
return userError("unknown permission")
}
return c.action(permissionsChangedAction{})
2020-04-25 17:36:35 +02:00
}
func kickClient(g *group, id string) error {
g.mu.Lock()
defer g.mu.Unlock()
c := g.getClientUnlocked(id)
if c == nil {
return userError("no such user")
}
return c.action(kickAction{})
2020-04-25 02:25:51 +02:00
}
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),
})
}
}
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 {
continue
}
if desc.Public {
addGroup(name, desc)
}
}
}