Skip to content

Commit

Permalink
[tracker] Allow comparing peers using PeerId
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
alanmcgovern committed Aug 21, 2019
1 parent 2ee0e41 commit 7bcd469
Show file tree
Hide file tree
Showing 4 changed files with 104 additions and 8 deletions.
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
//
// IPAddressComparer.cs
// ClientAddressComparer.cs
//
// Authors:
// Alan McGovern <[email protected]>
Expand Down Expand Up @@ -29,13 +29,16 @@

namespace MonoTorrent.Tracker
{
public interface IPeerComparer
{
object GetKey(AnnounceRequest parameters);
}

class IPAddressComparer : IPeerComparer
/// <summary>
/// Uses the <see cref="AnnounceRequest.ClientAddress"/> field to compare peers when handling Announce or Scrape requests.
/// </summary>
public class ClientAddressComparer : IPeerComparer
{
/// <summary>
/// Returns the <see cref="AnnounceRequest.ClientAddress"/> field to use to compare peers.
/// </summary>
/// <param name="parameters">The data sent as part of the Announce request</param>
/// <returns></returns>
public object GetKey(AnnounceRequest parameters)
{
return parameters.ClientAddress;
Expand Down
46 changes: 46 additions & 0 deletions src/MonoTorrent/MonoTorrent.Tracker/IPeerComparer.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
//
// IPeerComparer.cs
//
// Authors:
// Alan McGovern <[email protected]>
//
// 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
{
/// <summary>
/// Returns one of the properties from a peers <see cref="AnnounceRequest"/>, or a value derived from
/// properties on the <see cref="AnnounceRequest"/>, to use when comparing peers.
/// </summary>
public interface IPeerComparer
{
/// <summary>
/// Returns one of the properties from the <see cref="AnnounceRequest"/> object, or a value derived from
/// properties on the <see cref="AnnounceRequest"/> object, to use when comparing peers.
/// </summary>
/// <param name="parameters">The data sent as part of the Announce request</param>
/// <returns></returns>
object GetKey(AnnounceRequest parameters);
}
}
47 changes: 47 additions & 0 deletions src/MonoTorrent/MonoTorrent.Tracker/PeerIdComparer.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
//
// PeerIdComparer.cs
//
// Authors:
// Alan McGovern <[email protected]>
//
// 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
{
/// <summary>
/// Uses the <see cref="AnnounceRequest.PeerId"/> field to compare peers when handling Announce or Scrape requests.
/// </summary>
public class PeerIdComparer : IPeerComparer
{
/// <summary>
/// Returns the <see cref="AnnounceRequest.PeerId"/> field to use to compare peers.
/// </summary>
/// <param name="parameters">The data sent as part of the Announce request</param>
/// <returns></returns>
public object GetKey(AnnounceRequest parameters)
{
return parameters.PeerId;
}
}
}
2 changes: 1 addition & 1 deletion src/MonoTorrent/MonoTorrent.Tracker/TrackerServer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -166,7 +166,7 @@ public TrackerServer(BEncodedString trackerId)
/// <param name="trackable">The trackable to add</param>
/// <returns></returns>
public bool Add(ITrackable trackable)
=> Add(trackable, new IPAddressComparer());
=> Add(trackable, new ClientAddressComparer());

/// <summary>
/// Adds the trackable to the server
Expand Down

0 comments on commit 7bcd469

Please sign in to comment.