mirror of
https://github.com/Prowlarr/Prowlarr.git
synced 2026-04-21 22:25:03 -04:00
0ddc530dd4
Fixed: Consistency in C# Indexer Credential Fields
92 lines
3.4 KiB
C#
92 lines
3.4 KiB
C#
using System.Collections.Generic;
|
|
using System.Linq;
|
|
using System.Text.RegularExpressions;
|
|
using FluentValidation;
|
|
using NzbDrone.Common.Extensions;
|
|
using NzbDrone.Core.Annotations;
|
|
using NzbDrone.Core.Validation;
|
|
|
|
namespace NzbDrone.Core.Indexers.Newznab
|
|
{
|
|
public class NewznabSettingsValidator : AbstractValidator<NewznabSettings>
|
|
{
|
|
private static readonly string[] ApiKeyWhiteList =
|
|
{
|
|
"nzbs.org",
|
|
"nzb.su",
|
|
"dognzb.cr",
|
|
"nzbplanet.net",
|
|
"nzbid.org",
|
|
"nzbndx.com",
|
|
"nzbindex.in"
|
|
};
|
|
|
|
private static bool ShouldHaveApiKey(NewznabSettings settings)
|
|
{
|
|
if (settings.BaseUrl == null)
|
|
{
|
|
return false;
|
|
}
|
|
|
|
return ApiKeyWhiteList.Any(c => settings.BaseUrl.ToLowerInvariant().Contains(c));
|
|
}
|
|
|
|
private static readonly Regex AdditionalParametersRegex = new Regex(@"(&.+?\=.+?)+", RegexOptions.Compiled);
|
|
|
|
public NewznabSettingsValidator()
|
|
{
|
|
RuleFor(c => c.BaseUrl).ValidRootUrl();
|
|
RuleFor(c => c.ApiPath).ValidUrlBase("/api");
|
|
RuleFor(c => c.ApiKey).NotEmpty().When(ShouldHaveApiKey);
|
|
RuleFor(c => c.AdditionalParameters).Matches(AdditionalParametersRegex)
|
|
.When(c => !c.AdditionalParameters.IsNullOrWhiteSpace());
|
|
|
|
RuleFor(c => c.VipExpiration).Must(c => c.IsValidDate())
|
|
.When(c => c.VipExpiration.IsNotNullOrWhiteSpace())
|
|
.WithMessage("Correctly formatted date is required");
|
|
|
|
RuleFor(c => c.VipExpiration).Must(c => c.IsFutureDate())
|
|
.When(c => c.VipExpiration.IsNotNullOrWhiteSpace())
|
|
.WithMessage("Must be a future date");
|
|
}
|
|
}
|
|
|
|
public class NewznabSettings : IIndexerSettings
|
|
{
|
|
private static readonly NewznabSettingsValidator Validator = new NewznabSettingsValidator();
|
|
|
|
public NewznabSettings()
|
|
{
|
|
ApiPath = "/api";
|
|
VipExpiration = "";
|
|
}
|
|
|
|
[FieldDefinition(0, Label = "URL")]
|
|
public string BaseUrl { get; set; }
|
|
|
|
[FieldDefinition(1, Label = "API Path", HelpText = "Path to the api, usually /api", Advanced = true)]
|
|
public string ApiPath { get; set; }
|
|
|
|
[FieldDefinition(2, Label = "API Key", HelpText = "Site API Key", Privacy = PrivacyLevel.ApiKey)]
|
|
public string ApiKey { get; set; }
|
|
|
|
[FieldDefinition(5, Label = "Additional Parameters", HelpText = "Additional Newznab parameters", Advanced = true)]
|
|
public string AdditionalParameters { get; set; }
|
|
|
|
[FieldDefinition(6, Label = "VIP Expiration", HelpText = "Enter date (yyyy-mm-dd) for VIP Expiration or blank, Prowlarr will notify 1 week from expiration of VIP")]
|
|
public string VipExpiration { get; set; }
|
|
|
|
[FieldDefinition(7)]
|
|
public IndexerBaseSettings BaseSettings { get; set; } = new IndexerBaseSettings();
|
|
|
|
public List<IndexerCategory> Categories { get; set; }
|
|
|
|
// Field 8 is used by TorznabSettings MinimumSeeders
|
|
// If you need to add another field here, update TorznabSettings as well and this comment
|
|
public virtual NzbDroneValidationResult Validate()
|
|
{
|
|
return new NzbDroneValidationResult(Validator.Validate(this));
|
|
}
|
|
}
|
|
}
|