Moved UrlAcl registration to adapter

pull/30/head
Mark McDowall 11 years ago
parent f826890d2b
commit 32b287387d

@ -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);

@ -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;
}
}
}
Loading…
Cancel
Save