From 7bcd469e8522a025ad310744000e955d72b184a4 Mon Sep 17 00:00:00 2001 From: Alan McGovern Date: Wed, 21 Aug 2019 15:58:43 +0100 Subject: [PATCH] [tracker] Allow comparing peers using PeerId We still default to using IPAddress/Port as some bittorrent clients randomise their peerid, making it a little use suitable to use as a unique identifier. --- ...ssComparer.cs => ClientAddressComparer.cs} | 17 ++++--- .../MonoTorrent.Tracker/IPeerComparer.cs | 46 ++++++++++++++++++ .../MonoTorrent.Tracker/PeerIdComparer.cs | 47 +++++++++++++++++++ .../MonoTorrent.Tracker/TrackerServer.cs | 2 +- 4 files changed, 104 insertions(+), 8 deletions(-) rename src/MonoTorrent/MonoTorrent.Tracker/{IPAddressComparer.cs => ClientAddressComparer.cs} (72%) create mode 100644 src/MonoTorrent/MonoTorrent.Tracker/IPeerComparer.cs create mode 100644 src/MonoTorrent/MonoTorrent.Tracker/PeerIdComparer.cs diff --git a/src/MonoTorrent/MonoTorrent.Tracker/IPAddressComparer.cs b/src/MonoTorrent/MonoTorrent.Tracker/ClientAddressComparer.cs similarity index 72% rename from src/MonoTorrent/MonoTorrent.Tracker/IPAddressComparer.cs rename to src/MonoTorrent/MonoTorrent.Tracker/ClientAddressComparer.cs index 5b630d1d4..6b1913683 100644 --- a/src/MonoTorrent/MonoTorrent.Tracker/IPAddressComparer.cs +++ b/src/MonoTorrent/MonoTorrent.Tracker/ClientAddressComparer.cs @@ -1,5 +1,5 @@ // -// IPAddressComparer.cs +// ClientAddressComparer.cs // // Authors: // Alan McGovern @@ -29,13 +29,16 @@ namespace MonoTorrent.Tracker { - public interface IPeerComparer - { - object GetKey(AnnounceRequest parameters); - } - - class IPAddressComparer : IPeerComparer + /// + /// Uses the field to compare peers when handling Announce or Scrape requests. + /// + public class ClientAddressComparer : IPeerComparer { + /// + /// Returns the field to use to compare peers. + /// + /// The data sent as part of the Announce request + /// public object GetKey(AnnounceRequest parameters) { return parameters.ClientAddress; diff --git a/src/MonoTorrent/MonoTorrent.Tracker/IPeerComparer.cs b/src/MonoTorrent/MonoTorrent.Tracker/IPeerComparer.cs new file mode 100644 index 000000000..59c708f8c --- /dev/null +++ b/src/MonoTorrent/MonoTorrent.Tracker/IPeerComparer.cs @@ -0,0 +1,46 @@ +// +// IPeerComparer.cs +// +// Authors: +// Alan McGovern +// +// Copyright (C) 2006 Alan McGovern +// +// Permission is hereby granted, free of charge, to any person obtaining +// a copy of this software and associated documentation files (the +// "Software"), to deal in the Software without restriction, including +// without limitation the rights to use, copy, modify, merge, publish, +// distribute, sublicense, and/or sell copies of the Software, and to +// permit persons to whom the Software is furnished to do so, subject to +// the following conditions: +// +// The above copyright notice and this permission notice shall be +// included in all copies or substantial portions of the Software. +// +// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, +// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF +// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND +// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE +// LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION +// OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION +// WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. +// + + +namespace MonoTorrent.Tracker +{ + /// + /// Returns one of the properties from a peers , or a value derived from + /// properties on the , to use when comparing peers. + /// + public interface IPeerComparer + { + /// + /// Returns one of the properties from the object, or a value derived from + /// properties on the object, to use when comparing peers. + /// + /// The data sent as part of the Announce request + /// + object GetKey(AnnounceRequest parameters); + } +} diff --git a/src/MonoTorrent/MonoTorrent.Tracker/PeerIdComparer.cs b/src/MonoTorrent/MonoTorrent.Tracker/PeerIdComparer.cs new file mode 100644 index 000000000..70d64865e --- /dev/null +++ b/src/MonoTorrent/MonoTorrent.Tracker/PeerIdComparer.cs @@ -0,0 +1,47 @@ +// +// PeerIdComparer.cs +// +// Authors: +// Alan McGovern +// +// Copyright (C) 2019 Alan McGovern +// +// Permission is hereby granted, free of charge, to any person obtaining +// a copy of this software and associated documentation files (the +// "Software"), to deal in the Software without restriction, including +// without limitation the rights to use, copy, modify, merge, publish, +// distribute, sublicense, and/or sell copies of the Software, and to +// permit persons to whom the Software is furnished to do so, subject to +// the following conditions: +// +// The above copyright notice and this permission notice shall be +// included in all copies or substantial portions of the Software. +// +// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, +// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF +// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND +// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE +// LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION +// OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION +// WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. +// + + +namespace MonoTorrent.Tracker +{ + /// + /// Uses the field to compare peers when handling Announce or Scrape requests. + /// + public class PeerIdComparer : IPeerComparer + { + /// + /// Returns the field to use to compare peers. + /// + /// The data sent as part of the Announce request + /// + public object GetKey(AnnounceRequest parameters) + { + return parameters.PeerId; + } + } +} diff --git a/src/MonoTorrent/MonoTorrent.Tracker/TrackerServer.cs b/src/MonoTorrent/MonoTorrent.Tracker/TrackerServer.cs index e2eb5641b..cd7b9dc1c 100644 --- a/src/MonoTorrent/MonoTorrent.Tracker/TrackerServer.cs +++ b/src/MonoTorrent/MonoTorrent.Tracker/TrackerServer.cs @@ -166,7 +166,7 @@ public TrackerServer(BEncodedString trackerId) /// The trackable to add /// public bool Add(ITrackable trackable) - => Add(trackable, new IPAddressComparer()); + => Add(trackable, new ClientAddressComparer()); /// /// Adds the trackable to the server