mirror of
https://github.com/Sonarr/Sonarr.git
synced 2024-12-18 23:48:35 +02:00
bfe134ee54
Switch to use Port #0 which just uses the next free port Non Functional - Code Cleanup Tabs -> Spaces, Opps Deleted too much Refactoring & Code Cleanup & Move Config to DB remove unneeded line Clean Up Spaces Code Review Points on the UI FIx extra space Clean Up unrequired changes Add a HealthCheck. Extra Check in ProxyCheck. Correctly deal with Socks Authentication Remove SubModule Add in Nuget + Tweak ProxyCheck Code Review Points Missed Review Point Add Subnet Filtering, Add ProxyBypass for local addresses. UI updated for property changes. Fix typo, and copy&paste error Tweak URL to hit
89 lines
3.3 KiB
C#
89 lines
3.3 KiB
C#
using System.Linq;
|
|
using System.Reflection;
|
|
using FluentValidation;
|
|
using NzbDrone.Common.EnvironmentInfo;
|
|
using NzbDrone.Common.Extensions;
|
|
using NzbDrone.Core.Authentication;
|
|
using NzbDrone.Core.Configuration;
|
|
using NzbDrone.Core.Update;
|
|
using NzbDrone.Core.Validation;
|
|
using NzbDrone.Core.Validation.Paths;
|
|
using Omu.ValueInjecter;
|
|
|
|
namespace NzbDrone.Api.Config
|
|
{
|
|
public class HostConfigModule : NzbDroneRestModule<HostConfigResource>
|
|
{
|
|
private readonly IConfigFileProvider _configFileProvider;
|
|
private readonly IConfigService _configService;
|
|
private readonly IUserService _userService;
|
|
|
|
public HostConfigModule(IConfigFileProvider configFileProvider, IConfigService configService, IUserService userService)
|
|
: base("/config/host")
|
|
{
|
|
_configFileProvider = configFileProvider;
|
|
_configService = configService;
|
|
_userService = userService;
|
|
|
|
GetResourceSingle = GetHostConfig;
|
|
GetResourceById = GetHostConfig;
|
|
UpdateResource = SaveHostConfig;
|
|
|
|
SharedValidator.RuleFor(c => c.BindAddress)
|
|
.ValidIp4Address()
|
|
.NotListenAllIp4Address()
|
|
.When(c => c.BindAddress != "*");
|
|
|
|
SharedValidator.RuleFor(c => c.Port).ValidPort();
|
|
|
|
SharedValidator.RuleFor(c => c.UrlBase).ValidUrlBase();
|
|
|
|
SharedValidator.RuleFor(c => c.Username).NotEmpty().When(c => c.AuthenticationMethod != AuthenticationType.None);
|
|
SharedValidator.RuleFor(c => c.Password).NotEmpty().When(c => c.AuthenticationMethod != AuthenticationType.None);
|
|
|
|
SharedValidator.RuleFor(c => c.SslPort).ValidPort().When(c => c.EnableSsl);
|
|
SharedValidator.RuleFor(c => c.SslCertHash).NotEmpty().When(c => c.EnableSsl && OsInfo.IsWindows);
|
|
|
|
SharedValidator.RuleFor(c => c.Branch).NotEmpty().WithMessage("Branch name is required, 'master' is the default");
|
|
SharedValidator.RuleFor(c => c.UpdateScriptPath).IsValidPath().When(c => c.UpdateMechanism == UpdateMechanism.Script);
|
|
}
|
|
|
|
private HostConfigResource GetHostConfig()
|
|
{
|
|
var resource = new HostConfigResource();
|
|
resource.InjectFrom(_configFileProvider, _configService);
|
|
resource.Id = 1;
|
|
|
|
var user = _userService.FindUser();
|
|
|
|
if (user != null)
|
|
{
|
|
resource.Username = user.Username;
|
|
resource.Password = user.Password;
|
|
}
|
|
|
|
return resource;
|
|
}
|
|
|
|
private HostConfigResource GetHostConfig(int id)
|
|
{
|
|
return GetHostConfig();
|
|
}
|
|
|
|
private void SaveHostConfig(HostConfigResource resource)
|
|
{
|
|
var dictionary = resource.GetType()
|
|
.GetProperties(BindingFlags.Instance | BindingFlags.Public)
|
|
.ToDictionary(prop => prop.Name, prop => prop.GetValue(resource, null));
|
|
|
|
_configFileProvider.SaveConfigDictionary(dictionary);
|
|
_configService.SaveConfigDictionary(dictionary);
|
|
|
|
if (resource.Username.IsNotNullOrWhiteSpace() && resource.Password.IsNotNullOrWhiteSpace())
|
|
{
|
|
_userService.Upsert(resource.Username, resource.Password);
|
|
}
|
|
}
|
|
}
|
|
}
|