[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.
This commit is contained in:
Zoltan Papp
2026-08-05 16:14:11 +02:00
committed by Zoltán Papp
parent a3aabda1e9
commit fda00c06c2
4 changed files with 24 additions and 27 deletions
+19 -20
View File
@@ -18,7 +18,6 @@ import (
"github.com/netbirdio/netbird/client/iface/configurer" "github.com/netbirdio/netbird/client/iface/configurer"
"github.com/netbirdio/netbird/client/iface/wgproxy" "github.com/netbirdio/netbird/client/iface/wgproxy"
"github.com/netbirdio/netbird/client/internal/metrics" "github.com/netbirdio/netbird/client/internal/metrics"
"github.com/netbirdio/netbird/client/internal/peer/conntype"
"github.com/netbirdio/netbird/client/internal/peer/guard" "github.com/netbirdio/netbird/client/internal/peer/guard"
icemaker "github.com/netbirdio/netbird/client/internal/peer/ice" icemaker "github.com/netbirdio/netbird/client/internal/peer/ice"
"github.com/netbirdio/netbird/client/internal/peer/id" "github.com/netbirdio/netbird/client/internal/peer/id"
@@ -122,7 +121,7 @@ type Conn struct {
statusRelay *worker.AtomicWorkerStatus statusRelay *worker.AtomicWorkerStatus
statusICE *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 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 // 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 // handleICEReady starts proxying traffic from/to local WireGuard and sets the
// connection status to StatusConnected. // 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 { if conn.ctx.Err() != nil {
return return
} }
@@ -698,10 +697,10 @@ func (conn *Conn) handleICEDisconnected(sessionChanged bool) {
conn.Log.Errorf("failed to switch to relay conn: %v", err) conn.Log.Errorf("failed to switch to relay conn: %v", err)
} }
conn.currentConnPriority = conntype.Relay conn.currentConnPriority = Relay
} else { } else {
conn.Log.Infof("ICE disconnected, do not switch to Relay. Reset priority to: %s", conntype.None.String()) conn.Log.Infof("ICE disconnected, do not switch to Relay. Reset priority to: %s", None.String())
conn.currentConnPriority = conntype.None conn.currentConnPriority = None
if err := conn.config.WgConfig.WgInterface.RemoveEndpointAddress(conn.config.WgConfig.RemoteKey); err != nil { if err := conn.config.WgConfig.WgInterface.RemoveEndpointAddress(conn.config.WgConfig.RemoteKey); err != nil {
conn.Log.Errorf("failed to remove wg endpoint: %v", err) conn.Log.Errorf("failed to remove wg endpoint: %v", err)
} }
@@ -715,7 +714,7 @@ func (conn *Conn) handleICEDisconnected(sessionChanged bool) {
conn.disableWgWatcherIfNeeded() conn.disableWgWatcherIfNeeded()
if conn.currentConnPriority == conntype.None { if conn.currentConnPriority == None {
conn.metricsStages.Disconnected() conn.metricsStages.Disconnected()
} }
@@ -785,7 +784,7 @@ func (conn *Conn) handleRelayReady(rci RelayConnInfo) {
conn.injectPendingFirstPacket(wgProxy, nil) conn.injectPendingFirstPacket(wgProxy, nil)
conn.rosenpassRemoteKey = rci.rosenpassPubKey conn.rosenpassRemoteKey = rci.rosenpassPubKey
conn.currentConnPriority = conntype.Relay conn.currentConnPriority = Relay
conn.statusRelay.SetConnected() conn.statusRelay.SetConnected()
conn.setRelayedProxy(wgProxy) conn.setRelayedProxy(wgProxy)
conn.updateRelayStatus(rci.relayedConn.RemoteAddr().String(), rci.rosenpassPubKey, updateTime) conn.updateRelayStatus(rci.relayedConn.RemoteAddr().String(), rci.rosenpassPubKey, updateTime)
@@ -802,9 +801,9 @@ func (conn *Conn) handleRelayDisconnected() {
conn.Log.Debugf("relay connection is disconnected") conn.Log.Debugf("relay connection is disconnected")
if conn.currentConnPriority == conntype.Relay { if conn.currentConnPriority == Relay {
conn.Log.Debugf("clean up WireGuard config") 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 { if err := conn.config.WgConfig.WgInterface.RemoveEndpointAddress(conn.config.WgConfig.RemoteKey); err != nil {
conn.Log.Errorf("failed to remove wg endpoint: %v", err) conn.Log.Errorf("failed to remove wg endpoint: %v", err)
} }
@@ -823,7 +822,7 @@ func (conn *Conn) handleRelayDisconnected() {
conn.disableWgWatcherIfNeeded() conn.disableWgWatcherIfNeeded()
if conn.currentConnPriority == conntype.None { if conn.currentConnPriority == None {
conn.metricsStages.Disconnected() conn.metricsStages.Disconnected()
} }
@@ -849,10 +848,10 @@ func (conn *Conn) handleWGTimeout() {
// Close the active connection based on current priority // Close the active connection based on current priority
switch conn.currentConnPriority { switch conn.currentConnPriority {
case conntype.Relay: case Relay:
conn.workerRelay.CloseConn() conn.workerRelay.CloseConn()
conn.handleRelayDisconnected() conn.handleRelayDisconnected()
case conntype.ICEP2P, conntype.ICETurn: case ICEP2P, ICETurn:
conn.workerICE.Close() conn.workerICE.Close()
default: default:
conn.Log.Debugf("No active connection to close on WG timeout") conn.Log.Debugf("No active connection to close on WG timeout")
@@ -891,7 +890,7 @@ func (conn *Conn) handleWGCheckSuccess() {
conn.wgTimeouts = 0 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}) conn.post(evICEReady{priority: priority, info: iceConnInfo})
} }
@@ -997,7 +996,7 @@ func (conn *Conn) updateIceState(iceConnInfo ICEConnInfo, updateTime time.Time)
func (conn *Conn) setStatusToDisconnected() { func (conn *Conn) setStatusToDisconnected() {
conn.statusRelay.SetDisconnected() conn.statusRelay.SetDisconnected()
conn.statusICE.SetDisconnected() conn.statusICE.SetDisconnected()
conn.currentConnPriority = conntype.None conn.currentConnPriority = None
peerState := status.State{ peerState := status.State{
PubKey: conn.config.Key, PubKey: conn.config.Key,
@@ -1030,7 +1029,7 @@ func (conn *Conn) doOnConnected(remoteRosenpassPubKey []byte, remoteRosenpassAdd
func (conn *Conn) isRelayed() bool { func (conn *Conn) isRelayed() bool {
switch conn.currentConnPriority { switch conn.currentConnPriority {
case conntype.Relay, conntype.ICETurn: case Relay, ICETurn:
return true return true
default: default:
return false return false
@@ -1091,7 +1090,7 @@ func (conn *Conn) enableWgWatcherIfNeeded(enabledTime time.Time) {
} }
func (conn *Conn) disableWgWatcherIfNeeded() { func (conn *Conn) disableWgWatcherIfNeeded() {
if conn.currentConnPriority == conntype.None && conn.wgWatcherCancel != nil { if conn.currentConnPriority == None && conn.wgWatcherCancel != nil {
conn.wgWatcherCancel() conn.wgWatcherCancel()
conn.wgWatcherCancel = nil conn.wgWatcherCancel = nil
} }
@@ -1124,11 +1123,11 @@ func (conn *Conn) resetEndpoint() {
} }
func (conn *Conn) isReadyToUpgrade() bool { 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 { 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) { func (conn *Conn) handleConfigurationFailure(err error, wgProxy wgproxy.Proxy) {
@@ -1168,7 +1167,7 @@ func (conn *Conn) recordConnectionMetrics() {
var connType metrics.ConnectionType var connType metrics.ConnectionType
switch conn.currentConnPriority { switch conn.currentConnPriority {
case conntype.Relay: case Relay:
connType = metrics.ConnectionTypeRelay connType = metrics.ConnectionTypeRelay
default: default:
connType = metrics.ConnectionTypeICE connType = metrics.ConnectionTypeICE
+1 -2
View File
@@ -5,7 +5,6 @@ import (
"github.com/pion/ice/v4" "github.com/pion/ice/v4"
"github.com/netbirdio/netbird/client/internal/peer/conntype"
"github.com/netbirdio/netbird/route" "github.com/netbirdio/netbird/route"
) )
@@ -35,7 +34,7 @@ type evRemoteCandidate struct {
} }
type evICEReady struct { type evICEReady struct {
priority conntype.ConnPriority priority ConnPriority
info ICEConnInfo info ICEConnInfo
} }
@@ -1,4 +1,4 @@
package conntype package peer
import ( import (
"fmt" "fmt"
+3 -4
View File
@@ -13,7 +13,6 @@ import (
"github.com/netbirdio/netbird/client/iface" "github.com/netbirdio/netbird/client/iface"
"github.com/netbirdio/netbird/client/iface/udpmux" "github.com/netbirdio/netbird/client/iface/udpmux"
"github.com/netbirdio/netbird/client/internal/peer/conntype"
icemaker "github.com/netbirdio/netbird/client/internal/peer/ice" icemaker "github.com/netbirdio/netbird/client/internal/peer/ice"
"github.com/netbirdio/netbird/client/internal/peer/status" "github.com/netbirdio/netbird/client/internal/peer/status"
"github.com/netbirdio/netbird/client/internal/portforward" "github.com/netbirdio/netbird/client/internal/portforward"
@@ -611,10 +610,10 @@ func isRelayed(pair *ice.CandidatePair) bool {
return false return false
} }
func selectedPriority(pair *ice.CandidatePair) conntype.ConnPriority { func selectedPriority(pair *ice.CandidatePair) ConnPriority {
if isRelayed(pair) { if isRelayed(pair) {
return conntype.ICETurn return ICETurn
} else { } else {
return conntype.ICEP2P return ICEP2P
} }
} }