sonarr-repo-only/NzbDrone.Host/AccessControl/UrlAclAdapter.cs

102 lines
2.8 KiB
C#
Raw Normal View History

using System;
using System.Linq;
using NLog;
using NzbDrone.Common;
using NzbDrone.Common.EnvironmentInfo;
using NzbDrone.Common.Processes;
using NzbDrone.Core.Configuration;
2013-08-10 20:30:27 +00:00
namespace NzbDrone.Host.AccessControl
{
public interface IUrlAclAdapter
{
2013-09-21 02:50:57 +00:00
void ConfigureUrl();
2013-08-30 22:55:01 +00:00
string UrlAcl { get; }
}
public class UrlAclAdapter : IUrlAclAdapter
{
private const string URL_ACL = "http://{0}:{1}/";
private readonly IProcessProvider _processProvider;
private readonly IConfigFileProvider _configFileProvider;
2013-09-21 02:50:57 +00:00
private readonly IRuntimeInfo _runtimeInfo;
private readonly Logger _logger;
2013-09-21 02:50:57 +00:00
public string UrlAcl { get; private set; }
private string _localUrl;
private string _wildcardUrl;
public UrlAclAdapter(IProcessProvider processProvider,
IConfigFileProvider configFileProvider,
IRuntimeInfo runtimeInfo,
Logger logger)
{
_processProvider = processProvider;
_configFileProvider = configFileProvider;
2013-09-21 02:50:57 +00:00
_runtimeInfo = runtimeInfo;
_logger = logger;
2013-09-21 02:50:57 +00:00
_localUrl = String.Format(URL_ACL, "localhost", _configFileProvider.Port);
_wildcardUrl = String.Format(URL_ACL, "*", _configFileProvider.Port);
2013-09-21 02:50:57 +00:00
UrlAcl = _wildcardUrl;
}
2013-09-21 02:50:57 +00:00
public void ConfigureUrl()
2013-08-30 22:55:01 +00:00
{
2013-09-21 02:50:57 +00:00
if (!_runtimeInfo.IsAdmin && !IsRegistered)
2013-08-30 22:55:01 +00:00
{
2013-09-21 02:50:57 +00:00
UrlAcl = _localUrl;
}
2013-09-21 02:50:57 +00:00
if (_runtimeInfo.IsAdmin)
{
2013-09-21 02:50:57 +00:00
RefreshRegistration();
2013-08-30 22:55:01 +00:00
}
}
2013-09-21 02:50:57 +00:00
private void RefreshRegistration()
{
if (OsInfo.Version.Major < 6)
return;
2013-08-30 22:55:01 +00:00
RegisterUrl();
}
2013-09-21 02:50:57 +00:00
private bool IsRegistered
{
get
{
var arguments = String.Format("http show urlacl {0}", _wildcardUrl);
var output = RunNetsh(arguments);
if (output == null || !output.Standard.Any()) return false;
return output.Standard.Any(line => line.Contains(_wildcardUrl));
}
}
2013-08-30 22:55:01 +00:00
private void RegisterUrl()
{
2013-08-30 22:55:01 +00:00
var arguments = String.Format("http add urlacl {0} sddl=D:(A;;GX;;;S-1-1-0)", UrlAcl);
RunNetsh(arguments);
}
private ProcessOutput RunNetsh(string arguments)
{
try
{
var output = _processProvider.StartAndCapture("netsh.exe", arguments);
return output;
}
catch (Exception ex)
{
_logger.WarnException("Error executing netsh with arguments: " + arguments, ex);
}
return null;
}
}
}