From 0a827651a2f7ab90987e3b984218e35d8f95cd95 Mon Sep 17 00:00:00 2001 From: Kieran Date: Sat, 4 Mar 2023 20:31:53 +0000 Subject: [PATCH] Add trackers --- VoidCat/Controllers/DownloadController.cs | 2 +- VoidCat/Model/Extensions.cs | 5 ++++- VoidCat/Model/VoidSettings.cs | 16 ++++++++++++++-- VoidCat/Services/Files/LocalDiskFileStorage.cs | 11 +++++++++-- 4 files changed, 28 insertions(+), 6 deletions(-) diff --git a/VoidCat/Controllers/DownloadController.cs b/VoidCat/Controllers/DownloadController.cs index 9596f62..31d5809 100644 --- a/VoidCat/Controllers/DownloadController.cs +++ b/VoidCat/Controllers/DownloadController.cs @@ -54,7 +54,7 @@ public class DownloadController : Controller { var t = await voidFile.Metadata!.MakeTorrent( await _storage.Open(new(gid, Enumerable.Empty()), CancellationToken.None), - _settings.SiteUrl); + _settings.SiteUrl, _settings.TorrentTrackers); Response.Headers.ContentDisposition = $"inline; filename=\"{id}\""; Response.ContentType = "application/x-bittorent"; diff --git a/VoidCat/Model/Extensions.cs b/VoidCat/Model/Extensions.cs index 0791f84..b6c7df7 100644 --- a/VoidCat/Model/Extensions.cs +++ b/VoidCat/Model/Extensions.cs @@ -49,6 +49,7 @@ public static class Extensions base58 = Path.GetFileNameWithoutExtension(base58); var guidBytes = enc.DecodeData(base58); if (guidBytes.Length != 16) throw new VoidInvalidIdException(base58); + return new Guid(guidBytes); } @@ -275,7 +276,7 @@ public static class Extensions public static bool HasGoogle(this VoidSettings settings) => settings.Google != null; - public static async Task MakeTorrent(this FileMeta meta, Stream fileStream, Uri baseAddress) + public static async Task MakeTorrent(this FileMeta meta, Stream fileStream, Uri baseAddress, List trackers) { const int pieceSize = 262_144; const int pieceHashLen = 20; @@ -313,6 +314,8 @@ public static class Extensions IsPrivate = false, PieceSize = pieceSize, Pieces = await BuildPieces(), + // ReSharper disable once CoVariantArrayConversion + Trackers = trackers.Select(a => new[] {a}).ToArray(), ExtraFields = new BDictionary { {"url-list", webSeed.ToString()} diff --git a/VoidCat/Model/VoidSettings.cs b/VoidCat/Model/VoidSettings.cs index 15170dd..5cdd297 100644 --- a/VoidCat/Model/VoidSettings.cs +++ b/VoidCat/Model/VoidSettings.cs @@ -105,11 +105,23 @@ namespace VoidCat.Model /// Discord application settings /// public OAuthDetails? Discord { get; init; } - + /// /// Google application settings /// public OAuthDetails? Google { get; init; } + + /// + /// A list of trackers to attach to torrent files + /// + public List TorrentTrackers { get; init; } = new() + { + "wss://tracker.btorrent.xyz", + "wss://tracker.openwebtorrent.com", + "udp://tracker.opentrackr.org:1337/announce", + "udp://tracker.openbittorrent.com:6969/announce", + "http://tracker.openbittorrent.com:80/announce" + }; } public sealed class TorSettings @@ -190,4 +202,4 @@ namespace VoidCat.Model public string? ClientId { get; init; } public string? ClientSecret { get; init; } } -} \ No newline at end of file +} diff --git a/VoidCat/Services/Files/LocalDiskFileStorage.cs b/VoidCat/Services/Files/LocalDiskFileStorage.cs index 8e56ebc..baf8593 100644 --- a/VoidCat/Services/Files/LocalDiskFileStorage.cs +++ b/VoidCat/Services/Files/LocalDiskFileStorage.cs @@ -86,8 +86,15 @@ public class LocalDiskFileStore : StreamFileStore, IFileStore if (payload.Segment == payload.TotalSegments) { - var t = await vf.Metadata!.MakeTorrent(new FileStream(finalPath, FileMode.Open), _settings.SiteUrl); - vf.Metadata!.MagnetLink = t.GetMagnetLink(); + var t = await vf.Metadata!.MakeTorrent( + new FileStream(finalPath, FileMode.Open), + _settings.SiteUrl, + _settings.TorrentTrackers); + + var ub = new UriBuilder(_settings.SiteUrl); + ub.Path = $"/d/{vf.Id.ToBase58()}.torrent"; + + vf.Metadata!.MagnetLink = $"{t.GetMagnetLink()}&xs={Uri.EscapeDataString(ub.ToString())}"; } return vf;