2013-05-23 08:12:01 +03:00
|
|
|
using System;
|
2013-09-21 02:56:17 +03:00
|
|
|
using System.Linq;
|
2013-05-20 05:35:48 +03:00
|
|
|
using NLog;
|
2013-06-28 03:04:52 +03:00
|
|
|
using NzbDrone.Common.EnvironmentInfo;
|
2013-05-23 08:12:01 +03:00
|
|
|
using NzbDrone.Core.Configuration;
|
2013-05-20 05:35:48 +03:00
|
|
|
|
2013-08-10 23:30:27 +03:00
|
|
|
namespace NzbDrone.Host.AccessControl
|
2013-05-20 05:35:48 +03:00
|
|
|
{
|
|
|
|
public interface IUrlAclAdapter
|
|
|
|
{
|
2013-09-21 05:50:57 +03:00
|
|
|
void ConfigureUrl();
|
2013-09-22 22:57:03 +03:00
|
|
|
string Url { get; }
|
|
|
|
string HttpsUrl { get; }
|
2013-05-20 05:35:48 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
public class UrlAclAdapter : IUrlAclAdapter
|
|
|
|
{
|
2013-09-22 22:57:03 +03:00
|
|
|
private readonly INetshProvider _netshProvider;
|
2013-05-20 05:35:48 +03:00
|
|
|
private readonly IConfigFileProvider _configFileProvider;
|
2013-09-21 05:50:57 +03:00
|
|
|
private readonly IRuntimeInfo _runtimeInfo;
|
2013-05-20 05:35:48 +03:00
|
|
|
private readonly Logger _logger;
|
|
|
|
|
2013-09-22 22:57:03 +03:00
|
|
|
public string Url { get; private set; }
|
|
|
|
public string HttpsUrl { get; private set; }
|
|
|
|
|
2013-09-21 05:50:57 +03:00
|
|
|
private string _localUrl;
|
|
|
|
private string _wildcardUrl;
|
2013-09-22 22:57:03 +03:00
|
|
|
private string _localHttpsUrl;
|
|
|
|
private string _wildcardHttpsUrl;
|
2013-09-21 05:50:57 +03:00
|
|
|
|
2013-09-22 22:57:03 +03:00
|
|
|
public UrlAclAdapter(INetshProvider netshProvider,
|
2013-09-21 05:50:57 +03:00
|
|
|
IConfigFileProvider configFileProvider,
|
|
|
|
IRuntimeInfo runtimeInfo,
|
|
|
|
Logger logger)
|
2013-05-20 05:35:48 +03:00
|
|
|
{
|
2013-09-22 22:57:03 +03:00
|
|
|
_netshProvider = netshProvider;
|
2013-05-20 05:35:48 +03:00
|
|
|
_configFileProvider = configFileProvider;
|
2013-09-21 05:50:57 +03:00
|
|
|
_runtimeInfo = runtimeInfo;
|
2013-05-20 05:35:48 +03:00
|
|
|
_logger = logger;
|
2013-09-21 02:56:17 +03:00
|
|
|
|
2013-09-22 22:57:03 +03:00
|
|
|
_localUrl = String.Format("http://localhost:{0}/", _configFileProvider.Port);
|
|
|
|
_wildcardUrl = String.Format("http://*:{0}/", _configFileProvider.Port);
|
|
|
|
_localHttpsUrl = String.Format("https://localhost:{0}/", _configFileProvider.SslPort);
|
|
|
|
_wildcardHttpsUrl = String.Format("https://*:{0}/", _configFileProvider.SslPort);
|
2013-09-21 02:56:17 +03:00
|
|
|
|
2013-09-22 22:57:03 +03:00
|
|
|
Url = _wildcardUrl;
|
|
|
|
HttpsUrl = _wildcardHttpsUrl;
|
2013-09-21 02:56:17 +03:00
|
|
|
}
|
|
|
|
|
2013-09-21 05:50:57 +03:00
|
|
|
public void ConfigureUrl()
|
2013-08-31 01:55:01 +03:00
|
|
|
{
|
2013-09-22 22:57:03 +03:00
|
|
|
if (!_runtimeInfo.IsAdmin)
|
2013-08-31 01:55:01 +03:00
|
|
|
{
|
2013-09-22 22:57:03 +03:00
|
|
|
if (!IsRegistered(_wildcardUrl)) Url = _localUrl;
|
|
|
|
if (!IsRegistered(_wildcardHttpsUrl)) HttpsUrl = _localHttpsUrl;
|
2013-09-21 02:56:17 +03:00
|
|
|
}
|
|
|
|
|
2013-09-21 05:50:57 +03:00
|
|
|
if (_runtimeInfo.IsAdmin)
|
2013-09-21 02:56:17 +03:00
|
|
|
{
|
2013-09-21 05:50:57 +03:00
|
|
|
RefreshRegistration();
|
2013-08-31 01:55:01 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-09-21 05:50:57 +03:00
|
|
|
private void RefreshRegistration()
|
2013-05-20 05:35:48 +03:00
|
|
|
{
|
2013-06-28 03:04:52 +03:00
|
|
|
if (OsInfo.Version.Major < 6)
|
2013-05-20 05:35:48 +03:00
|
|
|
return;
|
|
|
|
|
2013-09-22 22:57:03 +03:00
|
|
|
RegisterUrl(Url);
|
|
|
|
RegisterUrl(HttpsUrl);
|
2013-05-20 05:35:48 +03:00
|
|
|
}
|
2013-09-22 22:57:03 +03:00
|
|
|
|
|
|
|
private bool IsRegistered(string urlAcl)
|
2013-09-21 05:50:57 +03:00
|
|
|
{
|
2013-09-22 22:57:03 +03:00
|
|
|
var arguments = String.Format("http show urlacl {0}", urlAcl);
|
|
|
|
var output = _netshProvider.Run(arguments);
|
2013-09-21 05:50:57 +03:00
|
|
|
|
2013-09-22 22:57:03 +03:00
|
|
|
if (output == null || !output.Standard.Any()) return false;
|
2013-09-21 05:50:57 +03:00
|
|
|
|
2013-09-22 22:57:03 +03:00
|
|
|
return output.Standard.Any(line => line.Contains(urlAcl));
|
2013-05-20 05:35:48 +03:00
|
|
|
}
|
|
|
|
|
2013-09-22 22:57:03 +03:00
|
|
|
private void RegisterUrl(string urlAcl)
|
2013-05-20 05:35:48 +03:00
|
|
|
{
|
2013-09-22 22:57:03 +03:00
|
|
|
var arguments = String.Format("http add urlacl {0} sddl=D:(A;;GX;;;S-1-1-0)", urlAcl);
|
|
|
|
_netshProvider.Run(arguments);
|
2013-05-20 05:35:48 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|