netbird/relay/server/server.go

77 lines
2.2 KiB
Go
Raw Normal View History

2024-05-17 17:43:28 +02:00
package server
import (
"context"
2024-07-01 11:50:18 +02:00
"crypto/tls"
2024-05-17 17:43:28 +02:00
log "github.com/sirupsen/logrus"
2024-07-24 16:26:26 +02:00
"go.opentelemetry.io/otel/metric"
2024-05-17 17:43:28 +02:00
2024-07-08 17:01:11 +02:00
"github.com/netbirdio/netbird/relay/auth"
2024-05-17 17:43:28 +02:00
"github.com/netbirdio/netbird/relay/server/listener"
"github.com/netbirdio/netbird/relay/server/listener/ws"
2024-06-26 15:26:19 +02:00
)
2024-07-29 21:53:07 +02:00
// ListenerConfig is the configuration for the listener.
// Address: the address to bind the listener to. It could be an address behind a reverse proxy.
// TLSConfig: the TLS configuration for the listener.
2024-07-02 11:57:17 +02:00
type ListenerConfig struct {
2024-07-01 11:50:18 +02:00
Address string
TLSConfig *tls.Config
}
2024-07-29 21:53:07 +02:00
// Server is the main entry point for the relay server.
// It is the gate between the WebSocket listener and the Relay server logic.
// In a new HTTP connection, the server will accept the connection and pass it to the Relay server via the Accept method.
2024-05-17 17:43:28 +02:00
type Server struct {
2024-07-25 11:03:54 +02:00
relay *Relay
wSListener listener.Listener
2024-05-17 17:43:28 +02:00
}
2024-07-29 21:53:07 +02:00
// NewServer creates a new relay server instance.
// meter: the OpenTelemetry meter
// exposedAddress: this address will be used as the instance URL. It should be a domain:port format.
// tlsSupport: if true, the server will support TLS
// authValidator: the auth validator to use for the server
2024-07-24 16:26:26 +02:00
func NewServer(meter metric.Meter, exposedAddress string, tlsSupport bool, authValidator auth.Validator) (*Server, error) {
relay, err := NewRelay(meter, exposedAddress, tlsSupport, authValidator)
if err != nil {
return nil, err
2024-05-17 17:43:28 +02:00
}
2024-07-24 16:26:26 +02:00
return &Server{
relay: relay,
}, nil
2024-05-17 17:43:28 +02:00
}
2024-07-29 21:53:07 +02:00
// Listen starts the relay server.
2024-07-02 11:57:17 +02:00
func (r *Server) Listen(cfg ListenerConfig) error {
2024-07-01 11:50:18 +02:00
r.wSListener = &ws.Listener{
Address: cfg.Address,
TLSConfig: cfg.TLSConfig,
}
2024-07-26 14:45:34 +02:00
wslErr := r.wSListener.Listen(r.relay.Accept)
if wslErr != nil {
log.Errorf("failed to bind ws server: %s", wslErr)
}
2024-05-26 22:14:33 +02:00
2024-07-25 11:03:54 +02:00
return wslErr
2024-05-17 17:43:28 +02:00
}
2024-08-21 16:17:39 +02:00
// Shutdown stops the relay server. If there are active connections, they will be closed gracefully. In case of a context,
2024-07-29 21:53:07 +02:00
// the connections will be forcefully closed.
2024-08-21 16:17:39 +02:00
func (r *Server) Shutdown(ctx context.Context) (err error) {
// stop service new connections
if r.wSListener != nil {
2024-08-21 16:17:39 +02:00
err = r.wSListener.Shutdown(ctx)
2024-05-17 17:43:28 +02:00
}
2024-06-05 19:49:30 +02:00
2024-08-21 16:17:39 +02:00
r.relay.Shutdown(ctx)
2024-07-25 11:03:54 +02:00
return
2024-05-17 17:43:28 +02:00
}
2024-07-29 21:53:07 +02:00
// InstanceURL returns the instance URL of the relay server.
func (r *Server) InstanceURL() string {
2024-07-24 16:34:47 +02:00
return r.relay.instanceURL
}