diff --git a/src/NzbDrone.Core/Download/Clients/Aria2/Aria2.cs b/src/NzbDrone.Core/Download/Clients/Aria2/Aria2.cs new file mode 100644 index 000000000..7eac71b7f --- /dev/null +++ b/src/NzbDrone.Core/Download/Clients/Aria2/Aria2.cs @@ -0,0 +1,140 @@ +using System; +using System.Collections.Generic; +using System.Threading; +using FluentValidation.Results; +using NLog; +using NzbDrone.Common.Disk; +using NzbDrone.Common.Extensions; +using NzbDrone.Common.Http; +using NzbDrone.Core.Configuration; +using NzbDrone.Core.Parser.Model; +using NzbDrone.Core.Validation; + +namespace NzbDrone.Core.Download.Clients.Aria2 +{ + public class Aria2 : TorrentClientBase + { + private readonly IAria2Proxy _proxy; + + public override string Name => "Aria2"; + + public Aria2(IAria2Proxy proxy, + ITorrentFileInfoReader torrentFileInfoReader, + IHttpClient httpClient, + IConfigService configService, + IDiskProvider diskProvider, + Logger logger) + : base(torrentFileInfoReader, httpClient, configService, diskProvider, logger) + { + _proxy = proxy; + } + + protected override string AddFromMagnetLink(ReleaseInfo release, string hash, string magnetLink) + { + var gid = _proxy.AddUri(Settings, magnetLink); + + var tries = 10; + var retryDelay = 500; + + // Wait a bit for the magnet to be resolved. + if (!WaitForTorrent(gid, hash, tries, retryDelay)) + { + _logger.Warn($"Aria2 could not add magnent within {tries * retryDelay / 1000} seconds, download may remain stuck: {magnetLink}."); + return hash; + } + + _logger.Debug($"Ă„ria2 AddFromMagnetLink '{hash}' -> '{gid}'"); + + return hash; + } + + protected override string AddFromTorrentFile(ReleaseInfo release, string hash, string filename, byte[] fileContent) + { + var gid = _proxy.AddTorrent(Settings, fileContent); + + var tries = 10; + var retryDelay = 500; + + // Wait a bit for the magnet to be resolved. + if (!WaitForTorrent(gid, hash, tries, retryDelay)) + { + _logger.Warn($"Aria2 could not add torrent within {tries * retryDelay / 1000} seconds, download may remain stuck: {filename}."); + return hash; + } + + return hash; + } + + private bool WaitForTorrent(string gid, string hash, int tries, int retryDelay) + { + for (var i = 0; i < tries; i++) + { + var found = _proxy.GetFromGID(Settings, gid); + + if (found?.InfoHash?.ToLower() == hash?.ToLower()) + { + return true; + } + + Thread.Sleep(retryDelay); + } + + _logger.Debug("Could not find hash {0} in {1} tries at {2} ms intervals.", hash, tries, retryDelay); + + return false; + } + + protected override void Test(List failures) + { + failures.AddIfNotNull(TestConnection()); + + if (failures.HasErrors()) + { + return; + } + } + + private ValidationFailure TestConnection() + { + try + { + var version = _proxy.GetVersion(Settings); + + if (new Version(version) < new Version("1.34.0")) + { + return new ValidationFailure(string.Empty, "Aria2 version should be at least 1.34.0. Version reported is {0}", version); + } + } + catch (Exception ex) + { + _logger.Error(ex, "Failed to test Aria2"); + + return new NzbDroneValidationFailure("Host", "Unable to connect to Aria2") + { + DetailedDescription = ex.Message + }; + } + + return null; + } + + protected override string AddFromTorrentLink(ReleaseInfo release, string hash, string torrentLink) + { + var gid = _proxy.AddUri(Settings, torrentLink); + + var tries = 10; + var retryDelay = 500; + + // Wait a bit for the magnet to be resolved. + if (!WaitForTorrent(gid, hash, tries, retryDelay)) + { + _logger.Warn($"Aria2 could not add torrent within {tries * retryDelay / 1000} seconds, download may remain stuck: {torrentLink}."); + return hash; + } + + _logger.Debug($"Aria2 AddFromTorrentLink '{hash}' -> '{gid}'"); + + return hash; + } + } +} diff --git a/src/NzbDrone.Core/Download/Clients/Aria2/Aria2Containers.cs b/src/NzbDrone.Core/Download/Clients/Aria2/Aria2Containers.cs new file mode 100644 index 000000000..d4ab5a49c --- /dev/null +++ b/src/NzbDrone.Core/Download/Clients/Aria2/Aria2Containers.cs @@ -0,0 +1,111 @@ +using CookComputing.XmlRpc; + +namespace NzbDrone.Core.Download.Clients.Aria2 +{ + public class Aria2Version + { + [XmlRpcMember("version")] + public string Version; + + [XmlRpcMember("enabledFeatures")] + public string[] EnabledFeatures; + } + + public class Aria2Uri + { + [XmlRpcMember("status")] + public string Status; + + [XmlRpcMember("uri")] + public string Uri; + } + + public class Aria2File + { + [XmlRpcMember("index")] + public string Index; + + [XmlRpcMember("length")] + public string Length; + + [XmlRpcMember("completedLength")] + public string CompletedLength; + + [XmlRpcMember("path")] + public string Path; + + [XmlRpcMember("selected")] + [XmlRpcMissingMapping(MappingAction.Ignore)] + public string Selected; + + [XmlRpcMember("uris")] + [XmlRpcMissingMapping(MappingAction.Ignore)] + public Aria2Uri[] Uris; + } + + public class Aria2Status + { + [XmlRpcMember("bittorrent")] + [XmlRpcMissingMapping(MappingAction.Ignore)] + public XmlRpcStruct Bittorrent; + + [XmlRpcMember("bitfield")] + [XmlRpcMissingMapping(MappingAction.Ignore)] + public string Bitfield; + + [XmlRpcMember("infoHash")] + [XmlRpcMissingMapping(MappingAction.Ignore)] + public string InfoHash; + + [XmlRpcMember("completedLength")] + [XmlRpcMissingMapping(MappingAction.Ignore)] + public string CompletedLength; + + [XmlRpcMember("connections")] + [XmlRpcMissingMapping(MappingAction.Ignore)] + public string Connections; + + [XmlRpcMember("dir")] + [XmlRpcMissingMapping(MappingAction.Ignore)] + public string Dir; + + [XmlRpcMember("downloadSpeed")] + [XmlRpcMissingMapping(MappingAction.Ignore)] + public string DownloadSpeed; + + [XmlRpcMember("files")] + [XmlRpcMissingMapping(MappingAction.Ignore)] + public Aria2File[] Files; + + [XmlRpcMember("gid")] + public string Gid; + + [XmlRpcMember("numPieces")] + [XmlRpcMissingMapping(MappingAction.Ignore)] + public string NumPieces; + + [XmlRpcMember("pieceLength")] + [XmlRpcMissingMapping(MappingAction.Ignore)] + public string PieceLength; + + [XmlRpcMember("status")] + [XmlRpcMissingMapping(MappingAction.Ignore)] + public string Status; + + [XmlRpcMember("totalLength")] + [XmlRpcMissingMapping(MappingAction.Ignore)] + public string TotalLength; + + [XmlRpcMember("uploadLength")] + [XmlRpcMissingMapping(MappingAction.Ignore)] + public string UploadLength; + + [XmlRpcMember("uploadSpeed")] + [XmlRpcMissingMapping(MappingAction.Ignore)] + public string UploadSpeed; + + [XmlRpcMember("errorMessage")] + [XmlRpcMissingMapping(MappingAction.Ignore)] + public string ErrorMessage; + } +} diff --git a/src/NzbDrone.Core/Download/Clients/Aria2/Aria2Proxy.cs b/src/NzbDrone.Core/Download/Clients/Aria2/Aria2Proxy.cs new file mode 100644 index 000000000..855d300ba --- /dev/null +++ b/src/NzbDrone.Core/Download/Clients/Aria2/Aria2Proxy.cs @@ -0,0 +1,144 @@ +using System; +using System.Collections; +using System.Collections.Generic; +using System.Net; +using CookComputing.XmlRpc; +using NLog; + +namespace NzbDrone.Core.Download.Clients.Aria2 +{ + public interface IAria2Proxy + { + string GetVersion(Aria2Settings settings); + string AddUri(Aria2Settings settings, string magnet); + string AddTorrent(Aria2Settings settings, byte[] torrent); + Aria2Status GetFromGID(Aria2Settings settings, string gid); + } + + public interface IAria2 : IXmlRpcProxy + { + [XmlRpcMethod("aria2.getVersion")] + Aria2Version GetVersion(string token); + + [XmlRpcMethod("aria2.addUri")] + string AddUri(string token, string[] uri); + + [XmlRpcMethod("aria2.addTorrent")] + string AddTorrent(string token, byte[] torrent); + + [XmlRpcMethod("aria2.forceRemove")] + string Remove(string token, string gid); + + [XmlRpcMethod("aria2.tellStatus")] + Aria2Status GetFromGid(string token, string gid); + + [XmlRpcMethod("aria2.getGlobalOption")] + XmlRpcStruct GetGlobalOption(string token); + + [XmlRpcMethod("aria2.tellActive")] + Aria2Status[] GetActives(string token); + + [XmlRpcMethod("aria2.tellWaiting")] + Aria2Status[] GetWaitings(string token, int offset, int num); + + [XmlRpcMethod("aria2.tellStopped")] + Aria2Status[] GetStoppeds(string token, int offset, int num); + } + + public class Aria2Proxy : IAria2Proxy + { + private readonly Logger _logger; + + public Aria2Proxy(Logger logger) + { + _logger = logger; + } + + private string GetToken(Aria2Settings settings) + { + return $"token:{settings?.SecretToken}"; + } + + private string GetURL(Aria2Settings settings) + { + return $"http{(settings.UseSsl ? "s" : "")}://{settings.Host}:{settings.Port}{settings.RpcPath}"; + } + + public string GetVersion(Aria2Settings settings) + { + _logger.Debug("> aria2.getVersion"); + + var client = BuildClient(settings); + var version = ExecuteRequest(() => client.GetVersion(GetToken(settings))); + + _logger.Debug("< aria2.getVersion"); + + return version.Version; + } + + public Aria2Status GetFromGID(Aria2Settings settings, string gid) + { + _logger.Debug("> aria2.tellStatus"); + + var client = BuildClient(settings); + var found = ExecuteRequest(() => client.GetFromGid(GetToken(settings), gid)); + + _logger.Debug("< aria2.tellStatus"); + + return found; + } + + public string AddUri(Aria2Settings settings, string magnet) + { + _logger.Debug("> aria2.addUri"); + + var client = BuildClient(settings); + var gid = ExecuteRequest(() => client.AddUri(GetToken(settings), new[] { magnet })); + + _logger.Debug("< aria2.addUri"); + + return gid; + } + + public string AddTorrent(Aria2Settings settings, byte[] torrent) + { + _logger.Debug("> aria2.addTorrent"); + + var client = BuildClient(settings); + var gid = ExecuteRequest(() => client.AddTorrent(GetToken(settings), torrent)); + + _logger.Debug("< aria2.addTorrent"); + + return gid; + } + + private IAria2 BuildClient(Aria2Settings settings) + { + var client = XmlRpcProxyGen.Create(); + client.Url = GetURL(settings); + + return client; + } + + private T ExecuteRequest(Func task) + { + try + { + return task(); + } + catch (XmlRpcServerException ex) + { + throw new DownloadClientException("Unable to connect to aria2, please check your settings", ex); + } + catch (WebException ex) + { + if (ex.Status == WebExceptionStatus.TrustFailure) + { + throw new DownloadClientUnavailableException("Unable to connect to aria2, certificate validation failed.", ex); + } + + throw new DownloadClientUnavailableException("Unable to connect to aria2, please check your settings", ex); + } + } + } +} diff --git a/src/NzbDrone.Core/Download/Clients/Aria2/Aria2Settings.cs b/src/NzbDrone.Core/Download/Clients/Aria2/Aria2Settings.cs new file mode 100644 index 000000000..a78732c52 --- /dev/null +++ b/src/NzbDrone.Core/Download/Clients/Aria2/Aria2Settings.cs @@ -0,0 +1,49 @@ +using FluentValidation; +using NzbDrone.Core.Annotations; +using NzbDrone.Core.ThingiProvider; +using NzbDrone.Core.Validation; + +namespace NzbDrone.Core.Download.Clients.Aria2 +{ + public class Aria2SettingsValidator : AbstractValidator + { + public Aria2SettingsValidator() + { + RuleFor(c => c.Host).ValidHost(); + } + } + + public class Aria2Settings : IProviderConfig + { + private static readonly Aria2SettingsValidator Validator = new Aria2SettingsValidator(); + + public Aria2Settings() + { + Host = "localhost"; + Port = 6800; + RpcPath = "/rpc"; + UseSsl = false; + SecretToken = "MySecretToken"; + } + + [FieldDefinition(0, Label = "Host", Type = FieldType.Textbox)] + public string Host { get; set; } + + [FieldDefinition(1, Label = "Port", Type = FieldType.Number)] + public int Port { get; set; } + + [FieldDefinition(2, Label = "RPC Path", Type = FieldType.Textbox)] + public string RpcPath { get; set; } + + [FieldDefinition(3, Label = "Use SSL", Type = FieldType.Checkbox)] + public bool UseSsl { get; set; } + + [FieldDefinition(4, Label = "Secret token", Type = FieldType.Password, Privacy = PrivacyLevel.Password)] + public string SecretToken { get; set; } + + public NzbDroneValidationResult Validate() + { + return new NzbDroneValidationResult(Validator.Validate(this)); + } + } +}