Code cleaning

This commit is contained in:
Zoltán Papp 2024-06-19 11:52:40 +02:00
parent 2f32e0d8cf
commit 4d2a25b728
3 changed files with 74 additions and 50 deletions

View File

@ -63,6 +63,15 @@ type ConnConfig struct {
type BeforeAddPeerHookFunc func(connID nbnet.ConnectionID, IP net.IP) error
type AfterRemovePeerHookFunc func(connID nbnet.ConnectionID) error
type WorkerCallbacks struct {
OnRelayReadyCallback func(info RelayConnInfo)
OnRelayStatusChanged func(ConnStatus)
OnICEConnReadyCallback func(ConnPriority, ICEConnInfo)
OnICEStatusChanged func(ConnStatus)
DoHandshake func(*OfferAnswer, error)
}
type Conn struct {
log *log.Entry
mu sync.Mutex
@ -116,8 +125,21 @@ func NewConn(engineCtx context.Context, config ConnConfig, statusRecorder *Statu
statusRelay: StatusDisconnected,
statusICE: StatusDisconnected,
}
conn.workerICE = NewWorkerICE(ctx, conn.log, config, config.ICEConfig, signaler, iFaceDiscover, statusRecorder, conn.iCEConnectionIsReady, conn.onWorkerICEStateChanged, conn.doHandshake)
conn.workerRelay = NewWorkerRelay(ctx, conn.log, relayManager, config, conn.relayConnectionIsReady, conn.onWorkerRelayStateChanged, conn.doHandshake)
rFns := WorkerRelayCallbacks{
OnConnReady: conn.relayConnectionIsReady,
OnStatusChanged: conn.onWorkerRelayStateChanged,
DoHandshake: conn.doHandshake,
}
wFns := WorkerICECallbacks{
OnConnReady: conn.iCEConnectionIsReady,
OnStatusChanged: conn.onWorkerICEStateChanged,
DoHandshake: conn.doHandshake,
}
conn.workerRelay = NewWorkerRelay(ctx, conn.log, relayManager, config, rFns)
conn.workerICE = NewWorkerICE(ctx, conn.log, config, config.ICEConfig, signaler, iFaceDiscover, statusRecorder, wFns)
return conn, nil
}

View File

@ -53,8 +53,6 @@ type ICEConfig struct {
NATExternalIPs []string
}
type OnICEConnReadyCallback func(ConnPriority, ICEConnInfo)
type ICEConnInfo struct {
RemoteConn net.Conn
RosenpassPubKey []byte
@ -68,17 +66,21 @@ type ICEConnInfo struct {
RelayedOnLocal bool
}
type WorkerICECallbacks struct {
OnConnReady func(ConnPriority, ICEConnInfo)
OnStatusChanged func(ConnStatus)
DoHandshake func() (*OfferAnswer, error)
}
type WorkerICE struct {
ctx context.Context
log *log.Entry
config ConnConfig
configICE ICEConfig
signaler *Signaler
iFaceDiscover stdnet.ExternalIFaceDiscover
statusRecorder *Status
onICEConnReady OnICEConnReadyCallback
onStatusChanged func(ConnStatus)
doHandshakeFn DoHandshake
ctx context.Context
log *log.Entry
config ConnConfig
configICE ICEConfig
signaler *Signaler
iFaceDiscover stdnet.ExternalIFaceDiscover
statusRecorder *Status
conn WorkerICECallbacks
selectedPriority ConnPriority
@ -92,18 +94,16 @@ type WorkerICE struct {
localPwd string
}
func NewWorkerICE(ctx context.Context, log *log.Entry, config ConnConfig, configICE ICEConfig, signaler *Signaler, ifaceDiscover stdnet.ExternalIFaceDiscover, statusRecorder *Status, onICEConnReady OnICEConnReadyCallback, onStatusChanged func(ConnStatus), doHandshakeFn DoHandshake) *WorkerICE {
func NewWorkerICE(ctx context.Context, log *log.Entry, config ConnConfig, configICE ICEConfig, signaler *Signaler, ifaceDiscover stdnet.ExternalIFaceDiscover, statusRecorder *Status, callBacks WorkerICECallbacks) *WorkerICE {
cice := &WorkerICE{
ctx: ctx,
log: log,
config: config,
configICE: configICE,
signaler: signaler,
iFaceDiscover: ifaceDiscover,
statusRecorder: statusRecorder,
onICEConnReady: onICEConnReady,
onStatusChanged: onStatusChanged,
doHandshakeFn: doHandshakeFn,
ctx: ctx,
log: log,
config: config,
configICE: configICE,
signaler: signaler,
iFaceDiscover: ifaceDiscover,
statusRecorder: statusRecorder,
conn: callBacks,
}
return cice
}
@ -118,9 +118,9 @@ func (w *WorkerICE) SetupICEConnection(hasRelayOnLocally bool) {
return
}
w.onStatusChanged(StatusConnecting)
w.conn.OnStatusChanged(StatusConnecting)
remoteOfferAnswer, err := w.doHandshakeFn()
remoteOfferAnswer, err := w.conn.DoHandshake()
if err != nil {
if errors.Is(err, ErrSignalIsNotReady) {
w.log.Infof("signal client isn't ready, skipping connection attempt")
@ -200,12 +200,12 @@ func (w *WorkerICE) SetupICEConnection(hasRelayOnLocally bool) {
Relayed: isRelayed(pair),
RelayedOnLocal: isRelayCandidate(pair.Local),
}
go w.onICEConnReady(w.selectedPriority, ci)
go w.conn.OnConnReady(w.selectedPriority, ci)
<-ctx.Done()
ctxCancel()
_ = w.agent.Close()
w.onStatusChanged(StatusDisconnected)
w.conn.OnStatusChanged(StatusDisconnected)
}
}

View File

@ -12,33 +12,33 @@ import (
relayClient "github.com/netbirdio/netbird/relay/client"
)
type OnRelayReadyCallback func(info RelayConnInfo)
type RelayConnInfo struct {
relayedConn net.Conn
rosenpassPubKey []byte
rosenpassAddr string
}
type WorkerRelay struct {
ctx context.Context
log *log.Entry
relayManager *relayClient.Manager
config ConnConfig
onRelayConnReadyFN OnRelayReadyCallback
onStatusChanged func(ConnStatus)
doHandshakeFn DoHandshake
type WorkerRelayCallbacks struct {
OnConnReady func(RelayConnInfo)
OnStatusChanged func(ConnStatus)
DoHandshake func() (*OfferAnswer, error)
}
func NewWorkerRelay(ctx context.Context, log *log.Entry, relayManager *relayClient.Manager, config ConnConfig, onRelayConnReadyFN OnRelayReadyCallback, onStatusChanged func(ConnStatus), doHandshakeFn DoHandshake) *WorkerRelay {
type WorkerRelay struct {
ctx context.Context
log *log.Entry
relayManager *relayClient.Manager
config ConnConfig
conn WorkerRelayCallbacks
}
func NewWorkerRelay(ctx context.Context, log *log.Entry, relayManager *relayClient.Manager, config ConnConfig, callbacks WorkerRelayCallbacks) *WorkerRelay {
return &WorkerRelay{
ctx: ctx,
log: log,
relayManager: relayManager,
config: config,
onRelayConnReadyFN: onRelayConnReadyFN,
onStatusChanged: onStatusChanged,
doHandshakeFn: doHandshakeFn,
ctx: ctx,
log: log,
relayManager: relayManager,
config: config,
conn: callbacks,
}
}
@ -49,7 +49,7 @@ func (w *WorkerRelay) SetupRelayConnection() {
return
}
remoteOfferAnswer, err := w.doHandshakeFn()
remoteOfferAnswer, err := w.conn.DoHandshake()
if err != nil {
if errors.Is(err, ErrSignalIsNotReady) {
w.log.Infof("signal client isn't ready, skipping connection attempt")
@ -75,7 +75,7 @@ func (w *WorkerRelay) SetupRelayConnection() {
continue
}
go w.onRelayConnReadyFN(RelayConnInfo{
go w.conn.OnConnReady(RelayConnInfo{
relayedConn: relayedConn,
rosenpassPubKey: remoteOfferAnswer.RosenpassPubKey,
rosenpassAddr: remoteOfferAnswer.RosenpassAddr,
@ -89,8 +89,10 @@ func (w *WorkerRelay) RelayAddress() (net.Addr, error) {
return w.relayManager.RelayAddress()
}
// todo check my side too
func (w *WorkerRelay) isRelaySupported(answer *OfferAnswer) bool {
if !w.relayManager.HasRelayAddress() {
return false
}
return answer.RelaySrvAddress != ""
}