mirror of
https://github.com/netbirdio/netbird.git
synced 2026-10-30 08:19:09 +01:00
Integrate the relay authentication
This commit is contained in:
+12
-4
@@ -10,6 +10,7 @@ import (
|
||||
|
||||
log "github.com/sirupsen/logrus"
|
||||
|
||||
auth "github.com/netbirdio/netbird/relay/auth/hmac"
|
||||
"github.com/netbirdio/netbird/relay/client/dialer/ws"
|
||||
"github.com/netbirdio/netbird/relay/healthcheck"
|
||||
"github.com/netbirdio/netbird/relay/messages"
|
||||
@@ -98,6 +99,7 @@ type Client struct {
|
||||
log *log.Entry
|
||||
parentCtx context.Context
|
||||
connectionURL string
|
||||
authStore *auth.Store
|
||||
hashedID []byte
|
||||
|
||||
bufPool *sync.Pool
|
||||
@@ -115,12 +117,13 @@ type Client struct {
|
||||
}
|
||||
|
||||
// NewClient creates a new client for the relay server. The client is not connected to the server until the Connect
|
||||
func NewClient(ctx context.Context, serverURL, peerID string) *Client {
|
||||
func NewClient(ctx context.Context, serverURL string, authStore *auth.Store, peerID string) *Client {
|
||||
hashedID, hashedStringId := messages.HashID(peerID)
|
||||
return &Client{
|
||||
log: log.WithField("client_id", hashedStringId),
|
||||
parentCtx: ctx,
|
||||
connectionURL: serverURL,
|
||||
authStore: authStore,
|
||||
hashedID: hashedID,
|
||||
bufPool: &sync.Pool{
|
||||
New: func() any {
|
||||
@@ -234,7 +237,12 @@ func (c *Client) connect() error {
|
||||
}
|
||||
|
||||
func (c *Client) handShake() error {
|
||||
msg, err := messages.MarshalHelloMsg(c.hashedID)
|
||||
t, err := c.authStore.Token()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
msg, err := messages.MarshalHelloMsg(c.hashedID, t)
|
||||
if err != nil {
|
||||
log.Errorf("failed to marshal hello message: %s", err)
|
||||
return err
|
||||
@@ -262,11 +270,11 @@ func (c *Client) handShake() error {
|
||||
return fmt.Errorf("unexpected message type")
|
||||
}
|
||||
|
||||
domain, err := messages.UnmarshalHelloResponse(buf[:n])
|
||||
ia, err := messages.UnmarshalHelloResponse(buf[:n])
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
c.instanceURL = domain
|
||||
c.instanceURL = ia
|
||||
return nil
|
||||
}
|
||||
|
||||
|
||||
+13
-5
@@ -8,6 +8,8 @@ import (
|
||||
"time"
|
||||
|
||||
log "github.com/sirupsen/logrus"
|
||||
|
||||
relayAuth "github.com/netbirdio/netbird/relay/auth/hmac"
|
||||
)
|
||||
|
||||
var (
|
||||
@@ -35,9 +37,10 @@ func NewRelayTrack() *RelayTrack {
|
||||
// relay servers will be closed if there is no active connection. Periodically the manager will check if there is any
|
||||
// unused relay connection and close it.
|
||||
type Manager struct {
|
||||
ctx context.Context
|
||||
serverURL string
|
||||
peerID string
|
||||
ctx context.Context
|
||||
serverURL string
|
||||
peerID string
|
||||
tokenStore *relayAuth.Store
|
||||
|
||||
relayClient *Client
|
||||
reconnectGuard *Guard
|
||||
@@ -54,6 +57,7 @@ func NewManager(ctx context.Context, serverURL string, peerID string) *Manager {
|
||||
ctx: ctx,
|
||||
serverURL: serverURL,
|
||||
peerID: peerID,
|
||||
tokenStore: &relayAuth.Store{},
|
||||
relayClients: make(map[string]*RelayTrack),
|
||||
onDisconnectedListeners: make(map[string]map[*func()]struct{}),
|
||||
}
|
||||
@@ -65,7 +69,7 @@ func (m *Manager) Serve() error {
|
||||
return fmt.Errorf("manager already serving")
|
||||
}
|
||||
|
||||
m.relayClient = NewClient(m.ctx, m.serverURL, m.peerID)
|
||||
m.relayClient = NewClient(m.ctx, m.serverURL, m.tokenStore, m.peerID)
|
||||
err := m.relayClient.Connect()
|
||||
if err != nil {
|
||||
log.Errorf("failed to connect to relay server: %s", err)
|
||||
@@ -158,7 +162,7 @@ func (m *Manager) openConnVia(serverAddress, peerKey string) (net.Conn, error) {
|
||||
m.relayClients[serverAddress] = rt
|
||||
m.relayClientsMutex.Unlock()
|
||||
|
||||
relayClient := NewClient(m.ctx, serverAddress, m.peerID)
|
||||
relayClient := NewClient(m.ctx, serverAddress, m.tokenStore, m.peerID)
|
||||
err := relayClient.Connect()
|
||||
if err != nil {
|
||||
rt.Unlock()
|
||||
@@ -260,3 +264,7 @@ func (m *Manager) notifyOnDisconnectListeners(serverAddress string) {
|
||||
m.listenerLock.Unlock()
|
||||
|
||||
}
|
||||
|
||||
func (m *Manager) UpdateToken(token relayAuth.Token) {
|
||||
m.tokenStore.UpdateToken(token)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user