From 1afc9bcac77c5b27cbc5ad69ddeb83dcb04f3a80 Mon Sep 17 00:00:00 2001 From: Zoltan Papp Date: Sat, 11 Jul 2026 15:24:54 +0200 Subject: [PATCH] [client] Merge conntype package into peer The conntype package held only ConnPriority and its constants and was imported solely by peer. Move it into the peer package as priority.go and drop the conntype. qualifier from conn.go, event.go and worker_ice.go. --- client/internal/peer/conn.go | 39 +++++++++---------- client/internal/peer/event.go | 3 +- .../internal/peer/{conntype => }/priority.go | 2 +- client/internal/peer/worker_ice.go | 7 ++-- 4 files changed, 24 insertions(+), 27 deletions(-) rename client/internal/peer/{conntype => }/priority.go (96%) diff --git a/client/internal/peer/conn.go b/client/internal/peer/conn.go index 76cb87fac..59e5621c6 100644 --- a/client/internal/peer/conn.go +++ b/client/internal/peer/conn.go @@ -18,7 +18,6 @@ import ( "github.com/netbirdio/netbird/client/iface/configurer" "github.com/netbirdio/netbird/client/iface/wgproxy" "github.com/netbirdio/netbird/client/internal/metrics" - "github.com/netbirdio/netbird/client/internal/peer/conntype" "github.com/netbirdio/netbird/client/internal/peer/guard" icemaker "github.com/netbirdio/netbird/client/internal/peer/ice" "github.com/netbirdio/netbird/client/internal/peer/id" @@ -122,7 +121,7 @@ type Conn struct { statusRelay *worker.AtomicWorkerStatus statusICE *worker.AtomicWorkerStatus - currentConnPriority conntype.ConnPriority + currentConnPriority ConnPriority opened bool // this flag is used to prevent close in case of not opened connection // mailbox delivers events to the event loop of the current open @@ -589,7 +588,7 @@ func (conn *Conn) handleGuardTick() { // handleICEReady starts proxying traffic from/to local WireGuard and sets the // connection status to StatusConnected. -func (conn *Conn) handleICEReady(priority conntype.ConnPriority, iceConnInfo ICEConnInfo) { +func (conn *Conn) handleICEReady(priority ConnPriority, iceConnInfo ICEConnInfo) { if conn.ctx.Err() != nil { return } @@ -698,10 +697,10 @@ func (conn *Conn) handleICEDisconnected(sessionChanged bool) { conn.Log.Errorf("failed to switch to relay conn: %v", err) } - conn.currentConnPriority = conntype.Relay + conn.currentConnPriority = Relay } else { - conn.Log.Infof("ICE disconnected, do not switch to Relay. Reset priority to: %s", conntype.None.String()) - conn.currentConnPriority = conntype.None + conn.Log.Infof("ICE disconnected, do not switch to Relay. Reset priority to: %s", None.String()) + conn.currentConnPriority = None if err := conn.config.WgConfig.WgInterface.RemoveEndpointAddress(conn.config.WgConfig.RemoteKey); err != nil { conn.Log.Errorf("failed to remove wg endpoint: %v", err) } @@ -715,7 +714,7 @@ func (conn *Conn) handleICEDisconnected(sessionChanged bool) { conn.disableWgWatcherIfNeeded() - if conn.currentConnPriority == conntype.None { + if conn.currentConnPriority == None { conn.metricsStages.Disconnected() } @@ -785,7 +784,7 @@ func (conn *Conn) handleRelayReady(rci RelayConnInfo) { conn.injectPendingFirstPacket(wgProxy, nil) conn.rosenpassRemoteKey = rci.rosenpassPubKey - conn.currentConnPriority = conntype.Relay + conn.currentConnPriority = Relay conn.statusRelay.SetConnected() conn.setRelayedProxy(wgProxy) conn.updateRelayStatus(rci.relayedConn.RemoteAddr().String(), rci.rosenpassPubKey, updateTime) @@ -802,9 +801,9 @@ func (conn *Conn) handleRelayDisconnected() { conn.Log.Debugf("relay connection is disconnected") - if conn.currentConnPriority == conntype.Relay { + if conn.currentConnPriority == Relay { conn.Log.Debugf("clean up WireGuard config") - conn.currentConnPriority = conntype.None + conn.currentConnPriority = None if err := conn.config.WgConfig.WgInterface.RemoveEndpointAddress(conn.config.WgConfig.RemoteKey); err != nil { conn.Log.Errorf("failed to remove wg endpoint: %v", err) } @@ -823,7 +822,7 @@ func (conn *Conn) handleRelayDisconnected() { conn.disableWgWatcherIfNeeded() - if conn.currentConnPriority == conntype.None { + if conn.currentConnPriority == None { conn.metricsStages.Disconnected() } @@ -849,10 +848,10 @@ func (conn *Conn) handleWGTimeout() { // Close the active connection based on current priority switch conn.currentConnPriority { - case conntype.Relay: + case Relay: conn.workerRelay.CloseConn() conn.handleRelayDisconnected() - case conntype.ICEP2P, conntype.ICETurn: + case ICEP2P, ICETurn: conn.workerICE.Close() default: conn.Log.Debugf("No active connection to close on WG timeout") @@ -891,7 +890,7 @@ func (conn *Conn) handleWGCheckSuccess() { conn.wgTimeouts = 0 } -func (conn *Conn) onICEConnectionIsReady(priority conntype.ConnPriority, iceConnInfo ICEConnInfo) { +func (conn *Conn) onICEConnectionIsReady(priority ConnPriority, iceConnInfo ICEConnInfo) { conn.post(evICEReady{priority: priority, info: iceConnInfo}) } @@ -997,7 +996,7 @@ func (conn *Conn) updateIceState(iceConnInfo ICEConnInfo, updateTime time.Time) func (conn *Conn) setStatusToDisconnected() { conn.statusRelay.SetDisconnected() conn.statusICE.SetDisconnected() - conn.currentConnPriority = conntype.None + conn.currentConnPriority = None peerState := status.State{ PubKey: conn.config.Key, @@ -1030,7 +1029,7 @@ func (conn *Conn) doOnConnected(remoteRosenpassPubKey []byte, remoteRosenpassAdd func (conn *Conn) isRelayed() bool { switch conn.currentConnPriority { - case conntype.Relay, conntype.ICETurn: + case Relay, ICETurn: return true default: return false @@ -1091,7 +1090,7 @@ func (conn *Conn) enableWgWatcherIfNeeded(enabledTime time.Time) { } func (conn *Conn) disableWgWatcherIfNeeded() { - if conn.currentConnPriority == conntype.None && conn.wgWatcherCancel != nil { + if conn.currentConnPriority == None && conn.wgWatcherCancel != nil { conn.wgWatcherCancel() conn.wgWatcherCancel = nil } @@ -1124,11 +1123,11 @@ func (conn *Conn) resetEndpoint() { } func (conn *Conn) isReadyToUpgrade() bool { - return conn.wgProxyRelay != nil && conn.currentConnPriority != conntype.Relay + return conn.wgProxyRelay != nil && conn.currentConnPriority != Relay } func (conn *Conn) isICEActive() bool { - return (conn.currentConnPriority == conntype.ICEP2P || conn.currentConnPriority == conntype.ICETurn) && conn.statusICE.Get() == worker.StatusConnected + return (conn.currentConnPriority == ICEP2P || conn.currentConnPriority == ICETurn) && conn.statusICE.Get() == worker.StatusConnected } func (conn *Conn) handleConfigurationFailure(err error, wgProxy wgproxy.Proxy) { @@ -1168,7 +1167,7 @@ func (conn *Conn) recordConnectionMetrics() { var connType metrics.ConnectionType switch conn.currentConnPriority { - case conntype.Relay: + case Relay: connType = metrics.ConnectionTypeRelay default: connType = metrics.ConnectionTypeICE diff --git a/client/internal/peer/event.go b/client/internal/peer/event.go index ed5507326..f05e6f80a 100644 --- a/client/internal/peer/event.go +++ b/client/internal/peer/event.go @@ -5,7 +5,6 @@ import ( "github.com/pion/ice/v4" - "github.com/netbirdio/netbird/client/internal/peer/conntype" "github.com/netbirdio/netbird/route" ) @@ -35,7 +34,7 @@ type evRemoteCandidate struct { } type evICEReady struct { - priority conntype.ConnPriority + priority ConnPriority info ICEConnInfo } diff --git a/client/internal/peer/conntype/priority.go b/client/internal/peer/priority.go similarity index 96% rename from client/internal/peer/conntype/priority.go rename to client/internal/peer/priority.go index 6746ca7d4..0aa7be86f 100644 --- a/client/internal/peer/conntype/priority.go +++ b/client/internal/peer/priority.go @@ -1,4 +1,4 @@ -package conntype +package peer import ( "fmt" diff --git a/client/internal/peer/worker_ice.go b/client/internal/peer/worker_ice.go index 99cf9fe17..4244e1500 100644 --- a/client/internal/peer/worker_ice.go +++ b/client/internal/peer/worker_ice.go @@ -13,7 +13,6 @@ import ( "github.com/netbirdio/netbird/client/iface" "github.com/netbirdio/netbird/client/iface/udpmux" - "github.com/netbirdio/netbird/client/internal/peer/conntype" icemaker "github.com/netbirdio/netbird/client/internal/peer/ice" "github.com/netbirdio/netbird/client/internal/peer/status" "github.com/netbirdio/netbird/client/internal/portforward" @@ -611,10 +610,10 @@ func isRelayed(pair *ice.CandidatePair) bool { return false } -func selectedPriority(pair *ice.CandidatePair) conntype.ConnPriority { +func selectedPriority(pair *ice.CandidatePair) ConnPriority { if isRelayed(pair) { - return conntype.ICETurn + return ICETurn } else { - return conntype.ICEP2P + return ICEP2P } }