2013-05-23 05:12:01 +00:00
|
|
|
using System;
|
2013-09-20 23:56:17 +00:00
|
|
|
using System.Linq;
|
2013-05-20 02:35:48 +00:00
|
|
|
using NLog;
|
2013-05-23 05:12:01 +00:00
|
|
|
using NzbDrone.Common;
|
2013-06-28 00:04:52 +00:00
|
|
|
using NzbDrone.Common.EnvironmentInfo;
|
2013-09-20 23:56:17 +00:00
|
|
|
using NzbDrone.Common.Processes;
|
2013-05-23 05:12:01 +00:00
|
|
|
using NzbDrone.Core.Configuration;
|
2013-05-20 02:35:48 +00:00
|
|
|
|
2013-08-10 20:30:27 +00:00
|
|
|
namespace NzbDrone.Host.AccessControl
|
2013-05-20 02:35:48 +00:00
|
|
|
{
|
|
|
|
public interface IUrlAclAdapter
|
|
|
|
{
|
|
|
|
void RefreshRegistration();
|
2013-09-20 23:56:17 +00:00
|
|
|
bool IsRegistered();
|
2013-08-30 22:55:01 +00:00
|
|
|
string UrlAcl { get; }
|
2013-09-20 23:56:17 +00:00
|
|
|
string LocalUrlAcl { get; }
|
2013-05-20 02:35:48 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
public class UrlAclAdapter : IUrlAclAdapter
|
|
|
|
{
|
2013-09-20 23:56:17 +00:00
|
|
|
private const string URL_ACL = "http://{0}:{1}/";
|
|
|
|
|
2013-05-20 02:35:48 +00:00
|
|
|
private readonly IProcessProvider _processProvider;
|
|
|
|
private readonly IConfigFileProvider _configFileProvider;
|
|
|
|
private readonly Logger _logger;
|
|
|
|
|
2013-06-28 00:04:52 +00:00
|
|
|
public UrlAclAdapter(IProcessProvider processProvider, IConfigFileProvider configFileProvider, Logger logger)
|
2013-05-20 02:35:48 +00:00
|
|
|
{
|
|
|
|
_processProvider = processProvider;
|
|
|
|
_configFileProvider = configFileProvider;
|
|
|
|
_logger = logger;
|
|
|
|
}
|
|
|
|
|
2013-09-20 23:56:17 +00:00
|
|
|
public bool IsRegistered()
|
|
|
|
{
|
|
|
|
var arguments = String.Format("http show urlacl {0}", UrlAcl);
|
|
|
|
var output = RunNetsh(arguments);
|
|
|
|
|
|
|
|
if (output == null || !output.Standard.Any()) return false;
|
|
|
|
|
|
|
|
return output.Standard.Any(line => line.Contains(UrlAcl));
|
|
|
|
}
|
|
|
|
|
2013-08-30 22:55:01 +00:00
|
|
|
public string UrlAcl
|
|
|
|
{
|
|
|
|
get
|
|
|
|
{
|
2013-09-20 23:56:17 +00:00
|
|
|
return String.Format(URL_ACL, "*", _configFileProvider.Port);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public string LocalUrlAcl
|
|
|
|
{
|
|
|
|
get
|
|
|
|
{
|
|
|
|
return String.Format(URL_ACL, "localhost", _configFileProvider.Port);
|
2013-08-30 22:55:01 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-05-20 02:35:48 +00:00
|
|
|
public void RefreshRegistration()
|
|
|
|
{
|
2013-06-28 00:04:52 +00:00
|
|
|
if (OsInfo.Version.Major < 6)
|
2013-05-20 02:35:48 +00:00
|
|
|
return;
|
|
|
|
|
2013-08-30 22:55:01 +00:00
|
|
|
RegisterUrl();
|
2013-05-20 02:35:48 +00:00
|
|
|
}
|
|
|
|
|
2013-08-30 22:55:01 +00:00
|
|
|
private void RegisterUrl()
|
2013-05-20 02:35:48 +00:00
|
|
|
{
|
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);
|
2013-05-20 02:35:48 +00:00
|
|
|
RunNetsh(arguments);
|
|
|
|
}
|
|
|
|
|
2013-09-20 23:56:17 +00:00
|
|
|
private ProcessOutput RunNetsh(string arguments)
|
2013-05-20 02:35:48 +00:00
|
|
|
{
|
|
|
|
try
|
|
|
|
{
|
2013-09-20 23:56:17 +00:00
|
|
|
var output = _processProvider.StartAndCapture("netsh.exe", arguments);
|
|
|
|
|
|
|
|
return output;
|
2013-05-20 02:35:48 +00:00
|
|
|
}
|
|
|
|
catch (Exception ex)
|
|
|
|
{
|
|
|
|
_logger.WarnException("Error executing netsh with arguments: " + arguments, ex);
|
|
|
|
}
|
2013-09-20 23:56:17 +00:00
|
|
|
|
|
|
|
return null;
|
2013-05-20 02:35:48 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|