Fixed: Blocklisting torrents from indexers that do not provide torrent hash

(cherry picked from commit 3541cd7ba877fb785c7f97123745abf51162eb8e)

Closes #3082
This commit is contained in:
Mark McDowall
2023-10-29 11:23:23 -07:00
committed by Bogdan
parent 526429bde4
commit 2351efd013
24 changed files with 164 additions and 45 deletions
@@ -6,6 +6,7 @@ using NLog;
using NzbDrone.Common.Disk;
using NzbDrone.Common.Extensions;
using NzbDrone.Common.Http;
using NzbDrone.Core.Blocklisting;
using NzbDrone.Core.Configuration;
using NzbDrone.Core.Exceptions;
using NzbDrone.Core.Indexers;
@@ -21,17 +22,20 @@ namespace NzbDrone.Core.Download
where TSettings : IProviderConfig, new()
{
protected readonly IHttpClient _httpClient;
private readonly IBlocklistService _blocklistService;
protected readonly ITorrentFileInfoReader _torrentFileInfoReader;
protected TorrentClientBase(ITorrentFileInfoReader torrentFileInfoReader,
IHttpClient httpClient,
IConfigService configService,
IDiskProvider diskProvider,
IRemotePathMappingService remotePathMappingService,
Logger logger)
IHttpClient httpClient,
IConfigService configService,
IDiskProvider diskProvider,
IRemotePathMappingService remotePathMappingService,
IBlocklistService blocklistService,
Logger logger)
: base(configService, diskProvider, remotePathMappingService, logger)
{
_httpClient = httpClient;
_blocklistService = blocklistService;
_torrentFileInfoReader = torrentFileInfoReader;
}
@@ -86,7 +90,7 @@ namespace NzbDrone.Core.Download
{
try
{
return DownloadFromMagnetUrl(remoteBook, magnetUrl);
return DownloadFromMagnetUrl(remoteBook, indexer, magnetUrl);
}
catch (NotSupportedException ex)
{
@@ -100,7 +104,7 @@ namespace NzbDrone.Core.Download
{
try
{
return DownloadFromMagnetUrl(remoteBook, magnetUrl);
return DownloadFromMagnetUrl(remoteBook, indexer, magnetUrl);
}
catch (NotSupportedException ex)
{
@@ -149,7 +153,7 @@ namespace NzbDrone.Core.Download
{
if (locationHeader.StartsWith("magnet:"))
{
return DownloadFromMagnetUrl(remoteBook, locationHeader);
return DownloadFromMagnetUrl(remoteBook, indexer, locationHeader);
}
request.Url += new HttpUri(locationHeader);
@@ -192,6 +196,9 @@ namespace NzbDrone.Core.Download
var filename = string.Format("{0}.torrent", FileNameBuilder.CleanFileName(remoteBook.Release.Title));
var hash = _torrentFileInfoReader.GetHashFromTorrentFile(torrentFile);
EnsureReleaseIsNotBlocklisted(remoteBook, indexer, hash);
var actualHash = AddFromTorrentFile(remoteBook, hash, filename, torrentFile);
if (actualHash.IsNotNullOrWhiteSpace() && hash != actualHash)
@@ -205,7 +212,7 @@ namespace NzbDrone.Core.Download
return actualHash;
}
private string DownloadFromMagnetUrl(RemoteBook remoteBook, string magnetUrl)
private string DownloadFromMagnetUrl(RemoteBook remoteBook, IIndexer indexer, string magnetUrl)
{
string hash = null;
string actualHash = null;
@@ -223,6 +230,8 @@ namespace NzbDrone.Core.Download
if (hash != null)
{
EnsureReleaseIsNotBlocklisted(remoteBook, indexer, hash);
actualHash = AddFromMagnetLink(remoteBook, hash, magnetUrl);
}
@@ -236,5 +245,29 @@ namespace NzbDrone.Core.Download
return actualHash;
}
private void EnsureReleaseIsNotBlocklisted(RemoteBook remoteBook, IIndexer indexer, string hash)
{
var indexerSettings = indexer?.Definition?.Settings as ITorrentIndexerSettings;
var torrentInfo = remoteBook.Release as TorrentInfo;
var torrentInfoHash = torrentInfo?.InfoHash;
// If the release didn't come from an interactive search,
// the hash wasn't known during processing and the
// indexer is configured to reject blocklisted releases
// during grab check if it's already been blocklisted.
if (torrentInfo != null && torrentInfoHash.IsNullOrWhiteSpace())
{
// If the hash isn't known from parsing we set it here so it can be used for blocklisting.
torrentInfo.InfoHash = hash;
if (remoteBook.ReleaseSource != ReleaseSourceType.InteractiveSearch &&
indexerSettings?.RejectBlocklistedTorrentHashesWhileGrabbing == true &&
_blocklistService.BlocklistedTorrentHash(remoteBook.Author.Id, hash))
{
throw new ReleaseBlockedException(remoteBook.Release, "Release previously added to blocklist");
}
}
}
}
}