From 77cc354d7ff64da809e1b2b36b27ff443f7415ef Mon Sep 17 00:00:00 2001 From: dinvlad <137337+dinvlad@users.noreply.github.com> Date: Mon, 2 Oct 2023 13:13:40 -0400 Subject: [PATCH] Respect IncludeLoopback in UDPMuxDefault Currently, when using UDPMuxDefault with unspecified address, the loopback address is included by default, but agentConfig.IncludeLoopback is not respected when gathering local candidates. The same holds true when UDPMuxDefault is configured with a loopback address, but agentConfig.IncludeLoopback is not explicitly set to true. This commit adds an extra check to gatherCandidatesLocalUDPMux() for respecting that setting in both cases. --- agent_udpmux_test.go | 1 + gather.go | 7 +++++++ gather_test.go | 28 ++++++++++++++++++++++++++++ 3 files changed, 36 insertions(+) diff --git a/agent_udpmux_test.go b/agent_udpmux_test.go index 6050ccd..524795d 100644 --- a/agent_udpmux_test.go +++ b/agent_udpmux_test.go @@ -49,6 +49,7 @@ func TestMuxAgent(t *testing.T) { NetworkTypes: []NetworkType{ NetworkTypeUDP4, }, + IncludeLoopback: addr.IP.IsLoopback(), }) require.NoError(t, err) diff --git a/gather.go b/gather.go index c2bf40b..931d368 100644 --- a/gather.go +++ b/gather.go @@ -266,6 +266,13 @@ func (a *Agent) gatherCandidatesLocalUDPMux(ctx context.Context) error { //nolin return errInvalidAddress } candidateIP := udpAddr.IP + + if _, ok := a.udpMux.(*UDPMuxDefault); ok && !a.includeLoopback && candidateIP.IsLoopback() { + // Unlike MultiUDPMux Default, UDPMuxDefault doesn't have + // a separate param to include loopback, so we respect agent config + continue + } + if a.mDNSMode != MulticastDNSModeQueryAndGather && a.extIPMapper != nil && a.extIPMapper.candidateType == CandidateTypeHost { diff --git a/gather_test.go b/gather_test.go index 668891a..8f31cd6 100644 --- a/gather_test.go +++ b/gather_test.go @@ -133,6 +133,16 @@ func TestLoopbackCandidate(t *testing.T) { assert.NoError(t, err) muxWithLo, errlo := NewMultiUDPMuxFromPort(12501, UDPMuxFromPortWithLoopback()) assert.NoError(t, errlo) + + unspecConn, errconn := net.ListenPacket("udp", ":0") + assert.NoError(t, errconn) + defer func() { + _ = unspecConn.Close() + }() + muxUnspecDefault := NewUDPMuxDefault(UDPMuxParams{ + UDPConn: unspecConn, + }) + testCases := []testCase{ { name: "mux should not have loopback candidate", @@ -150,6 +160,23 @@ func TestLoopbackCandidate(t *testing.T) { }, loExpected: true, }, + { + name: "UDPMuxDefault with unspecified IP should not have loopback candidate", + agentConfig: &AgentConfig{ + NetworkTypes: []NetworkType{NetworkTypeUDP4, NetworkTypeUDP6}, + UDPMux: muxUnspecDefault, + }, + loExpected: false, + }, + { + name: "UDPMuxDefault with unspecified IP should respect agent includeloopback", + agentConfig: &AgentConfig{ + NetworkTypes: []NetworkType{NetworkTypeUDP4, NetworkTypeUDP6}, + UDPMux: muxUnspecDefault, + IncludeLoopback: true, + }, + loExpected: true, + }, { name: "includeloopback enabled", agentConfig: &AgentConfig{ @@ -198,6 +225,7 @@ func TestLoopbackCandidate(t *testing.T) { assert.NoError(t, mux.Close()) assert.NoError(t, muxWithLo.Close()) + assert.NoError(t, muxUnspecDefault.Close()) } // Assert that STUN gathering is done concurrently