From 735f412b1874c09d47fd5b834bc0f674cbf31a06 Mon Sep 17 00:00:00 2001 From: "alan.mcgovern" Date: Wed, 1 Feb 2023 21:25:13 +0000 Subject: [PATCH] [core] Add improved logging for unexpected peer_ids If a peer sends a HandshakeMessage with a 'peer_id' that *does not* match the 'peer_id' it sent to the tracker - log both the expected and actual peer_ids. Helps diagnose https://github.com/alanmcgovern/monotorrent/pull/614 It's probably a normal bittorrent client which is just randomising the peer id on every connection attempt. --- src/MonoTorrent.Client/MonoTorrent.Client.Modes/Mode.cs | 2 +- src/MonoTorrent.Client/MonoTorrent.Logging/Logger.cs | 6 ++++++ 2 files changed, 7 insertions(+), 1 deletion(-) diff --git a/src/MonoTorrent.Client/MonoTorrent.Client.Modes/Mode.cs b/src/MonoTorrent.Client/MonoTorrent.Client.Modes/Mode.cs index f5a4a5f10..47ee9be69 100644 --- a/src/MonoTorrent.Client/MonoTorrent.Client.Modes/Mode.cs +++ b/src/MonoTorrent.Client/MonoTorrent.Client.Modes/Mode.cs @@ -226,7 +226,7 @@ protected virtual void HandleHandshakeMessage (PeerId id, HandshakeMessage messa // match we should close the connection. I *think* uTorrent doesn't randomise peerids // for private torrents. It's not documented very well. We may need to relax this check // if other clients randomize for private torrents. - logger.Info (id.Connection, "HandShake.Handle - Invalid peerid"); + logger.InfoFormatted (id.Connection, "HandShake.Handle - Invalid peerid. Expected '{0}' but received '{1}'", id.Peer.Info.PeerId, message.PeerId); throw new TorrentException ("Supplied PeerID didn't match the one the tracker gave us"); } else { // We don't care about the mismatch for public torrents. uTorrent randomizes its PeerId, as do other clients. diff --git a/src/MonoTorrent.Client/MonoTorrent.Logging/Logger.cs b/src/MonoTorrent.Client/MonoTorrent.Logging/Logger.cs index e971802cd..26bd18e5e 100644 --- a/src/MonoTorrent.Client/MonoTorrent.Logging/Logger.cs +++ b/src/MonoTorrent.Client/MonoTorrent.Logging/Logger.cs @@ -106,6 +106,12 @@ internal void InfoFormatted (IPeerConnection connection, string formatString, ob Writer.Info ($"{connection.Uri}: {string.Format (formatString, p1)}"); } + internal void InfoFormatted (IPeerConnection connection, string formatString, object p1, object p2) + { + if (Writer != null) + Writer.Info ($"{connection.Uri}: {string.Format (formatString, p1, p2)}"); + } + internal void Error (string message) { if (Writer != null)