Skip to content

Commit

Permalink
[tracker] Generate useful and random tracker IDs
Browse files Browse the repository at this point in the history
They indicate that monotorrent is used, the version which is running
and then end in a random unique(ish) positive integer.
  • Loading branch information
alanmcgovern committed Aug 14, 2019
1 parent 02ba386 commit 4946010
Show file tree
Hide file tree
Showing 4 changed files with 18 additions and 5 deletions.
2 changes: 1 addition & 1 deletion src/MonoTorrent/MonoTorrent.Client.Tracker/HTTPTracker.cs
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ public HTTPTracker(Uri announceUrl)

// Use a random integer prefixed by our identifier.
lock (random)
Key = VersionInfo.ClientIdentifier + random.Next (1, int.MaxValue).ToString ();
Key = $"{VersionInfo.ClientVersion}-{random.Next (1, int.MaxValue)}";
}

protected override async Task<List<Peer>> DoAnnounceAsync(AnnounceParameters parameters)
Expand Down
2 changes: 2 additions & 0 deletions src/MonoTorrent/MonoTorrent.Client/ClientEngine.cs
Original file line number Diff line number Diff line change
Expand Up @@ -460,7 +460,9 @@ internal void Stop()
static string GeneratePeerId()
{
StringBuilder sb = new StringBuilder(20);
sb.Append ("-");
sb.Append(VersionInfo.ClientVersion);
sb.Append ("-");

var random = new Random(count++);
while (sb.Length < 20)
Expand Down
10 changes: 9 additions & 1 deletion src/MonoTorrent/MonoTorrent.Tracker/Tracker.cs
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,8 @@ public class Tracker : IEnumerable<SimpleTorrentManager>, IDisposable
{
#region Static BEncodedStrings

static readonly Random Random = new Random ();

internal static readonly BEncodedString PeersKey = "peers";
internal static readonly BEncodedString IntervalKey = "interval";
internal static readonly BEncodedString MinIntervalKey = "min interval";
Expand Down Expand Up @@ -149,7 +151,7 @@ public BEncodedString TrackerId
/// Creates a new tracker
/// </summary>
public Tracker()
: this(new BEncodedString("monotorrent-tracker"))
: this (null)
{

}
Expand All @@ -160,6 +162,12 @@ public Tracker(BEncodedString trackerId)
allowScrape = true;
monitor = new RequestMonitor();
torrents = new Dictionary<InfoHash, SimpleTorrentManager>();

// Generate an ID which shows that this is monotorrent, and the version, and then a unique(ish) integer.
if (trackerId == null) {
lock (Random)
trackerId = $"{VersionInfo.ClientVersion}-{Random.Next (1, int.MaxValue)}";
}
this.trackerId = trackerId;

announceInterval = TimeSpan.FromMinutes(45);
Expand Down
9 changes: 6 additions & 3 deletions src/MonoTorrent/MonoTorrent/VersionInfo.cs
Original file line number Diff line number Diff line change
Expand Up @@ -39,15 +39,18 @@ public static class VersionInfo
/// </summary>
internal static readonly string ProtocolStringV100 = "BitTorrent protocol";

public static readonly string ClientIdentifier = "MO";
static readonly string ClientIdentifier = "MO";

/// <summary>
/// The current version of the client
/// The current version of the client in the form "MO1234", which represents 'MonoTorrent version 1.2.3.4'.
/// </summary>
internal static readonly string ClientVersion;

internal static readonly string DhtClientVersion = $"{ClientIdentifier}06";

/// <summary>
/// The full version of this library.
/// </summary>
public static readonly Version Version;

static VersionInfo ()
Expand All @@ -63,7 +66,7 @@ static VersionInfo ()
version = version.Substring (0, 4);
else
version = version.PadRight (4, '0');
ClientVersion = $"-{ClientIdentifier}{version}-";
ClientVersion = $"{ClientIdentifier}{version}";
}
}
}

0 comments on commit 4946010

Please sign in to comment.