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

Harmonize log messages #588

Closed
wants to merge 3 commits into from
Closed
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
10 changes: 5 additions & 5 deletions agent.go
Original file line number Diff line number Diff line change
Expand Up @@ -535,7 +535,7 @@ func (a *Agent) setSelectedPair(p *CandidatePair) {
}

func (a *Agent) pingAllCandidates() {
a.log.Trace("pinging all candidates")
a.log.Trace("Pinging all candidates")

if len(a.checklist) == 0 {
a.log.Warn("Failed to ping without candidate pairs. Connection is not possible yet.")
Expand All @@ -549,7 +549,7 @@ func (a *Agent) pingAllCandidates() {
}

if p.bindingRequestCount > a.maxBindingRequests {
a.log.Tracef("max requests reached for pair %s, marking it as failed", p)
a.log.Tracef("Maximum requests reached for pair %s, marking it as failed", p)
p.state = CandidatePairStateFailed
} else {
a.selector.PingCandidate(p.Local, p.Remote)
Expand Down Expand Up @@ -959,7 +959,7 @@ func (a *Agent) findRemoteCandidate(networkType NetworkType, addr net.Addr) Cand
}

func (a *Agent) sendBindingRequest(m *stun.Message, local, remote Candidate) {
a.log.Tracef("ping STUN from %s to %s", local.String(), remote.String())
a.log.Tracef("Ping STUN from %s to %s", local.String(), remote.String())

a.invalidatePendingBindingRequests(time.Now())
a.pendingBindingRequests = append(a.pendingBindingRequests, bindingRequest{
Expand Down Expand Up @@ -1041,7 +1041,7 @@ func (a *Agent) handleInbound(m *stun.Message, local Candidate, remote net.Addr)
!(m.Type.Class == stun.ClassSuccessResponse ||
m.Type.Class == stun.ClassRequest ||
m.Type.Class == stun.ClassIndication) {
a.log.Tracef("unhandled STUN from %s to %s class(%s) method(%s)", remote, local, m.Type.Class, m.Type.Method)
a.log.Tracef("Unhandled STUN from %s to %s class(%s) method(%s)", remote, local, m.Type.Class, m.Type.Method)
return
}

Expand Down Expand Up @@ -1109,7 +1109,7 @@ func (a *Agent) handleInbound(m *stun.Message, local Candidate, remote net.Addr)
a.addRemoteCandidate(remoteCandidate)
}

a.log.Tracef("inbound STUN (Request) from %s to %s", remote.String(), local.String())
a.log.Tracef("Inbound STUN (Request) from %s to %s", remote.String(), local.String())

a.selector.HandleBindingRequest(m, local, remoteCandidate)
}
Expand Down
2 changes: 1 addition & 1 deletion candidatepair.go
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,6 @@ func (p *CandidatePair) Write(b []byte) (int, error) {
func (a *Agent) sendSTUN(msg *stun.Message, local, remote Candidate) {
_, err := local.writeTo(msg.Raw, remote)
if err != nil {
a.log.Tracef("failed to send STUN message: %s", err)
a.log.Tracef("Failed to send STUN message: %s", err)
}
}
150 changes: 91 additions & 59 deletions gather.go
Original file line number Diff line number Diff line change
Expand Up @@ -539,89 +539,114 @@ func (a *Agent) gatherCandidatesRelay(ctx context.Context, urls []*stun.URI) { /
wg.Add(1)
go func(url stun.URI) {
defer wg.Done()
turnServerAddr := fmt.Sprintf("%s:%d", url.Host, url.Port)

var (
locConn net.PacketConn
err error
relAddr string
relPort int
relayProtocol string
err error
addr net.Addr
conn net.PacketConn
proto string
relAddr string
relPort int
svrAddr = fmt.Sprintf("%s:%d", url.Host, url.Port)
)

switch {
case url.Proto == stun.ProtoTypeUDP && url.Scheme == stun.SchemeTypeTURN:
if locConn, err = a.net.ListenPacket(network, "0.0.0.0:0"); err != nil {
udpAddr, resolvErr := a.net.ResolveUDPAddr("udp", svrAddr)
if resolvErr != nil {
a.log.Warnf("Failed to resolve UDP address %s: %v", svrAddr, resolvErr)
return
}

if conn, err = a.net.ListenPacket(network, "0.0.0.0:0"); err != nil {
a.log.Warnf("Failed to listen %s: %v", network, err)
return
}

relAddr = locConn.LocalAddr().(*net.UDPAddr).IP.String() //nolint:forcetypeassert
relPort = locConn.LocalAddr().(*net.UDPAddr).Port //nolint:forcetypeassert
relayProtocol = udp
lAddr := conn.LocalAddr().(*net.UDPAddr) //nolint:forcetypeassert
relAddr = lAddr.IP.String()
relPort = lAddr.Port
proto = udp
addr = udpAddr
case a.proxyDialer != nil && url.Proto == stun.ProtoTypeTCP &&
(url.Scheme == stun.SchemeTypeTURN || url.Scheme == stun.SchemeTypeTURNS):
conn, connectErr := a.proxyDialer.Dial(NetworkTypeTCP4.String(), turnServerAddr)

udpAddr, resolveErr := a.net.ResolveUDPAddr("udp", svrAddr)
if resolveErr != nil {
a.log.Warnf("Failed to resolve UDP address %s: %v", svrAddr, resolveErr)
return
}

proxyConn, connectErr := a.proxyDialer.Dial(NetworkTypeTCP4.String(), svrAddr)
if connectErr != nil {
a.log.Warnf("Failed to dial TCP address %s via proxy dialer: %v", turnServerAddr, connectErr)
a.log.Warnf("Failed to dial TCP address %s via proxy dialer: %v", svrAddr, connectErr)
return
}

relAddr = conn.LocalAddr().(*net.TCPAddr).IP.String() //nolint:forcetypeassert
relPort = conn.LocalAddr().(*net.TCPAddr).Port //nolint:forcetypeassert
lAddr := proxyConn.LocalAddr().(*net.TCPAddr) //nolint:forcetypeassert
relAddr = lAddr.IP.String()
relPort = lAddr.Port

if url.Scheme == stun.SchemeTypeTURN {
relayProtocol = tcp
proto = tcp
} else if url.Scheme == stun.SchemeTypeTURNS {
relayProtocol = "tls"
proto = "tls"
}
locConn = turn.NewSTUNConn(conn)

conn = turn.NewSTUNConn(proxyConn)
addr = udpAddr
case url.Proto == stun.ProtoTypeTCP && url.Scheme == stun.SchemeTypeTURN:
tcpAddr, connectErr := a.net.ResolveTCPAddr(NetworkTypeTCP4.String(), turnServerAddr)
if connectErr != nil {
a.log.Warnf("Failed to resolve TCP address %s: %v", turnServerAddr, connectErr)
tcpAddr, resolveErr := a.net.ResolveTCPAddr(NetworkTypeTCP4.String(), svrAddr)
if resolveErr != nil {
a.log.Warnf("Failed to resolve TCP address %s: %v", svrAddr, resolveErr)
return
}

conn, connectErr := a.net.DialTCP(NetworkTypeTCP4.String(), nil, tcpAddr)
tcpConn, connectErr := a.net.DialTCP(NetworkTypeTCP4.String(), nil, tcpAddr)
if connectErr != nil {
a.log.Warnf("Failed to dial TCP address %s: %v", turnServerAddr, connectErr)
a.log.Warnf("Failed to dial TCP address %s: %v", svrAddr, connectErr)
return
}

relAddr = conn.LocalAddr().(*net.TCPAddr).IP.String() //nolint:forcetypeassert
relPort = conn.LocalAddr().(*net.TCPAddr).Port //nolint:forcetypeassert
relayProtocol = tcp
locConn = turn.NewSTUNConn(conn)
lAddr := tcpConn.LocalAddr().(*net.TCPAddr) //nolint:forcetypeassert
relAddr = lAddr.IP.String()
relPort = lAddr.Port

addr = tcpAddr
proto = tcp
conn = turn.NewSTUNConn(tcpConn)
case url.Proto == stun.ProtoTypeUDP && url.Scheme == stun.SchemeTypeTURNS:
udpAddr, connectErr := a.net.ResolveUDPAddr(network, turnServerAddr)
if connectErr != nil {
a.log.Warnf("Failed to resolve UDP address %s: %v", turnServerAddr, connectErr)
udpAddr, resolveErr := a.net.ResolveUDPAddr(network, svrAddr)
if resolveErr != nil {
a.log.Warnf("Failed to resolve UDP address %s: %v", svrAddr, resolveErr)
return
}

udpConn, dialErr := a.net.DialUDP("udp", nil, udpAddr)
if dialErr != nil {
a.log.Warnf("Failed to dial DTLS address %s: %v", turnServerAddr, dialErr)
a.log.Warnf("Failed to dial DTLS address %s: %v", svrAddr, dialErr)
return
}

conn, connectErr := dtls.ClientWithContext(ctx, udpConn, &dtls.Config{
dtlsConn, connectErr := dtls.ClientWithContext(ctx, udpConn, &dtls.Config{
ServerName: url.Host,
InsecureSkipVerify: a.insecureSkipVerify, //nolint:gosec
})
if connectErr != nil {
a.log.Warnf("Failed to create DTLS client: %v", turnServerAddr, connectErr)
a.log.Warnf("Failed to create DTLS client: %v", svrAddr, connectErr)
return
}

relAddr = conn.LocalAddr().(*net.UDPAddr).IP.String() //nolint:forcetypeassert
relPort = conn.LocalAddr().(*net.UDPAddr).Port //nolint:forcetypeassert
relayProtocol = "dtls"
locConn = &fakenet.PacketConn{Conn: conn}
lAddr := dtlsConn.LocalAddr().(*net.UDPAddr) //nolint:forcetypeassert
relAddr = lAddr.IP.String()
relPort = lAddr.Port

addr = udpAddr
proto = "dtls"
conn = &fakenet.PacketConn{Conn: dtlsConn}
case url.Proto == stun.ProtoTypeTCP && url.Scheme == stun.SchemeTypeTURNS:
tcpAddr, resolvErr := a.net.ResolveTCPAddr(NetworkTypeTCP4.String(), turnServerAddr)
tcpAddr, resolvErr := a.net.ResolveTCPAddr(NetworkTypeTCP4.String(), svrAddr)
if resolvErr != nil {
a.log.Warnf("Failed to resolve relay address %s: %v", turnServerAddr, resolvErr)
a.log.Warnf("Failed to resolve relay address %s: %v", svrAddr, resolvErr)
return
}

Expand All @@ -631,79 +656,86 @@ func (a *Agent) gatherCandidatesRelay(ctx context.Context, urls []*stun.URI) { /
return
}

conn := tls.Client(tcpConn, &tls.Config{
tlsConn := tls.Client(tcpConn, &tls.Config{
ServerName: url.Host,
InsecureSkipVerify: a.insecureSkipVerify, //nolint:gosec
})

if hsErr := conn.HandshakeContext(ctx); hsErr != nil {
if hsErr := tlsConn.HandshakeContext(ctx); hsErr != nil {
if closeErr := tcpConn.Close(); closeErr != nil {
a.log.Errorf("Failed to close relay connection: %v", closeErr)
}
a.log.Warnf("Failed to connect to relay: %v", hsErr)
return
}

relAddr = conn.LocalAddr().(*net.TCPAddr).IP.String() //nolint:forcetypeassert
relPort = conn.LocalAddr().(*net.TCPAddr).Port //nolint:forcetypeassert
relayProtocol = "tls"
locConn = turn.NewSTUNConn(conn)
lAddr := tlsConn.LocalAddr().(*net.TCPAddr) //nolint:forcetypeassert
relAddr = lAddr.IP.String()
relPort = lAddr.Port

addr = tcpAddr
proto = "tls"
conn = turn.NewSTUNConn(tlsConn)
default:
a.log.Warnf("Unable to handle URL in gatherCandidatesRelay %v", url)
return
}

client, err := turn.NewClient(&turn.ClientConfig{
TURNServerAddr: turnServerAddr,
Conn: locConn,
clientCfg := &turn.ClientConfig{
TURNServerAddr: addr,
Conn: conn,
Username: url.Username,
Password: url.Password,
LoggerFactory: a.loggerFactory,
Net: a.net,
})
}

client, err := turn.NewClient(clientCfg)
if err != nil {
closeConnAndLog(locConn, a.log, "failed to create new TURN client %s %s", turnServerAddr, err)
closeConnAndLog(conn, a.log, "failed to create new TURN client %s %s", svrAddr, err)
return
}

if err = client.Listen(); err != nil {
client.Close()
closeConnAndLog(locConn, a.log, "failed to listen on TURN client %s %s", turnServerAddr, err)
closeConnAndLog(conn, a.log, "failed to listen on TURN client %s %s", svrAddr, err)
return
}

relayConn, err := client.Allocate()
if err != nil {
client.Close()
closeConnAndLog(locConn, a.log, "failed to allocate on TURN client %s %s", turnServerAddr, err)
closeConnAndLog(conn, a.log, "failed to allocate on TURN client %s %s", svrAddr, err)
return
}

rAddr := relayConn.LocalAddr().(*net.UDPAddr) //nolint:forcetypeassert
relayAddr := relayConn.LocalAddr().(*net.UDPAddr) //nolint:forcetypeassert
relayConfig := CandidateRelayConfig{
Network: network,
Component: ComponentRTP,
Address: rAddr.IP.String(),
Port: rAddr.Port,
Address: relayAddr.IP.String(),
Port: relayAddr.Port,
RelAddr: relAddr,
RelPort: relPort,
RelayProtocol: relayProtocol,
RelayProtocol: proto,
OnClose: func() error {
client.Close()
return locConn.Close()
return conn.Close()
},
}

relayConnClose := func() {
if relayConErr := relayConn.Close(); relayConErr != nil {
a.log.Warnf("Failed to close relay %v", relayConErr)
}
}

candidate, err := NewCandidateRelay(&relayConfig)
if err != nil {
relayConnClose()

client.Close()
closeConnAndLog(locConn, a.log, "failed to create relay candidate: %s %s: %v", network, rAddr.String(), err)
closeConnAndLog(conn, a.log, "failed to create relay candidate: %s %s: %v", network, relayAddr.String(), err)
return
}

Expand All @@ -713,7 +745,7 @@ func (a *Agent) gatherCandidatesRelay(ctx context.Context, urls []*stun.URI) { /
if closeErr := candidate.close(); closeErr != nil {
a.log.Warnf("Failed to close candidate: %v", closeErr)
}
a.log.Warnf("Failed to append to localCandidates and run onCandidateHdlr: %v", err)
a.log.Warnf("Failed to add local candidates: %v", err)
}
}(*urls[i])
}
Expand Down
4 changes: 2 additions & 2 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,9 @@ require (
github.com/pion/logging v0.2.2
github.com/pion/mdns v0.0.7
github.com/pion/randutil v0.1.0
github.com/pion/stun v0.6.0
github.com/pion/stun v0.6.1
github.com/pion/transport/v2 v2.2.1
github.com/pion/turn/v2 v2.1.0
github.com/pion/turn/v2 v2.1.1
github.com/stretchr/testify v1.8.4
golang.org/x/crypto v0.9.0 // indirect
golang.org/x/net v0.10.0
Expand Down
9 changes: 4 additions & 5 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -16,14 +16,13 @@ github.com/pion/mdns v0.0.7 h1:P0UB4Sr6xDWEox0kTVxF0LmQihtCbSAdW0H2nEgkA3U=
github.com/pion/mdns v0.0.7/go.mod h1:4iP2UbeFhLI/vWju/bw6ZfwjJzk0z8DNValjGxR/dD8=
github.com/pion/randutil v0.1.0 h1:CFG1UdESneORglEsnimhUjf33Rwjubwj6xfiOXBa3mA=
github.com/pion/randutil v0.1.0/go.mod h1:XcJrSMMbbMRhASFVOlj/5hQial/Y8oH/HVo7TBZq+j8=
github.com/pion/stun v0.4.0/go.mod h1:QPsh1/SbXASntw3zkkrIk3ZJVKz4saBY2G7S10P3wCw=
github.com/pion/stun v0.6.0 h1:JHT/2iyGDPrFWE8NNC15wnddBN8KifsEDw8swQmrEmU=
github.com/pion/stun v0.6.0/go.mod h1:HPqcfoeqQn9cuaet7AOmB5e5xkObu9DwBdurwLKO9oA=
github.com/pion/stun v0.6.1 h1:8lp6YejULeHBF8NmV8e2787BogQhduZugh5PdhDyyN4=
github.com/pion/stun v0.6.1/go.mod h1:/hO7APkX4hZKu/D0f2lHzNyvdkTGtIy3NDmLR7kSz/8=
github.com/pion/transport/v2 v2.0.0/go.mod h1:HS2MEBJTwD+1ZI2eSXSvHJx/HnzQqRy2/LXxt6eVMHc=
github.com/pion/transport/v2 v2.2.1 h1:7qYnCBlpgSJNYMbLCKuSY9KbQdBFoETvPNETv0y4N7c=
github.com/pion/transport/v2 v2.2.1/go.mod h1:cXXWavvCnFF6McHTft3DWS9iic2Mftcz1Aq29pGcU5g=
github.com/pion/turn/v2 v2.1.0 h1:5wGHSgGhJhP/RpabkUb/T9PdsAjkGLS6toYz5HNzoSI=
github.com/pion/turn/v2 v2.1.0/go.mod h1:yrT5XbXSGX1VFSF31A3c1kCNB5bBZgk/uu5LET162qs=
github.com/pion/turn/v2 v2.1.1 h1:r66JUkWDCBHvtDdItx2xfdHbiK7ON/UdlTAPmgDU8OQ=
github.com/pion/turn/v2 v2.1.1/go.mod h1:1kjnPkBcex3dhCU2Am+AAmxDcGhLX3WnMfmkNpvSTQU=
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
Expand Down
Loading