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