2020-09-13 11:56:35 +02:00
|
|
|
package group
|
2020-04-24 19:38:21 +02:00
|
|
|
|
|
|
|
import (
|
2020-04-25 02:25:51 +02:00
|
|
|
"encoding/json"
|
2020-12-02 00:07:31 +01:00
|
|
|
"errors"
|
2023-07-12 12:11:40 +02:00
|
|
|
"fmt"
|
2022-07-12 19:22:03 +02:00
|
|
|
"io/fs"
|
2020-04-24 19:38:21 +02:00
|
|
|
"log"
|
2021-10-29 23:37:05 +02:00
|
|
|
"net/url"
|
2020-04-25 02:25:51 +02:00
|
|
|
"os"
|
2020-11-22 19:54:54 +01:00
|
|
|
"path"
|
2020-04-25 02:25:51 +02:00
|
|
|
"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-25 03:40:01 +02:00
|
|
|
"time"
|
2020-04-24 19:38:21 +02:00
|
|
|
|
2020-10-06 06:08:29 +02:00
|
|
|
"github.com/pion/ice/v2"
|
2021-04-29 17:03:25 +02:00
|
|
|
"github.com/pion/sdp/v3"
|
2020-07-16 20:17:32 +02:00
|
|
|
"github.com/pion/webrtc/v3"
|
2021-10-29 23:37:05 +02:00
|
|
|
|
|
|
|
"github.com/jech/galene/token"
|
2020-04-24 19:38:21 +02:00
|
|
|
)
|
|
|
|
|
2021-10-26 20:10:24 +02:00
|
|
|
var Directory, DataDirectory string
|
2020-10-06 06:08:29 +02:00
|
|
|
var UseMDNS bool
|
2021-04-29 22:02:36 +02:00
|
|
|
var UDPMin, UDPMax uint16
|
2020-09-13 11:56:35 +02:00
|
|
|
|
2024-03-03 13:34:18 +01:00
|
|
|
type NotAuthorisedError struct {
|
|
|
|
err error
|
|
|
|
}
|
2024-04-09 16:51:29 +02:00
|
|
|
|
2024-03-03 13:34:18 +01:00
|
|
|
func (err *NotAuthorisedError) Error() string {
|
|
|
|
if err.err != nil {
|
|
|
|
return "not authorised: " + err.err.Error()
|
|
|
|
}
|
|
|
|
return "not authorised"
|
|
|
|
}
|
|
|
|
func (err *NotAuthorisedError) Unwrap() error {
|
|
|
|
return err.err
|
|
|
|
}
|
|
|
|
|
|
|
|
var ErrDuplicateUsername = &NotAuthorisedError{
|
|
|
|
errors.New("this username is taken"),
|
|
|
|
}
|
2020-12-02 00:07:31 +01:00
|
|
|
|
2020-09-13 11:56:35 +02:00
|
|
|
type UserError string
|
|
|
|
|
|
|
|
func (err UserError) Error() string {
|
|
|
|
return string(err)
|
|
|
|
}
|
|
|
|
|
2020-11-30 16:26:11 +01:00
|
|
|
type KickError struct {
|
|
|
|
Id string
|
2023-03-22 17:41:16 +01:00
|
|
|
Username *string
|
2020-11-30 16:26:11 +01:00
|
|
|
Message string
|
|
|
|
}
|
|
|
|
|
|
|
|
func (err KickError) Error() string {
|
|
|
|
m := "kicked out"
|
|
|
|
if err.Message != "" {
|
2021-08-24 00:04:30 +02:00
|
|
|
m += " (" + err.Message + ")"
|
2020-11-30 16:26:11 +01:00
|
|
|
}
|
2023-03-22 17:41:16 +01:00
|
|
|
if err.Username != nil && *err.Username != "" {
|
|
|
|
m += " by " + *err.Username
|
2020-11-30 16:26:11 +01:00
|
|
|
}
|
|
|
|
return m
|
|
|
|
}
|
|
|
|
|
2020-09-13 11:56:35 +02:00
|
|
|
type ProtocolError string
|
|
|
|
|
|
|
|
func (err ProtocolError) Error() string {
|
|
|
|
return string(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
type ChatHistoryEntry struct {
|
2024-08-11 19:00:17 +02:00
|
|
|
Id string
|
|
|
|
Source string
|
|
|
|
User *string
|
|
|
|
Time time.Time
|
|
|
|
Kind string
|
|
|
|
Value interface{}
|
2020-04-25 21:16:49 +02:00
|
|
|
}
|
|
|
|
|
2020-05-09 12:06:13 +02:00
|
|
|
const (
|
2021-05-11 15:28:30 +02:00
|
|
|
LowBitrate = 100 * 1024
|
|
|
|
MinBitrate = LowBitrate * 2
|
2022-01-25 19:45:49 +01:00
|
|
|
MaxBitrate = 1024 * 1024 * 1024
|
2020-05-09 12:06:13 +02:00
|
|
|
)
|
|
|
|
|
2020-09-13 11:56:35 +02:00
|
|
|
type Group struct {
|
2020-09-13 13:24:05 +02:00
|
|
|
name string
|
2020-04-24 19:38:21 +02:00
|
|
|
|
2020-09-13 13:24:05 +02:00
|
|
|
mu sync.Mutex
|
2021-04-22 17:58:30 +02:00
|
|
|
description *Description
|
2020-12-01 19:30:45 +01:00
|
|
|
locked *string
|
|
|
|
clients map[string]Client
|
|
|
|
history []ChatHistoryEntry
|
|
|
|
timestamp time.Time
|
2022-01-29 22:54:44 +01:00
|
|
|
data map[string]interface{}
|
2020-04-24 19:38:21 +02:00
|
|
|
}
|
|
|
|
|
2020-09-13 11:56:35 +02:00
|
|
|
func (g *Group) Name() string {
|
|
|
|
return g.name
|
|
|
|
}
|
|
|
|
|
2020-09-18 11:40:00 +02:00
|
|
|
func (g *Group) Locked() (bool, string) {
|
2020-09-13 13:24:05 +02:00
|
|
|
g.mu.Lock()
|
|
|
|
defer g.mu.Unlock()
|
2020-09-24 22:03:41 +02:00
|
|
|
if g.locked != nil {
|
2020-09-18 11:40:00 +02:00
|
|
|
return true, *g.locked
|
|
|
|
} else {
|
|
|
|
return false, ""
|
|
|
|
}
|
2020-09-13 13:24:05 +02:00
|
|
|
}
|
|
|
|
|
2020-09-18 11:40:00 +02:00
|
|
|
func (g *Group) SetLocked(locked bool, message string) {
|
2020-09-13 13:24:05 +02:00
|
|
|
g.mu.Lock()
|
2020-09-18 11:40:00 +02:00
|
|
|
if locked {
|
|
|
|
g.locked = &message
|
|
|
|
} else {
|
|
|
|
g.locked = nil
|
|
|
|
}
|
2021-07-16 19:41:00 +02:00
|
|
|
clients := g.getClientsUnlocked(nil)
|
|
|
|
g.mu.Unlock()
|
|
|
|
|
|
|
|
for _, c := range clients {
|
|
|
|
c.Joined(g.Name(), "change")
|
|
|
|
}
|
2020-09-13 13:24:05 +02:00
|
|
|
}
|
|
|
|
|
2022-01-29 22:54:44 +01:00
|
|
|
func (g *Group) Data() map[string]interface{} {
|
|
|
|
g.mu.Lock()
|
|
|
|
defer g.mu.Unlock()
|
|
|
|
return g.data
|
|
|
|
}
|
|
|
|
|
|
|
|
func (g *Group) UpdateData(d map[string]interface{}) {
|
|
|
|
g.mu.Lock()
|
|
|
|
if g.data == nil {
|
|
|
|
g.data = make(map[string]interface{})
|
|
|
|
}
|
|
|
|
for k, v := range d {
|
|
|
|
if v == nil {
|
|
|
|
delete(g.data, k)
|
|
|
|
} else {
|
|
|
|
g.data[k] = v
|
|
|
|
}
|
|
|
|
}
|
|
|
|
clients := g.getClientsUnlocked(nil)
|
|
|
|
g.mu.Unlock()
|
|
|
|
|
|
|
|
for _, c := range clients {
|
|
|
|
c.Joined(g.Name(), "change")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-10-30 18:32:49 +02:00
|
|
|
func (g *Group) Description() *Description {
|
2020-09-13 13:24:05 +02:00
|
|
|
g.mu.Lock()
|
|
|
|
defer g.mu.Unlock()
|
2021-10-30 18:32:49 +02:00
|
|
|
return g.description
|
2021-07-16 19:41:00 +02:00
|
|
|
}
|
|
|
|
|
2021-10-26 22:22:48 +02:00
|
|
|
func (g *Group) ClientCount() int {
|
|
|
|
g.mu.Lock()
|
|
|
|
defer g.mu.Unlock()
|
|
|
|
return len(g.clients)
|
|
|
|
}
|
|
|
|
|
2022-10-08 22:46:28 +02:00
|
|
|
func (g *Group) mayExpire() bool {
|
2021-08-22 16:36:18 +02:00
|
|
|
g.mu.Lock()
|
|
|
|
defer g.mu.Unlock()
|
2022-10-08 22:46:28 +02:00
|
|
|
|
|
|
|
if g.description.Public {
|
|
|
|
return false
|
|
|
|
}
|
2021-08-22 16:36:18 +02:00
|
|
|
if len(g.clients) > 0 {
|
2022-10-08 22:46:28 +02:00
|
|
|
return false
|
2021-08-22 16:36:18 +02:00
|
|
|
}
|
2022-10-08 22:46:28 +02:00
|
|
|
return time.Since(g.timestamp) > maxHistoryAge(g.description)
|
2021-08-22 16:36:18 +02:00
|
|
|
}
|
|
|
|
|
2020-04-24 19:38:21 +02:00
|
|
|
var groups struct {
|
|
|
|
mu sync.Mutex
|
2020-09-13 11:56:35 +02:00
|
|
|
groups map[string]*Group
|
2020-04-24 19:38:21 +02:00
|
|
|
}
|
|
|
|
|
2021-04-29 20:38:06 +02:00
|
|
|
func (g *Group) API() (*webrtc.API, error) {
|
2021-02-04 21:03:17 +01:00
|
|
|
g.mu.Lock()
|
|
|
|
codecs := g.description.Codecs
|
|
|
|
g.mu.Unlock()
|
|
|
|
|
|
|
|
return APIFromNames(codecs)
|
2020-12-25 17:33:44 +01:00
|
|
|
}
|
|
|
|
|
2021-08-02 15:53:33 +02:00
|
|
|
func fmtpValue(fmtp, key string) string {
|
|
|
|
fields := strings.Split(fmtp, ";")
|
|
|
|
for _, f := range fields {
|
2023-12-09 18:23:06 +01:00
|
|
|
k, v, found := strings.Cut(f, "=")
|
|
|
|
if found && k == key {
|
|
|
|
return v
|
2021-08-02 15:53:33 +02:00
|
|
|
}
|
2020-12-25 17:33:44 +01:00
|
|
|
}
|
2021-08-02 15:53:33 +02:00
|
|
|
return ""
|
2020-09-13 13:01:06 +02:00
|
|
|
}
|
|
|
|
|
2021-08-02 15:53:33 +02:00
|
|
|
func CodecPayloadType(codec webrtc.RTPCodecCapability) (webrtc.PayloadType, error) {
|
2020-12-25 17:33:44 +01:00
|
|
|
switch strings.ToLower(codec.MimeType) {
|
|
|
|
case "video/vp8":
|
|
|
|
return 96, nil
|
|
|
|
case "video/vp9":
|
2021-08-02 15:53:33 +02:00
|
|
|
profile := fmtpValue(codec.SDPFmtpLine, "profile-id")
|
2021-08-22 16:36:18 +02:00
|
|
|
switch profile {
|
2023-07-18 12:05:26 +02:00
|
|
|
case "", "0":
|
2021-08-02 15:53:33 +02:00
|
|
|
return 98, nil
|
|
|
|
case "2":
|
|
|
|
return 100, nil
|
|
|
|
default:
|
2023-07-12 12:11:40 +02:00
|
|
|
return 0, fmt.Errorf("unknown VP9 profile %v", profile)
|
|
|
|
|
2021-08-02 15:53:33 +02:00
|
|
|
}
|
2022-01-04 13:09:07 +01:00
|
|
|
case "video/av1":
|
2021-08-02 15:53:33 +02:00
|
|
|
return 35, nil
|
2020-12-25 17:33:44 +01:00
|
|
|
case "video/h264":
|
2021-08-02 15:53:33 +02:00
|
|
|
profile := fmtpValue(codec.SDPFmtpLine, "profile-level-id")
|
2023-07-18 12:05:26 +02:00
|
|
|
if profile == "" {
|
|
|
|
return 102, nil
|
|
|
|
}
|
2021-08-02 15:53:33 +02:00
|
|
|
if len(profile) < 4 {
|
|
|
|
return 0, errors.New("malforned H.264 profile")
|
|
|
|
}
|
2021-08-22 16:36:18 +02:00
|
|
|
switch strings.ToLower(profile[:4]) {
|
2021-08-02 15:53:33 +02:00
|
|
|
case "4200":
|
|
|
|
return 102, nil
|
|
|
|
case "42e0":
|
|
|
|
return 108, nil
|
|
|
|
default:
|
2023-07-12 12:11:40 +02:00
|
|
|
return 0, fmt.Errorf(
|
|
|
|
"unknown H.264 profile %v", profile,
|
|
|
|
)
|
2021-08-02 15:53:33 +02:00
|
|
|
}
|
2020-12-25 17:33:44 +01:00
|
|
|
case "audio/opus":
|
|
|
|
return 111, nil
|
|
|
|
case "audio/g722":
|
|
|
|
return 9, nil
|
|
|
|
case "audio/pcmu":
|
|
|
|
return 0, nil
|
|
|
|
case "audio/pcma":
|
|
|
|
return 8, nil
|
|
|
|
default:
|
2023-07-12 12:11:40 +02:00
|
|
|
return 0, fmt.Errorf("unknown codec %v", codec.MimeType)
|
2020-12-04 22:42:20 +01:00
|
|
|
}
|
2020-12-25 17:33:44 +01:00
|
|
|
}
|
2020-12-04 22:42:20 +01:00
|
|
|
|
2024-11-30 17:55:24 +01:00
|
|
|
// VideoRTCPFeedback are the RTCP feedback types that we expect for video
|
|
|
|
// tracks.
|
|
|
|
var VideoRTCPFeedback = []webrtc.RTCPFeedback{
|
|
|
|
{"goog-remb", ""},
|
|
|
|
{"nack", ""},
|
|
|
|
{"nack", "pli"},
|
|
|
|
{"ccm", "fir"},
|
|
|
|
}
|
2021-08-02 15:53:33 +02:00
|
|
|
|
2024-11-30 17:55:24 +01:00
|
|
|
// AudioRTCPFeedback is like VideoRTCPFeedback but for audio tracks.
|
|
|
|
var AudioRTCPFeedback = []webrtc.RTCPFeedback(nil)
|
|
|
|
|
|
|
|
func codecsFromName(name string) ([]webrtc.RTPCodecParameters, error) {
|
2021-08-02 15:53:33 +02:00
|
|
|
var codecs []webrtc.RTPCodecCapability
|
|
|
|
|
|
|
|
switch name {
|
|
|
|
case "vp8":
|
|
|
|
codecs = []webrtc.RTPCodecCapability{
|
|
|
|
{
|
|
|
|
"video/VP8", 90000, 0,
|
|
|
|
"",
|
2024-11-30 17:55:24 +01:00
|
|
|
VideoRTCPFeedback,
|
2021-08-02 15:53:33 +02:00
|
|
|
},
|
|
|
|
}
|
|
|
|
case "vp9":
|
|
|
|
codecs = []webrtc.RTPCodecCapability{
|
|
|
|
{
|
|
|
|
"video/VP9", 90000, 0,
|
|
|
|
"profile-id=0",
|
2024-11-30 17:55:24 +01:00
|
|
|
VideoRTCPFeedback,
|
2021-08-02 15:53:33 +02:00
|
|
|
},
|
|
|
|
{
|
|
|
|
"video/VP9", 90000, 0,
|
|
|
|
"profile-id=2",
|
2024-11-30 17:55:24 +01:00
|
|
|
VideoRTCPFeedback,
|
2021-08-02 15:53:33 +02:00
|
|
|
},
|
|
|
|
}
|
|
|
|
case "av1":
|
|
|
|
codecs = []webrtc.RTPCodecCapability{
|
|
|
|
{
|
2022-01-04 13:09:07 +01:00
|
|
|
"video/AV1", 90000, 0,
|
2021-08-02 15:53:33 +02:00
|
|
|
"",
|
2024-11-30 17:55:24 +01:00
|
|
|
VideoRTCPFeedback,
|
2021-08-02 15:53:33 +02:00
|
|
|
},
|
|
|
|
}
|
|
|
|
case "h264":
|
|
|
|
codecs = []webrtc.RTPCodecCapability{
|
|
|
|
{
|
|
|
|
"video/H264", 90000, 0,
|
|
|
|
"level-asymmetry-allowed=1;packetization-mode=1;profile-level-id=42e01f",
|
2024-11-30 17:55:24 +01:00
|
|
|
VideoRTCPFeedback,
|
2021-08-02 15:53:33 +02:00
|
|
|
},
|
|
|
|
}
|
|
|
|
case "opus":
|
|
|
|
codecs = []webrtc.RTPCodecCapability{
|
|
|
|
{
|
|
|
|
"audio/opus", 48000, 2,
|
|
|
|
"minptime=10;useinbandfec=1;stereo=1;sprop-stereo=1",
|
2024-11-30 17:55:24 +01:00
|
|
|
AudioRTCPFeedback,
|
2021-08-02 15:53:33 +02:00
|
|
|
},
|
|
|
|
}
|
|
|
|
case "g722":
|
|
|
|
codecs = []webrtc.RTPCodecCapability{
|
|
|
|
{
|
|
|
|
"audio/G722", 8000, 1,
|
|
|
|
"",
|
2024-11-30 17:55:24 +01:00
|
|
|
AudioRTCPFeedback,
|
2021-08-02 15:53:33 +02:00
|
|
|
},
|
|
|
|
}
|
|
|
|
case "pcmu":
|
|
|
|
codecs = []webrtc.RTPCodecCapability{
|
|
|
|
{
|
|
|
|
"audio/PCMU", 8000, 1,
|
|
|
|
"",
|
2024-11-30 17:55:24 +01:00
|
|
|
AudioRTCPFeedback,
|
2021-08-02 15:53:33 +02:00
|
|
|
},
|
|
|
|
}
|
|
|
|
case "pcma":
|
|
|
|
codecs = []webrtc.RTPCodecCapability{
|
|
|
|
{
|
|
|
|
"audio/PCMU", 8000, 1,
|
|
|
|
"",
|
2024-11-30 17:55:24 +01:00
|
|
|
AudioRTCPFeedback,
|
2021-08-02 15:53:33 +02:00
|
|
|
},
|
|
|
|
}
|
|
|
|
default:
|
|
|
|
return nil, errors.New("unknown codec")
|
|
|
|
}
|
|
|
|
|
|
|
|
parms := make([]webrtc.RTPCodecParameters, 0, len(codecs))
|
|
|
|
for _, c := range codecs {
|
|
|
|
ptype, err := CodecPayloadType(c)
|
|
|
|
if err != nil {
|
|
|
|
log.Printf("Couldn't determine ptype for codec %v: %v",
|
|
|
|
c.MimeType, err)
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
parms = append(parms, webrtc.RTPCodecParameters{
|
|
|
|
RTPCodecCapability: c,
|
|
|
|
PayloadType: ptype,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
return parms, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func APIFromCodecs(codecs []webrtc.RTPCodecParameters) (*webrtc.API, error) {
|
2020-12-25 17:33:44 +01:00
|
|
|
s := webrtc.SettingEngine{}
|
|
|
|
s.SetSRTPReplayProtectionWindow(512)
|
2023-07-14 14:44:24 +02:00
|
|
|
s.DisableActiveTCP(true)
|
2020-12-25 17:33:44 +01:00
|
|
|
if !UseMDNS {
|
|
|
|
s.SetICEMulticastDNSMode(ice.MulticastDNSModeDisabled)
|
|
|
|
}
|
|
|
|
m := webrtc.MediaEngine{}
|
|
|
|
|
|
|
|
for _, codec := range codecs {
|
2021-08-02 15:53:33 +02:00
|
|
|
tpe := webrtc.RTPCodecTypeVideo
|
|
|
|
if strings.HasPrefix(strings.ToLower(codec.MimeType), "audio/") {
|
2020-12-25 17:33:44 +01:00
|
|
|
tpe = webrtc.RTPCodecTypeAudio
|
|
|
|
}
|
2021-08-02 15:53:33 +02:00
|
|
|
err := m.RegisterCodec(codec, tpe)
|
2021-04-29 20:38:06 +02:00
|
|
|
if err != nil {
|
|
|
|
log.Printf("%v", err)
|
|
|
|
continue
|
|
|
|
}
|
2020-04-24 19:38:21 +02:00
|
|
|
}
|
2021-04-29 22:02:36 +02:00
|
|
|
|
|
|
|
if UDPMin > 0 && UDPMax > 0 {
|
|
|
|
s.SetEphemeralUDPPortRange(UDPMin, UDPMax)
|
|
|
|
}
|
2021-04-29 17:03:25 +02:00
|
|
|
m.RegisterHeaderExtension(
|
|
|
|
webrtc.RTPHeaderExtensionCapability{sdp.SDESMidURI},
|
|
|
|
webrtc.RTPCodecTypeVideo)
|
|
|
|
m.RegisterHeaderExtension(
|
|
|
|
webrtc.RTPHeaderExtensionCapability{sdp.SDESRTPStreamIDURI},
|
|
|
|
webrtc.RTPCodecTypeVideo)
|
2021-04-29 22:02:36 +02:00
|
|
|
|
2020-12-25 17:33:44 +01:00
|
|
|
return webrtc.NewAPI(
|
|
|
|
webrtc.WithSettingEngine(s),
|
|
|
|
webrtc.WithMediaEngine(&m),
|
2021-04-29 20:38:06 +02:00
|
|
|
), nil
|
2020-12-25 17:33:44 +01:00
|
|
|
}
|
|
|
|
|
2021-04-29 20:38:06 +02:00
|
|
|
func APIFromNames(names []string) (*webrtc.API, error) {
|
2021-01-04 01:41:34 +01:00
|
|
|
if len(names) == 0 {
|
2023-09-01 16:00:09 +02:00
|
|
|
names = []string{"vp8", "opus"}
|
2021-01-04 01:41:34 +01:00
|
|
|
}
|
2021-08-02 15:53:33 +02:00
|
|
|
var codecs []webrtc.RTPCodecParameters
|
2021-01-04 01:41:34 +01:00
|
|
|
for _, n := range names {
|
2021-08-02 15:53:33 +02:00
|
|
|
cs, err := codecsFromName(n)
|
2021-01-04 01:41:34 +01:00
|
|
|
if err != nil {
|
|
|
|
log.Printf("Codec %v: %v", n, err)
|
|
|
|
continue
|
|
|
|
}
|
2021-08-02 15:53:33 +02:00
|
|
|
codecs = append(codecs, cs...)
|
2021-01-04 01:41:34 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
return APIFromCodecs(codecs)
|
|
|
|
}
|
|
|
|
|
2021-04-22 17:58:30 +02:00
|
|
|
func Add(name string, desc *Description) (*Group, error) {
|
2021-07-16 19:41:00 +02:00
|
|
|
g, notify, err := add(name, desc)
|
|
|
|
for _, c := range notify {
|
|
|
|
c.Joined(g.Name(), "change")
|
|
|
|
}
|
|
|
|
return g, err
|
|
|
|
}
|
|
|
|
|
2021-08-24 00:31:46 +02:00
|
|
|
func validGroupName(name string) bool {
|
|
|
|
if filepath.Separator != '/' &&
|
|
|
|
strings.ContainsRune(name, filepath.Separator) {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
s := path.Clean("/" + name)
|
|
|
|
if s == "/" {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
return s == "/"+name
|
|
|
|
}
|
|
|
|
|
2021-07-16 19:41:00 +02:00
|
|
|
func add(name string, desc *Description) (*Group, []Client, error) {
|
2021-08-24 00:31:46 +02:00
|
|
|
if !validGroupName(name) {
|
2021-07-16 19:41:00 +02:00
|
|
|
return nil, nil, UserError("illegal group name")
|
2020-12-25 17:33:44 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
groups.mu.Lock()
|
|
|
|
defer groups.mu.Unlock()
|
|
|
|
|
|
|
|
if groups.groups == nil {
|
|
|
|
groups.groups = make(map[string]*Group)
|
|
|
|
}
|
2020-04-24 19:38:21 +02:00
|
|
|
|
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 {
|
2024-04-08 23:52:29 +02:00
|
|
|
desc, err = readDescription(name, true)
|
2020-04-25 04:08:43 +02:00
|
|
|
if err != nil {
|
2021-07-16 19:41:00 +02:00
|
|
|
return nil, nil, err
|
2020-04-25 04:08:43 +02:00
|
|
|
}
|
2020-04-25 02:25:51 +02:00
|
|
|
}
|
2020-12-25 17:33:44 +01:00
|
|
|
|
2020-09-13 11:56:35 +02:00
|
|
|
g = &Group{
|
2020-04-25 02:25:51 +02:00
|
|
|
name: name,
|
|
|
|
description: desc,
|
2020-09-13 11:56:35 +02:00
|
|
|
clients: make(map[string]Client),
|
2020-12-01 19:30:45 +01:00
|
|
|
timestamp: time.Now(),
|
2020-04-24 19:38:21 +02:00
|
|
|
}
|
|
|
|
groups.groups[name] = g
|
2020-09-13 13:24:05 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
g.mu.Lock()
|
|
|
|
defer g.mu.Unlock()
|
|
|
|
|
2022-10-07 03:02:35 +02:00
|
|
|
notify := false
|
2020-09-13 13:24:05 +02:00
|
|
|
if desc != nil {
|
2022-09-22 17:30:48 +02:00
|
|
|
if !descriptionMatch(g.description, desc) {
|
|
|
|
g.description = desc
|
2022-10-07 03:02:35 +02:00
|
|
|
notify = true
|
2022-07-12 19:22:03 +02:00
|
|
|
}
|
2022-09-22 17:30:48 +02:00
|
|
|
} else if !descriptionUnchanged(name, g.description) {
|
2024-04-08 23:52:29 +02:00
|
|
|
desc, err = readDescription(name, true)
|
2022-07-12 19:22:03 +02:00
|
|
|
if err != nil {
|
2024-04-14 13:33:52 +02:00
|
|
|
if !errors.Is(err, os.ErrNotExist) {
|
2022-07-12 19:22:03 +02:00
|
|
|
log.Printf("Reading group %v: %v", name, err)
|
|
|
|
}
|
|
|
|
deleteUnlocked(g)
|
|
|
|
return nil, nil, err
|
2020-04-25 03:40:01 +02:00
|
|
|
}
|
2022-07-12 19:22:03 +02:00
|
|
|
g.description = desc
|
2022-10-07 03:02:35 +02:00
|
|
|
notify = true
|
2020-04-24 19:38:21 +02:00
|
|
|
}
|
2022-07-12 19:22:03 +02:00
|
|
|
|
2022-09-22 17:21:49 +02:00
|
|
|
autoLockKick(g)
|
2020-04-24 19:38:21 +02:00
|
|
|
|
2022-10-07 03:02:35 +02:00
|
|
|
var clients []Client
|
|
|
|
if notify {
|
|
|
|
clients = g.getClientsUnlocked(nil)
|
|
|
|
}
|
|
|
|
return g, clients, nil
|
2020-04-24 19:38:21 +02:00
|
|
|
}
|
|
|
|
|
2020-09-13 11:56:35 +02:00
|
|
|
func Range(f func(g *Group) bool) {
|
2020-04-29 16:08:07 +02:00
|
|
|
groups.mu.Lock()
|
|
|
|
defer groups.mu.Unlock()
|
|
|
|
|
2020-09-13 12:24:06 +02:00
|
|
|
for _, g := range groups.groups {
|
|
|
|
ok := f(g)
|
|
|
|
if !ok {
|
|
|
|
break
|
|
|
|
}
|
2020-04-29 16:08:07 +02:00
|
|
|
}
|
2020-09-13 12:24:06 +02:00
|
|
|
}
|
|
|
|
|
2020-09-13 11:56:35 +02:00
|
|
|
func GetNames() []string {
|
2020-09-13 12:24:06 +02:00
|
|
|
names := make([]string, 0)
|
|
|
|
|
2020-09-13 11:56:35 +02:00
|
|
|
Range(func(g *Group) bool {
|
2020-09-13 12:24:06 +02:00
|
|
|
names = append(names, g.name)
|
|
|
|
return true
|
|
|
|
})
|
2020-04-29 16:08:07 +02:00
|
|
|
return names
|
|
|
|
}
|
|
|
|
|
2020-12-02 19:47:32 +01:00
|
|
|
type SubGroup struct {
|
|
|
|
Name string
|
|
|
|
Clients int
|
|
|
|
}
|
|
|
|
|
|
|
|
func GetSubGroups(parent string) []SubGroup {
|
|
|
|
prefix := parent + "/"
|
|
|
|
subgroups := make([]SubGroup, 0)
|
|
|
|
|
|
|
|
Range(func(g *Group) bool {
|
|
|
|
if strings.HasPrefix(g.name, prefix) {
|
|
|
|
g.mu.Lock()
|
|
|
|
count := len(g.clients)
|
|
|
|
g.mu.Unlock()
|
|
|
|
if count > 0 {
|
|
|
|
subgroups = append(subgroups,
|
|
|
|
SubGroup{g.name, count})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return true
|
|
|
|
})
|
|
|
|
return subgroups
|
|
|
|
}
|
|
|
|
|
2020-09-13 11:56:35 +02:00
|
|
|
func Get(name string) *Group {
|
2020-04-29 16:08:07 +02:00
|
|
|
groups.mu.Lock()
|
|
|
|
defer groups.mu.Unlock()
|
2021-09-11 15:14:13 +02:00
|
|
|
if groups.groups == nil {
|
|
|
|
return nil
|
|
|
|
}
|
2020-04-29 16:08:07 +02:00
|
|
|
return groups.groups[name]
|
|
|
|
}
|
|
|
|
|
2020-12-01 19:30:45 +01:00
|
|
|
func Delete(name string) bool {
|
|
|
|
groups.mu.Lock()
|
|
|
|
defer groups.mu.Unlock()
|
2020-04-24 19:38:21 +02:00
|
|
|
g := groups.groups[name]
|
|
|
|
if g == nil {
|
2020-12-02 00:07:31 +01:00
|
|
|
return false
|
2020-04-24 19:38:21 +02:00
|
|
|
}
|
|
|
|
|
2020-12-01 19:30:45 +01:00
|
|
|
g.mu.Lock()
|
|
|
|
defer g.mu.Unlock()
|
|
|
|
return deleteUnlocked(g)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Called with both groups.mu and g.mu taken.
|
|
|
|
func deleteUnlocked(g *Group) bool {
|
2020-04-24 19:38:21 +02:00
|
|
|
if len(g.clients) != 0 {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
2020-12-01 19:30:45 +01:00
|
|
|
delete(groups.groups, g.name)
|
2020-04-24 19:38:21 +02:00
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
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
|
|
|
|
}
|
|
|
|
|
2021-10-27 04:15:44 +02:00
|
|
|
func AddClient(group string, c Client, creds ClientCredentials) (*Group, error) {
|
2020-11-29 14:26:42 +01:00
|
|
|
g, err := Add(group, 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-09-13 13:24:05 +02:00
|
|
|
g.mu.Lock()
|
|
|
|
defer g.mu.Unlock()
|
|
|
|
|
2021-01-17 21:52:26 +01:00
|
|
|
clients := g.getClientsUnlocked(nil)
|
|
|
|
|
2022-02-19 23:43:44 +01:00
|
|
|
if !member("system", c.Permissions()) {
|
2022-09-08 20:55:05 +02:00
|
|
|
username, perms, err := g.getPermission(creds)
|
2020-11-29 16:42:42 +01:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2020-09-13 10:16:10 +02:00
|
|
|
|
2022-02-20 15:32:18 +01:00
|
|
|
c.SetUsername(username)
|
2020-11-29 16:42:42 +01:00
|
|
|
c.SetPermissions(perms)
|
2020-04-25 02:25:51 +02:00
|
|
|
|
2022-02-19 23:43:44 +01:00
|
|
|
if !member("op", perms) {
|
2021-01-17 21:52:26 +01:00
|
|
|
if g.locked != nil {
|
|
|
|
m := *g.locked
|
|
|
|
if m == "" {
|
|
|
|
m = "this group is locked"
|
|
|
|
}
|
|
|
|
return nil, UserError(m)
|
|
|
|
}
|
2024-03-03 16:45:54 +01:00
|
|
|
if g.description.NotBefore != nil ||
|
|
|
|
g.description.Expires != nil {
|
|
|
|
now := time.Now()
|
|
|
|
if g.description.NotBefore != nil &&
|
|
|
|
g.description.NotBefore.After(now) {
|
|
|
|
return nil, UserError(
|
|
|
|
"this group is not open yet",
|
|
|
|
)
|
|
|
|
}
|
|
|
|
if g.description.Expires != nil &&
|
|
|
|
g.description.Expires.Before(now) {
|
|
|
|
return nil, UserError(
|
|
|
|
"this group is closed",
|
|
|
|
)
|
|
|
|
}
|
|
|
|
}
|
2021-01-17 21:52:26 +01:00
|
|
|
if g.description.Autokick {
|
|
|
|
ops := false
|
|
|
|
for _, c := range clients {
|
2022-02-19 23:43:44 +01:00
|
|
|
if member("op", c.Permissions()) {
|
2021-01-17 21:52:26 +01:00
|
|
|
ops = true
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if !ops {
|
|
|
|
return nil, UserError(
|
|
|
|
"there are no operators " +
|
|
|
|
"in this group",
|
|
|
|
)
|
|
|
|
}
|
2020-10-01 13:38:09 +02:00
|
|
|
}
|
2020-09-18 11:40:00 +02:00
|
|
|
}
|
2020-05-18 15:24:04 +02:00
|
|
|
|
2022-02-19 23:43:44 +01:00
|
|
|
if !member("op", perms) && g.description.MaxClients > 0 {
|
2020-10-01 13:38:09 +02:00
|
|
|
if len(g.clients) >= g.description.MaxClients {
|
|
|
|
return nil, UserError("too many users")
|
|
|
|
}
|
2020-04-25 02:37:41 +02:00
|
|
|
}
|
|
|
|
}
|
2023-04-28 16:51:17 +02:00
|
|
|
id := c.Id()
|
|
|
|
if id == "" {
|
|
|
|
return nil, errors.New("client has empty id")
|
|
|
|
}
|
|
|
|
if g.clients[id] != nil {
|
2020-09-13 11:56:35 +02:00
|
|
|
return nil, ProtocolError("duplicate client id")
|
2020-04-25 21:29:21 +02:00
|
|
|
}
|
2023-04-28 16:51:17 +02:00
|
|
|
g.clients[id] = c
|
2020-12-01 19:30:45 +01:00
|
|
|
g.timestamp = time.Now()
|
2020-05-31 20:41:17 +02:00
|
|
|
|
2021-07-16 19:41:00 +02:00
|
|
|
c.Joined(g.Name(), "join")
|
|
|
|
|
2021-02-14 16:40:21 +01:00
|
|
|
u := c.Username()
|
2021-04-27 18:58:21 +02:00
|
|
|
p := c.Permissions()
|
2022-01-29 22:54:44 +01:00
|
|
|
s := c.Data()
|
2021-07-16 19:41:00 +02:00
|
|
|
c.PushClient(g.Name(), "add", c.Id(), u, p, s)
|
2021-02-15 13:43:10 +01:00
|
|
|
for _, cc := range clients {
|
2021-05-10 16:58:28 +02:00
|
|
|
pp := cc.Permissions()
|
2023-03-22 17:41:16 +01:00
|
|
|
uu := cc.Username()
|
|
|
|
c.PushClient(g.Name(), "add", cc.Id(), uu, pp, cc.Data())
|
2021-07-16 19:41:00 +02:00
|
|
|
cc.PushClient(g.Name(), "add", id, u, p, s)
|
2021-02-14 16:40:21 +01:00
|
|
|
}
|
2020-05-31 20:41:17 +02:00
|
|
|
|
|
|
|
return g, nil
|
2020-04-24 19:38:21 +02:00
|
|
|
}
|
|
|
|
|
2021-01-14 03:56:37 +01:00
|
|
|
// called locked
|
2022-09-22 17:21:49 +02:00
|
|
|
func autoLockKick(g *Group) {
|
2021-01-17 21:29:07 +01:00
|
|
|
if !(g.description.Autolock && g.locked == nil) &&
|
|
|
|
!g.description.Autokick {
|
|
|
|
return
|
|
|
|
}
|
2022-09-22 17:21:49 +02:00
|
|
|
|
|
|
|
clients := g.getClientsUnlocked(nil)
|
2021-01-17 21:29:07 +01:00
|
|
|
for _, c := range clients {
|
2022-02-19 23:43:44 +01:00
|
|
|
if member("op", c.Permissions()) {
|
2021-01-17 21:29:07 +01:00
|
|
|
return
|
2021-01-14 03:56:37 +01:00
|
|
|
}
|
|
|
|
}
|
2021-01-17 21:29:07 +01:00
|
|
|
if g.description.Autolock && g.locked == nil {
|
|
|
|
m := "this group is locked"
|
|
|
|
g.locked = &m
|
2022-09-22 17:21:49 +02:00
|
|
|
for _, c := range clients {
|
|
|
|
c.Joined(g.Name(), "change")
|
|
|
|
}
|
2021-01-17 21:29:07 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
if g.description.Autokick {
|
2024-11-11 16:54:59 +01:00
|
|
|
// we cannot call kickall, since it requires the group to
|
|
|
|
// be unlocked. And calling it asynchronously might
|
|
|
|
// spuriously kick out an operator.
|
|
|
|
go func(clients []Client) {
|
|
|
|
for _, c := range clients {
|
|
|
|
c.Kick(
|
|
|
|
"", nil,
|
|
|
|
"there are no operators in this group",
|
|
|
|
)
|
|
|
|
}
|
|
|
|
}(g.getClientsUnlocked(nil))
|
2021-01-17 21:29:07 +01:00
|
|
|
}
|
2021-01-14 03:56:37 +01:00
|
|
|
}
|
|
|
|
|
2020-09-13 11:56:35 +02:00
|
|
|
func DelClient(c Client) {
|
2020-06-08 22:14:28 +02:00
|
|
|
g := c.Group()
|
2020-08-12 11:50:30 +02:00
|
|
|
if g == nil {
|
|
|
|
return
|
|
|
|
}
|
2020-05-28 02:35:09 +02:00
|
|
|
g.mu.Lock()
|
2020-06-08 22:14:28 +02:00
|
|
|
if g.clients[c.Id()] != c {
|
2020-04-25 21:29:21 +02:00
|
|
|
log.Printf("Deleting unknown client")
|
2021-07-16 19:41:00 +02:00
|
|
|
g.mu.Unlock()
|
2020-04-25 21:29:21 +02:00
|
|
|
return
|
|
|
|
}
|
2020-06-08 22:14:28 +02:00
|
|
|
delete(g.clients, c.Id())
|
2020-12-01 19:30:45 +01:00
|
|
|
g.timestamp = time.Now()
|
2021-01-14 03:56:37 +01:00
|
|
|
clients := g.getClientsUnlocked(nil)
|
2021-07-16 19:41:00 +02:00
|
|
|
g.mu.Unlock()
|
2021-01-14 03:56:37 +01:00
|
|
|
|
2021-07-16 19:41:00 +02:00
|
|
|
c.Joined(g.Name(), "leave")
|
|
|
|
for _, cc := range clients {
|
|
|
|
cc.PushClient(
|
2023-03-22 17:41:16 +01:00
|
|
|
g.Name(), "delete", c.Id(), c.Username(), nil, nil,
|
2021-07-16 19:41:00 +02:00
|
|
|
)
|
|
|
|
}
|
2022-09-22 17:21:49 +02:00
|
|
|
autoLockKick(g)
|
2020-04-24 19:38:21 +02:00
|
|
|
}
|
|
|
|
|
2020-09-13 11:56:35 +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)
|
|
|
|
}
|
|
|
|
|
2020-09-13 11:56:35 +02:00
|
|
|
func (g *Group) getClientsUnlocked(except Client) []Client {
|
|
|
|
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-09-13 11:56:35 +02:00
|
|
|
func (g *Group) GetClient(id string) Client {
|
2020-09-12 14:00:14 +02:00
|
|
|
g.mu.Lock()
|
|
|
|
defer g.mu.Unlock()
|
|
|
|
return g.getClientUnlocked(id)
|
|
|
|
}
|
|
|
|
|
2020-09-13 11:56:35 +02:00
|
|
|
func (g *Group) getClientUnlocked(id string) Client {
|
2020-05-28 02:35:09 +02:00
|
|
|
for idd, c := range g.clients {
|
|
|
|
if idd == id {
|
2020-04-25 17:36:35 +02:00
|
|
|
return c
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2020-09-13 11:56:35 +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
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-01-17 21:29:07 +01:00
|
|
|
func kickall(g *Group, message string) {
|
2020-09-13 11:56:35 +02:00
|
|
|
g.Range(func(c Client) bool {
|
2023-03-22 17:41:16 +01:00
|
|
|
c.Kick("", nil, message)
|
2020-09-12 12:42:48 +02:00
|
|
|
return true
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2021-09-11 15:42:14 +02:00
|
|
|
func Shutdown(message string) {
|
|
|
|
Range(func(g *Group) bool {
|
|
|
|
g.SetLocked(true, message)
|
|
|
|
kickall(g, message)
|
|
|
|
return true
|
|
|
|
})
|
2021-01-17 21:29:07 +01:00
|
|
|
}
|
|
|
|
|
2020-12-23 21:32:32 +01:00
|
|
|
type warner interface {
|
|
|
|
Warn(oponly bool, message string) error
|
|
|
|
}
|
|
|
|
|
|
|
|
func (g *Group) WallOps(message string) {
|
|
|
|
clients := g.GetClients(nil)
|
|
|
|
for _, c := range clients {
|
|
|
|
w, ok := c.(warner)
|
|
|
|
if !ok {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
err := w.Warn(true, message)
|
|
|
|
if err != nil {
|
|
|
|
log.Printf("WallOps: %v", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-09-18 11:48:21 +02:00
|
|
|
const maxChatHistory = 50
|
2020-04-25 21:16:49 +02:00
|
|
|
|
2024-08-11 23:57:11 +02:00
|
|
|
// deleteFunc is just like slices.DeleteFunc.
|
|
|
|
// Remove this once we require Go 1.21.
|
|
|
|
func deleteFunc[S ~[]E, E any](s S, f func(E) bool) S {
|
|
|
|
i := 0
|
|
|
|
for i = range s {
|
|
|
|
if f(s[i]) {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if i >= len(s) {
|
|
|
|
return s
|
|
|
|
}
|
|
|
|
|
|
|
|
for j := i + 1; j < len(s); j++ {
|
|
|
|
if v := s[j]; !f(v) {
|
|
|
|
s[i] = v
|
|
|
|
i++
|
|
|
|
}
|
|
|
|
}
|
|
|
|
var zero E
|
|
|
|
for j := i; j < len(s); j++ {
|
|
|
|
s[j] = zero
|
|
|
|
}
|
|
|
|
return s[:i]
|
|
|
|
}
|
|
|
|
|
|
|
|
func (g *Group) ClearChatHistory(id string, userId string) {
|
2020-04-30 19:13:10 +02:00
|
|
|
g.mu.Lock()
|
|
|
|
defer g.mu.Unlock()
|
2024-08-11 23:57:11 +02:00
|
|
|
if id == "" && userId == "" {
|
|
|
|
g.history = nil
|
|
|
|
return
|
|
|
|
}
|
|
|
|
g.history = deleteFunc(g.history, func(e ChatHistoryEntry) bool {
|
|
|
|
return e.Source == userId && (id == "" || e.Id == id)
|
|
|
|
})
|
2020-04-30 19:13:10 +02:00
|
|
|
}
|
|
|
|
|
2024-08-11 19:00:17 +02:00
|
|
|
func (g *Group) AddToChatHistory(id, source string, user *string, time time.Time, kind string, value interface{}) {
|
2020-04-25 21:16:49 +02:00
|
|
|
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,
|
2024-08-11 19:00:17 +02:00
|
|
|
ChatHistoryEntry{Id: id, Source: source, User: user, Time: time, Kind: kind, Value: value},
|
2020-04-25 21:16:49 +02:00
|
|
|
)
|
|
|
|
}
|
|
|
|
|
2020-12-01 19:30:45 +01:00
|
|
|
func discardObsoleteHistory(h []ChatHistoryEntry, duration time.Duration) []ChatHistoryEntry {
|
2020-10-08 14:38:33 +02:00
|
|
|
i := 0
|
|
|
|
for i < len(h) {
|
2022-12-16 18:56:36 +01:00
|
|
|
if time.Since(h[i].Time) <= duration {
|
2020-10-08 14:38:33 +02:00
|
|
|
break
|
|
|
|
}
|
|
|
|
i++
|
|
|
|
}
|
|
|
|
if i > 0 {
|
|
|
|
copy(h, h[i:])
|
|
|
|
h = h[:len(h)-i]
|
|
|
|
}
|
|
|
|
return h
|
|
|
|
}
|
|
|
|
|
2020-09-13 11:56:35 +02:00
|
|
|
func (g *Group) GetChatHistory() []ChatHistoryEntry {
|
2020-04-25 21:16:49 +02:00
|
|
|
g.mu.Lock()
|
2020-05-12 12:48:56 +02:00
|
|
|
defer g.mu.Unlock()
|
2020-04-25 21:16:49 +02:00
|
|
|
|
2020-12-01 19:30:45 +01:00
|
|
|
g.history = discardObsoleteHistory(
|
|
|
|
g.history, maxHistoryAge(g.description),
|
|
|
|
)
|
2020-10-08 14:38:33 +02:00
|
|
|
|
2020-09-13 11:56:35 +02:00
|
|
|
h := make([]ChatHistoryEntry, len(g.history))
|
2020-04-25 21:16:49 +02:00
|
|
|
copy(h, g.history)
|
|
|
|
return h
|
|
|
|
}
|
|
|
|
|
2021-10-26 20:10:24 +02:00
|
|
|
// Configuration represents the contents of the data/config.json file.
|
|
|
|
type Configuration struct {
|
|
|
|
// The modtime and size of the file. These are used to detect
|
|
|
|
// when a file has changed on disk.
|
|
|
|
modTime time.Time `json:"-"`
|
|
|
|
fileSize int64 `json:"-"`
|
|
|
|
|
2024-04-11 00:50:56 +02:00
|
|
|
PublicServer bool `json:"publicServer"`
|
|
|
|
CanonicalHost string `json:"canonicalHost"`
|
|
|
|
ProxyURL string `json:"proxyURL"`
|
|
|
|
WritableGroups bool `json:"writableGroups"`
|
|
|
|
Users map[string]UserDescription
|
2024-01-02 18:36:09 +01:00
|
|
|
|
|
|
|
// obsolete fields
|
|
|
|
Admin []ClientPattern `json:"admin"`
|
2021-10-26 20:10:24 +02:00
|
|
|
}
|
|
|
|
|
2021-10-31 22:39:35 +01:00
|
|
|
func (conf Configuration) Zero() bool {
|
|
|
|
return conf.modTime.Equal(time.Time{}) &&
|
|
|
|
conf.fileSize == 0
|
|
|
|
}
|
|
|
|
|
2021-10-26 20:10:24 +02:00
|
|
|
var configuration struct {
|
|
|
|
mu sync.Mutex
|
|
|
|
configuration *Configuration
|
|
|
|
}
|
|
|
|
|
|
|
|
func GetConfiguration() (*Configuration, error) {
|
|
|
|
configuration.mu.Lock()
|
|
|
|
defer configuration.mu.Unlock()
|
|
|
|
|
|
|
|
if configuration.configuration == nil {
|
|
|
|
configuration.configuration = &Configuration{}
|
|
|
|
}
|
|
|
|
|
|
|
|
filename := filepath.Join(DataDirectory, "config.json")
|
|
|
|
fi, err := os.Stat(filename)
|
|
|
|
if err != nil {
|
2024-04-14 13:33:52 +02:00
|
|
|
if errors.Is(err, os.ErrNotExist) {
|
2021-10-31 22:39:35 +01:00
|
|
|
if !configuration.configuration.Zero() {
|
2021-10-26 20:10:24 +02:00
|
|
|
configuration.configuration = &Configuration{}
|
|
|
|
}
|
2021-10-31 22:39:35 +01:00
|
|
|
return configuration.configuration, nil
|
2021-10-26 20:10:24 +02:00
|
|
|
}
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
if configuration.configuration.modTime.Equal(fi.ModTime()) &&
|
|
|
|
configuration.configuration.fileSize == fi.Size() {
|
|
|
|
return configuration.configuration, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
f, err := os.Open(filename)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
defer f.Close()
|
|
|
|
|
|
|
|
d := json.NewDecoder(f)
|
|
|
|
d.DisallowUnknownFields()
|
|
|
|
var conf Configuration
|
|
|
|
err = d.Decode(&conf)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2024-01-02 18:36:09 +01:00
|
|
|
if conf.Admin != nil {
|
|
|
|
log.Printf("%v: field \"admin\" is obsolete, ignored", filename)
|
|
|
|
conf.Admin = nil
|
|
|
|
}
|
2021-10-26 20:10:24 +02:00
|
|
|
configuration.configuration = &conf
|
|
|
|
return configuration.configuration, nil
|
|
|
|
}
|
|
|
|
|
2022-09-08 20:55:05 +02:00
|
|
|
// called locked
|
2024-01-02 18:36:09 +01:00
|
|
|
func (g *Group) getPasswordPermission(creds ClientCredentials) (Permissions, error) {
|
2022-09-08 20:55:05 +02:00
|
|
|
desc := g.description
|
2023-03-22 00:07:13 +01:00
|
|
|
|
2023-03-22 17:41:16 +01:00
|
|
|
if creds.Username == nil {
|
2024-01-02 18:36:09 +01:00
|
|
|
return Permissions{}, errors.New("username not provided")
|
2023-03-22 17:41:16 +01:00
|
|
|
}
|
2024-01-02 18:36:09 +01:00
|
|
|
if desc.Users != nil {
|
|
|
|
if c, found := desc.Users[*creds.Username]; found {
|
|
|
|
ok, err := c.Password.Match(creds.Password)
|
|
|
|
if err != nil {
|
|
|
|
return Permissions{}, err
|
2020-05-30 00:23:54 +02:00
|
|
|
}
|
2024-01-02 18:36:09 +01:00
|
|
|
if ok {
|
|
|
|
return c.Permissions, nil
|
|
|
|
} else {
|
|
|
|
return Permissions{}, &NotAuthorisedError{}
|
2023-04-03 20:09:22 +02:00
|
|
|
}
|
2020-04-25 02:25:51 +02:00
|
|
|
}
|
2023-03-22 00:07:13 +01:00
|
|
|
}
|
2024-01-02 18:36:09 +01:00
|
|
|
|
2024-05-03 19:12:12 +02:00
|
|
|
if desc.WildcardUser != nil {
|
|
|
|
ok, _ := desc.WildcardUser.Password.Match(creds.Password)
|
2024-01-02 18:36:09 +01:00
|
|
|
if ok {
|
2024-05-03 19:12:12 +02:00
|
|
|
return desc.WildcardUser.Permissions, nil
|
2024-01-02 18:36:09 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return Permissions{}, &NotAuthorisedError{}
|
2023-03-22 00:07:13 +01:00
|
|
|
}
|
2021-10-29 23:37:05 +02:00
|
|
|
|
2023-03-22 04:04:22 +01:00
|
|
|
// Return true if there is a user entry with the given username.
|
|
|
|
// Always return false for an empty username.
|
|
|
|
func (g *Group) UserExists(username string) bool {
|
|
|
|
g.mu.Lock()
|
|
|
|
defer g.mu.Unlock()
|
|
|
|
return g.userExists(username)
|
|
|
|
}
|
|
|
|
|
|
|
|
// called locked
|
|
|
|
func (g *Group) userExists(username string) bool {
|
|
|
|
desc := g.description
|
2024-01-02 18:36:09 +01:00
|
|
|
if desc.Users == nil {
|
|
|
|
return false
|
2023-03-22 04:04:22 +01:00
|
|
|
}
|
2024-01-02 18:36:09 +01:00
|
|
|
_, found := desc.Users[username]
|
|
|
|
return found
|
2023-03-22 04:04:22 +01:00
|
|
|
}
|
|
|
|
|
2023-03-22 00:07:13 +01:00
|
|
|
// called locked
|
|
|
|
func (g *Group) getPermission(creds ClientCredentials) (string, []string, error) {
|
|
|
|
desc := g.description
|
|
|
|
var username string
|
|
|
|
var perms []string
|
|
|
|
if creds.Token != "" {
|
|
|
|
tok, err := token.Parse(creds.Token, desc.AuthKeys)
|
2021-10-29 23:37:05 +02:00
|
|
|
if err != nil {
|
2024-03-03 15:38:08 +01:00
|
|
|
return "", nil, &NotAuthorisedError{err: err}
|
2021-10-29 23:37:05 +02:00
|
|
|
}
|
2023-03-22 00:07:13 +01:00
|
|
|
|
|
|
|
conf, err := GetConfiguration()
|
|
|
|
if err != nil {
|
|
|
|
return "", nil, err
|
2021-10-29 23:37:05 +02:00
|
|
|
}
|
2023-03-22 00:07:13 +01:00
|
|
|
|
|
|
|
username, perms, err =
|
2023-03-22 17:41:16 +01:00
|
|
|
tok.Check(conf.CanonicalHost, g.name, creds.Username)
|
2023-03-22 00:07:13 +01:00
|
|
|
if err != nil {
|
2024-03-03 13:34:18 +01:00
|
|
|
return "", nil, &NotAuthorisedError{err: err}
|
2023-03-22 00:07:13 +01:00
|
|
|
}
|
2023-03-22 04:04:22 +01:00
|
|
|
if username == "" && creds.Username != nil {
|
|
|
|
if g.userExists(*creds.Username) {
|
|
|
|
return "", nil, ErrDuplicateUsername
|
|
|
|
}
|
|
|
|
username = *creds.Username
|
|
|
|
}
|
2023-03-22 17:41:16 +01:00
|
|
|
} else if creds.Username != nil {
|
|
|
|
username = *creds.Username
|
2024-01-02 18:36:09 +01:00
|
|
|
ps, err := g.getPasswordPermission(creds)
|
2023-03-22 00:07:13 +01:00
|
|
|
if err != nil {
|
|
|
|
return "", nil, err
|
2021-10-29 23:37:05 +02:00
|
|
|
}
|
2024-01-02 18:36:09 +01:00
|
|
|
perms = ps.Permissions(desc)
|
2023-03-22 17:41:16 +01:00
|
|
|
} else {
|
|
|
|
return "", nil, errors.New("neither username nor token provided")
|
2021-10-29 23:37:05 +02:00
|
|
|
}
|
2023-03-22 00:07:13 +01:00
|
|
|
|
2023-01-09 15:48:09 +01:00
|
|
|
return username, perms, nil
|
2020-04-25 17:36:35 +02:00
|
|
|
}
|
|
|
|
|
2022-09-08 20:55:05 +02:00
|
|
|
func (g *Group) GetPermission(creds ClientCredentials) (string, []string, error) {
|
|
|
|
g.mu.Lock()
|
|
|
|
defer g.mu.Unlock()
|
|
|
|
return g.getPermission(creds)
|
|
|
|
}
|
|
|
|
|
2021-10-26 22:22:48 +02:00
|
|
|
type Status struct {
|
2024-04-11 00:50:56 +02:00
|
|
|
Name string `json:"name"`
|
|
|
|
Redirect string `json:"redirect,omitempty"`
|
|
|
|
Location string `json:"location,omitempty"`
|
|
|
|
Endpoint string `json:"endpoint,omitempty"`
|
|
|
|
DisplayName string `json:"displayName,omitempty"`
|
|
|
|
Description string `json:"description,omitempty"`
|
|
|
|
AuthServer string `json:"authServer,omitempty"`
|
|
|
|
AuthPortal string `json:"authPortal,omitempty"`
|
|
|
|
Locked bool `json:"locked,omitempty"`
|
|
|
|
ClientCount *int `json:"clientCount,omitempty"`
|
|
|
|
CanChangePassword bool `json:"canChangePassword,omitempty"`
|
2021-10-26 22:22:48 +02:00
|
|
|
}
|
|
|
|
|
2022-09-01 15:38:29 +02:00
|
|
|
// Status returns a group's status.
|
|
|
|
// Base is the base URL for groups; if omitted, then both the Location and
|
|
|
|
// Endpoint members are omitted from the result.
|
2024-02-24 12:09:10 +01:00
|
|
|
func (g *Group) Status(authentified bool, base *url.URL) Status {
|
2021-10-26 22:22:48 +02:00
|
|
|
desc := g.Description()
|
2022-09-01 15:38:29 +02:00
|
|
|
|
2022-09-16 20:45:54 +02:00
|
|
|
if desc.Redirect != "" {
|
|
|
|
return Status{
|
|
|
|
Name: g.name,
|
|
|
|
Redirect: desc.Redirect,
|
|
|
|
DisplayName: desc.DisplayName,
|
|
|
|
Description: desc.Description,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-09-01 15:38:29 +02:00
|
|
|
var location, endpoint string
|
2024-02-24 12:09:10 +01:00
|
|
|
if base != nil {
|
|
|
|
wss := "wss"
|
|
|
|
if base.Scheme == "http" {
|
|
|
|
wss = "ws"
|
|
|
|
}
|
|
|
|
l := url.URL{
|
|
|
|
Scheme: base.Scheme,
|
|
|
|
Host: base.Host,
|
|
|
|
Path: path.Join(
|
|
|
|
path.Join(base.Path, "/group/"),
|
|
|
|
g.Name()) + "/",
|
|
|
|
}
|
|
|
|
location = l.String()
|
|
|
|
e := url.URL{
|
|
|
|
Scheme: wss,
|
|
|
|
Host: base.Host,
|
|
|
|
Path: path.Join(base.Path, "/ws"),
|
2022-09-01 15:38:29 +02:00
|
|
|
}
|
2024-02-24 12:09:10 +01:00
|
|
|
endpoint = e.String()
|
2022-09-01 15:38:29 +02:00
|
|
|
}
|
|
|
|
|
2021-10-26 22:22:48 +02:00
|
|
|
d := Status{
|
|
|
|
Name: g.name,
|
2022-09-01 15:38:29 +02:00
|
|
|
Location: location,
|
2022-09-01 14:55:52 +02:00
|
|
|
Endpoint: endpoint,
|
2021-10-26 22:22:48 +02:00
|
|
|
DisplayName: desc.DisplayName,
|
2021-10-29 23:37:05 +02:00
|
|
|
AuthServer: desc.AuthServer,
|
2022-02-19 23:58:31 +01:00
|
|
|
AuthPortal: desc.AuthPortal,
|
2021-10-26 22:22:48 +02:00
|
|
|
Description: desc.Description,
|
|
|
|
}
|
|
|
|
|
|
|
|
if authentified || desc.Public {
|
|
|
|
// these are considered private information
|
|
|
|
locked, _ := g.Locked()
|
|
|
|
count := g.ClientCount()
|
|
|
|
d.Locked = locked
|
|
|
|
d.ClientCount = &count
|
|
|
|
}
|
2024-04-11 00:50:56 +02:00
|
|
|
if authentified {
|
|
|
|
conf, err := GetConfiguration()
|
|
|
|
if err == nil {
|
|
|
|
d.CanChangePassword = conf.WritableGroups
|
|
|
|
}
|
|
|
|
}
|
2021-10-26 22:22:48 +02:00
|
|
|
return d
|
2020-04-24 19:38:21 +02:00
|
|
|
}
|
|
|
|
|
2024-02-24 12:09:10 +01:00
|
|
|
func GetPublic(base *url.URL) []Status {
|
2021-10-26 22:22:48 +02:00
|
|
|
gs := make([]Status, 0)
|
2020-09-13 11:56:35 +02:00
|
|
|
Range(func(g *Group) bool {
|
2021-10-26 22:22:48 +02:00
|
|
|
if g.Description().Public {
|
2022-09-01 15:38:29 +02:00
|
|
|
gs = append(gs, g.Status(false, base))
|
2020-04-24 19:38:21 +02:00
|
|
|
}
|
2020-09-13 12:24:06 +02:00
|
|
|
return true
|
|
|
|
})
|
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
|
|
|
|
2021-08-22 16:36:18 +02:00
|
|
|
// Update checks that all in-memory groups are up-to-date and updates the
|
|
|
|
// list of public groups. It also removes from memory any non-public
|
|
|
|
// groups that haven't been accessed in maxHistoryAge.
|
|
|
|
func Update() {
|
2021-10-30 19:28:51 +02:00
|
|
|
_, err := GetConfiguration()
|
|
|
|
if err != nil {
|
|
|
|
log.Printf("%v: %v",
|
|
|
|
filepath.Join(DataDirectory, "config.json"),
|
|
|
|
err,
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
2021-08-22 16:36:18 +02:00
|
|
|
names := GetNames()
|
|
|
|
for _, name := range names {
|
|
|
|
g := Get(name)
|
|
|
|
if g == nil {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
deleted := false
|
2022-10-08 22:46:28 +02:00
|
|
|
if g.mayExpire() {
|
2021-08-22 16:36:18 +02:00
|
|
|
// Delete checks if the group is still empty
|
|
|
|
deleted = Delete(name)
|
|
|
|
}
|
|
|
|
|
2022-10-08 22:46:28 +02:00
|
|
|
// update group description
|
|
|
|
if !deleted {
|
2021-08-22 16:36:18 +02:00
|
|
|
Add(name, nil)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-07-12 19:22:03 +02:00
|
|
|
err = filepath.WalkDir(
|
2021-01-13 22:56:43 +01:00
|
|
|
Directory,
|
2022-07-12 19:22:03 +02:00
|
|
|
func(path string, d fs.DirEntry, err error) error {
|
2021-01-13 22:56:43 +01:00
|
|
|
if err != nil {
|
|
|
|
log.Printf("Group file %v: %v", path, err)
|
2021-01-14 01:45:50 +01:00
|
|
|
return nil
|
2021-01-13 22:56:43 +01:00
|
|
|
}
|
2022-07-12 19:22:03 +02:00
|
|
|
if d.IsDir() {
|
2024-02-13 22:02:57 +01:00
|
|
|
base := filepath.Base(path)
|
|
|
|
if base[0] == '.' {
|
|
|
|
log.Printf(
|
|
|
|
"Ignoring group directory %v",
|
|
|
|
path,
|
|
|
|
)
|
|
|
|
return fs.SkipDir
|
|
|
|
}
|
2021-01-13 22:56:43 +01:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
filename, err := filepath.Rel(Directory, path)
|
|
|
|
if err != nil {
|
|
|
|
log.Printf("Group file %v: %v", path, err)
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
if !strings.HasSuffix(filename, ".json") {
|
|
|
|
log.Printf(
|
|
|
|
"Unexpected extension for group file %v",
|
|
|
|
path,
|
|
|
|
)
|
|
|
|
return nil
|
|
|
|
}
|
2021-10-28 20:01:11 +02:00
|
|
|
base := filepath.Base(filename)
|
|
|
|
if base[0] == '.' {
|
2024-02-13 22:02:57 +01:00
|
|
|
log.Printf("Ignoring group file %v", filename)
|
2021-10-28 20:01:11 +02:00
|
|
|
return nil
|
|
|
|
}
|
2022-07-12 19:22:03 +02:00
|
|
|
name := strings.TrimSuffix(filename, ".json")
|
2021-01-13 22:56:43 +01:00
|
|
|
desc, err := GetDescription(name)
|
|
|
|
if err != nil {
|
|
|
|
log.Printf("Group file %v: %v", path, err)
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
if desc.Public {
|
|
|
|
Add(name, desc)
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
},
|
|
|
|
)
|
2020-04-25 04:08:43 +02:00
|
|
|
|
|
|
|
if err != nil {
|
2021-01-14 01:45:50 +01:00
|
|
|
log.Printf("Couldn't read groups: %v", err)
|
2020-04-25 04:08:43 +02:00
|
|
|
}
|
|
|
|
}
|