From 39171df0c65e257adeb58939ca26e889e6fd2982 Mon Sep 17 00:00:00 2001 From: Matt Joiner Date: Sun, 3 May 2020 18:37:26 +1000 Subject: [PATCH] Fix PeerConn outgoing field for webrtc Due to jumping straight into handshakes, outgoing=true was assumed. This didn't actually solve the issue I thought it might, but is important for determining "preferred" connection direction, which may result in dropping connections. --- client.go | 11 +++++------ torrent.go | 3 ++- 2 files changed, 7 insertions(+), 7 deletions(-) diff --git a/client.go b/client.go index 735d58ca..41645367 100644 --- a/client.go +++ b/client.go @@ -658,18 +658,17 @@ func (cl *Client) noLongerHalfOpen(t *Torrent, addr string) { // Performs initiator handshakes and returns a connection. Returns nil *connection if no connection // for valid reasons. -func (cl *Client) handshakesConnection( +func (cl *Client) initiateProtocolHandshakes( ctx context.Context, nc net.Conn, t *Torrent, - encryptHeader bool, + outgoing, encryptHeader bool, remoteAddr net.Addr, - network, - connString string, + network, connString string, ) ( c *PeerConn, err error, ) { - c = cl.newConnection(nc, true, remoteAddr, network, connString) + c = cl.newConnection(nc, outgoing, remoteAddr, network, connString) c.headerEncrypted = encryptHeader ctx, cancel := context.WithTimeout(ctx, cl.config.HandshakesTimeout) defer cancel() @@ -701,7 +700,7 @@ func (cl *Client) establishOutgoingConnEx(t *Torrent, addr net.Addr, obfuscatedH } return nil, errors.New("dial failed") } - c, err := cl.handshakesConnection(context.Background(), nc, t, obfuscatedHeader, addr, dr.Network, regularConnString(nc)) + c, err := cl.initiateProtocolHandshakes(context.Background(), nc, t, true, obfuscatedHeader, addr, dr.Network, regularConnString(nc)) if err != nil { nc.Close() } diff --git a/torrent.go b/torrent.go index b4d17648..0a8cfe4b 100644 --- a/torrent.go +++ b/torrent.go @@ -1287,10 +1287,11 @@ func (t *Torrent) onWebRtcConn( dcc webtorrent.DataChannelContext, ) { defer c.Close() - pc, err := t.cl.handshakesConnection( + pc, err := t.cl.initiateProtocolHandshakes( context.Background(), webrtcNetConn{c, dcc}, t, + dcc.LocalOffered, false, webrtcNetAddr{dcc.Remote}, webrtcNetwork,