Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support multiple urls per backend #770

Draft
wants to merge 4 commits into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
48 changes: 34 additions & 14 deletions api_backend.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ import (
"net/http"
"net/url"
"regexp"
"slices"
"strings"
"time"
)
Expand Down Expand Up @@ -432,10 +433,12 @@ type TurnCredentials struct {
// Information on a backend in the etcd cluster.

type BackendInformationEtcd struct {
parsedUrl *url.URL
// Compat setting.
Url string `json:"url,omitempty"`

Url string `json:"url"`
Secret string `json:"secret"`
Urls []string `json:"urls,omitempty"`
parsedUrls []*url.URL
Secret string `json:"secret"`

MaxStreamBitrate int `json:"maxstreambitrate,omitempty"`
MaxScreenBitrate int `json:"maxscreenbitrate,omitempty"`
Expand All @@ -444,23 +447,40 @@ type BackendInformationEtcd struct {
}

func (p *BackendInformationEtcd) CheckValid() error {
if p.Url == "" {
return fmt.Errorf("url missing")
}
if p.Secret == "" {
return fmt.Errorf("secret missing")
}

parsedUrl, err := url.Parse(p.Url)
if err != nil {
return fmt.Errorf("invalid url: %w", err)
}
if len(p.Urls) > 0 {
slices.Sort(p.Urls)
p.Urls = slices.Compact(p.Urls)
for idx, u := range p.Urls {
parsedUrl, err := url.Parse(u)
if err != nil {
return fmt.Errorf("invalid url %s: %w", u, err)
}
if strings.Contains(parsedUrl.Host, ":") && hasStandardPort(parsedUrl) {
parsedUrl.Host = parsedUrl.Hostname()
p.Urls[idx] = parsedUrl.String()
}

p.parsedUrls = append(p.parsedUrls, parsedUrl)
}
} else if p.Url != "" {
parsedUrl, err := url.Parse(p.Url)
if err != nil {
return fmt.Errorf("invalid url: %w", err)
}
if strings.Contains(parsedUrl.Host, ":") && hasStandardPort(parsedUrl) {
parsedUrl.Host = parsedUrl.Hostname()
p.Url = parsedUrl.String()
}

if strings.Contains(parsedUrl.Host, ":") && hasStandardPort(parsedUrl) {
parsedUrl.Host = parsedUrl.Hostname()
p.Url = parsedUrl.String()
p.Urls = append(p.Urls, p.Url)
p.parsedUrls = append(p.parsedUrls, parsedUrl)
} else {
return fmt.Errorf("urls missing")
}

p.parsedUrl = parsedUrl
return nil
}
76 changes: 62 additions & 14 deletions api_backend_easyjson.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

20 changes: 18 additions & 2 deletions api_signaling.go
Original file line number Diff line number Diff line change
Expand Up @@ -370,11 +370,17 @@ type ClientTypeInternalAuthParams struct {
func (p *ClientTypeInternalAuthParams) CheckValid() error {
if p.Backend == "" {
return fmt.Errorf("backend missing")
} else if u, err := url.Parse(p.Backend); err != nil {
}

if p.Backend[len(p.Backend)-1] != '/' {
p.Backend += "/"
}
if u, err := url.Parse(p.Backend); err != nil {
return err
} else {
if strings.Contains(u.Host, ":") && hasStandardPort(u) {
u.Host = u.Hostname()
p.Backend = u.String()
}

p.parsedBackend = u
Expand Down Expand Up @@ -475,11 +481,21 @@ func (m *HelloClientMessage) CheckValid() error {
case HelloClientTypeFederation:
if m.Auth.Url == "" {
return fmt.Errorf("url missing")
} else if u, err := url.ParseRequestURI(m.Auth.Url); err != nil {
}

if m.Auth.Url[len(m.Auth.Url)-1] != '/' {
m.Auth.Url += "/"
}
if pos := strings.Index(m.Auth.Url, "ocs/v2.php/apps/spreed/"); pos != -1 {
m.Auth.Url = m.Auth.Url[:pos]
}

if u, err := url.ParseRequestURI(m.Auth.Url); err != nil {
return err
} else {
if strings.Contains(u.Host, ":") && hasStandardPort(u) {
u.Host = u.Hostname()
m.Auth.Url = u.String()
}

m.Auth.parsedUrl = u
Expand Down
4 changes: 2 additions & 2 deletions backend_client.go
Original file line number Diff line number Diff line change
Expand Up @@ -171,13 +171,13 @@ func (b *BackendClient) PerformJSONRequest(ctx context.Context, u *url.URL, requ

ct := resp.Header.Get("Content-Type")
if !strings.HasPrefix(ct, "application/json") {
log.Printf("Received unsupported content-type from %s: %s (%s)", req.URL, ct, resp.Status)
log.Printf("Received unsupported content-type from %s for %s: %s (%s)", req.URL, string(data), ct, resp.Status)
return ErrUnsupportedContentType
}

body, err := io.ReadAll(resp.Body)
if err != nil {
log.Printf("Could not read response body from %s: %s", req.URL, err)
log.Printf("Could not read response body from %s for %s: %s", req.URL, string(data), err)
return err
}

Expand Down
52 changes: 38 additions & 14 deletions backend_configuration.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,10 @@
package signaling

import (
"bytes"
"fmt"
"net/url"
"slices"
"strings"
"sync"

Expand All @@ -42,11 +44,9 @@ var (
)

type Backend struct {
id string
url string
parsedUrl *url.URL
secret []byte
compat bool
id string
urls []string
secret []byte

allowHttp bool

Expand All @@ -67,7 +67,23 @@ func (b *Backend) Secret() []byte {
}

func (b *Backend) IsCompat() bool {
return b.compat
return len(b.urls) == 0
}

func (b *Backend) Equal(other *Backend) bool {
if b == other {
return true
} else if b == nil || other == nil {
return false
}

return b.id == other.id &&
b.allowHttp == other.allowHttp &&
b.maxStreamBitrate == other.maxStreamBitrate &&
b.maxScreenBitrate == other.maxScreenBitrate &&
b.sessionLimit == other.sessionLimit &&
bytes.Equal(b.secret, other.secret) &&
slices.Equal(b.urls, other.urls)
}

func (b *Backend) IsUrlAllowed(u *url.URL) bool {
Expand All @@ -81,12 +97,23 @@ func (b *Backend) IsUrlAllowed(u *url.URL) bool {
}
}

func (b *Backend) Url() string {
return b.url
func (b *Backend) HasUrl(url string) bool {
if b.IsCompat() {
// Old-style configuration, only hosts are configured.
return true
}

for _, u := range b.urls {
if strings.HasPrefix(url, u) {
return true
}
}

return false
}

func (b *Backend) ParsedUrl() *url.URL {
return b.parsedUrl
func (b *Backend) Urls() []string {
return b.urls
}

func (b *Backend) Limit() int {
Expand Down Expand Up @@ -173,10 +200,7 @@ func (s *backendStorageCommon) getBackendLocked(u *url.URL) *Backend {
continue
}

if entry.url == "" {
// Old-style configuration, only hosts are configured.
return entry
} else if strings.HasPrefix(url, entry.url) {
if entry.HasUrl(url) {
return entry
}
}
Expand Down
Loading
Loading