mirror of
https://github.com/netbirdio/netbird.git
synced 2025-04-09 01:38:46 +02:00
Remove unused variables from peer conn (#2074)
Remove unused variables from peer conn
This commit is contained in:
parent
4da29451d0
commit
983d7bafbe
@ -983,7 +983,6 @@ func (e *Engine) createPeerConn(pubKey string, allowedIPs string) (*peer.Conn, e
|
|||||||
WgConfig: wgConfig,
|
WgConfig: wgConfig,
|
||||||
LocalWgPort: e.config.WgPort,
|
LocalWgPort: e.config.WgPort,
|
||||||
NATExternalIPs: e.parseNATExternalIPMappings(),
|
NATExternalIPs: e.parseNATExternalIPMappings(),
|
||||||
UserspaceBind: e.wgInterface.IsUserspaceBind(),
|
|
||||||
RosenpassPubKey: e.getRosenpassPubKey(),
|
RosenpassPubKey: e.getRosenpassPubKey(),
|
||||||
RosenpassAddr: e.getRosenpassAddr(),
|
RosenpassAddr: e.getRosenpassAddr(),
|
||||||
}
|
}
|
||||||
@ -1046,8 +1045,6 @@ func (e *Engine) receiveSignalEvents() {
|
|||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
conn.RegisterProtoSupportMeta(msg.Body.GetFeaturesSupported())
|
|
||||||
|
|
||||||
var rosenpassPubKey []byte
|
var rosenpassPubKey []byte
|
||||||
rosenpassAddr := ""
|
rosenpassAddr := ""
|
||||||
if msg.GetBody().GetRosenpassConfig() != nil {
|
if msg.GetBody().GetRosenpassConfig() != nil {
|
||||||
@ -1070,8 +1067,6 @@ func (e *Engine) receiveSignalEvents() {
|
|||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
conn.RegisterProtoSupportMeta(msg.GetBody().GetFeaturesSupported())
|
|
||||||
|
|
||||||
var rosenpassPubKey []byte
|
var rosenpassPubKey []byte
|
||||||
rosenpassAddr := ""
|
rosenpassAddr := ""
|
||||||
if msg.GetBody().GetRosenpassConfig() != nil {
|
if msg.GetBody().GetRosenpassConfig() != nil {
|
||||||
|
@ -20,7 +20,6 @@ import (
|
|||||||
"github.com/netbirdio/netbird/iface"
|
"github.com/netbirdio/netbird/iface"
|
||||||
"github.com/netbirdio/netbird/iface/bind"
|
"github.com/netbirdio/netbird/iface/bind"
|
||||||
"github.com/netbirdio/netbird/route"
|
"github.com/netbirdio/netbird/route"
|
||||||
signal "github.com/netbirdio/netbird/signal/client"
|
|
||||||
sProto "github.com/netbirdio/netbird/signal/proto"
|
sProto "github.com/netbirdio/netbird/signal/proto"
|
||||||
nbnet "github.com/netbirdio/netbird/util/net"
|
nbnet "github.com/netbirdio/netbird/util/net"
|
||||||
"github.com/netbirdio/netbird/version"
|
"github.com/netbirdio/netbird/version"
|
||||||
@ -70,9 +69,6 @@ type ConnConfig struct {
|
|||||||
|
|
||||||
NATExternalIPs []string
|
NATExternalIPs []string
|
||||||
|
|
||||||
// UsesBind indicates whether the WireGuard interface is userspace and uses bind.ICEBind
|
|
||||||
UserspaceBind bool
|
|
||||||
|
|
||||||
// RosenpassPubKey is this peer's Rosenpass public key
|
// RosenpassPubKey is this peer's Rosenpass public key
|
||||||
RosenpassPubKey []byte
|
RosenpassPubKey []byte
|
||||||
// RosenpassPubKey is this peer's RosenpassAddr server address (IP:port)
|
// RosenpassPubKey is this peer's RosenpassAddr server address (IP:port)
|
||||||
@ -135,32 +131,15 @@ type Conn struct {
|
|||||||
wgProxyFactory *wgproxy.Factory
|
wgProxyFactory *wgproxy.Factory
|
||||||
wgProxy wgproxy.Proxy
|
wgProxy wgproxy.Proxy
|
||||||
|
|
||||||
remoteModeCh chan ModeMessage
|
|
||||||
meta meta
|
|
||||||
|
|
||||||
adapter iface.TunAdapter
|
adapter iface.TunAdapter
|
||||||
iFaceDiscover stdnet.ExternalIFaceDiscover
|
iFaceDiscover stdnet.ExternalIFaceDiscover
|
||||||
sentExtraSrflx bool
|
sentExtraSrflx bool
|
||||||
|
|
||||||
remoteEndpoint *net.UDPAddr
|
|
||||||
remoteConn *ice.Conn
|
|
||||||
|
|
||||||
connID nbnet.ConnectionID
|
connID nbnet.ConnectionID
|
||||||
beforeAddPeerHooks []BeforeAddPeerHookFunc
|
beforeAddPeerHooks []BeforeAddPeerHookFunc
|
||||||
afterRemovePeerHooks []AfterRemovePeerHookFunc
|
afterRemovePeerHooks []AfterRemovePeerHookFunc
|
||||||
}
|
}
|
||||||
|
|
||||||
// meta holds meta information about a connection
|
|
||||||
type meta struct {
|
|
||||||
protoSupport signal.FeaturesSupport
|
|
||||||
}
|
|
||||||
|
|
||||||
// ModeMessage represents a connection mode chosen by the peer
|
|
||||||
type ModeMessage struct {
|
|
||||||
// Direct indicates that it decided to use a direct connection
|
|
||||||
Direct bool
|
|
||||||
}
|
|
||||||
|
|
||||||
// GetConf returns the connection config
|
// GetConf returns the connection config
|
||||||
func (conn *Conn) GetConf() ConnConfig {
|
func (conn *Conn) GetConf() ConnConfig {
|
||||||
return conn.config
|
return conn.config
|
||||||
@ -187,7 +166,6 @@ func NewConn(config ConnConfig, statusRecorder *Status, wgProxyFactory *wgproxy.
|
|||||||
remoteOffersCh: make(chan OfferAnswer),
|
remoteOffersCh: make(chan OfferAnswer),
|
||||||
remoteAnswerCh: make(chan OfferAnswer),
|
remoteAnswerCh: make(chan OfferAnswer),
|
||||||
statusRecorder: statusRecorder,
|
statusRecorder: statusRecorder,
|
||||||
remoteModeCh: make(chan ModeMessage, 1),
|
|
||||||
wgProxyFactory: wgProxyFactory,
|
wgProxyFactory: wgProxyFactory,
|
||||||
adapter: adapter,
|
adapter: adapter,
|
||||||
iFaceDiscover: iFaceDiscover,
|
iFaceDiscover: iFaceDiscover,
|
||||||
@ -378,8 +356,6 @@ func (conn *Conn) Open(ctx context.Context) error {
|
|||||||
remoteWgPort = remoteOfferAnswer.WgListenPort
|
remoteWgPort = remoteOfferAnswer.WgListenPort
|
||||||
}
|
}
|
||||||
|
|
||||||
conn.remoteConn = remoteConn
|
|
||||||
|
|
||||||
// the ice connection has been established successfully so we are ready to start the proxy
|
// the ice connection has been established successfully so we are ready to start the proxy
|
||||||
remoteAddr, err := conn.configureConnection(remoteConn, remoteWgPort, remoteOfferAnswer.RosenpassPubKey,
|
remoteAddr, err := conn.configureConnection(remoteConn, remoteWgPort, remoteOfferAnswer.RosenpassPubKey,
|
||||||
remoteOfferAnswer.RosenpassAddr)
|
remoteOfferAnswer.RosenpassAddr)
|
||||||
@ -437,7 +413,6 @@ func (conn *Conn) configureConnection(remoteConn net.Conn, remoteWgPort int, rem
|
|||||||
}
|
}
|
||||||
|
|
||||||
endpointUdpAddr, _ := net.ResolveUDPAddr(endpoint.Network(), endpoint.String())
|
endpointUdpAddr, _ := net.ResolveUDPAddr(endpoint.Network(), endpoint.String())
|
||||||
conn.remoteEndpoint = endpointUdpAddr
|
|
||||||
log.Debugf("Conn resolved IP for %s: %s", endpoint, endpointUdpAddr.IP)
|
log.Debugf("Conn resolved IP for %s: %s", endpoint, endpointUdpAddr.IP)
|
||||||
|
|
||||||
conn.connID = nbnet.GenerateConnID()
|
conn.connID = nbnet.GenerateConnID()
|
||||||
@ -623,40 +598,39 @@ func (conn *Conn) SetSendSignalMessage(handler func(message *sProto.Message) err
|
|||||||
// onICECandidate is a callback attached to an ICE Agent to receive new local connection candidates
|
// onICECandidate is a callback attached to an ICE Agent to receive new local connection candidates
|
||||||
// and then signals them to the remote peer
|
// and then signals them to the remote peer
|
||||||
func (conn *Conn) onICECandidate(candidate ice.Candidate) {
|
func (conn *Conn) onICECandidate(candidate ice.Candidate) {
|
||||||
if candidate != nil {
|
// nil means candidate gathering has been ended
|
||||||
// TODO: reported port is incorrect for CandidateTypeHost, makes understanding ICE use via logs confusing as port is ignored
|
if candidate == nil {
|
||||||
log.Debugf("discovered local candidate %s", candidate.String())
|
return
|
||||||
go func() {
|
|
||||||
err := conn.signalCandidate(candidate)
|
|
||||||
if err != nil {
|
|
||||||
log.Errorf("failed signaling candidate to the remote peer %s %s", conn.config.Key, err)
|
|
||||||
}
|
|
||||||
|
|
||||||
// sends an extra server reflexive candidate to the remote peer with our related port (usually the wireguard port)
|
|
||||||
// this is useful when network has an existing port forwarding rule for the wireguard port and this peer
|
|
||||||
if !conn.sentExtraSrflx && candidate.Type() == ice.CandidateTypeServerReflexive && candidate.Port() != candidate.RelatedAddress().Port {
|
|
||||||
relatedAdd := candidate.RelatedAddress()
|
|
||||||
extraSrflx, err := ice.NewCandidateServerReflexive(&ice.CandidateServerReflexiveConfig{
|
|
||||||
Network: candidate.NetworkType().String(),
|
|
||||||
Address: candidate.Address(),
|
|
||||||
Port: relatedAdd.Port,
|
|
||||||
Component: candidate.Component(),
|
|
||||||
RelAddr: relatedAdd.Address,
|
|
||||||
RelPort: relatedAdd.Port,
|
|
||||||
})
|
|
||||||
if err != nil {
|
|
||||||
log.Errorf("failed creating extra server reflexive candidate %s", err)
|
|
||||||
return
|
|
||||||
}
|
|
||||||
err = conn.signalCandidate(extraSrflx)
|
|
||||||
if err != nil {
|
|
||||||
log.Errorf("failed signaling the extra server reflexive candidate to the remote peer %s: %s", conn.config.Key, err)
|
|
||||||
return
|
|
||||||
}
|
|
||||||
conn.sentExtraSrflx = true
|
|
||||||
}
|
|
||||||
}()
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// TODO: reported port is incorrect for CandidateTypeHost, makes understanding ICE use via logs confusing as port is ignored
|
||||||
|
log.Debugf("discovered local candidate %s", candidate.String())
|
||||||
|
go func() {
|
||||||
|
err := conn.signalCandidate(candidate)
|
||||||
|
if err != nil {
|
||||||
|
log.Errorf("failed signaling candidate to the remote peer %s %s", conn.config.Key, err)
|
||||||
|
}
|
||||||
|
}()
|
||||||
|
|
||||||
|
if !conn.shouldSendExtraSrflxCandidate(candidate) {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
// sends an extra server reflexive candidate to the remote peer with our related port (usually the wireguard port)
|
||||||
|
// this is useful when network has an existing port forwarding rule for the wireguard port and this peer
|
||||||
|
extraSrflx, err := extraSrflxCandidate(candidate)
|
||||||
|
if err != nil {
|
||||||
|
log.Errorf("failed creating extra server reflexive candidate %s", err)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
conn.sentExtraSrflx = true
|
||||||
|
|
||||||
|
go func() {
|
||||||
|
err = conn.signalCandidate(extraSrflx)
|
||||||
|
if err != nil {
|
||||||
|
log.Errorf("failed signaling the extra server reflexive candidate to the remote peer %s: %s", conn.config.Key, err)
|
||||||
|
}
|
||||||
|
}()
|
||||||
}
|
}
|
||||||
|
|
||||||
func (conn *Conn) onICESelectedCandidatePair(c1 ice.Candidate, c2 ice.Candidate) {
|
func (conn *Conn) onICESelectedCandidatePair(c1 ice.Candidate, c2 ice.Candidate) {
|
||||||
@ -807,10 +781,23 @@ func (conn *Conn) GetKey() string {
|
|||||||
return conn.config.Key
|
return conn.config.Key
|
||||||
}
|
}
|
||||||
|
|
||||||
// RegisterProtoSupportMeta register supported proto message in the connection metadata
|
func (conn *Conn) shouldSendExtraSrflxCandidate(candidate ice.Candidate) bool {
|
||||||
func (conn *Conn) RegisterProtoSupportMeta(support []uint32) {
|
if !conn.sentExtraSrflx && candidate.Type() == ice.CandidateTypeServerReflexive && candidate.Port() != candidate.RelatedAddress().Port {
|
||||||
protoSupport := signal.ParseFeaturesSupported(support)
|
return true
|
||||||
conn.meta.protoSupport = protoSupport
|
}
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
|
||||||
|
func extraSrflxCandidate(candidate ice.Candidate) (*ice.CandidateServerReflexive, error) {
|
||||||
|
relatedAdd := candidate.RelatedAddress()
|
||||||
|
return ice.NewCandidateServerReflexive(&ice.CandidateServerReflexiveConfig{
|
||||||
|
Network: candidate.NetworkType().String(),
|
||||||
|
Address: candidate.Address(),
|
||||||
|
Port: relatedAdd.Port,
|
||||||
|
Component: candidate.Component(),
|
||||||
|
RelAddr: relatedAdd.Address,
|
||||||
|
RelPort: relatedAdd.Port,
|
||||||
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
func candidateViaRoutes(candidate ice.Candidate, clientRoutes route.HAMap) bool {
|
func candidateViaRoutes(candidate ice.Candidate, clientRoutes route.HAMap) bool {
|
||||||
|
@ -25,11 +25,6 @@ const (
|
|||||||
DirectCheck uint32 = 1
|
DirectCheck uint32 = 1
|
||||||
)
|
)
|
||||||
|
|
||||||
// FeaturesSupport register protocol supported features
|
|
||||||
type FeaturesSupport struct {
|
|
||||||
DirectCheck bool
|
|
||||||
}
|
|
||||||
|
|
||||||
type Client interface {
|
type Client interface {
|
||||||
io.Closer
|
io.Closer
|
||||||
StreamConnected() bool
|
StreamConnected() bool
|
||||||
@ -79,15 +74,3 @@ type Credential struct {
|
|||||||
UFrag string
|
UFrag string
|
||||||
Pwd string
|
Pwd string
|
||||||
}
|
}
|
||||||
|
|
||||||
// ParseFeaturesSupported parses a slice of supported features into FeaturesSupport
|
|
||||||
func ParseFeaturesSupported(featuresMessage []uint32) FeaturesSupport {
|
|
||||||
var protoSupport FeaturesSupport
|
|
||||||
for _, feature := range featuresMessage {
|
|
||||||
if feature == DirectCheck {
|
|
||||||
protoSupport.DirectCheck = true
|
|
||||||
return protoSupport
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return protoSupport
|
|
||||||
}
|
|
||||||
|
@ -4,7 +4,6 @@ import (
|
|||||||
"context"
|
"context"
|
||||||
"net"
|
"net"
|
||||||
"sync"
|
"sync"
|
||||||
"testing"
|
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
. "github.com/onsi/ginkgo"
|
. "github.com/onsi/ginkgo"
|
||||||
@ -168,41 +167,6 @@ var _ = Describe("GrpcClient", func() {
|
|||||||
|
|
||||||
})
|
})
|
||||||
|
|
||||||
func TestParseFeaturesSupported(t *testing.T) {
|
|
||||||
expectedOnEmptyOrUnsupported := FeaturesSupport{DirectCheck: false}
|
|
||||||
expectedWithDirectCheck := FeaturesSupport{DirectCheck: true}
|
|
||||||
testCases := []struct {
|
|
||||||
name string
|
|
||||||
input []uint32
|
|
||||||
expected FeaturesSupport
|
|
||||||
}{
|
|
||||||
{
|
|
||||||
name: "Should Return DirectCheck Supported",
|
|
||||||
input: []uint32{DirectCheck},
|
|
||||||
expected: expectedWithDirectCheck,
|
|
||||||
},
|
|
||||||
{
|
|
||||||
name: "Should Return DirectCheck Unsupported When Nil",
|
|
||||||
input: nil,
|
|
||||||
expected: expectedOnEmptyOrUnsupported,
|
|
||||||
},
|
|
||||||
{
|
|
||||||
name: "Should Return DirectCheck Unsupported When Not Known Feature",
|
|
||||||
input: []uint32{9999},
|
|
||||||
expected: expectedOnEmptyOrUnsupported,
|
|
||||||
},
|
|
||||||
}
|
|
||||||
|
|
||||||
for _, testCase := range testCases {
|
|
||||||
t.Run(testCase.name, func(t *testing.T) {
|
|
||||||
result := ParseFeaturesSupported(testCase.input)
|
|
||||||
if result.DirectCheck != testCase.expected.DirectCheck {
|
|
||||||
t.Errorf("Direct check feature should match: Expected: %t, Got: %t", testCase.expected.DirectCheck, result.DirectCheck)
|
|
||||||
}
|
|
||||||
})
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
func createSignalClient(addr string, key wgtypes.Key) *GrpcClient {
|
func createSignalClient(addr string, key wgtypes.Key) *GrpcClient {
|
||||||
var sigTLSEnabled = false
|
var sigTLSEnabled = false
|
||||||
client, err := NewClient(context.Background(), addr, key, sigTLSEnabled)
|
client, err := NewClient(context.Background(), addr, key, sigTLSEnabled)
|
||||||
|
Loading…
Reference in New Issue
Block a user