mirror of
https://github.com/netbirdio/netbird.git
synced 2026-04-16 07:16:38 +00:00
[relay] Use instanceURL instead of Exposed address. (#4905)
Replaces string-based exposed address handling with URL-based InstanceURL() (type url.URL) across relay/server and relay/healthcheck; adds SchemeREL/SchemeRELS constants; updates getInstanceURL to return *url.URL with scheme and TLS validation; adjusts WS dialing and health-check logic to use URL fields.
This commit is contained in:
@@ -6,13 +6,14 @@ import (
|
||||
"errors"
|
||||
"net"
|
||||
"net/http"
|
||||
"strings"
|
||||
"net/url"
|
||||
"sync"
|
||||
"time"
|
||||
|
||||
log "github.com/sirupsen/logrus"
|
||||
|
||||
"github.com/netbirdio/netbird/relay/protocol"
|
||||
"github.com/netbirdio/netbird/relay/server"
|
||||
)
|
||||
|
||||
const (
|
||||
@@ -26,7 +27,7 @@ const (
|
||||
|
||||
type ServiceChecker interface {
|
||||
ListenerProtocols() []protocol.Protocol
|
||||
ExposedAddress() string
|
||||
InstanceURL() url.URL
|
||||
}
|
||||
|
||||
type HealthStatus struct {
|
||||
@@ -134,7 +135,7 @@ func (s *Server) getHealthStatus(ctx context.Context) (*HealthStatus, bool) {
|
||||
}
|
||||
status.Listeners = listeners
|
||||
|
||||
if !strings.HasPrefix(s.config.ServiceChecker.ExposedAddress(), "rels") {
|
||||
if s.config.ServiceChecker.InstanceURL().Scheme != server.SchemeRELS {
|
||||
status.CertificateValid = false
|
||||
}
|
||||
|
||||
@@ -156,14 +157,9 @@ func (s *Server) validateListeners() ([]protocol.Protocol, bool) {
|
||||
}
|
||||
|
||||
func (s *Server) validateConnection(ctx context.Context) bool {
|
||||
exposedAddress := s.config.ServiceChecker.ExposedAddress()
|
||||
if exposedAddress == "" {
|
||||
log.Error("exposed address is empty, cannot validate certificate")
|
||||
return false
|
||||
}
|
||||
|
||||
if err := dialWS(ctx, exposedAddress); err != nil {
|
||||
log.Errorf("failed to dial WebSocket listener at %s: %v", exposedAddress, err)
|
||||
addr := s.config.ServiceChecker.InstanceURL()
|
||||
if err := dialWS(ctx, addr); err != nil {
|
||||
log.Errorf("failed to dial WebSocket listener at %s: %v", addr.String(), err)
|
||||
return false
|
||||
}
|
||||
|
||||
|
||||
@@ -3,22 +3,22 @@ package healthcheck
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"strings"
|
||||
"net/url"
|
||||
|
||||
"github.com/coder/websocket"
|
||||
|
||||
"github.com/netbirdio/netbird/relay/server"
|
||||
"github.com/netbirdio/netbird/shared/relay"
|
||||
)
|
||||
|
||||
func dialWS(ctx context.Context, address string) error {
|
||||
addressSplit := strings.Split(address, "/")
|
||||
func dialWS(ctx context.Context, address url.URL) error {
|
||||
scheme := "ws"
|
||||
if addressSplit[0] == "rels:" {
|
||||
if address.Scheme == server.SchemeRELS {
|
||||
scheme = "wss"
|
||||
}
|
||||
url := fmt.Sprintf("%s://%s%s", scheme, addressSplit[2], relay.WebSocketURLPath)
|
||||
wsURL := fmt.Sprintf("%s://%s%s", scheme, address.Host, relay.WebSocketURLPath)
|
||||
|
||||
conn, resp, err := websocket.Dial(ctx, url, nil)
|
||||
conn, resp, err := websocket.Dial(ctx, wsURL, nil)
|
||||
if resp != nil {
|
||||
defer func() {
|
||||
if resp.Body != nil {
|
||||
|
||||
Reference in New Issue
Block a user