1
Fork 0
mirror of https://github.com/jech/galene.git synced 2024-11-22 08:35:57 +01:00

Reload group descriptions periodically.

This commit is contained in:
Juliusz Chroboczek 2020-04-25 03:40:01 +02:00
parent 69fcdfbaba
commit 5796d4065b

View file

@ -11,6 +11,7 @@ import (
"os" "os"
"path/filepath" "path/filepath"
"sync" "sync"
"time"
"github.com/pion/webrtc/v2" "github.com/pion/webrtc/v2"
) )
@ -51,6 +52,7 @@ type client struct {
type group struct { type group struct {
name string name string
dead bool
description *groupDescription description *groupDescription
mu sync.Mutex mu sync.Mutex
@ -104,7 +106,6 @@ func addGroup(name string) (*group, error) {
} }
g := groups.groups[name] g := groups.groups[name]
if g == nil { if g == nil {
desc, err := getDescription(name) desc, err := getDescription(name)
if err != nil { if err != nil {
@ -115,6 +116,29 @@ func addGroup(name string) (*group, error) {
description: desc, description: desc,
} }
groups.groups[name] = g groups.groups[name] = g
} 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()
}
} }
return g, nil return g, nil
@ -175,7 +199,7 @@ func delClient(c *client) {
g.clients = g.clients =
append(g.clients[:i], g.clients[i+1:]...) append(g.clients[:i], g.clients[i+1:]...)
c.group = nil c.group = nil
if len(g.clients) == 0 { if len(g.clients) == 0 && !g.description.Public {
delGroupUnlocked(g.name) delGroupUnlocked(g.name)
} }
return return
@ -253,6 +277,9 @@ func matchUser(user, pass string, users []groupUser) (bool, bool) {
} }
type groupDescription struct { type groupDescription struct {
loadTime time.Time `json:"-"`
modTime time.Time `json:"-"`
fileSize int64 `json:"-"`
Public bool `json:"public,omitempty"` Public bool `json:"public,omitempty"`
MaxClients int `json:"max-clients,omitempty"` MaxClients int `json:"max-clients,omitempty"`
AllowAnonymous bool `json:"allow-anonymous,omitempty"` AllowAnonymous bool `json:"allow-anonymous,omitempty"`
@ -261,29 +288,50 @@ type groupDescription struct {
Other []groupUser `json:"other,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
}
func getDescription(name string) (*groupDescription, error) { func getDescription(name string) (*groupDescription, error) {
r, err := os.Open(filepath.Join(groupsDir, name+".json")) r, err := os.Open(filepath.Join(groupsDir, name+".json"))
if err != nil { if err != nil {
if os.IsNotExist(err) { if os.IsNotExist(err) {
err = userError("group does not exist") err = userError("group does not exist")
} else if os.IsPermission(err) {
err = userError("unauthorised")
} }
return nil, err return nil, err
} }
defer r.Close() defer r.Close()
var desc groupDescription var desc groupDescription
fi, err := r.Stat()
if err != nil {
return nil, err
}
desc.fileSize = fi.Size()
desc.modTime = fi.ModTime()
d := json.NewDecoder(r) d := json.NewDecoder(r)
err = d.Decode(&desc) err = d.Decode(&desc)
if err != nil { if err != nil {
return nil, err return nil, err
} }
desc.loadTime = time.Now()
return &desc, nil return &desc, nil
} }
type userPermission struct { type userPermission struct {
Admin bool `json:"admin,omitempty"` Admin bool `json:"admin,omitempty"`
Present bool `json:"present,omitempty"` Present bool `json:"present,omitempty"`
} }