From 1cdae1370000e1e66d13c01919980359f57995f2 Mon Sep 17 00:00:00 2001 From: Matt Joiner Date: Tue, 29 Sep 2020 16:21:54 +1000 Subject: [PATCH] Fix conn status string for WebRTC connections --- client.go | 9 +++++---- peer-impl.go | 1 + peerconn.go | 19 ++++++++++++++----- torrent.go | 1 - webseed-peer.go | 4 ++++ 5 files changed, 24 insertions(+), 10 deletions(-) diff --git a/client.go b/client.go index dff94a1d..fafb6e2d 100644 --- a/client.go +++ b/client.go @@ -495,7 +495,8 @@ func (cl *Client) acceptConnections(l net.Listener) { } } -func regularConnString(nc net.Conn) string { +// Creates the PeerConn.connString for a regular net.Conn PeerConn. +func regularNetConnPeerConnConnString(nc net.Conn) string { return fmt.Sprintf("%s-%s", nc.LocalAddr(), nc.RemoteAddr()) } @@ -505,7 +506,7 @@ func (cl *Client) incomingConnection(nc net.Conn) { tc.SetLinger(0) } c := cl.newConnection(nc, false, nc.RemoteAddr(), nc.RemoteAddr().Network(), - regularConnString(nc)) + regularNetConnPeerConnConnString(nc)) c.Discovery = PeerSourceIncoming cl.runReceivedConn(c) } @@ -704,7 +705,7 @@ func (cl *Client) establishOutgoingConnEx(t *Torrent, addr net.Addr, obfuscatedH } return nil, errors.New("dial failed") } - c, err := cl.initiateProtocolHandshakes(context.Background(), nc, t, true, obfuscatedHeader, addr, dr.Network, regularConnString(nc)) + c, err := cl.initiateProtocolHandshakes(context.Background(), nc, t, true, obfuscatedHeader, addr, dr.Network, regularNetConnPeerConnConnString(nc)) if err != nil { nc.Close() } @@ -1338,8 +1339,8 @@ func (cl *Client) newConnection(nc net.Conn, outgoing bool, remoteAddr net.Addr, RemoteAddr: remoteAddr, network: network, - connString: connString, }, + connString: connString, conn: nc, writeBuffer: new(bytes.Buffer), } diff --git a/peer-impl.go b/peer-impl.go index 2b65d1ad..10a6d249 100644 --- a/peer-impl.go +++ b/peer-impl.go @@ -19,4 +19,5 @@ type peerImpl interface { onGotInfo(*metainfo.Info) drop() String() string + connStatusString() string } diff --git a/peerconn.go b/peerconn.go index adb60319..a54f6fb5 100644 --- a/peerconn.go +++ b/peerconn.go @@ -44,7 +44,6 @@ type peer struct { peerImpl - connString string outgoing bool network string RemoteAddr net.Addr @@ -86,11 +85,9 @@ type peer struct { pex pexConnState // Stuff controlled by the remote peer. - PeerID PeerID peerInterested bool peerChoking bool peerRequests map[request]struct{} - PeerExtensionBytes pp.PeerExtensionBits PeerPrefersEncryption bool // as indicated by 'e' field in extension handshake PeerListenPort int // The pieces the peer has claimed to have. @@ -116,10 +113,18 @@ type peer struct { logger log.Logger } -// Maintains the state of a connection with a peer. +// Maintains the state of a BitTorrent-protocol based connection with a peer. type PeerConn struct { peer + // A string that should identify the PeerConn's net.Conn endpoints. The net.Conn could + // be wrapping WebRTC, uTP, or TCP etc. Used in writing the conn status for peers. + connString string + + // See BEP 3 etc. + PeerID PeerID + PeerExtensionBytes pp.PeerExtensionBits + // The actual Conn, used for closing, and setting socket options. conn net.Conn // The Reader and Writer for this Conn, with hooks installed for stats, @@ -132,6 +137,10 @@ type PeerConn struct { writerCond sync.Cond } +func (cn *PeerConn) connStatusString() string { + return fmt.Sprintf("%+-55q %s %s\n", cn.PeerID, cn.PeerExtensionBytes, cn.connString) +} + func (cn *peer) updateExpectingChunks() { if cn.expectingChunks() { if cn.lastStartedExpectingToReceiveChunks.IsZero() { @@ -295,7 +304,7 @@ func (cn *peer) downloadRate() float64 { func (cn *peer) writeStatus(w io.Writer, t *Torrent) { // \t isn't preserved in
 blocks?
-	fmt.Fprintf(w, "%+-55q %s %s\n", cn.PeerID, cn.PeerExtensionBytes, cn.connString)
+	fmt.Fprintln(w, cn.connStatusString())
 	fmt.Fprintf(w, "    last msg: %s, connected: %s, last helpful: %s, itime: %s, etime: %s\n",
 		eventAgeString(cn.lastMessageReceived),
 		eventAgeString(cn.completedHandshake),
diff --git a/torrent.go b/torrent.go
index 493d7e7f..639e330e 100644
--- a/torrent.go
+++ b/torrent.go
@@ -2036,7 +2036,6 @@ func (t *Torrent) addWebSeed(url string) {
 	ws := webseedPeer{
 		peer: peer{
 			t:                        t,
-			connString:               url,
 			outgoing:                 true,
 			network:                  "http",
 			reconciledHandshakeStats: true,
diff --git a/webseed-peer.go b/webseed-peer.go
index e5ba748e..064a197c 100644
--- a/webseed-peer.go
+++ b/webseed-peer.go
@@ -20,6 +20,10 @@ type webseedPeer struct {
 
 var _ peerImpl = (*webseedPeer)(nil)
 
+func (me *webseedPeer) connStatusString() string {
+	return me.client.Url
+}
+
 func (ws *webseedPeer) String() string {
 	return fmt.Sprintf("webseed peer for %q", ws.client.Url)
 }