mirror of
https://github.com/strukturag/nextcloud-spreed-signaling
synced 2026-03-14 14:35:44 +01:00
131 lines
3.5 KiB
Go
131 lines
3.5 KiB
Go
/**
|
|
* Standalone signaling server for the Nextcloud Spreed app.
|
|
* Copyright (C) 2025 struktur AG
|
|
*
|
|
* @author Joachim Bauch <bauch@struktur.de>
|
|
*
|
|
* @license GNU AGPL version 3 or any later version
|
|
*
|
|
* This program is free software: you can redistribute it and/or modify
|
|
* it under the terms of the GNU Affero General Public License as published by
|
|
* the Free Software Foundation, either version 3 of the License, or
|
|
* (at your option) any later version.
|
|
*
|
|
* This program is distributed in the hope that it will be useful,
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
* GNU Affero General Public License for more details.
|
|
*
|
|
* You should have received a copy of the GNU Affero General Public License
|
|
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
*/
|
|
package etcd
|
|
|
|
import (
|
|
"context"
|
|
"errors"
|
|
"fmt"
|
|
"net/url"
|
|
"slices"
|
|
|
|
"github.com/strukturag/nextcloud-spreed-signaling/v2/api"
|
|
"github.com/strukturag/nextcloud-spreed-signaling/v2/internal"
|
|
clientv3 "go.etcd.io/etcd/client/v3"
|
|
)
|
|
|
|
type ClientListener interface {
|
|
EtcdClientCreated(client Client)
|
|
}
|
|
|
|
type ClientWatcher interface {
|
|
EtcdWatchCreated(client Client, key string)
|
|
EtcdKeyUpdated(client Client, key string, value []byte, prevValue []byte)
|
|
EtcdKeyDeleted(client Client, key string, prevValue []byte)
|
|
}
|
|
|
|
type Client interface {
|
|
IsConfigured() bool
|
|
WaitForConnection(ctx context.Context) error
|
|
GetServerInfoEtcd() *BackendServerInfoEtcd
|
|
Close() error
|
|
|
|
AddListener(listener ClientListener)
|
|
RemoveListener(listener ClientListener)
|
|
|
|
Get(ctx context.Context, key string, opts ...clientv3.OpOption) (*clientv3.GetResponse, error)
|
|
Watch(ctx context.Context, key string, nextRevision int64, watcher ClientWatcher, opts ...clientv3.OpOption) (int64, error)
|
|
}
|
|
|
|
// Information on a backend in the etcd cluster.
|
|
|
|
type BackendInformationEtcd struct {
|
|
// Compat setting.
|
|
Url string `json:"url,omitempty"`
|
|
|
|
Urls []string `json:"urls,omitempty"`
|
|
ParsedUrls []*url.URL `json:"-"`
|
|
Secret string `json:"secret"`
|
|
|
|
MaxStreamBitrate api.Bandwidth `json:"maxstreambitrate,omitempty"`
|
|
MaxScreenBitrate api.Bandwidth `json:"maxscreenbitrate,omitempty"`
|
|
|
|
SessionLimit uint64 `json:"sessionlimit,omitempty"`
|
|
}
|
|
|
|
func (p *BackendInformationEtcd) CheckValid() (err error) {
|
|
if p.Secret == "" {
|
|
return errors.New("secret missing")
|
|
}
|
|
|
|
if len(p.Urls) > 0 {
|
|
slices.Sort(p.Urls)
|
|
p.Urls = slices.Compact(p.Urls)
|
|
seen := make(map[string]bool)
|
|
outIdx := 0
|
|
for _, u := range p.Urls {
|
|
parsedUrl, err := url.Parse(u)
|
|
if err != nil {
|
|
return fmt.Errorf("invalid url %s: %w", u, err)
|
|
}
|
|
|
|
var changed bool
|
|
if parsedUrl, changed = internal.CanonicalizeUrl(parsedUrl); changed {
|
|
u = parsedUrl.String()
|
|
}
|
|
p.Urls[outIdx] = u
|
|
if seen[u] {
|
|
continue
|
|
}
|
|
seen[u] = true
|
|
p.ParsedUrls = append(p.ParsedUrls, parsedUrl)
|
|
outIdx++
|
|
}
|
|
if len(p.Urls) != outIdx {
|
|
clear(p.Urls[outIdx:])
|
|
p.Urls = p.Urls[:outIdx]
|
|
}
|
|
} else if p.Url != "" {
|
|
parsedUrl, err := url.Parse(p.Url)
|
|
if err != nil {
|
|
return fmt.Errorf("invalid url: %w", err)
|
|
}
|
|
var changed bool
|
|
if parsedUrl, changed = internal.CanonicalizeUrl(parsedUrl); changed {
|
|
p.Url = parsedUrl.String()
|
|
}
|
|
|
|
p.Urls = append(p.Urls, p.Url)
|
|
p.ParsedUrls = append(p.ParsedUrls, parsedUrl)
|
|
} else {
|
|
return errors.New("urls missing")
|
|
}
|
|
|
|
return nil
|
|
}
|
|
|
|
type BackendServerInfoEtcd struct {
|
|
Endpoints []string `json:"endpoints"`
|
|
|
|
Active string `json:"active,omitempty"`
|
|
Connected *bool `json:"connected,omitempty"`
|
|
}
|