From 6098d4af9d4e3a639c6ad7f81059e16274fd6c89 Mon Sep 17 00:00:00 2001 From: Juliusz Chroboczek Date: Thu, 14 Jan 2021 03:56:37 +0100 Subject: [PATCH] Implement autolock. --- README | 2 ++ diskwriter/diskwriter.go | 4 +++ group/client.go | 1 + group/group.go | 55 +++++++++++++++++++++++++++------------- rtpconn/webclient.go | 4 +++ 5 files changed, 48 insertions(+), 18 deletions(-) diff --git a/README b/README index 01b0aa3..11bae08 100644 --- a/README +++ b/README @@ -235,6 +235,8 @@ the group. - `allow-anonymous`: if true, then users may connect with an empty username; - `allow-subgroups`: if true, then subgroups of the form `group/subgroup` are automatically created when first accessed; + - `autolock`: if true, the group will start locked and become locked + whenever there are no clients with operator privileges; - `redirect`: if set, then attempts to join the group will be redirected to the given URL; most other fields are ignored in this case; - `codecs`: this is a list of codecs allowed in this group. The default diff --git a/diskwriter/diskwriter.go b/diskwriter/diskwriter.go index 725d737..52db542 100644 --- a/diskwriter/diskwriter.go +++ b/diskwriter/diskwriter.go @@ -67,6 +67,10 @@ func (client *Client) SetPermissions(perms group.ClientPermissions) { return } +func (client *Client) Permissions() group.ClientPermissions { + return group.ClientPermissions{} +} + func (client *Client) PushClient(id, username string, add bool) error { return nil } diff --git a/group/client.go b/group/client.go index 67212a3..db51b87 100644 --- a/group/client.go +++ b/group/client.go @@ -96,6 +96,7 @@ type Client interface { Group() *Group Id() string Challengeable + Permissions() ClientPermissions SetPermissions(ClientPermissions) OverridePermissions(*Group) bool PushConn(g *Group, id string, conn conn.Up, tracks []conn.UpTrack) error diff --git a/group/group.go b/group/group.go index 16fa8ea..413e2fa 100644 --- a/group/group.go +++ b/group/group.go @@ -293,6 +293,7 @@ func Add(name string, desc *description) (*Group, error) { timestamp: time.Now(), api: APIFromNames(desc.Codecs), } + autolock(g, g.getClientsUnlocked(nil)) groups.groups[name] = g return g, nil } @@ -302,27 +303,24 @@ func Add(name string, desc *description) (*Group, error) { if desc != nil { g.description = desc - g.api = APIFromNames(desc.Codecs) + } else if time.Since(g.description.loadTime) < 5*time.Second { + return g, nil + } else if !descriptionChanged(name, g.description) { + g.description.loadTime = time.Now() return g, nil } - if time.Since(g.description.loadTime) > 5*time.Second { - if descriptionChanged(name, g.description) { - desc, err := GetDescription(name) - if err != nil { - if !os.IsNotExist(err) { - log.Printf("Reading group %v: %v", - name, err) - } - deleteUnlocked(g) - return nil, err - } - g.description = desc - g.api = APIFromNames(desc.Codecs) - } else { - g.description.loadTime = time.Now() + desc, err = GetDescription(name) + if err != nil { + if !os.IsNotExist(err) { + log.Printf("Reading group %v: %v", name, err) } + deleteUnlocked(g) + return nil, err } + g.description = desc + g.api = APIFromNames(desc.Codecs) + autolock(g, g.getClientsUnlocked(nil)) return g, nil } @@ -451,7 +449,7 @@ func AddClient(group string, c Client) (*Group, error) { if !perms.Op && g.locked != nil { m := *g.locked if m == "" { - m = "group is locked" + m = "this group is locked" } return nil, UserError(m) } @@ -483,6 +481,22 @@ func AddClient(group string, c Client) (*Group, error) { return g, nil } +// called locked +func autolock(g *Group, clients []Client) { + if g.locked == nil && g.description.Autolock { + lock := true + for _, c := range clients { + if c.Permissions().Op { + lock = false + } + } + if lock { + m := "this group is locked" + g.locked = &m + } + } +} + func DelClient(c Client) { g := c.Group() if g == nil { @@ -498,11 +512,15 @@ func DelClient(c Client) { delete(g.clients, c.Id()) g.timestamp = time.Now() + clients := g.getClientsUnlocked(nil) + go func(clients []Client) { for _, cc := range clients { cc.PushClient(c.Id(), c.Username(), false) } - }(g.getClientsUnlocked(nil)) + }(clients) + + autolock(g, clients) } func (g *Group) GetClients(except Client) []Client { @@ -668,6 +686,7 @@ type description struct { AllowAnonymous bool `json:"allow-anonymous,omitempty"` AllowRecording bool `json:"allow-recording,omitempty"` AllowSubgroups bool `json:"allow-subgroups,omitempty"` + Autolock bool `json:"autolock,omitempty"` Op []ClientCredentials `json:"op,omitempty"` Presenter []ClientCredentials `json:"presenter,omitempty"` Other []ClientCredentials `json:"other,omitempty"` diff --git a/rtpconn/webclient.go b/rtpconn/webclient.go index 7842772..fada36c 100644 --- a/rtpconn/webclient.go +++ b/rtpconn/webclient.go @@ -93,6 +93,10 @@ func (c *webClient) Challenge(group string, creds group.ClientCredentials) bool return m } +func (c *webClient) Permissions() group.ClientPermissions { + return c.permissions +} + func (c *webClient) SetPermissions(perms group.ClientPermissions) { c.permissions = perms }