netbird/client/internal/peer/worker_relay.go

92 lines
2.4 KiB
Go
Raw Normal View History

package peer
import (
"context"
"net"
log "github.com/sirupsen/logrus"
relayClient "github.com/netbirdio/netbird/relay/client"
)
type RelayConnInfo struct {
relayedConn net.Conn
rosenpassPubKey []byte
rosenpassAddr string
}
2024-06-19 11:52:40 +02:00
type WorkerRelayCallbacks struct {
OnConnReady func(RelayConnInfo)
OnStatusChanged func(ConnStatus)
}
2024-06-18 11:22:40 +02:00
type WorkerRelay struct {
2024-06-19 11:52:40 +02:00
ctx context.Context
log *log.Entry
config ConnConfig
2024-06-21 15:35:15 +02:00
relayManager *relayClient.Manager
2024-06-19 11:52:40 +02:00
conn WorkerRelayCallbacks
}
2024-06-21 15:35:15 +02:00
func NewWorkerRelay(ctx context.Context, log *log.Entry, config ConnConfig, relayManager *relayClient.Manager, callbacks WorkerRelayCallbacks) *WorkerRelay {
2024-06-18 11:22:40 +02:00
return &WorkerRelay{
2024-06-19 11:52:40 +02:00
ctx: ctx,
log: log,
config: config,
2024-06-21 15:35:15 +02:00
relayManager: relayManager,
2024-06-19 11:52:40 +02:00
conn: callbacks,
}
}
2024-06-21 12:35:28 +02:00
func (w *WorkerRelay) OnNewOffer(remoteOfferAnswer *OfferAnswer) {
if !w.isRelaySupported(remoteOfferAnswer) {
w.log.Infof("Relay is not supported by remote peer")
// todo should we retry?
// if the remote peer doesn't support relay make no sense to retry infinity
// but if the remote peer supports relay just the connection is lost we should retry
return
}
// the relayManager will return with error in case if the connection has lost with relay server
currentRelayAddress, err := w.relayManager.RelayAddress()
if err != nil {
w.log.Infof("local Relay connection is lost, skipping connection attempt")
return
}
srv := w.preferredRelayServer(currentRelayAddress.String(), remoteOfferAnswer.RelaySrvAddress)
relayedConn, err := w.relayManager.OpenConn(srv, w.config.Key)
if err != nil {
w.log.Infof("do not need to reopen relay connection: %s", err)
return
}
2024-06-21 12:35:28 +02:00
go w.conn.OnConnReady(RelayConnInfo{
relayedConn: relayedConn,
rosenpassPubKey: remoteOfferAnswer.RosenpassPubKey,
rosenpassAddr: remoteOfferAnswer.RosenpassAddr,
})
}
2024-06-18 11:22:40 +02:00
func (w *WorkerRelay) RelayAddress() (net.Addr, error) {
return w.relayManager.RelayAddress()
}
2024-06-18 11:22:40 +02:00
func (w *WorkerRelay) isRelaySupported(answer *OfferAnswer) bool {
2024-06-19 11:52:40 +02:00
if !w.relayManager.HasRelayAddress() {
return false
}
return answer.RelaySrvAddress != ""
}
2024-06-18 11:22:40 +02:00
func (w *WorkerRelay) preferredRelayServer(myRelayAddress, remoteRelayAddress string) string {
if w.config.LocalKey > w.config.Key {
return myRelayAddress
}
return remoteRelayAddress
}
2024-06-18 11:22:40 +02:00
func (w *WorkerRelay) RelayIsSupportedLocally() bool {
return w.relayManager.HasRelayAddress()
}