[client] Decouple ICE and relay workers from Conn via interfaces

WorkerICE and WorkerRelay took a concrete *Conn back-pointer. Replace it
with small callback interfaces (iceCallbacks, relayCallbacks) covering
only the methods each worker invokes on the connection. WorkerICE also
receives its portForwardManager as an explicit parameter instead of
reaching into conn.portForwardManager.
This commit is contained in:
Zoltan Papp
2026-07-11 18:29:26 +02:00
parent 1afc9bcac7
commit 9e75d5c732
3 changed files with 34 additions and 22 deletions
+1 -1
View File
@@ -229,7 +229,7 @@ func (conn *Conn) open(engineCtx context.Context, firstPacket []byte) error {
if !IsForceRelayed() { if !IsForceRelayed() {
relayIsSupportedLocally := conn.workerRelay.RelayIsSupportedLocally() relayIsSupportedLocally := conn.workerRelay.RelayIsSupportedLocally()
workerICE, err := NewWorkerICE(conn.ctx, conn.Log, conn.config, conn, conn.signaler, conn.iFaceDiscover, conn.statusRecorder, relayIsSupportedLocally) workerICE, err := NewWorkerICE(conn.ctx, conn.Log, conn.config, conn, conn.signaler, conn.iFaceDiscover, conn.statusRecorder, conn.portForwardManager, relayIsSupportedLocally)
if err != nil { if err != nil {
return err return err
} }
+26 -19
View File
@@ -20,6 +20,11 @@ import (
"github.com/netbirdio/netbird/route" "github.com/netbirdio/netbird/route"
) )
type iceCallbacks interface {
onICEConnectionIsReady(priority ConnPriority, iceConnInfo ICEConnInfo)
onICEStateDisconnected(sessionChanged bool)
}
type ICEConnInfo struct { type ICEConnInfo struct {
RemoteConn net.Conn RemoteConn net.Conn
RosenpassPubKey []byte RosenpassPubKey []byte
@@ -33,14 +38,15 @@ type ICEConnInfo struct {
} }
type WorkerICE struct { type WorkerICE struct {
ctx context.Context ctx context.Context
log *log.Entry log *log.Entry
config ConnConfig config ConnConfig
conn *Conn conn iceCallbacks
signaler *Signaler signaler *Signaler
iFaceDiscover stdnet.ExternalIFaceDiscover iFaceDiscover stdnet.ExternalIFaceDiscover
statusRecorder *status.Recorder statusRecorder *status.Recorder
hasRelayOnLocally bool portForwardManager *portforward.Manager
hasRelayOnLocally bool
agent *icemaker.ThreadSafeAgent agent *icemaker.ThreadSafeAgent
agentDialerCancel context.CancelFunc agentDialerCancel context.CancelFunc
@@ -65,22 +71,23 @@ type WorkerICE struct {
portForwardAttempted bool portForwardAttempted bool
} }
func NewWorkerICE(ctx context.Context, log *log.Entry, config ConnConfig, conn *Conn, signaler *Signaler, ifaceDiscover stdnet.ExternalIFaceDiscover, statusRecorder *status.Recorder, hasRelayOnLocally bool) (*WorkerICE, error) { func NewWorkerICE(ctx context.Context, log *log.Entry, config ConnConfig, conn iceCallbacks, signaler *Signaler, ifaceDiscover stdnet.ExternalIFaceDiscover, statusRecorder *status.Recorder, portForwardManager *portforward.Manager, hasRelayOnLocally bool) (*WorkerICE, error) {
sessionID, err := NewICESessionID() sessionID, err := NewICESessionID()
if err != nil { if err != nil {
return nil, err return nil, err
} }
w := &WorkerICE{ w := &WorkerICE{
ctx: ctx, ctx: ctx,
log: log, log: log,
config: config, config: config,
conn: conn, conn: conn,
signaler: signaler, signaler: signaler,
iFaceDiscover: ifaceDiscover, iFaceDiscover: ifaceDiscover,
statusRecorder: statusRecorder, statusRecorder: statusRecorder,
hasRelayOnLocally: hasRelayOnLocally, portForwardManager: portForwardManager,
sessionID: sessionID, hasRelayOnLocally: hasRelayOnLocally,
sessionID: sessionID,
} }
localUfrag, localPwd, err := icemaker.GenerateICECredentials() localUfrag, localPwd, err := icemaker.GenerateICECredentials()
@@ -382,7 +389,7 @@ func (w *WorkerICE) onICECandidate(candidate ice.Candidate) {
// injectPortForwardedCandidate signals an additional candidate using the pre-created port mapping. // injectPortForwardedCandidate signals an additional candidate using the pre-created port mapping.
func (w *WorkerICE) injectPortForwardedCandidate(srflxCandidate ice.Candidate) { func (w *WorkerICE) injectPortForwardedCandidate(srflxCandidate ice.Candidate) {
pfManager := w.conn.portForwardManager pfManager := w.portForwardManager
if pfManager == nil { if pfManager == nil {
return return
} }
+7 -2
View File
@@ -19,12 +19,17 @@ type RelayConnInfo struct {
rosenpassAddr string rosenpassAddr string
} }
type relayCallbacks interface {
onRelayConnectionIsReady(rci RelayConnInfo)
onRelayDisconnected()
}
type WorkerRelay struct { type WorkerRelay struct {
peerCtx context.Context peerCtx context.Context
log *log.Entry log *log.Entry
isController bool isController bool
config ConnConfig config ConnConfig
conn *Conn conn relayCallbacks
relayManager *relayClient.Manager relayManager *relayClient.Manager
relayedConn net.Conn relayedConn net.Conn
@@ -33,7 +38,7 @@ type WorkerRelay struct {
relaySupportedOnRemotePeer atomic.Bool relaySupportedOnRemotePeer atomic.Bool
} }
func NewWorkerRelay(ctx context.Context, log *log.Entry, ctrl bool, config ConnConfig, conn *Conn, relayManager *relayClient.Manager) *WorkerRelay { func NewWorkerRelay(ctx context.Context, log *log.Entry, ctrl bool, config ConnConfig, conn relayCallbacks, relayManager *relayClient.Manager) *WorkerRelay {
r := &WorkerRelay{ r := &WorkerRelay{
peerCtx: ctx, peerCtx: ctx,
log: log, log: log,