diff --git a/client/internal/peer/conn.go b/client/internal/peer/conn.go index a84c84890..783f6cba4 100644 --- a/client/internal/peer/conn.go +++ b/client/internal/peer/conn.go @@ -140,13 +140,18 @@ func NewConn(engineCtx context.Context, config ConnConfig, statusRecorder *Statu DoHandshake: conn.doHandshake, } - conn.handshaker = NewHandshaker(ctx, connLog, config, signaler, conn.onNewOffer) - go conn.handshaker.Listen() - conn.workerRelay = NewWorkerRelay(ctx, connLog, relayManager, config, rFns) + conn.handshaker = NewHandshaker(ctx, connLog, config, signaler) + conn.workerRelay = NewWorkerRelay(ctx, connLog, config, relayManager, rFns) conn.workerICE, err = NewWorkerICE(ctx, connLog, config, config.ICEConfig, signaler, iFaceDiscover, statusRecorder, wFns) if err != nil { return nil, err } + + conn.handshaker.AddOnNewOfferListener(conn.workerRelay.OnNewOffer) + conn.handshaker.AddOnNewOfferListener(conn.workerICE.OnNewOffer) + + go conn.handshaker.Listen() + return conn, nil } @@ -169,9 +174,6 @@ func (conn *Conn) Open() { } relayIsSupportedLocally := conn.workerRelay.RelayIsSupportedLocally() - if relayIsSupportedLocally { - go conn.workerRelay.SetupRelayConnection() - } go conn.workerICE.SetupICEConnection(relayIsSupportedLocally) } @@ -557,12 +559,6 @@ func (conn *Conn) evalStatus() ConnStatus { return StatusDisconnected } -func (conn *Conn) onNewOffer(answer *OfferAnswer) { - // todo move to this callback into handshaker - go conn.workerRelay.OnNewOffer(answer) - go conn.workerICE.OnNewOffer(answer) -} - func isRosenpassEnabled(remoteRosenpassPubKey []byte) bool { return remoteRosenpassPubKey != nil } diff --git a/client/internal/peer/handshaker.go b/client/internal/peer/handshaker.go index 04a93e454..881d985be 100644 --- a/client/internal/peer/handshaker.go +++ b/client/internal/peer/handshaker.go @@ -53,12 +53,12 @@ type HandshakeArgs struct { } type Handshaker struct { - mu sync.Mutex - ctx context.Context - log *log.Entry - config ConnConfig - signaler *Signaler - onNewOfferListener func(*OfferAnswer) + mu sync.Mutex + ctx context.Context + log *log.Entry + config ConnConfig + signaler *Signaler + onNewOfferListeners []func(*OfferAnswer) // remoteOffersCh is a channel used to wait for remote credentials to proceed with the connection remoteOffersCh chan OfferAnswer @@ -71,18 +71,21 @@ type Handshaker struct { lastOfferArgs HandshakeArgs } -func NewHandshaker(ctx context.Context, log *log.Entry, config ConnConfig, signaler *Signaler, onNewOfferListener func(*OfferAnswer)) *Handshaker { +func NewHandshaker(ctx context.Context, log *log.Entry, config ConnConfig, signaler *Signaler) *Handshaker { return &Handshaker{ - ctx: ctx, - log: log, - config: config, - signaler: signaler, - remoteOffersCh: make(chan OfferAnswer), - remoteAnswerCh: make(chan OfferAnswer), - onNewOfferListener: onNewOfferListener, + ctx: ctx, + log: log, + config: config, + signaler: signaler, + remoteOffersCh: make(chan OfferAnswer), + remoteAnswerCh: make(chan OfferAnswer), } } +func (h *Handshaker) AddOnNewOfferListener(offer func(remoteOfferAnswer *OfferAnswer)) { + h.onNewOfferListeners = append(h.onNewOfferListeners, offer) +} + func (h *Handshaker) Listen() { for { remoteOfferAnswer, err := h.waitForRemoteOfferConfirmation() @@ -96,7 +99,9 @@ func (h *Handshaker) Listen() { } h.log.Debugf("received connection confirmation, running version %s and with remote WireGuard listen port %d", remoteOfferAnswer.Version, remoteOfferAnswer.WgListenPort) - go h.onNewOfferListener(remoteOfferAnswer) + for _, listener := range h.onNewOfferListeners { + go listener(remoteOfferAnswer) + } } } diff --git a/client/internal/peer/worker_relay.go b/client/internal/peer/worker_relay.go index 53b32dc22..158b8e068 100644 --- a/client/internal/peer/worker_relay.go +++ b/client/internal/peer/worker_relay.go @@ -23,26 +23,21 @@ type WorkerRelayCallbacks struct { type WorkerRelay struct { ctx context.Context log *log.Entry - relayManager *relayClient.Manager config ConnConfig + relayManager *relayClient.Manager conn WorkerRelayCallbacks } -func NewWorkerRelay(ctx context.Context, log *log.Entry, relayManager *relayClient.Manager, config ConnConfig, callbacks WorkerRelayCallbacks) *WorkerRelay { +func NewWorkerRelay(ctx context.Context, log *log.Entry, config ConnConfig, relayManager *relayClient.Manager, callbacks WorkerRelayCallbacks) *WorkerRelay { return &WorkerRelay{ ctx: ctx, log: log, - relayManager: relayManager, config: config, + relayManager: relayManager, conn: callbacks, } } -// 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 -func (w *WorkerRelay) SetupRelayConnection() { - -} - func (w *WorkerRelay) OnNewOffer(remoteOfferAnswer *OfferAnswer) { if !w.isRelaySupported(remoteOfferAnswer) { w.log.Infof("Relay is not supported by remote peer")