diff --git a/NzbDrone.Host/AccessControl/UrlAclAdapter.cs b/NzbDrone.Host/AccessControl/UrlAclAdapter.cs index dff7d1f19..0fbbd3611 100644 --- a/NzbDrone.Host/AccessControl/UrlAclAdapter.cs +++ b/NzbDrone.Host/AccessControl/UrlAclAdapter.cs @@ -10,10 +10,8 @@ namespace NzbDrone.Host.AccessControl { public interface IUrlAclAdapter { - void RefreshRegistration(); - bool IsRegistered(); + void ConfigureUrl(); string UrlAcl { get; } - string LocalUrlAcl { get; } } public class UrlAclAdapter : IUrlAclAdapter @@ -22,42 +20,43 @@ namespace NzbDrone.Host.AccessControl private readonly IProcessProvider _processProvider; private readonly IConfigFileProvider _configFileProvider; + private readonly IRuntimeInfo _runtimeInfo; 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; _configFileProvider = configFileProvider; + _runtimeInfo = runtimeInfo; _logger = logger; - } - - public bool IsRegistered() - { - var arguments = String.Format("http show urlacl {0}", UrlAcl); - var output = RunNetsh(arguments); - if (output == null || !output.Standard.Any()) return false; + _localUrl = String.Format(URL_ACL, "localhost", _configFileProvider.Port); + _wildcardUrl = String.Format(URL_ACL, "*", _configFileProvider.Port); - return output.Standard.Any(line => line.Contains(UrlAcl)); + UrlAcl = _wildcardUrl; } - public string UrlAcl + public void ConfigureUrl() { - get + if (!_runtimeInfo.IsAdmin && !IsRegistered) { - return String.Format(URL_ACL, "*", _configFileProvider.Port); + UrlAcl = _localUrl; } - } - public string LocalUrlAcl - { - get + if (_runtimeInfo.IsAdmin) { - return String.Format(URL_ACL, "localhost", _configFileProvider.Port); + RefreshRegistration(); } } - public void RefreshRegistration() + private void RefreshRegistration() { if (OsInfo.Version.Major < 6) return; @@ -65,6 +64,19 @@ namespace NzbDrone.Host.AccessControl 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() { var arguments = String.Format("http add urlacl {0} sddl=D:(A;;GX;;;S-1-1-0)", UrlAcl); diff --git a/NzbDrone.Host/Owin/OwinHostController.cs b/NzbDrone.Host/Owin/OwinHostController.cs index 14d5d331c..52d2878e5 100644 --- a/NzbDrone.Host/Owin/OwinHostController.cs +++ b/NzbDrone.Host/Owin/OwinHostController.cs @@ -38,14 +38,23 @@ namespace NzbDrone.Host.Owin public void StartServer() { 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" }; - _logger.Info("starting server on {0}", urlAcl); + _logger.Info("starting server on {0}", _urlAclAdapter.UrlAcl); try { @@ -94,27 +103,5 @@ namespace NzbDrone.Host.Owin _host = null; _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; - } } } \ No newline at end of file