2024-06-17 17:52:22 +02:00
|
|
|
package peer
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"errors"
|
|
|
|
"math/rand"
|
|
|
|
"net"
|
|
|
|
"time"
|
|
|
|
|
|
|
|
log "github.com/sirupsen/logrus"
|
|
|
|
|
|
|
|
relayClient "github.com/netbirdio/netbird/relay/client"
|
|
|
|
)
|
|
|
|
|
|
|
|
type OnRelayReadyCallback func(info RelayConnInfo)
|
|
|
|
|
|
|
|
type RelayConnInfo struct {
|
|
|
|
relayedConn net.Conn
|
|
|
|
rosenpassPubKey []byte
|
|
|
|
rosenpassAddr string
|
|
|
|
}
|
|
|
|
|
2024-06-18 11:22:40 +02:00
|
|
|
type WorkerRelay struct {
|
2024-06-17 17:52:22 +02:00
|
|
|
ctx context.Context
|
|
|
|
log *log.Entry
|
|
|
|
relayManager *relayClient.Manager
|
|
|
|
config ConnConfig
|
|
|
|
onRelayConnReadyFN OnRelayReadyCallback
|
2024-06-18 17:40:37 +02:00
|
|
|
onStatusChanged func(ConnStatus)
|
2024-06-17 17:52:22 +02:00
|
|
|
doHandshakeFn DoHandshake
|
|
|
|
}
|
|
|
|
|
2024-06-18 17:40:37 +02:00
|
|
|
func NewWorkerRelay(ctx context.Context, log *log.Entry, relayManager *relayClient.Manager, config ConnConfig, onRelayConnReadyFN OnRelayReadyCallback, onStatusChanged func(ConnStatus), doHandshakeFn DoHandshake) *WorkerRelay {
|
2024-06-18 11:22:40 +02:00
|
|
|
return &WorkerRelay{
|
2024-06-17 17:52:22 +02:00
|
|
|
ctx: ctx,
|
|
|
|
log: log,
|
|
|
|
relayManager: relayManager,
|
|
|
|
config: config,
|
|
|
|
onRelayConnReadyFN: onRelayConnReadyFN,
|
2024-06-18 17:40:37 +02:00
|
|
|
onStatusChanged: onStatusChanged,
|
2024-06-17 17:52:22 +02:00
|
|
|
doHandshakeFn: doHandshakeFn,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// SetupRelayConnection todo: this function is not completed. Make no sense to put it in a for loop because we are not waiting for any event
|
2024-06-18 11:22:40 +02:00
|
|
|
func (w *WorkerRelay) SetupRelayConnection() {
|
2024-06-17 17:52:22 +02:00
|
|
|
for {
|
2024-06-18 11:22:40 +02:00
|
|
|
if !w.waitForReconnectTry() {
|
2024-06-17 17:52:22 +02:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2024-06-18 11:22:40 +02:00
|
|
|
remoteOfferAnswer, err := w.doHandshakeFn()
|
2024-06-17 17:52:22 +02:00
|
|
|
if err != nil {
|
|
|
|
if errors.Is(err, ErrSignalIsNotReady) {
|
2024-06-18 11:22:40 +02:00
|
|
|
w.log.Infof("signal client isn't ready, skipping connection attempt")
|
2024-06-17 17:52:22 +02:00
|
|
|
}
|
2024-06-18 11:22:40 +02:00
|
|
|
w.log.Errorf("failed to do handshake: %v", err)
|
2024-06-17 17:52:22 +02:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
2024-06-18 11:22:40 +02:00
|
|
|
if !w.isRelaySupported(remoteOfferAnswer) {
|
2024-06-17 17:52:22 +02:00
|
|
|
// todo should we retry?
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
// the relayManager will return with error in case if the connection has lost with relay server
|
2024-06-18 11:22:40 +02:00
|
|
|
currentRelayAddress, err := w.relayManager.RelayAddress()
|
2024-06-17 17:52:22 +02:00
|
|
|
if err != nil {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
2024-06-18 11:22:40 +02:00
|
|
|
srv := w.preferredRelayServer(currentRelayAddress.String(), remoteOfferAnswer.RelaySrvAddress)
|
|
|
|
relayedConn, err := w.relayManager.OpenConn(srv, w.config.Key)
|
2024-06-17 17:52:22 +02:00
|
|
|
if err != nil {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
2024-06-18 11:22:40 +02:00
|
|
|
go w.onRelayConnReadyFN(RelayConnInfo{
|
2024-06-17 17:52:22 +02:00
|
|
|
relayedConn: relayedConn,
|
|
|
|
rosenpassPubKey: remoteOfferAnswer.RosenpassPubKey,
|
|
|
|
rosenpassAddr: remoteOfferAnswer.RosenpassAddr,
|
|
|
|
})
|
2024-06-18 11:10:17 +02:00
|
|
|
|
|
|
|
// todo: waitForDisconnection()
|
2024-06-17 17:52:22 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-06-18 11:22:40 +02:00
|
|
|
func (w *WorkerRelay) RelayAddress() (net.Addr, error) {
|
|
|
|
return w.relayManager.RelayAddress()
|
2024-06-17 17:52:22 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// todo check my side too
|
2024-06-18 11:22:40 +02:00
|
|
|
func (w *WorkerRelay) isRelaySupported(answer *OfferAnswer) bool {
|
2024-06-17 17:52:22 +02:00
|
|
|
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 {
|
2024-06-17 17:52:22 +02:00
|
|
|
return myRelayAddress
|
|
|
|
}
|
|
|
|
return remoteRelayAddress
|
|
|
|
}
|
|
|
|
|
2024-06-18 11:22:40 +02:00
|
|
|
func (w *WorkerRelay) RelayIsSupportedLocally() bool {
|
|
|
|
return w.relayManager.HasRelayAddress()
|
2024-06-17 17:52:22 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// waitForReconnectTry waits for a random duration before trying to reconnect
|
2024-06-18 11:22:40 +02:00
|
|
|
func (w *WorkerRelay) waitForReconnectTry() bool {
|
2024-06-17 17:52:22 +02:00
|
|
|
minWait := 500
|
|
|
|
maxWait := 2000
|
|
|
|
duration := time.Duration(rand.Intn(maxWait-minWait)+minWait) * time.Millisecond
|
2024-06-18 11:20:01 +02:00
|
|
|
|
|
|
|
timeout := time.NewTimer(duration)
|
|
|
|
defer timeout.Stop()
|
|
|
|
|
2024-06-17 17:52:22 +02:00
|
|
|
select {
|
2024-06-18 11:22:40 +02:00
|
|
|
case <-w.ctx.Done():
|
2024-06-17 17:52:22 +02:00
|
|
|
return false
|
2024-06-18 11:20:01 +02:00
|
|
|
case <-timeout.C:
|
2024-06-17 17:52:22 +02:00
|
|
|
return true
|
|
|
|
}
|
|
|
|
}
|