Moved UrlAcl registration to adapter

This commit is contained in:
Mark McDowall 2013-09-20 19:50:57 -07:00
parent f826890d2b
commit 32b287387d
2 changed files with 50 additions and 51 deletions

View File

@ -10,10 +10,8 @@ namespace NzbDrone.Host.AccessControl
{ {
public interface IUrlAclAdapter public interface IUrlAclAdapter
{ {
void RefreshRegistration(); void ConfigureUrl();
bool IsRegistered();
string UrlAcl { get; } string UrlAcl { get; }
string LocalUrlAcl { get; }
} }
public class UrlAclAdapter : IUrlAclAdapter public class UrlAclAdapter : IUrlAclAdapter
@ -22,42 +20,43 @@ namespace NzbDrone.Host.AccessControl
private readonly IProcessProvider _processProvider; private readonly IProcessProvider _processProvider;
private readonly IConfigFileProvider _configFileProvider; private readonly IConfigFileProvider _configFileProvider;
private readonly IRuntimeInfo _runtimeInfo;
private readonly Logger _logger; private readonly Logger _logger;
public UrlAclAdapter(IProcessProvider processProvider, IConfigFileProvider configFileProvider, Logger logger) public string UrlAcl { get; private set; }
private string _localUrl;
private string _wildcardUrl;
public UrlAclAdapter(IProcessProvider processProvider,
IConfigFileProvider configFileProvider,
IRuntimeInfo runtimeInfo,
Logger logger)
{ {
_processProvider = processProvider; _processProvider = processProvider;
_configFileProvider = configFileProvider; _configFileProvider = configFileProvider;
_runtimeInfo = runtimeInfo;
_logger = logger; _logger = logger;
_localUrl = String.Format(URL_ACL, "localhost", _configFileProvider.Port);
_wildcardUrl = String.Format(URL_ACL, "*", _configFileProvider.Port);
UrlAcl = _wildcardUrl;
} }
public bool IsRegistered() public void ConfigureUrl()
{ {
var arguments = String.Format("http show urlacl {0}", UrlAcl); if (!_runtimeInfo.IsAdmin && !IsRegistered)
var output = RunNetsh(arguments);
if (output == null || !output.Standard.Any()) return false;
return output.Standard.Any(line => line.Contains(UrlAcl));
}
public string UrlAcl
{ {
get UrlAcl = _localUrl;
}
if (_runtimeInfo.IsAdmin)
{ {
return String.Format(URL_ACL, "*", _configFileProvider.Port); RefreshRegistration();
} }
} }
public string LocalUrlAcl private void RefreshRegistration()
{
get
{
return String.Format(URL_ACL, "localhost", _configFileProvider.Port);
}
}
public void RefreshRegistration()
{ {
if (OsInfo.Version.Major < 6) if (OsInfo.Version.Major < 6)
return; return;
@ -65,6 +64,19 @@ namespace NzbDrone.Host.AccessControl
RegisterUrl(); RegisterUrl();
} }
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));
}
}
private void RegisterUrl() private void RegisterUrl()
{ {
var arguments = String.Format("http add urlacl {0} sddl=D:(A;;GX;;;S-1-1-0)", UrlAcl); var arguments = String.Format("http add urlacl {0} sddl=D:(A;;GX;;;S-1-1-0)", UrlAcl);

View File

@ -38,14 +38,23 @@ namespace NzbDrone.Host.Owin
public void StartServer() public void StartServer()
{ {
IgnoreCertErrorPolicy.Register(); IgnoreCertErrorPolicy.Register();
var urlAcl = DetermineUrlAcl();
var options = new StartOptions(urlAcl) if (OsInfo.IsWindows)
{
if (_runtimeInfo.IsAdmin)
{
_firewallAdapter.MakeAccessible();
}
_urlAclAdapter.ConfigureUrl();
}
var options = new StartOptions(_urlAclAdapter.UrlAcl)
{ {
ServerFactory = "Microsoft.Owin.Host.HttpListener" ServerFactory = "Microsoft.Owin.Host.HttpListener"
}; };
_logger.Info("starting server on {0}", urlAcl); _logger.Info("starting server on {0}", _urlAclAdapter.UrlAcl);
try try
{ {
@ -94,27 +103,5 @@ namespace NzbDrone.Host.Owin
_host = null; _host = null;
_logger.Info("Host has stopped"); _logger.Info("Host has stopped");
} }
private string DetermineUrlAcl()
{
if (OsInfo.IsWindows && _runtimeInfo.IsAdmin)
{
if (_runtimeInfo.IsAdmin)
{
_urlAclAdapter.RefreshRegistration();
_firewallAdapter.MakeAccessible();
}
else
{
if (!_urlAclAdapter.IsRegistered())
{
return _urlAclAdapter.LocalUrlAcl;
}
}
}
return _urlAclAdapter.UrlAcl;
}
} }
} }