Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Prioritize TURN servers according to they relay protocol #629

Merged
merged 1 commit into from
Mar 20, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 17 additions & 0 deletions candidate_relay.go
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,23 @@
}, nil
}

// LocalPreference returns the local preference for this candidate
func (c *CandidateRelay) LocalPreference() uint16 {
// These preference values come from libwebrtc
// https://github.com/mozilla/libwebrtc/blob/1389c76d9c79839a2ca069df1db48aa3f2e6a1ac/p2p/base/turn_port.cc#L61
var relayPreference uint16
switch c.relayProtocol {
case relayProtocolTLS, relayProtocolDTLS:
relayPreference = 2
case tcp:
relayPreference = 1
default:
relayPreference = 0

Check warning on line 84 in candidate_relay.go

View check run for this annotation

Codecov / codecov/patch

candidate_relay.go#L74-L84

Added lines #L74 - L84 were not covered by tests
}

return c.candidateBase.LocalPreference() + relayPreference

Check warning on line 87 in candidate_relay.go

View check run for this annotation

Codecov / codecov/patch

candidate_relay.go#L87

Added line #L87 was not covered by tests
}

// RelayProtocol returns the protocol used between the endpoint and the relay server.
func (c *CandidateRelay) RelayProtocol() string {
return c.relayProtocol
Expand Down
4 changes: 2 additions & 2 deletions gather.go
Original file line number Diff line number Diff line change
Expand Up @@ -632,7 +632,7 @@ func (a *Agent) gatherCandidatesRelay(ctx context.Context, urls []*stun.URI) { /

relAddr = conn.LocalAddr().(*net.UDPAddr).IP.String() //nolint:forcetypeassert
relPort = conn.LocalAddr().(*net.UDPAddr).Port //nolint:forcetypeassert
relayProtocol = "dtls"
relayProtocol = relayProtocolDTLS
locConn = &fakenet.PacketConn{Conn: conn}
case url.Proto == stun.ProtoTypeTCP && url.Scheme == stun.SchemeTypeTURNS:
tcpAddr, resolvErr := a.net.ResolveTCPAddr(NetworkTypeTCP4.String(), turnServerAddr)
Expand Down Expand Up @@ -662,7 +662,7 @@ func (a *Agent) gatherCandidatesRelay(ctx context.Context, urls []*stun.URI) { /

relAddr = conn.LocalAddr().(*net.TCPAddr).IP.String() //nolint:forcetypeassert
relPort = conn.LocalAddr().(*net.TCPAddr).Port //nolint:forcetypeassert
relayProtocol = "tls"
relayProtocol = relayProtocolTLS
locConn = turn.NewSTUNConn(conn)
default:
a.log.Warnf("Unable to handle URL in gatherCandidatesRelay %v", url)
Expand Down
5 changes: 5 additions & 0 deletions ice.go
Original file line number Diff line number Diff line change
Expand Up @@ -83,3 +83,8 @@ func (t GatheringState) String() string {
return ErrUnknownType.Error()
}
}

const (
relayProtocolDTLS = "dtls"
relayProtocolTLS = "tls"
)
Loading