client.go | 9 +++++---- peer-impl.go | 1 + peerconn.go | 19 ++++++++++++++----- torrent.go | 1 - webseed-peer.go | 4 ++++ diff --git a/client.go b/client.go index dff94a1d27a0689a7580292d93be1758688ba5c4..fafb6e2d72d7f94d96cc2d4f5eb67f29b6595622 100644 --- a/client.go +++ b/client.go @@ -495,7 +495,8 @@ }() } } -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 @@ if tc, ok := nc.(*net.TCPConn); ok { 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 @@ return nil, xerrors.Errorf("dialing: %w", dialCtx.Err()) } 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 @@ PeerMaxRequests: 250, 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 2b65d1ad94c757334d2f7e4d25898d19e4e9c3f2..10a6d249f74bb9dbd7faf455f2cd15ee262135f5 100644 --- a/peer-impl.go +++ b/peer-impl.go @@ -19,4 +19,5 @@ _postCancel(request) onGotInfo(*metainfo.Info) drop() String() string + connStatusString() string } diff --git a/peerconn.go b/peerconn.go index adb60319a6c6110f6dadfac3d686cabcdcc977b9..a54f6fb5d551c58d4cf9a5a701e7e620bb8291df 100644 --- a/peerconn.go +++ b/peerconn.go @@ -44,7 +44,6 @@ t *Torrent peerImpl - connString string outgoing bool network string RemoteAddr net.Addr @@ -86,11 +85,9 @@ sentHaves bitmap.Bitmap 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 @@ 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, @@ -130,6 +135,10 @@ writeBuffer *bytes.Buffer uploadTimer *time.Timer 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() { @@ -295,7 +304,7 @@ } 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 493d7e7fa3567ede209834ea6716f83bc3ac503e..639e330e21ad61f2cbeb2ea778c7b4554e9e3846 100644
--- a/torrent.go
+++ b/torrent.go
@@ -2036,7 +2036,6 @@ const maxRequests = 10
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 e5ba748e5df9fc4f71873368fed9a23b6b0f6b43..064a197cf8e7c79b50c1ad3ecfbdf1dea01521c8 100644
--- a/webseed-peer.go
+++ b/webseed-peer.go
@@ -20,6 +20,10 @@ }
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)
}